wikijs-fork/server/graph/resolvers/group.js

133 lines
4.1 KiB
JavaScript
Raw Normal View History

const graphHelper = require('../../helpers/graph')
2019-01-12 23:33:30 +00:00
const safeRegex = require('safe-regex')
2019-01-13 03:33:30 +00:00
const _ = require('lodash')
2017-08-07 01:05:10 +00:00
/* global WIKI */
2017-08-07 01:05:10 +00:00
const gql = require('graphql')
2017-08-07 01:05:10 +00:00
module.exports = {
Query: {
async groups() { return {} }
},
Mutation: {
async groups() { return {} }
},
GroupQuery: {
async list(obj, args, context, info) {
return WIKI.models.groups.query().select(
'groups.*',
WIKI.models.groups.relatedQuery('users').count().as('userCount')
)
2018-03-28 04:02:32 +00:00
},
async single(obj, args, context, info) {
return WIKI.models.groups.query().findById(args.id)
}
},
GroupMutation: {
async assignUser(obj, args) {
const grp = await WIKI.models.groups.query().findById(args.groupId)
if (!grp) {
throw new gql.GraphQLError('Invalid Group ID')
}
const usr = await WIKI.models.users.query().findById(args.userId)
if (!usr) {
throw new gql.GraphQLError('Invalid User ID')
}
const relExist = await WIKI.models.knex('userGroups').where({
userId: args.userId,
groupId: args.groupId
}).first()
if (relExist) {
throw new gql.GraphQLError('User is already assigned to group.')
}
await grp.$relatedQuery('users').relate(usr.id)
WIKI.auth.revokeUserTokens({ id: usr.id, kind: 'u' })
WIKI.events.outbound.emit('addAuthRevoke', { id: usr.id, kind: 'u' })
return {
responseResult: graphHelper.generateSuccess('User has been assigned to group.')
}
},
async create(obj, args) {
const group = await WIKI.models.groups.query().insertAndFetch({
2018-10-14 21:38:39 +00:00
name: args.name,
permissions: JSON.stringify(WIKI.data.groups.defaultPermissions),
pageRules: JSON.stringify(WIKI.data.groups.defaultPageRules),
2018-10-14 21:38:39 +00:00
isSystem: false
})
2019-01-12 23:33:30 +00:00
await WIKI.auth.reloadGroups()
2020-04-20 00:26:26 +00:00
WIKI.events.outbound.emit('reloadGroups')
return {
responseResult: graphHelper.generateSuccess('Group created successfully.'),
group
}
},
2018-03-26 05:11:49 +00:00
async delete(obj, args) {
await WIKI.models.groups.query().deleteById(args.id)
WIKI.auth.revokeUserTokens({ id: args.id, kind: 'g' })
WIKI.events.outbound.emit('addAuthRevoke', { id: args.id, kind: 'g' })
2019-01-12 23:33:30 +00:00
await WIKI.auth.reloadGroups()
2020-04-20 00:26:26 +00:00
WIKI.events.outbound.emit('reloadGroups')
2018-03-26 05:11:49 +00:00
return {
responseResult: graphHelper.generateSuccess('Group has been deleted.')
}
},
async unassignUser(obj, args) {
const grp = await WIKI.models.groups.query().findById(args.groupId)
if (!grp) {
throw new gql.GraphQLError('Invalid Group ID')
}
const usr = await WIKI.models.users.query().findById(args.userId)
if (!usr) {
throw new gql.GraphQLError('Invalid User ID')
}
await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
WIKI.auth.revokeUserTokens({ id: usr.id, kind: 'u' })
WIKI.events.outbound.emit('addAuthRevoke', { id: usr.id, kind: 'u' })
return {
responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
}
},
async update(obj, args) {
if (_.some(args.pageRules, pr => {
2019-01-13 20:37:45 +00:00
return pr.match === 'REGEX' && !safeRegex(pr.path)
2019-01-12 23:33:30 +00:00
})) {
throw new gql.GraphQLError('Some Page Rules contains unsafe or exponential time regex.')
}
if (_.isEmpty(args.redirectOnLogin)) {
args.redirectOnLogin = '/'
}
await WIKI.models.groups.query().patch({
name: args.name,
redirectOnLogin: args.redirectOnLogin,
permissions: JSON.stringify(args.permissions),
pageRules: JSON.stringify(args.pageRules)
}).where('id', args.id)
2019-01-12 23:33:30 +00:00
WIKI.auth.revokeUserTokens({ id: args.id, kind: 'g' })
WIKI.events.outbound.emit('addAuthRevoke', { id: args.id, kind: 'g' })
2019-01-12 23:33:30 +00:00
await WIKI.auth.reloadGroups()
2020-04-20 00:26:26 +00:00
WIKI.events.outbound.emit('reloadGroups')
2019-01-12 23:33:30 +00:00
return {
responseResult: graphHelper.generateSuccess('Group has been updated.')
}
}
2017-08-07 01:05:10 +00:00
},
Group: {
2017-08-07 01:05:10 +00:00
users(grp) {
return grp.$relatedQuery('users')
2017-08-07 01:05:10 +00:00
}
}
}