2018-03-25 02:35:47 +00:00
|
|
|
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')
|
2020-09-10 02:37:49 +00:00
|
|
|
const gql = require('graphql')
|
2017-08-07 01:05:10 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-08-07 01:05:10 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2017-08-09 02:36:29 +00:00
|
|
|
Query: {
|
2020-09-10 02:37:49 +00:00
|
|
|
async groups () { return {} }
|
2018-03-19 03:12:56 +00:00
|
|
|
},
|
|
|
|
Mutation: {
|
2020-09-10 02:37:49 +00:00
|
|
|
async groups () { return {} }
|
2018-03-19 03:12:56 +00:00
|
|
|
},
|
|
|
|
GroupQuery: {
|
2020-09-10 02:37:49 +00:00
|
|
|
/**
|
|
|
|
* FETCH ALL GROUPS
|
|
|
|
*/
|
|
|
|
async list () {
|
2018-07-30 02:23:33 +00:00
|
|
|
return WIKI.models.groups.query().select(
|
2018-05-19 20:40:07 +00:00
|
|
|
'groups.*',
|
2018-07-30 02:23:33 +00:00
|
|
|
WIKI.models.groups.relatedQuery('users').count().as('userCount')
|
2018-05-19 20:40:07 +00:00
|
|
|
)
|
2018-03-28 04:02:32 +00:00
|
|
|
},
|
2020-09-10 02:37:49 +00:00
|
|
|
/**
|
|
|
|
* FETCH A SINGLE GROUP
|
|
|
|
*/
|
|
|
|
async single(obj, args) {
|
2018-07-30 02:23:33 +00:00
|
|
|
return WIKI.models.groups.query().findById(args.id)
|
2017-08-09 02:36:29 +00:00
|
|
|
}
|
|
|
|
},
|
2018-03-19 03:12:56 +00:00
|
|
|
GroupMutation: {
|
2020-09-10 02:37:49 +00:00
|
|
|
/**
|
|
|
|
* ASSIGN USER TO GROUP
|
|
|
|
*/
|
|
|
|
async assignUser (obj, args, { req }) {
|
|
|
|
// Check for guest user
|
|
|
|
if (args.userId === 2) {
|
|
|
|
throw new gql.GraphQLError('Cannot assign the Guest user to a group.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for valid group
|
2018-07-30 02:23:33 +00:00
|
|
|
const grp = await WIKI.models.groups.query().findById(args.groupId)
|
2018-05-12 20:13:04 +00:00
|
|
|
if (!grp) {
|
|
|
|
throw new gql.GraphQLError('Invalid Group ID')
|
|
|
|
}
|
2020-09-10 02:37:49 +00:00
|
|
|
|
|
|
|
// Check assigned permissions for write:groups
|
|
|
|
if (
|
|
|
|
WIKI.auth.checkExclusiveAccess(req.user, ['write:groups'], ['manage:groups', 'manage:system']) &&
|
|
|
|
grp.permissions.some(p => {
|
|
|
|
const resType = _.last(p.split(':'))
|
|
|
|
return ['users', 'groups', 'navigation', 'theme', 'api', 'system'].includes(resType)
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
throw new gql.GraphQLError('You are not authorized to assign a user to this elevated group.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for valid user
|
2018-07-30 02:23:33 +00:00
|
|
|
const usr = await WIKI.models.users.query().findById(args.userId)
|
2018-05-12 20:13:04 +00:00
|
|
|
if (!usr) {
|
|
|
|
throw new gql.GraphQLError('Invalid User ID')
|
|
|
|
}
|
2020-09-10 02:37:49 +00:00
|
|
|
|
|
|
|
// Check for existing relation
|
2019-10-15 03:56:37 +00:00
|
|
|
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.')
|
|
|
|
}
|
2020-09-10 02:37:49 +00:00
|
|
|
|
|
|
|
// Assign user to group
|
2018-05-19 20:40:07 +00:00
|
|
|
await grp.$relatedQuery('users').relate(usr.id)
|
2020-06-25 03:56:37 +00:00
|
|
|
|
2020-09-10 02:37:49 +00:00
|
|
|
// Revoke tokens for this user
|
2020-06-25 03:56:37 +00:00
|
|
|
WIKI.auth.revokeUserTokens({ id: usr.id, kind: 'u' })
|
|
|
|
WIKI.events.outbound.emit('addAuthRevoke', { id: usr.id, kind: 'u' })
|
|
|
|
|
2018-05-12 20:13:04 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User has been assigned to group.')
|
|
|
|
}
|
2017-08-14 00:33:06 +00:00
|
|
|
},
|
2020-09-10 02:37:49 +00:00
|
|
|
/**
|
|
|
|
* CREATE NEW GROUP
|
|
|
|
*/
|
|
|
|
async create (obj, args, { req }) {
|
2018-07-30 02:23:33 +00:00
|
|
|
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),
|
2019-05-05 05:03:42 +00:00
|
|
|
pageRules: JSON.stringify(WIKI.data.groups.defaultPageRules),
|
2018-10-14 21:38:39 +00:00
|
|
|
isSystem: false
|
2018-03-25 02:35:47 +00:00
|
|
|
})
|
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-25 02:35:47 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Group created successfully.'),
|
|
|
|
group
|
|
|
|
}
|
2017-08-14 00:33:06 +00:00
|
|
|
},
|
2020-09-10 02:37:49 +00:00
|
|
|
/**
|
|
|
|
* DELETE GROUP
|
|
|
|
*/
|
|
|
|
async delete (obj, args) {
|
|
|
|
if (args.id === 1 || args.id === 2) {
|
|
|
|
throw new gql.GraphQLError('Cannot delete this group.')
|
|
|
|
}
|
|
|
|
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.groups.query().deleteById(args.id)
|
2020-06-25 03:56:37 +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')
|
2020-06-25 03:56:37 +00:00
|
|
|
|
2018-03-26 05:11:49 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Group has been deleted.')
|
|
|
|
}
|
2017-08-14 00:33:06 +00:00
|
|
|
},
|
2020-09-10 02:37:49 +00:00
|
|
|
/**
|
|
|
|
* UNASSIGN USER FROM GROUP
|
|
|
|
*/
|
|
|
|
async unassignUser (obj, args) {
|
|
|
|
if (args.userId === 2) {
|
|
|
|
throw new gql.GraphQLError('Cannot unassign Guest user')
|
|
|
|
}
|
|
|
|
if (args.userId === 1 && args.groupId === 1) {
|
|
|
|
throw new gql.GraphQLError('Cannot unassign Administrator user from Administrators group.')
|
|
|
|
}
|
2018-07-30 02:23:33 +00:00
|
|
|
const grp = await WIKI.models.groups.query().findById(args.groupId)
|
2018-05-12 20:13:04 +00:00
|
|
|
if (!grp) {
|
|
|
|
throw new gql.GraphQLError('Invalid Group ID')
|
|
|
|
}
|
2018-07-30 02:23:33 +00:00
|
|
|
const usr = await WIKI.models.users.query().findById(args.userId)
|
2018-05-12 20:13:04 +00:00
|
|
|
if (!usr) {
|
|
|
|
throw new gql.GraphQLError('Invalid User ID')
|
|
|
|
}
|
2018-05-19 20:40:07 +00:00
|
|
|
await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
|
2020-06-25 03:56:37 +00:00
|
|
|
|
|
|
|
WIKI.auth.revokeUserTokens({ id: usr.id, kind: 'u' })
|
|
|
|
WIKI.events.outbound.emit('addAuthRevoke', { id: usr.id, kind: 'u' })
|
|
|
|
|
2018-05-12 20:13:04 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
|
|
|
|
}
|
2017-08-20 02:51:25 +00:00
|
|
|
},
|
2020-09-10 02:37:49 +00:00
|
|
|
/**
|
|
|
|
* UPDATE GROUP
|
|
|
|
*/
|
|
|
|
async update (obj, args, { req }) {
|
|
|
|
// Check for unsafe regex page rules
|
2019-08-04 20:31:13 +00:00
|
|
|
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.')
|
|
|
|
}
|
|
|
|
|
2020-09-10 02:37:49 +00:00
|
|
|
// Set default redirect on login value
|
2020-07-03 23:36:33 +00:00
|
|
|
if (_.isEmpty(args.redirectOnLogin)) {
|
|
|
|
args.redirectOnLogin = '/'
|
|
|
|
}
|
|
|
|
|
2020-09-10 02:37:49 +00:00
|
|
|
// Check assigned permissions for write:groups
|
|
|
|
if (
|
|
|
|
WIKI.auth.checkExclusiveAccess(req.user, ['write:groups'], ['manage:groups', 'manage:system']) &&
|
|
|
|
args.permissions.some(p => {
|
|
|
|
const resType = _.last(p.split(':'))
|
|
|
|
return ['users', 'groups', 'navigation', 'theme', 'api', 'system'].includes(resType)
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
throw new gql.GraphQLError('You are not authorized to manage this group or assign these permissions.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update group
|
2018-12-30 02:30:51 +00:00
|
|
|
await WIKI.models.groups.query().patch({
|
|
|
|
name: args.name,
|
2020-07-03 23:36:33 +00:00
|
|
|
redirectOnLogin: args.redirectOnLogin,
|
2018-12-30 02:30:51 +00:00
|
|
|
permissions: JSON.stringify(args.permissions),
|
|
|
|
pageRules: JSON.stringify(args.pageRules)
|
|
|
|
}).where('id', args.id)
|
2019-01-12 23:33:30 +00:00
|
|
|
|
2020-09-10 02:37:49 +00:00
|
|
|
// Revoke tokens for this group
|
2020-06-25 03:56:37 +00:00
|
|
|
WIKI.auth.revokeUserTokens({ id: args.id, kind: 'g' })
|
|
|
|
WIKI.events.outbound.emit('addAuthRevoke', { id: args.id, kind: 'g' })
|
|
|
|
|
2020-09-10 02:37:49 +00:00
|
|
|
// Reload group permissions
|
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
|
|
|
|
2018-05-12 20:13:04 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Group has been updated.')
|
|
|
|
}
|
2017-08-09 02:36:29 +00:00
|
|
|
}
|
2017-08-07 01:05:10 +00:00
|
|
|
},
|
2017-08-09 02:36:29 +00:00
|
|
|
Group: {
|
2020-09-10 02:37:49 +00:00
|
|
|
users (grp) {
|
2018-05-19 20:40:07 +00:00
|
|
|
return grp.$relatedQuery('users')
|
2017-08-07 01:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|