2018-02-15 13:35:04 +00:00
|
|
|
const md5 = require('md5');
|
|
|
|
|
2018-05-04 09:46:03 +00:00
|
|
|
module.exports = Self => {
|
2019-01-04 12:32:04 +00:00
|
|
|
require('../methods/account/login')(Self);
|
|
|
|
require('../methods/account/logout')(Self);
|
2019-01-23 12:11:44 +00:00
|
|
|
require('../methods/account/acl')(Self);
|
|
|
|
require('../methods/account/validate-token')(Self);
|
2019-01-04 12:32:04 +00:00
|
|
|
|
2017-10-13 14:22:45 +00:00
|
|
|
// Validations
|
|
|
|
|
|
|
|
Self.validatesUniquenessOf('name', {
|
2018-07-06 06:18:55 +00:00
|
|
|
message: `A client with that Web User name already exists`
|
2017-10-13 14:22:45 +00:00
|
|
|
});
|
2018-02-15 13:35:04 +00:00
|
|
|
|
|
|
|
Self.observe('before save', (ctx, next) => {
|
2018-11-12 10:17:47 +00:00
|
|
|
if (ctx.currentInstance && ctx.currentInstance.id && ctx.data && ctx.data.password)
|
2018-02-15 13:35:04 +00:00
|
|
|
ctx.data.password = md5(ctx.data.password);
|
2018-11-12 10:17:47 +00:00
|
|
|
|
2018-02-15 13:35:04 +00:00
|
|
|
next();
|
|
|
|
});
|
2018-03-06 10:13:05 +00:00
|
|
|
|
|
|
|
Self.remoteMethod('getCurrentUserName', {
|
|
|
|
description: 'Gets the current user name',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'context',
|
|
|
|
type: 'object',
|
|
|
|
http: function(ctx) {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'GET',
|
|
|
|
path: '/getCurrentUserName'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.getCurrentUserName = async function(ctx) {
|
|
|
|
let filter = {fields: ['name']};
|
|
|
|
let userId = ctx.req.accessToken.userId;
|
|
|
|
let account = await Self.findById(userId, filter);
|
|
|
|
|
|
|
|
return account.name;
|
|
|
|
};
|
2018-05-08 07:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if user has a role.
|
|
|
|
*
|
|
|
|
* @param {Integer} userId The user id
|
2018-10-23 09:25:51 +00:00
|
|
|
* @param {String} name The role name
|
2018-05-08 07:32:30 +00:00
|
|
|
* @return {Boolean} %true if user has the role, %false otherwise
|
|
|
|
*/
|
2018-10-23 09:25:51 +00:00
|
|
|
Self.hasRole = async function(userId, name) {
|
|
|
|
let roles = await Self.getRoles(userId);
|
|
|
|
|
|
|
|
return roles.find(role => {
|
|
|
|
return role.toLowerCase() == name.toLowerCase();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all user roles.
|
|
|
|
*
|
|
|
|
* @param {Integer} userId The user id
|
|
|
|
* @return {Object} User role list
|
|
|
|
*/
|
2018-11-12 10:17:47 +00:00
|
|
|
Self.getRoles = async userId => {
|
2018-05-08 07:32:30 +00:00
|
|
|
let result = await Self.rawSql(
|
2018-10-23 09:25:51 +00:00
|
|
|
`SELECT r.name
|
2018-05-08 07:32:30 +00:00
|
|
|
FROM account.user u
|
|
|
|
JOIN account.roleRole rr ON rr.role = u.role
|
|
|
|
JOIN account.role r ON r.id = rr.inheritsFrom
|
2018-10-23 09:25:51 +00:00
|
|
|
WHERE u.id = ?`, [userId]);
|
|
|
|
|
|
|
|
let roles = [];
|
2018-11-12 10:17:47 +00:00
|
|
|
for (role of result)
|
2018-10-23 09:25:51 +00:00
|
|
|
roles.push(role.name);
|
2018-11-12 10:17:47 +00:00
|
|
|
|
2018-10-23 09:25:51 +00:00
|
|
|
|
|
|
|
return roles;
|
2018-05-08 07:32:30 +00:00
|
|
|
};
|
2017-10-13 14:22:45 +00:00
|
|
|
};
|