36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
module.exports = Self => {
|
|
Self.remoteMethod('setPassword', {
|
|
description: 'Sets the password of a non-worker client',
|
|
accepts: [
|
|
{
|
|
arg: 'id',
|
|
type: 'number',
|
|
description: 'The user id',
|
|
http: {source: 'path'}
|
|
}, {
|
|
arg: 'newPassword',
|
|
type: 'string',
|
|
description: 'The new password',
|
|
required: true
|
|
}
|
|
],
|
|
http: {
|
|
path: `/:id/setPassword`,
|
|
verb: 'PATCH'
|
|
}
|
|
});
|
|
|
|
Self.setPassword = async function(id, newPassword) {
|
|
const models = Self.app.models;
|
|
|
|
const isClient = await models.Client.findById(id);
|
|
const isAccount = await models.Account.findById(id);
|
|
|
|
if (isClient && !isAccount)
|
|
await models.VnUser.setPassword(id, newPassword);
|
|
else
|
|
throw new UserError(`Modifiable password only via recovery or by an administrator`);
|
|
};
|
|
};
|