Added PA host owner

This commit is contained in:
Douglas Barone 2021-01-18 16:03:08 -04:00
parent c13239e9d8
commit f47c2f10a0
5 changed files with 45 additions and 7 deletions

View File

@ -127,7 +127,7 @@ function decryptKey(encryptedKey) {
return AES.decrypt(encryptedKey, process.env.CRYPT_SECRET).toString(enc.Utf8) return AES.decrypt(encryptedKey, process.env.CRYPT_SECRET).toString(enc.Utf8)
} }
async function addHost({ cidr, user, password, description, note }) { async function addHost({ cidr, user, password, description, note, owner }) {
if (!CIDR_RE.test(cidr)) throw new Error('Este não é um CIDR válido') if (!CIDR_RE.test(cidr)) throw new Error('Este não é um CIDR válido')
const ipAddr = cidr.split('/')[0] const ipAddr = cidr.split('/')[0]
@ -152,7 +152,12 @@ async function addHost({ cidr, user, password, description, note }) {
encryptedKey, encryptedKey,
user, user,
description, description,
note note,
owner: {
connect: {
sAMAccountName: owner.sAMAccountName
}
}
} }
const host = id const host = id

View File

@ -53,7 +53,14 @@ const Mutation = {
{ data: { cidr, user, password, description, note } }, { data: { cidr, user, password, description, note } },
{ auth } { auth }
) { ) {
const host = await addHost({ cidr, user, password, description, note }) const host = await addHost({
cidr,
user,
password,
description,
note,
owner: auth
})
if (host) if (host)
logSuccess({ logSuccess({
@ -64,7 +71,12 @@ const Mutation = {
return host return host
}, },
async delPAHost(_, { id }) { async delPAHost(_, { 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 } }) return prisma.pAHost.delete({ where: { id } })
} }
} }

View File

@ -1,7 +1,9 @@
import { decryptKey } from '../lib/paloalto' import { decryptKey } from '../lib/paloalto'
import prisma from '../prisma'
const PAHost = { const PAHost = {
key: parent => `${decryptKey(parent.encryptedKey).slice(0, 5)}*****` key: parent => `${decryptKey(parent.encryptedKey).slice(0, 5)}*****`,
owner: parent => prisma.user.findUnique({ where: { id: parent.ownerId } })
} }
export { PAHost } export { PAHost }

View File

@ -275,6 +275,8 @@ const typeDefs = gql`
note: String note: String
"The API key owner" "The API key owner"
user: String user: String
"The user who added the host"
owner: User!
createdAt: String createdAt: String
updatedAt: String updatedAt: String

View File

@ -57,12 +57,17 @@
</v-list> </v-list>
<v-card-text> <v-card-text>
{{ pAHost.note || 'Sem observações' }} {{ pAHost.note || 'Sem observações' }}
<v-divider />
<small>Adicionado por: {{ pAHost.owner.displayName }}</small>
</v-card-text> </v-card-text>
<v-card-actions> <v-card-actions>
<v-spacer /> <v-spacer />
<v-dialog max-width="600"> <v-dialog
v-if="me.sAMAccountName == pAHost.owner.sAMAccountName"
max-width="600"
>
<template #activator="{ on, attrs }"> <template #activator="{ on, attrs }">
<v-btn icon v-bind="attrs" v-on="on"> <v-btn icon v-bind="attrs" v-on="on">
<v-icon>mdi-trash-can</v-icon> <v-icon>mdi-trash-can</v-icon>
@ -129,6 +134,11 @@ const PAHOSTS_QUERY = gql`
user user
createdAt createdAt
updatedAt updatedAt
owner {
id
sAMAccountName
displayName
}
} }
} }
` `
@ -159,7 +169,14 @@ export default {
addDialog: false addDialog: false
}), }),
apollo: { apollo: {
pAHosts: { query: PAHOSTS_QUERY, fetchPolicy: 'network-only' } pAHosts: { query: PAHOSTS_QUERY, fetchPolicy: 'network-only' },
me: gql`
{
me {
sAMAccountName
}
}
`
}, },
methods: { methods: {
async delHost(id) { async delHost(id) {