2023-01-24 08:04:43 +00:00
|
|
|
const vnModel = require('vn-loopback/common/models/vn-model');
|
2023-01-31 13:57:24 +00:00
|
|
|
const {Email} = require('vn-print');
|
2023-10-02 12:51:19 +00:00
|
|
|
const ForbiddenError = require('vn-loopback/util/forbiddenError');
|
2023-10-23 08:08:26 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2023-11-14 12:00:20 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2023-01-31 13:57:24 +00:00
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
module.exports = function(Self) {
|
2023-01-24 08:04:43 +00:00
|
|
|
vnModel(Self);
|
2023-01-23 14:24:00 +00:00
|
|
|
|
2023-04-18 11:15:04 +00:00
|
|
|
require('../methods/vn-user/sign-in')(Self);
|
2023-01-23 14:24:00 +00:00
|
|
|
require('../methods/vn-user/acl')(Self);
|
2023-01-24 09:35:21 +00:00
|
|
|
require('../methods/vn-user/recover-password')(Self);
|
2023-01-23 14:24:00 +00:00
|
|
|
require('../methods/vn-user/privileges')(Self);
|
2023-04-18 11:15:04 +00:00
|
|
|
require('../methods/vn-user/validate-auth')(Self);
|
2023-05-24 13:01:59 +00:00
|
|
|
require('../methods/vn-user/renew-token')(Self);
|
2024-02-27 13:26:54 +00:00
|
|
|
require('../methods/vn-user/share-token')(Self);
|
2023-09-25 06:33:16 +00:00
|
|
|
require('../methods/vn-user/update-user')(Self);
|
2024-05-03 10:17:51 +00:00
|
|
|
require('../methods/vn-user/acls')(Self);
|
2023-05-23 06:45:03 +00:00
|
|
|
|
2023-05-30 08:07:17 +00:00
|
|
|
Self.definition.settings.acls = Self.definition.settings.acls.filter(acl => acl.property !== 'create');
|
2023-01-23 14:24:00 +00:00
|
|
|
|
|
|
|
// Validations
|
|
|
|
|
|
|
|
Self.validatesFormatOf('email', {
|
|
|
|
message: 'Invalid email',
|
|
|
|
allowNull: true,
|
2023-07-21 10:33:14 +00:00
|
|
|
allowBlank: false,
|
2023-01-23 14:24:00 +00:00
|
|
|
with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.validatesUniquenessOf('name', {
|
|
|
|
message: `A client with that Web User name already exists`
|
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const 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) => {
|
|
|
|
const result = await Self.rawSql(
|
|
|
|
`SELECT r.name
|
2024-01-25 13:58:48 +00:00
|
|
|
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);
|
2023-01-23 14:24:00 +00:00
|
|
|
|
|
|
|
const roles = [];
|
|
|
|
for (const role of result)
|
|
|
|
roles.push(role.name);
|
|
|
|
|
|
|
|
return roles;
|
|
|
|
};
|
2023-01-31 13:57:24 +00:00
|
|
|
|
|
|
|
Self.on('resetPasswordRequest', async function(info) {
|
2023-11-13 11:26:31 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
const httpCtx = {req: loopBackContext.active};
|
|
|
|
const httpRequest = httpCtx.req.http.req;
|
|
|
|
const headers = httpRequest.headers;
|
|
|
|
const origin = headers.origin;
|
2023-01-31 13:57:24 +00:00
|
|
|
|
2023-08-02 07:29:09 +00:00
|
|
|
const defaultHash = '/reset-password?access_token=$token$';
|
|
|
|
const recoverHashes = {
|
|
|
|
hedera: 'verificationToken=$token$'
|
|
|
|
};
|
|
|
|
|
2023-08-04 12:33:38 +00:00
|
|
|
const app = info.options?.app;
|
2023-08-02 07:29:09 +00:00
|
|
|
let recoverHash = app ? recoverHashes[app] : defaultHash;
|
|
|
|
recoverHash = recoverHash.replace('$token$', info.accessToken.id);
|
|
|
|
|
2023-01-31 13:57:24 +00:00
|
|
|
const user = await Self.app.models.VnUser.findById(info.user.id);
|
2023-07-17 12:24:50 +00:00
|
|
|
|
2023-01-31 13:57:24 +00:00
|
|
|
const params = {
|
|
|
|
recipient: info.email,
|
|
|
|
lang: user.lang,
|
2023-11-13 11:26:31 +00:00
|
|
|
url: origin + '/#!' + recoverHash
|
2023-01-31 13:57:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const options = Object.assign({}, info.options);
|
|
|
|
for (const param in options)
|
|
|
|
params[param] = options[param];
|
|
|
|
|
|
|
|
const email = new Email(options.emailTemplate, params);
|
|
|
|
|
|
|
|
return email.send();
|
|
|
|
});
|
2023-04-18 11:15:04 +00:00
|
|
|
|
2023-12-13 10:59:00 +00:00
|
|
|
/**
|
|
|
|
* Sign-in validate
|
|
|
|
* @param {String} user The user
|
|
|
|
* @param {Object} userToken Options
|
|
|
|
* @param {Object} token accessToken
|
|
|
|
* @param {Object} ctx context
|
|
|
|
*/
|
|
|
|
Self.signInValidate = async(user, userToken, token, ctx) => {
|
|
|
|
const [[key, value]] = Object.entries(Self.userUses(user));
|
|
|
|
const isOwner = Self.rawSql(`SELECT ? = ? `, [userToken[key], value]);
|
2023-12-13 11:07:57 +00:00
|
|
|
if (!isOwner) {
|
|
|
|
await Self.app.models.SignInLog.create({
|
|
|
|
userName: user,
|
|
|
|
token: token.id,
|
|
|
|
userFk: userToken.id,
|
|
|
|
ip: ctx.req.ip,
|
|
|
|
owner: isOwner
|
|
|
|
});
|
2024-01-25 13:58:48 +00:00
|
|
|
throw new UserError('Try again');
|
2023-12-20 09:27:25 +00:00
|
|
|
}
|
2023-12-13 10:59:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate login params
|
|
|
|
* @param {String} user The user
|
|
|
|
* @param {String} password
|
|
|
|
* @param {Object} ctx context
|
|
|
|
*/
|
2023-12-13 11:01:18 +00:00
|
|
|
Self.validateLogin = async function(user, password, ctx) {
|
|
|
|
const loginInfo = Object.assign({password}, Self.userUses(user));
|
|
|
|
const token = await Self.login(loginInfo, 'user');
|
2023-07-25 13:00:08 +00:00
|
|
|
|
|
|
|
const userToken = await token.user.get();
|
2023-11-14 12:00:20 +00:00
|
|
|
|
2023-11-28 12:22:17 +00:00
|
|
|
if (ctx)
|
|
|
|
await Self.signInValidate(user, userToken, token, ctx);
|
2023-11-14 12:00:20 +00:00
|
|
|
|
2023-07-25 13:00:08 +00:00
|
|
|
try {
|
|
|
|
await Self.app.models.Account.sync(userToken.name, password);
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
|
2024-02-27 13:26:54 +00:00
|
|
|
return {token: token.id, ttl: token.ttl};
|
2023-07-03 11:33:48 +00:00
|
|
|
};
|
2023-04-18 11:15:04 +00:00
|
|
|
|
2023-07-03 11:33:48 +00:00
|
|
|
Self.userUses = function(user) {
|
|
|
|
return user.indexOf('@') !== -1
|
2023-04-18 11:15:04 +00:00
|
|
|
? {email: user}
|
|
|
|
: {username: user};
|
|
|
|
};
|
2023-06-13 09:49:42 +00:00
|
|
|
|
2023-06-02 13:07:02 +00:00
|
|
|
const _setPassword = Self.prototype.setPassword;
|
|
|
|
Self.prototype.setPassword = async function(newPassword, options, cb) {
|
|
|
|
if (cb === undefined && typeof options === 'function') {
|
|
|
|
cb = options;
|
2023-06-01 13:29:46 +00:00
|
|
|
options = undefined;
|
2023-06-02 13:07:02 +00:00
|
|
|
}
|
2023-05-03 13:19:19 +00:00
|
|
|
|
2023-05-22 07:17:40 +00:00
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
|
|
|
}
|
|
|
|
options = myOptions;
|
|
|
|
|
|
|
|
try {
|
2023-06-02 13:07:02 +00:00
|
|
|
await Self.rawSql(`CALL account.user_checkPassword(?)`, [newPassword], options);
|
|
|
|
await _setPassword.call(this, newPassword, options);
|
|
|
|
await this.updateAttribute('passExpired', null, options);
|
|
|
|
await Self.app.models.Account.sync(this.name, newPassword, null, options);
|
|
|
|
tx && await tx.commit();
|
|
|
|
cb && cb();
|
2023-06-01 13:29:46 +00:00
|
|
|
} catch (err) {
|
2023-06-02 13:07:02 +00:00
|
|
|
tx && await tx.rollback();
|
|
|
|
if (cb) cb(err); else throw err;
|
2023-05-22 07:17:40 +00:00
|
|
|
}
|
2023-05-04 13:01:29 +00:00
|
|
|
};
|
|
|
|
|
2023-07-17 12:41:15 +00:00
|
|
|
Self.sharedClass._methods.find(method => method.name == 'changePassword').ctor.settings.acls =
|
2023-11-28 12:22:17 +00:00
|
|
|
Self.sharedClass._methods.find(method => method.name == 'changePassword').ctor.settings.acls
|
|
|
|
.filter(acl => acl.property != 'changePassword');
|
2023-06-16 07:35:20 +00:00
|
|
|
|
2023-09-25 06:33:16 +00:00
|
|
|
Self.userSecurity = async(ctx, userId, options) => {
|
2023-09-20 13:10:28 +00:00
|
|
|
const models = Self.app.models;
|
2023-09-25 06:33:16 +00:00
|
|
|
const accessToken = ctx?.options?.accessToken || LoopBackContext.getCurrentContext().active.accessToken;
|
2023-09-20 13:10:28 +00:00
|
|
|
const ctxToken = {req: {accessToken}};
|
|
|
|
|
2023-09-25 06:33:16 +00:00
|
|
|
if (userId === accessToken.userId) return;
|
|
|
|
|
|
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
|
|
|
const hasHigherPrivileges = await models.ACL.checkAccessAcl(ctxToken, 'VnUser', 'higherPrivileges', myOptions);
|
2023-09-20 13:10:28 +00:00
|
|
|
if (hasHigherPrivileges) return;
|
|
|
|
|
2023-09-25 06:33:16 +00:00
|
|
|
const hasMediumPrivileges = await models.ACL.checkAccessAcl(ctxToken, 'VnUser', 'mediumPrivileges', myOptions);
|
|
|
|
const user = await models.VnUser.findById(userId, {fields: ['id', 'emailVerified']}, myOptions);
|
2023-09-20 13:10:28 +00:00
|
|
|
if (!user.emailVerified && hasMediumPrivileges) return;
|
|
|
|
|
2023-10-02 12:51:19 +00:00
|
|
|
throw new ForbiddenError();
|
2023-09-20 13:10:28 +00:00
|
|
|
};
|
|
|
|
|
2023-09-25 06:33:16 +00:00
|
|
|
Self.observe('after save', async ctx => {
|
2023-10-02 12:51:19 +00:00
|
|
|
const instance = ctx?.instance;
|
|
|
|
const newEmail = instance?.email;
|
2023-09-25 06:33:16 +00:00
|
|
|
const oldEmail = ctx?.hookState?.oldInstance?.email;
|
|
|
|
if (!ctx.isNewInstance && (!newEmail || !oldEmail || newEmail == oldEmail)) return;
|
2023-09-20 13:10:28 +00:00
|
|
|
|
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
const httpCtx = {req: loopBackContext.active};
|
|
|
|
const httpRequest = httpCtx.req.http.req;
|
|
|
|
const headers = httpRequest.headers;
|
|
|
|
const origin = headers.origin;
|
|
|
|
const url = origin.split(':');
|
|
|
|
|
2023-10-02 12:51:19 +00:00
|
|
|
const env = process.env.NODE_ENV;
|
|
|
|
const liliumUrl = await Self.app.models.Url.findOne({
|
2023-11-28 12:22:17 +00:00
|
|
|
where: {
|
|
|
|
and: [
|
|
|
|
{appName: 'lilium'},
|
|
|
|
{environment: env}
|
|
|
|
]
|
|
|
|
}
|
2023-10-02 12:51:19 +00:00
|
|
|
});
|
|
|
|
|
2023-09-20 13:10:28 +00:00
|
|
|
class Mailer {
|
|
|
|
async send(verifyOptions, cb) {
|
2024-01-08 13:35:01 +00:00
|
|
|
try {
|
|
|
|
const url = new URL(verifyOptions.verifyHref);
|
|
|
|
if (process.env.NODE_ENV) url.port = '';
|
|
|
|
|
|
|
|
const email = new Email('email-verify', {
|
|
|
|
url: url.href,
|
|
|
|
recipient: verifyOptions.to
|
|
|
|
});
|
|
|
|
await email.send();
|
|
|
|
|
|
|
|
cb(null, verifyOptions.to);
|
|
|
|
} catch (err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
2023-09-20 13:10:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-18 06:52:17 +00:00
|
|
|
|
2023-09-20 13:10:28 +00:00
|
|
|
const options = {
|
|
|
|
type: 'email',
|
2023-09-25 06:33:16 +00:00
|
|
|
to: newEmail,
|
2023-09-20 13:10:28 +00:00
|
|
|
from: {},
|
2023-10-18 06:52:06 +00:00
|
|
|
redirect: `${liliumUrl.url}verifyEmail?userId=${instance.id}`,
|
2023-09-20 13:10:28 +00:00
|
|
|
template: false,
|
|
|
|
mailer: new Mailer,
|
|
|
|
host: url[1].split('/')[2],
|
|
|
|
port: url[2],
|
|
|
|
protocol: url[0],
|
|
|
|
user: Self
|
|
|
|
};
|
|
|
|
|
2023-10-02 12:51:19 +00:00
|
|
|
await instance.verify(options, ctx.options);
|
2023-09-20 13:10:28 +00:00
|
|
|
});
|
2023-01-23 14:24:00 +00:00
|
|
|
};
|