2018-02-15 13:35:04 +00:00
|
|
|
const md5 = require('md5');
|
|
|
|
|
2017-10-13 14:22:45 +00:00
|
|
|
module.exports = function(Self) {
|
|
|
|
// Validations
|
|
|
|
|
|
|
|
Self.validatesUniquenessOf('name', {
|
2018-04-05 11:20:27 +00:00
|
|
|
message: 'A user with that 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) => {
|
|
|
|
if (ctx.currentInstance && ctx.currentInstance.id && ctx.data && ctx.data.password) {
|
|
|
|
ctx.data.password = md5(ctx.data.password);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
};
|
2017-10-13 14:22:45 +00:00
|
|
|
};
|