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.

132 lines
4.1 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. WIKI.auth.revokeUserTokens({ id: usr.id, kind: 'u' })
  43. WIKI.events.outbound.emit('addAuthRevoke', { id: usr.id, kind: 'u' })
  44. return {
  45. responseResult: graphHelper.generateSuccess('User has been assigned to group.')
  46. }
  47. },
  48. async create(obj, args) {
  49. const group = await WIKI.models.groups.query().insertAndFetch({
  50. name: args.name,
  51. permissions: JSON.stringify(WIKI.data.groups.defaultPermissions),
  52. pageRules: JSON.stringify(WIKI.data.groups.defaultPageRules),
  53. isSystem: false
  54. })
  55. await WIKI.auth.reloadGroups()
  56. WIKI.events.outbound.emit('reloadGroups')
  57. return {
  58. responseResult: graphHelper.generateSuccess('Group created successfully.'),
  59. group
  60. }
  61. },
  62. async delete(obj, args) {
  63. await WIKI.models.groups.query().deleteById(args.id)
  64. WIKI.auth.revokeUserTokens({ id: args.id, kind: 'g' })
  65. WIKI.events.outbound.emit('addAuthRevoke', { id: args.id, kind: 'g' })
  66. await WIKI.auth.reloadGroups()
  67. WIKI.events.outbound.emit('reloadGroups')
  68. return {
  69. responseResult: graphHelper.generateSuccess('Group has been deleted.')
  70. }
  71. },
  72. async unassignUser(obj, args) {
  73. const grp = await WIKI.models.groups.query().findById(args.groupId)
  74. if (!grp) {
  75. throw new gql.GraphQLError('Invalid Group ID')
  76. }
  77. const usr = await WIKI.models.users.query().findById(args.userId)
  78. if (!usr) {
  79. throw new gql.GraphQLError('Invalid User ID')
  80. }
  81. await grp.$relatedQuery('users').unrelate().where('userId', usr.id)
  82. WIKI.auth.revokeUserTokens({ id: usr.id, kind: 'u' })
  83. WIKI.events.outbound.emit('addAuthRevoke', { id: usr.id, kind: 'u' })
  84. return {
  85. responseResult: graphHelper.generateSuccess('User has been unassigned from group.')
  86. }
  87. },
  88. async update(obj, args) {
  89. if (_.some(args.pageRules, pr => {
  90. return pr.match === 'REGEX' && !safeRegex(pr.path)
  91. })) {
  92. throw new gql.GraphQLError('Some Page Rules contains unsafe or exponential time regex.')
  93. }
  94. if (_.isEmpty(args.redirectOnLogin)) {
  95. args.redirectOnLogin = '/'
  96. }
  97. await WIKI.models.groups.query().patch({
  98. name: args.name,
  99. redirectOnLogin: args.redirectOnLogin,
  100. permissions: JSON.stringify(args.permissions),
  101. pageRules: JSON.stringify(args.pageRules)
  102. }).where('id', args.id)
  103. WIKI.auth.revokeUserTokens({ id: args.id, kind: 'g' })
  104. WIKI.events.outbound.emit('addAuthRevoke', { id: args.id, kind: 'g' })
  105. await WIKI.auth.reloadGroups()
  106. WIKI.events.outbound.emit('reloadGroups')
  107. return {
  108. responseResult: graphHelper.generateSuccess('Group has been updated.')
  109. }
  110. }
  111. },
  112. Group: {
  113. users(grp) {
  114. return grp.$relatedQuery('users')
  115. }
  116. }
  117. }