2022-10-03 13:11:29 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
2023-01-23 14:24:00 +00:00
|
|
|
Self.remoteMethod('privileges', {
|
2022-10-03 13:11:29 +00:00
|
|
|
description: 'Change role and hasGrant if user has privileges',
|
|
|
|
accepts: [
|
2023-01-23 14:24:00 +00:00
|
|
|
{
|
|
|
|
arg: 'ctx',
|
|
|
|
type: 'Object',
|
|
|
|
http: {source: 'context'}
|
|
|
|
},
|
2022-10-03 13:11:29 +00:00
|
|
|
{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'The user id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'roleFk',
|
|
|
|
type: 'number',
|
|
|
|
description: 'The new role for user',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'hasGrant',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'Whether to has grant'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
http: {
|
|
|
|
path: `/:id/privileges`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.privileges = async function(ctx, id, roleFk, hasGrant, options) {
|
2022-10-26 06:27:28 +00:00
|
|
|
if (!(hasGrant != null || roleFk)) return;
|
|
|
|
|
2022-10-03 13:11:29 +00:00
|
|
|
const models = Self.app.models;
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
|
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
const user = await models.VnUser.findById(userId, {fields: ['hasGrant']}, myOptions);
|
2022-10-03 13:11:29 +00:00
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
const userToUpdate = await models.VnUser.findById(id, {
|
2022-10-27 07:01:09 +00:00
|
|
|
fields: ['id', 'name', 'hasGrant', 'roleFk', 'password'],
|
|
|
|
include: {
|
|
|
|
relation: 'role',
|
|
|
|
scope: {
|
|
|
|
fields: ['name']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, myOptions);
|
|
|
|
|
2022-10-28 06:09:47 +00:00
|
|
|
if (!user.hasGrant)
|
|
|
|
throw new UserError(`You don't have grant privilege`);
|
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
const hasRoleFromUser = await models.VnUser.hasRole(userId, userToUpdate.role().name, myOptions);
|
2022-10-28 06:09:47 +00:00
|
|
|
|
|
|
|
if (!hasRoleFromUser)
|
|
|
|
throw new UserError(`You don't own the role and you can't assign it to another user`);
|
|
|
|
|
2022-10-03 13:11:29 +00:00
|
|
|
if (hasGrant != null)
|
2022-10-26 06:27:28 +00:00
|
|
|
userToUpdate.hasGrant = hasGrant;
|
|
|
|
|
|
|
|
if (roleFk) {
|
|
|
|
const role = await models.Role.findById(roleFk, {fields: ['name']}, myOptions);
|
2023-01-23 14:24:00 +00:00
|
|
|
const hasRole = await models.VnUser.hasRole(userId, role.name, myOptions);
|
2022-10-03 13:11:29 +00:00
|
|
|
|
2022-10-28 06:09:47 +00:00
|
|
|
if (!hasRole)
|
2022-10-26 06:27:28 +00:00
|
|
|
throw new UserError(`You don't own the role and you can't assign it to another user`);
|
2022-10-03 13:11:29 +00:00
|
|
|
|
2022-10-26 06:27:28 +00:00
|
|
|
userToUpdate.roleFk = roleFk;
|
|
|
|
}
|
2022-10-03 13:11:29 +00:00
|
|
|
|
2022-10-26 06:27:28 +00:00
|
|
|
await userToUpdate.save(userToUpdate);
|
|
|
|
await models.UserAccount.sync(userToUpdate.name);
|
2022-10-03 13:11:29 +00:00
|
|
|
};
|
|
|
|
};
|