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.

237 lines
7.1 KiB

  1. const graphHelper = require('../../helpers/graph')
  2. const _ = require('lodash')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async users() { return {} }
  7. },
  8. Mutation: {
  9. async users() { return {} }
  10. },
  11. UserQuery: {
  12. async list(obj, args, context, info) {
  13. return WIKI.models.users.query()
  14. .select('id', 'email', 'name', 'providerKey', 'isSystem', 'isActive', 'createdAt', 'lastLoginAt')
  15. },
  16. async search(obj, args, context, info) {
  17. return WIKI.models.users.query()
  18. .where('email', 'like', `%${args.query}%`)
  19. .orWhere('name', 'like', `%${args.query}%`)
  20. .limit(10)
  21. .select('id', 'email', 'name', 'providerKey', 'createdAt')
  22. },
  23. async single(obj, args, context, info) {
  24. let usr = await WIKI.models.users.query().findById(args.id)
  25. usr.password = ''
  26. usr.tfaSecret = ''
  27. return usr
  28. },
  29. async profile (obj, args, context, info) {
  30. if (!context.req.user || context.req.user.id < 1 || context.req.user.id === 2) {
  31. throw new WIKI.Error.AuthRequired()
  32. }
  33. const usr = await WIKI.models.users.query().findById(context.req.user.id)
  34. if (!usr.isActive) {
  35. throw new WIKI.Error.AuthAccountBanned()
  36. }
  37. const providerInfo = _.find(WIKI.data.authentication, ['key', usr.providerKey])
  38. usr.providerName = _.get(providerInfo, 'title', 'Unknown')
  39. usr.lastLoginAt = usr.lastLoginAt || usr.updatedAt
  40. usr.password = ''
  41. usr.providerId = ''
  42. usr.tfaSecret = ''
  43. return usr
  44. },
  45. async lastLogins (obj, args, context, info) {
  46. return WIKI.models.users.query()
  47. .select('id', 'name', 'lastLoginAt')
  48. .whereNotNull('lastLoginAt')
  49. .orderBy('lastLoginAt', 'desc')
  50. .limit(10)
  51. }
  52. },
  53. UserMutation: {
  54. async create (obj, args) {
  55. try {
  56. await WIKI.models.users.createNewUser(args)
  57. return {
  58. responseResult: graphHelper.generateSuccess('User created successfully')
  59. }
  60. } catch (err) {
  61. return graphHelper.generateError(err)
  62. }
  63. },
  64. async delete (obj, args) {
  65. try {
  66. if (args.id <= 2) {
  67. throw new WIKI.Error.UserDeleteProtected()
  68. }
  69. await WIKI.models.users.deleteUser(args.id, args.replaceId)
  70. WIKI.auth.revokeUserTokens({ id: args.id, kind: 'u' })
  71. WIKI.events.outbound.emit('addAuthRevoke', { id: args.id, kind: 'u' })
  72. return {
  73. responseResult: graphHelper.generateSuccess('User deleted successfully')
  74. }
  75. } catch (err) {
  76. if (err.message.indexOf('foreign') >= 0) {
  77. return graphHelper.generateError(new WIKI.Error.UserDeleteForeignConstraint())
  78. } else {
  79. return graphHelper.generateError(err)
  80. }
  81. }
  82. },
  83. async update (obj, args) {
  84. try {
  85. await WIKI.models.users.updateUser(args)
  86. return {
  87. responseResult: graphHelper.generateSuccess('User created successfully')
  88. }
  89. } catch (err) {
  90. return graphHelper.generateError(err)
  91. }
  92. },
  93. async verify (obj, args) {
  94. try {
  95. await WIKI.models.users.query().patch({ isVerified: true }).findById(args.id)
  96. return {
  97. responseResult: graphHelper.generateSuccess('User verified successfully')
  98. }
  99. } catch (err) {
  100. return graphHelper.generateError(err)
  101. }
  102. },
  103. async activate (obj, args) {
  104. try {
  105. await WIKI.models.users.query().patch({ isActive: true }).findById(args.id)
  106. return {
  107. responseResult: graphHelper.generateSuccess('User activated successfully')
  108. }
  109. } catch (err) {
  110. return graphHelper.generateError(err)
  111. }
  112. },
  113. async deactivate (obj, args) {
  114. try {
  115. if (args.id <= 2) {
  116. throw new Error('Cannot deactivate system accounts.')
  117. }
  118. await WIKI.models.users.query().patch({ isActive: false }).findById(args.id)
  119. WIKI.auth.revokeUserTokens({ id: args.id, kind: 'u' })
  120. WIKI.events.outbound.emit('addAuthRevoke', { id: args.id, kind: 'u' })
  121. return {
  122. responseResult: graphHelper.generateSuccess('User deactivated successfully')
  123. }
  124. } catch (err) {
  125. return graphHelper.generateError(err)
  126. }
  127. },
  128. resetPassword (obj, args) {
  129. return false
  130. },
  131. async updateProfile (obj, args, context) {
  132. try {
  133. if (!context.req.user || context.req.user.id < 1 || context.req.user.id === 2) {
  134. throw new WIKI.Error.AuthRequired()
  135. }
  136. const usr = await WIKI.models.users.query().findById(context.req.user.id)
  137. if (!usr.isActive) {
  138. throw new WIKI.Error.AuthAccountBanned()
  139. }
  140. if (!usr.isVerified) {
  141. throw new WIKI.Error.AuthAccountNotVerified()
  142. }
  143. if (!['', 'DD/MM/YYYY', 'DD.MM.YYYY', 'MM/DD/YYYY', 'YYYY-MM-DD', 'YYYY/MM/DD'].includes(args.dateFormat)) {
  144. throw new WIKI.Error.InputInvalid()
  145. }
  146. if (!['', 'light', 'dark'].includes(args.appearance)) {
  147. throw new WIKI.Error.InputInvalid()
  148. }
  149. await WIKI.models.users.updateUser({
  150. id: usr.id,
  151. name: _.trim(args.name),
  152. jobTitle: _.trim(args.jobTitle),
  153. location: _.trim(args.location),
  154. timezone: args.timezone,
  155. dateFormat: args.dateFormat,
  156. appearance: args.appearance
  157. })
  158. const newToken = await WIKI.models.users.refreshToken(usr.id)
  159. return {
  160. responseResult: graphHelper.generateSuccess('User profile updated successfully'),
  161. jwt: newToken.token
  162. }
  163. } catch (err) {
  164. return graphHelper.generateError(err)
  165. }
  166. },
  167. async changePassword (obj, args, context) {
  168. try {
  169. if (!context.req.user || context.req.user.id < 1 || context.req.user.id === 2) {
  170. throw new WIKI.Error.AuthRequired()
  171. }
  172. const usr = await WIKI.models.users.query().findById(context.req.user.id)
  173. if (!usr.isActive) {
  174. throw new WIKI.Error.AuthAccountBanned()
  175. }
  176. if (!usr.isVerified) {
  177. throw new WIKI.Error.AuthAccountNotVerified()
  178. }
  179. if (usr.providerKey !== 'local') {
  180. throw new WIKI.Error.AuthProviderInvalid()
  181. }
  182. try {
  183. await usr.verifyPassword(args.current)
  184. } catch (err) {
  185. throw new WIKI.Error.AuthPasswordInvalid()
  186. }
  187. await WIKI.models.users.updateUser({
  188. id: usr.id,
  189. newPassword: args.new
  190. })
  191. const newToken = await WIKI.models.users.refreshToken(usr)
  192. return {
  193. responseResult: graphHelper.generateSuccess('Password changed successfully'),
  194. jwt: newToken.token
  195. }
  196. } catch (err) {
  197. return graphHelper.generateError(err)
  198. }
  199. }
  200. },
  201. User: {
  202. groups (usr) {
  203. return usr.$relatedQuery('groups')
  204. }
  205. },
  206. UserProfile: {
  207. async groups (usr) {
  208. const usrGroups = await usr.$relatedQuery('groups')
  209. return usrGroups.map(g => g.name)
  210. },
  211. async pagesTotal (usr) {
  212. const result = await WIKI.models.pages.query().count('* as total').where('creatorId', usr.id).first()
  213. return _.toSafeInteger(result.total)
  214. }
  215. }
  216. }