You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.4 KiB

  1. const graphHelper = require('../../helpers/graph')
  2. const safeRegex = require('safe-regex')
  3. const _ = require('lodash')
  4. /* global WIKI */
  5. const gql = require('graphql')
  6. module.exports = {
  7. Query: {
  8. async groups() { return {} }
  9. },
  10. Mutation: {
  11. async groups() { return {} }
  12. },
  13. GroupQuery: {
  14. async list(obj, args, context, info) {
  15. return WIKI.models.groups.query().select(
  16. 'groups.*',
  17. WIKI.models.groups.relatedQuery('users').count().as('userCount')
  18. )
  19. },
  20. async single(obj, args, context, info) {
  21. return WIKI.models.groups.query().findById(args.id)
  22. }
  23. },
  24. GroupMutation: {
  25. async assignUser(obj, args) {
  26. const grp = await WIKI.models.groups.query().findById(args.groupId)
  27. if (!grp) {
  28. throw new gql.GraphQLError('Invalid Group ID')
  29. }
  30. const usr = await WIKI.models.users.query().findById(args.userId)
  31. if (!usr) {
  32. throw new gql.GraphQLError('Invalid User ID')
  33. }
  34. const relExist = await WIKI.models.knex('userGroups').where({
  35. userId: args.userId,
  36. groupId: args.groupId
  37. }).first()
  38. if (relExist) {
  39. throw new gql.GraphQLError('User is already assigned to group.')
  40. }
  41. await grp.$relatedQuery('users').relate(usr.id)
  42. return {
  43. responseResult: graphHelper.generateSuccess('User has been assigned to group.')
  44. }
  45. },
  46. async create(obj, args) {
  47. const group = await WIKI.models.groups.query().insertAndFetch({
  48. name: args.name,
  49. permissions: JSON.stringify(WIKI.data.groups.defaultPermissions),
  50. pageRules: JSON.stringify(WIKI.data.groups.defaultPageRules),
  51. isSystem: false
  52. })
  53. await WIKI.auth.reloadGroups()
  54. WIKI.events.outbound.emit('reloadGroups')
  55. return {
  56. responseResult: graphHelper.generateSuccess('Group created successfully.'),
  57. group
  58. }
  59. },
  60. async delete(obj, args) {
  61. await WIKI.models.groups.query().deleteById(args.id)
  62. await WIKI.auth.reloadGroups()
  63. WIKI.events.outbound.emit('reloadGroups')
  64. return {
  65. responseResult: graphHelper.generateSuccess('Group has been deleted.')
  66. }
  67. },
  68. async unassignUser(obj, args) {
  69. const grp = await WIKI.models.groups.query().findById(args.groupId)
  70. if (!grp) {
  71. throw new gql.GraphQLError('Invalid Group ID')
  72. }
  73. const usr = await WIKI.models.users.query().findById(args.userId)
  74. if (!usr) {
  75. throw new gql.GraphQLError('Invalid User ID')
  76. }
  77. await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
  78. return {
  79. responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
  80. }
  81. },
  82. async update(obj, args) {
  83. if (_.some(args.pageRules, pr => {
  84. return pr.match === 'REGEX' && !safeRegex(pr.path)
  85. })) {
  86. throw new gql.GraphQLError('Some Page Rules contains unsafe or exponential time regex.')
  87. }
  88. await WIKI.models.groups.query().patch({
  89. name: args.name,
  90. permissions: JSON.stringify(args.permissions),
  91. pageRules: JSON.stringify(args.pageRules)
  92. }).where('id', args.id)
  93. await WIKI.auth.reloadGroups()
  94. WIKI.events.outbound.emit('reloadGroups')
  95. return {
  96. responseResult: graphHelper.generateSuccess('Group has been updated.')
  97. }
  98. }
  99. },
  100. Group: {
  101. users(grp) {
  102. return grp.$relatedQuery('users')
  103. }
  104. }
  105. }