salix/back/models/account.js

152 lines
5.0 KiB
JavaScript

/* eslint max-len: ["error", { "code": 150 }]*/
const md5 = require('md5');
const app = require('../../loopback/server/server.js');
const dataSources = require('../../loopback/server/datasources.json');
const LoopBackContext = require('loopback-context');
const path = require('path');
module.exports = Self => {
require('../methods/account/login')(Self);
require('../methods/account/logout')(Self);
require('../methods/account/acl')(Self);
require('../methods/account/change-password')(Self);
require('../methods/account/set-password')(Self);
require('../methods/account/recover-password')(Self);
require('../methods/account/validate-token')(Self);
require('../methods/account/privileges')(Self);
// Validations
Self.validatesFormatOf('email', {
message: 'Invalid email',
allowNull: true,
allowBlank: true,
with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/
});
Self.validatesUniquenessOf('name', {
message: `A client with that Web User name already exists`
});
Self.observe('before save', async function(ctx) {
if (ctx.currentInstance && ctx.currentInstance.id && ctx.data && ctx.data.password)
ctx.data.password = md5(ctx.data.password);
});
Self.afterRemote('create', async(ctx, instance) => {
const models = Self.app.models;
const loopBackContext = LoopBackContext.getCurrentContext();
const httpCtx = {req: loopBackContext.active};
const httpRequest = httpCtx.req.http.req;
const headers = httpRequest.headers;
const origin = headers.origin;
const userId = ctx.instance.id;
const options = {
type: 'email',
to: instance.email,
from: dataSources.email.transports[0].auth.from,
subject: 'hanks for registering',
template: path.resolve(__dirname, '../views/newVerify.ejs'),
redirect: `${origin}/#!/account/${instance.id}/basic-data?emailConfirmed`,
host: origin.split(':')[1].split('/')[2],
port: origin.split(':')[2],
protocol: origin.split(':')[0],
user: Self
};
const user = await models.user.findById(userId);
await user.verify(options);
});
Self.afterRemote('prototype.patchAttributes', async(ctx, instance) => {
if (!ctx.args || !ctx.args.data.email) return;
const models = Self.app.models;
const loopBackContext = LoopBackContext.getCurrentContext();
const httpCtx = {req: loopBackContext.active};
const httpRequest = httpCtx.req.http.req;
const headers = httpRequest.headers;
const origin = headers.origin;
const userId = ctx.instance.id;
const options = {
type: 'email',
to: instance.email,
from: dataSources.email.transports[0].auth.from,
subject: 'Verify email',
template: path.resolve(__dirname, '../views/verify.ejs'),
redirect: `${origin}/#!/account/${instance.id}/basic-data?emailConfirmed`,
host: origin.split(':')[1].split('/')[2],
port: origin.split(':')[2],
protocol: origin.split(':')[0],
user: Self
};
const user = await models.user.findById(userId);
await user.verify(options);
});
Self.remoteMethod('getCurrentUserData', {
description: 'Gets the current user data',
accepts: [
{
arg: 'ctx',
type: 'object',
http: {source: 'context'}
}
],
returns: {
type: 'object',
root: true
},
http: {
verb: 'GET',
path: '/getCurrentUserData'
}
});
Self.getCurrentUserData = async function(ctx) {
let userId = ctx.req.accessToken.userId;
return await Self.findById(userId, {
fields: ['id', 'name', 'nickname']
});
};
/**
* Checks if user has a role.
*
* @param {Integer} userId The user id
* @param {String} name The role name
* @param {object} options Options
* @return {Boolean} %true if user has the role, %false otherwise
*/
Self.hasRole = async function(userId, name, options) {
let roles = await Self.getRoles(userId, options);
return roles.some(role => role == name);
};
/**
* Get all user roles.
*
* @param {Integer} userId The user id
* @param {object} options Options
* @return {object} User role list
*/
Self.getRoles = async(userId, options) => {
let result = await Self.rawSql(
`SELECT r.name
FROM account.user u
JOIN account.roleRole rr ON rr.role = u.role
JOIN account.role r ON r.id = rr.inheritsFrom
WHERE u.id = ?`, [userId], options);
let roles = [];
for (role of result)
roles.push(role.name);
return roles;
};
};