Refactor mutation into individual files
This commit is contained in:
parent
4b63b28c9f
commit
91315b06c3
4
server/package-lock.json
generated
4
server/package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "ifms-pti-svr",
|
||||
"version": "2.12.2",
|
||||
"version": "2.12.3",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "ifms-pti-svr",
|
||||
"version": "2.12.2",
|
||||
"version": "2.12.3",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@prisma/client": "^3.11.0",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "ifms-pti-svr",
|
||||
"version": "2.12.2",
|
||||
"version": "2.12.3",
|
||||
"description": "Servidor do Portal de TI do IFMS",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
|
|
25
server/src/resolvers/Mutation/addPAHost.js
Normal file
25
server/src/resolvers/Mutation/addPAHost.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { addHost } from '../../lib/paloalto'
|
||||
import { logSuccess } from '../../lib/logger'
|
||||
|
||||
export async function addPAHost(
|
||||
parent,
|
||||
{ data: { cidr, user, password, description, note } },
|
||||
{ auth }
|
||||
) {
|
||||
const host = await addHost({
|
||||
cidr,
|
||||
user,
|
||||
password,
|
||||
description,
|
||||
note,
|
||||
owner: auth
|
||||
})
|
||||
|
||||
if (host)
|
||||
logSuccess({
|
||||
message: `Usuário ${auth.sAMAccountName} (${auth.displayName}) adicionou um novo host Palo Alto (${host.cidr}).`,
|
||||
tags: ['paloalto']
|
||||
})
|
||||
|
||||
return host
|
||||
}
|
5
server/src/resolvers/Mutation/createResetToken.js
Normal file
5
server/src/resolvers/Mutation/createResetToken.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { ResetToken } from '../../classes/ResetToken'
|
||||
|
||||
export async function createResetToken(parent, { data }, { auth }) {
|
||||
return ResetToken.createToken(data.username, auth.sAMAccountName)
|
||||
}
|
10
server/src/resolvers/Mutation/delPAHost.js
Normal file
10
server/src/resolvers/Mutation/delPAHost.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
import prisma from '../../prisma'
|
||||
|
||||
export async function delPAHost(parent, { id }, { auth }) {
|
||||
const host = await prisma.pAHost.findUnique({ where: { id } })
|
||||
|
||||
if (host.ownerId != auth.id)
|
||||
throw new Error('Você não pode apagar o host de outro usuário')
|
||||
|
||||
return prisma.pAHost.delete({ where: { id } })
|
||||
}
|
5
server/src/resolvers/Mutation/deleteExpiredTokens.js
Normal file
5
server/src/resolvers/Mutation/deleteExpiredTokens.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { ResetToken } from '../../classes/ResetToken'
|
||||
|
||||
export async function deleteExpiredTokens() {
|
||||
return `Tokens deletados ${await ResetToken.deleteExpiredTokens()}`
|
||||
}
|
6
server/src/resolvers/Mutation/importUsers.js
Normal file
6
server/src/resolvers/Mutation/importUsers.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { User } from '../../classes/User'
|
||||
|
||||
export async function importUsers() {
|
||||
User.importAllUsers()
|
||||
return 'A importação está sendo feita. Isso pode demorar alguns minutos.'
|
||||
}
|
|
@ -1,150 +1,28 @@
|
|||
import { replacePassword } from '../../lib/activeDirectory/passwordUtils'
|
||||
import { User } from '../../classes/User'
|
||||
import { ResetToken } from '../../classes/ResetToken'
|
||||
|
||||
import { updateDevicesInfo } from '../../lib/wifiDevices'
|
||||
import { updateUserIdMappings, addHost } from '../../lib/paloalto'
|
||||
import { logInfo, logSuccess, logError } from '../../lib/logger'
|
||||
|
||||
import { updateAccessPoints } from '../../lib/accessPoints'
|
||||
|
||||
import prisma from '../../prisma'
|
||||
|
||||
import { ACCESS_POINTS_UPDATED, pubsub } from '../../pubsub'
|
||||
import { login } from './login'
|
||||
import { updatePassword } from './updatePassword'
|
||||
import { replacePassword } from './replacePassword'
|
||||
import { replaceStudentPassword } from './replaceStudentPassword'
|
||||
import { createResetToken } from './createResetToken'
|
||||
import { useResetToken } from './useResetToken'
|
||||
import { deleteExpiredTokens } from './deleteExpiredTokens'
|
||||
import { importUsers } from './importUsers'
|
||||
import { addPAHost } from './addPAHost'
|
||||
import { delPAHost } from './delPAHost'
|
||||
import { updateAccessPoint } from './updateAccessPoint'
|
||||
|
||||
const Mutation = {
|
||||
async login(parent, { data }) {
|
||||
return User.login(data.username, data.password)
|
||||
},
|
||||
|
||||
async updatePassword(parent, { data }, { auth }) {
|
||||
return auth.updatePassword(data.oldPassword, data.newPassword)
|
||||
},
|
||||
|
||||
async replacePassword(parent, { data }, { auth }) {
|
||||
logInfo({
|
||||
tags: ['replacePassword', 'user'],
|
||||
message: `Usuário ${auth.sAMAccountName} (${auth.displayName}) está trocando a senha do usuário ${data.username}`
|
||||
})
|
||||
|
||||
return replacePassword(data.username, data.newPassword)
|
||||
},
|
||||
async replaceStudentPassword(parent, { data }, { auth }) {
|
||||
const user = await new User(data.username).init()
|
||||
|
||||
const isStudent = !!user.groups.find(
|
||||
group => group.name == (process.env.STUDENT_GROUP || 'Estudantes')
|
||||
)
|
||||
|
||||
const isServant = !!user.groups.find(
|
||||
group => group.name == (process.env.SERVANT_GROUP || 'G_SERVIDORES')
|
||||
)
|
||||
|
||||
if (isServant) throw new Error(`Usuário ${data.username} é um servidor`)
|
||||
|
||||
if (!isStudent)
|
||||
throw new Error(`Usuário ${data.username} não é um estudante`)
|
||||
|
||||
logInfo({
|
||||
tags: ['replaceStudentPassword', 'user'],
|
||||
message: `Usuário ${auth.sAMAccountName} (${auth.displayName}) está trocando a senha do aluno ${data.username}`
|
||||
})
|
||||
|
||||
return replacePassword(data.username, data.newPassword)
|
||||
},
|
||||
async createResetToken(parent, { data }, { auth }) {
|
||||
return ResetToken.createToken(data.username, auth.sAMAccountName)
|
||||
},
|
||||
|
||||
async useResetToken(parent, { data }) {
|
||||
return ResetToken.useToken(data.token, data.newPassword)
|
||||
},
|
||||
|
||||
async deleteExpiredTokens() {
|
||||
return `Tokens deletados ${await ResetToken.deleteExpiredTokens()}`
|
||||
},
|
||||
|
||||
async importUsers() {
|
||||
User.importAllUsers()
|
||||
|
||||
return 'A importação está sendo feita. Isso pode demorar alguns minutos.'
|
||||
},
|
||||
|
||||
async updateWifiDevices() {
|
||||
return updateDevicesInfo()
|
||||
},
|
||||
|
||||
async updateUserIdMappings() {
|
||||
updateUserIdMappings()
|
||||
return 'A atualização está em andamento. Acompanhe os logs do servidor para mais informações.'
|
||||
},
|
||||
|
||||
async addPAHost(
|
||||
parent,
|
||||
{ data: { cidr, user, password, description, note } },
|
||||
{ auth }
|
||||
) {
|
||||
const host = await addHost({
|
||||
cidr,
|
||||
user,
|
||||
password,
|
||||
description,
|
||||
note,
|
||||
owner: auth
|
||||
})
|
||||
|
||||
if (host)
|
||||
logSuccess({
|
||||
message: `Usuário ${auth.sAMAccountName} (${auth.displayName}) adicionou um novo host Palo Alto (${host.cidr}).`,
|
||||
tags: ['paloalto']
|
||||
})
|
||||
|
||||
return host
|
||||
},
|
||||
|
||||
async delPAHost(parent, { id }, { auth }) {
|
||||
const host = await prisma.pAHost.findUnique({ where: { id } })
|
||||
|
||||
if (host.ownerId != auth.id)
|
||||
throw new Error('Você não pode apagar o host de outro usuário')
|
||||
|
||||
return prisma.pAHost.delete({ where: { id } })
|
||||
},
|
||||
|
||||
async updateAccessPoints() {
|
||||
try {
|
||||
await updateAccessPoints()
|
||||
return 'Atualização concluída'
|
||||
} catch (e) {
|
||||
logError({
|
||||
tags: ['accessPoints'],
|
||||
message: `Erro tentando atualizar os pontos de acesso: ${e.message}`,
|
||||
data: { error: e }
|
||||
})
|
||||
throw e
|
||||
}
|
||||
},
|
||||
async updateAccessPoint(
|
||||
parent,
|
||||
{ data: { id, name, local, notes } },
|
||||
context,
|
||||
info
|
||||
) {
|
||||
const accessPoint = await prisma.accessPoint.update({
|
||||
where: { id: parseInt(id) },
|
||||
data: { name, local, notes }
|
||||
})
|
||||
|
||||
const accessPoints = await prisma.accessPoint.findMany({
|
||||
include: { wifiDevices: true }
|
||||
})
|
||||
|
||||
pubsub.publish(ACCESS_POINTS_UPDATED, {
|
||||
accessPointsUpdated: accessPoints
|
||||
})
|
||||
|
||||
return accessPoint
|
||||
}
|
||||
login,
|
||||
updatePassword,
|
||||
replacePassword,
|
||||
replaceStudentPassword,
|
||||
createResetToken,
|
||||
useResetToken,
|
||||
deleteExpiredTokens,
|
||||
importUsers,
|
||||
addPAHost,
|
||||
delPAHost,
|
||||
updateAccessPoint
|
||||
}
|
||||
|
||||
export { Mutation }
|
||||
|
|
5
server/src/resolvers/Mutation/login.js
Normal file
5
server/src/resolvers/Mutation/login.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { User } from '../../classes/User'
|
||||
|
||||
export async function login(parent, { data }) {
|
||||
return User.login(data.username, data.password)
|
||||
}
|
11
server/src/resolvers/Mutation/replacePassword.js
Normal file
11
server/src/resolvers/Mutation/replacePassword.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { logInfo } from '../../lib/logger'
|
||||
import { replacePassword as replaceADPassword } from '../../lib/activeDirectory/passwordUtils'
|
||||
|
||||
export async function replacePassword(parent, { data }, { auth }) {
|
||||
logInfo({
|
||||
tags: ['replacePassword', 'user'],
|
||||
message: `Usuário ${auth.sAMAccountName} (${auth.displayName}) está trocando a senha do usuário ${data.username}`
|
||||
})
|
||||
|
||||
return replaceADPassword(data.username, data.newPassword)
|
||||
}
|
27
server/src/resolvers/Mutation/replaceStudentPassword.js
Normal file
27
server/src/resolvers/Mutation/replaceStudentPassword.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { User } from '../../classes/User'
|
||||
import { replacePassword } from '../../lib/activeDirectory/passwordUtils'
|
||||
import { logInfo } from '../../lib/logger'
|
||||
|
||||
export async function replaceStudentPassword(parent, { data }, { auth }) {
|
||||
const user = await new User(data.username).init()
|
||||
|
||||
const isStudent = !!user.groups.find(
|
||||
group => group.name == (process.env.STUDENT_GROUP || 'Estudantes')
|
||||
)
|
||||
|
||||
const isServant = !!user.groups.find(
|
||||
group => group.name == (process.env.SERVANT_GROUP || 'G_SERVIDORES')
|
||||
)
|
||||
|
||||
if (isServant) throw new Error(`Usuário ${data.username} é um servidor`)
|
||||
|
||||
if (!isStudent)
|
||||
throw new Error(`Usuário ${data.username} não é um estudante`)
|
||||
|
||||
logInfo({
|
||||
tags: ['replaceStudentPassword', 'user'],
|
||||
message: `Usuário ${auth.sAMAccountName} (${auth.displayName}) está trocando a senha do aluno ${data.username}`
|
||||
})
|
||||
|
||||
return replacePassword(data.username, data.newPassword)
|
||||
}
|
25
server/src/resolvers/Mutation/updateAccessPoint.js
Normal file
25
server/src/resolvers/Mutation/updateAccessPoint.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
|
||||
import prisma from '../../prisma'
|
||||
import { ACCESS_POINTS_UPDATED, pubsub } from '../../pubsub'
|
||||
|
||||
export async function updateAccessPoint(
|
||||
parent,
|
||||
{ data: { id, name, local, notes } },
|
||||
context,
|
||||
info
|
||||
) {
|
||||
const accessPoint = await prisma.accessPoint.update({
|
||||
where: { id: parseInt(id) },
|
||||
data: { name, local, notes }
|
||||
})
|
||||
|
||||
const accessPoints = await prisma.accessPoint.findMany({
|
||||
include: { wifiDevices: true }
|
||||
})
|
||||
|
||||
pubsub.publish(ACCESS_POINTS_UPDATED, {
|
||||
accessPointsUpdated: accessPoints
|
||||
})
|
||||
|
||||
return accessPoint
|
||||
}
|
3
server/src/resolvers/Mutation/updatePassword.js
Normal file
3
server/src/resolvers/Mutation/updatePassword.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export async function updatePassword(parent, { data }, { auth }) {
|
||||
return auth.updatePassword(data.oldPassword, data.newPassword)
|
||||
}
|
5
server/src/resolvers/Mutation/useResetToken.js
Normal file
5
server/src/resolvers/Mutation/useResetToken.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { ResetToken } from '../../classes/ResetToken'
|
||||
|
||||
export async function useResetToken(parent, { data }) {
|
||||
return ResetToken.useToken(data.token, data.newPassword)
|
||||
}
|
|
@ -89,21 +89,12 @@ const typeDefs = gql`
|
|||
"Import all users from Active Directory"
|
||||
importUsers: String! @auth(roles: ["superAdmin"])
|
||||
|
||||
"Force update devices connected to Wi-Fi"
|
||||
updateWifiDevices: String! @auth(roles: ["superAdmin"])
|
||||
|
||||
"Force update user-id mapping on firewall"
|
||||
updateUserIdMappings: String! @auth(roles: ["superAdmin"])
|
||||
|
||||
"Add a PA host"
|
||||
addPAHost(data: AddPAHostInput!): PAHost! @auth(roles: ["superAdmin"])
|
||||
|
||||
"Remove a PA host"
|
||||
delPAHost(id: Int!): PAHost! @auth(roles: ["superAdmin"])
|
||||
|
||||
"Force Update Access Points"
|
||||
updateAccessPoints: String! @auth(roles: ["superAdmin"])
|
||||
|
||||
"Update an Access Point"
|
||||
updateAccessPoint(data: UpdateAccessPointInput): AccessPoint!
|
||||
@auth(roles: ["superAdmin"])
|
||||
|
|
4
web/package-lock.json
generated
4
web/package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "ifms-pti",
|
||||
"version": "2.12.2",
|
||||
"version": "2.12.3",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "ifms-pti",
|
||||
"version": "2.12.2",
|
||||
"version": "2.12.3",
|
||||
"dependencies": {
|
||||
"@mdi/font": "^6.6.96",
|
||||
"apollo-link-ws": "^1.0.20",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "ifms-pti",
|
||||
"version": "2.12.2",
|
||||
"version": "2.12.3",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"serve": "vue-cli-service serve",
|
||||
|
|
Loading…
Reference in New Issue
Block a user