2019-08-11 02:14:53 +00:00
|
|
|
const graphHelper = require('../../helpers/graph')
|
2020-04-05 22:51:48 +00:00
|
|
|
const _ = require('lodash')
|
2017-08-07 01:05:10 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-08-07 01:05:10 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2017-08-09 02:36:29 +00:00
|
|
|
Query: {
|
2018-05-12 20:13:04 +00:00
|
|
|
async users() { return {} }
|
2017-08-09 02:36:29 +00:00
|
|
|
},
|
|
|
|
Mutation: {
|
2018-05-12 20:13:04 +00:00
|
|
|
async users() { return {} }
|
|
|
|
},
|
|
|
|
UserQuery: {
|
|
|
|
async list(obj, args, context, info) {
|
2018-07-30 02:23:33 +00:00
|
|
|
return WIKI.models.users.query()
|
2018-12-15 22:15:13 +00:00
|
|
|
.select('id', 'email', 'name', 'providerKey', 'isSystem', 'createdAt')
|
2018-05-12 20:13:04 +00:00
|
|
|
},
|
|
|
|
async search(obj, args, context, info) {
|
2018-07-30 02:23:33 +00:00
|
|
|
return WIKI.models.users.query()
|
2018-05-19 20:40:07 +00:00
|
|
|
.where('email', 'like', `%${args.query}%`)
|
|
|
|
.orWhere('name', 'like', `%${args.query}%`)
|
|
|
|
.limit(10)
|
2018-10-14 03:22:42 +00:00
|
|
|
.select('id', 'email', 'name', 'providerKey', 'createdAt')
|
2018-05-12 20:13:04 +00:00
|
|
|
},
|
|
|
|
async single(obj, args, context, info) {
|
2018-07-30 02:23:33 +00:00
|
|
|
let usr = await WIKI.models.users.query().findById(args.id)
|
2018-06-24 04:20:35 +00:00
|
|
|
usr.password = ''
|
|
|
|
usr.tfaSecret = ''
|
|
|
|
return usr
|
2020-03-30 05:30:10 +00:00
|
|
|
},
|
|
|
|
async profile (obj, args, context, info) {
|
|
|
|
if (!context.req.user || context.req.user.id < 1 || context.req.user.id === 2) {
|
|
|
|
throw new WIKI.Error.AuthRequired()
|
|
|
|
}
|
|
|
|
const usr = await WIKI.models.users.query().findById(context.req.user.id)
|
|
|
|
if (!usr.isActive) {
|
|
|
|
throw new WIKI.Error.AuthAccountBanned()
|
|
|
|
}
|
2020-04-05 22:51:48 +00:00
|
|
|
|
|
|
|
const providerInfo = _.find(WIKI.data.authentication, ['key', usr.providerKey])
|
|
|
|
|
|
|
|
usr.providerName = _.get(providerInfo, 'title', 'Unknown')
|
|
|
|
usr.lastLoginAt = usr.lastLoginAt || usr.updatedAt
|
|
|
|
usr.password = ''
|
|
|
|
usr.providerId = ''
|
|
|
|
usr.tfaSecret = ''
|
|
|
|
|
|
|
|
return usr
|
2020-04-13 19:54:10 +00:00
|
|
|
},
|
|
|
|
async lastLogins (obj, args, context, info) {
|
|
|
|
return WIKI.models.users.query()
|
|
|
|
.select('id', 'name', 'lastLoginAt')
|
|
|
|
.whereNotNull('lastLoginAt')
|
|
|
|
.orderBy('lastLoginAt', 'desc')
|
|
|
|
.limit(10)
|
2018-05-12 20:13:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
UserMutation: {
|
2019-12-22 21:08:18 +00:00
|
|
|
async create (obj, args) {
|
2019-08-11 02:14:53 +00:00
|
|
|
try {
|
|
|
|
await WIKI.models.users.createNewUser(args)
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User created successfully')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2017-08-14 00:33:06 +00:00
|
|
|
},
|
2019-12-22 21:08:18 +00:00
|
|
|
async delete (obj, args) {
|
|
|
|
try {
|
|
|
|
if (args.id <= 2) {
|
|
|
|
throw new WIKI.Error.UserDeleteProtected()
|
|
|
|
}
|
2020-03-15 16:06:45 +00:00
|
|
|
await WIKI.models.users.deleteUser(args.id)
|
2019-12-22 21:08:18 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User deleted successfully')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err.message.indexOf('foreign') >= 0) {
|
|
|
|
return graphHelper.generateError(new WIKI.Error.UserDeleteForeignConstraint())
|
|
|
|
} else {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
}
|
2017-08-20 02:51:25 +00:00
|
|
|
},
|
2019-12-22 21:08:18 +00:00
|
|
|
async update (obj, args) {
|
2019-08-17 22:29:58 +00:00
|
|
|
try {
|
|
|
|
await WIKI.models.users.updateUser(args)
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User created successfully')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2017-08-20 02:51:25 +00:00
|
|
|
},
|
2020-01-02 19:19:02 +00:00
|
|
|
async verify (obj, args) {
|
|
|
|
try {
|
|
|
|
await WIKI.models.users.query().patch({ isVerified: true }).findById(args.id)
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User verified successfully')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async activate (obj, args) {
|
|
|
|
try {
|
|
|
|
await WIKI.models.users.query().patch({ isActive: true }).findById(args.id)
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User activated successfully')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async deactivate (obj, args) {
|
|
|
|
try {
|
|
|
|
if (args.id <= 2) {
|
|
|
|
throw new Error('Cannot deactivate system accounts.')
|
|
|
|
}
|
|
|
|
await WIKI.models.users.query().patch({ isActive: false }).findById(args.id)
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User deactivated successfully')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
2019-12-22 21:08:18 +00:00
|
|
|
resetPassword (obj, args) {
|
2017-08-20 02:51:25 +00:00
|
|
|
return false
|
2020-04-05 22:51:48 +00:00
|
|
|
},
|
|
|
|
async updateProfile (obj, args, context) {
|
|
|
|
try {
|
|
|
|
if (!context.req.user || context.req.user.id < 1 || context.req.user.id === 2) {
|
|
|
|
throw new WIKI.Error.AuthRequired()
|
|
|
|
}
|
|
|
|
const usr = await WIKI.models.users.query().findById(context.req.user.id)
|
|
|
|
if (!usr.isActive) {
|
|
|
|
throw new WIKI.Error.AuthAccountBanned()
|
|
|
|
}
|
|
|
|
if (!usr.isVerified) {
|
|
|
|
throw new WIKI.Error.AuthAccountNotVerified()
|
|
|
|
}
|
|
|
|
|
2020-05-03 04:38:02 +00:00
|
|
|
if (!['', 'DD/MM/YYYY', 'DD.MM.YYYY', 'MM/DD/YYYY', 'YYYY-MM-DD', 'YYYY/MM/DD'].includes(args.dateFormat)) {
|
|
|
|
throw new WIKI.Error.InputInvalid()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!['', 'light', 'dark'].includes(args.appearance)) {
|
|
|
|
throw new WIKI.Error.InputInvalid()
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:51:48 +00:00
|
|
|
await WIKI.models.users.updateUser({
|
|
|
|
id: usr.id,
|
|
|
|
name: _.trim(args.name),
|
|
|
|
jobTitle: _.trim(args.jobTitle),
|
|
|
|
location: _.trim(args.location),
|
2020-05-03 04:38:02 +00:00
|
|
|
timezone: args.timezone,
|
|
|
|
dateFormat: args.dateFormat,
|
|
|
|
appearance: args.appearance
|
2020-04-05 22:51:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const newToken = await WIKI.models.users.refreshToken(usr.id)
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('User profile updated successfully'),
|
|
|
|
jwt: newToken.token
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async changePassword (obj, args, context) {
|
|
|
|
try {
|
|
|
|
if (!context.req.user || context.req.user.id < 1 || context.req.user.id === 2) {
|
|
|
|
throw new WIKI.Error.AuthRequired()
|
|
|
|
}
|
|
|
|
const usr = await WIKI.models.users.query().findById(context.req.user.id)
|
|
|
|
if (!usr.isActive) {
|
|
|
|
throw new WIKI.Error.AuthAccountBanned()
|
|
|
|
}
|
|
|
|
if (!usr.isVerified) {
|
|
|
|
throw new WIKI.Error.AuthAccountNotVerified()
|
|
|
|
}
|
|
|
|
if (usr.providerKey !== 'local') {
|
|
|
|
throw new WIKI.Error.AuthProviderInvalid()
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await usr.verifyPassword(args.current)
|
|
|
|
} catch (err) {
|
|
|
|
throw new WIKI.Error.AuthPasswordInvalid()
|
|
|
|
}
|
|
|
|
|
|
|
|
await WIKI.models.users.updateUser({
|
|
|
|
id: usr.id,
|
|
|
|
newPassword: args.new
|
|
|
|
})
|
|
|
|
|
|
|
|
const newToken = await WIKI.models.users.refreshToken(usr)
|
|
|
|
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Password changed successfully'),
|
|
|
|
jwt: newToken.token
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2017-08-09 02:36:29 +00:00
|
|
|
}
|
2017-08-07 01:05:10 +00:00
|
|
|
},
|
2017-08-09 02:36:29 +00:00
|
|
|
User: {
|
2020-04-05 22:51:48 +00:00
|
|
|
groups (usr) {
|
2018-12-15 22:15:13 +00:00
|
|
|
return usr.$relatedQuery('groups')
|
2017-08-07 01:05:10 +00:00
|
|
|
}
|
2020-04-05 22:51:48 +00:00
|
|
|
},
|
|
|
|
UserProfile: {
|
|
|
|
async groups (usr) {
|
|
|
|
const usrGroups = await usr.$relatedQuery('groups')
|
|
|
|
return usrGroups.map(g => g.name)
|
|
|
|
},
|
|
|
|
async pagesTotal (usr) {
|
|
|
|
const result = await WIKI.models.pages.query().count('* as total').where('creatorId', usr.id).first()
|
|
|
|
return _.toSafeInteger(result.total)
|
|
|
|
}
|
2017-08-07 01:05:10 +00:00
|
|
|
}
|
|
|
|
}
|