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.

108 lines
3.2 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. return {
  55. responseResult: graphHelper.generateSuccess('Group created successfully.'),
  56. group
  57. }
  58. },
  59. async delete(obj, args) {
  60. await WIKI.models.groups.query().deleteById(args.id)
  61. await WIKI.auth.reloadGroups()
  62. return {
  63. responseResult: graphHelper.generateSuccess('Group has been deleted.')
  64. }
  65. },
  66. async unassignUser(obj, args) {
  67. const grp = await WIKI.models.groups.query().findById(args.groupId)
  68. if (!grp) {
  69. throw new gql.GraphQLError('Invalid Group ID')
  70. }
  71. const usr = await WIKI.models.users.query().findById(args.userId)
  72. if (!usr) {
  73. throw new gql.GraphQLError('Invalid User ID')
  74. }
  75. await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
  76. return {
  77. responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
  78. }
  79. },
  80. async update(obj, args) {
  81. if (_.some(args.pageRules, pr => {
  82. return pr.match === 'REGEX' && !safeRegex(pr.path)
  83. })) {
  84. throw new gql.GraphQLError('Some Page Rules contains unsafe or exponential time regex.')
  85. }
  86. await WIKI.models.groups.query().patch({
  87. name: args.name,
  88. permissions: JSON.stringify(args.permissions),
  89. pageRules: JSON.stringify(args.pageRules)
  90. }).where('id', args.id)
  91. await WIKI.auth.reloadGroups()
  92. return {
  93. responseResult: graphHelper.generateSuccess('Group has been updated.')
  94. }
  95. }
  96. },
  97. Group: {
  98. users(grp) {
  99. return grp.$relatedQuery('users')
  100. }
  101. }
  102. }