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.

101 lines
3.0 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. await grp.$relatedQuery('users').relate(usr.id)
  35. return {
  36. responseResult: graphHelper.generateSuccess('User has been assigned to group.')
  37. }
  38. },
  39. async create(obj, args) {
  40. const group = await WIKI.models.groups.query().insertAndFetch({
  41. name: args.name,
  42. permissions: JSON.stringify(WIKI.data.groups.defaultPermissions),
  43. pageRules: JSON.stringify([]),
  44. isSystem: false
  45. })
  46. await WIKI.auth.reloadGroups()
  47. return {
  48. responseResult: graphHelper.generateSuccess('Group created successfully.'),
  49. group
  50. }
  51. },
  52. async delete(obj, args) {
  53. await WIKI.models.groups.query().deleteById(args.id)
  54. await WIKI.auth.reloadGroups()
  55. return {
  56. responseResult: graphHelper.generateSuccess('Group has been deleted.')
  57. }
  58. },
  59. async unassignUser(obj, args) {
  60. const grp = await WIKI.models.groups.query().findById(args.groupId)
  61. if (!grp) {
  62. throw new gql.GraphQLError('Invalid Group ID')
  63. }
  64. const usr = await WIKI.models.users.query().findById(args.userId)
  65. if (!usr) {
  66. throw new gql.GraphQLError('Invalid User ID')
  67. }
  68. await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
  69. return {
  70. responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
  71. }
  72. },
  73. async update(obj, args) {
  74. if(_.some(args.pageRules, pr => {
  75. return pr.match === 'REGEX' && !safeRegex(pr.path)
  76. })) {
  77. throw new gql.GraphQLError('Some Page Rules contains unsafe or exponential time regex.')
  78. }
  79. await WIKI.models.groups.query().patch({
  80. name: args.name,
  81. permissions: JSON.stringify(args.permissions),
  82. pageRules: JSON.stringify(args.pageRules)
  83. }).where('id', args.id)
  84. await WIKI.auth.reloadGroups()
  85. return {
  86. responseResult: graphHelper.generateSuccess('Group has been updated.')
  87. }
  88. }
  89. },
  90. Group: {
  91. users(grp) {
  92. return grp.$relatedQuery('users')
  93. }
  94. }
  95. }