Merge branch 'test' into dev

This commit is contained in:
Juan Ferrer 2020-11-13 10:49:59 +01:00
commit 80cbb53dfe
5 changed files with 53 additions and 66 deletions

View File

@ -1,4 +1,5 @@
const md5 = require('md5'); const md5 = require('md5');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('login', { Self.remoteMethod('login', {
@ -12,7 +13,7 @@ module.exports = Self => {
}, { }, {
arg: 'password', arg: 'password',
type: 'String', type: 'String',
description: 'The user name or email' description: 'The password'
} }
], ],
returns: { returns: {
@ -29,44 +30,41 @@ module.exports = Self => {
let $ = Self.app.models; let $ = Self.app.models;
let token; let token;
let usesEmail = user.indexOf('@') !== -1; let usesEmail = user.indexOf('@') !== -1;
let userInfo = usesEmail let userInfo = usesEmail
? {email: user} ? {email: user}
: {username: user}; : {username: user};
let instance = await $.User.findOne({
fields: ['username', 'password'],
where: userInfo
});
let loginInfo = Object.assign({password}, userInfo); let where = usesEmail
? {email: user}
: {name: user};
let account = await Self.findOne({
fields: ['active', 'password'],
where
});
let validCredentials = instance && (
await instance.hasPassword(password) ||
account.password == md5(password || '')
);
if (validCredentials) {
if (!account.active)
throw new UserError('User disabled');
try {
token = await $.User.login(loginInfo, 'user');
try { try {
let instance = await $.User.findOne({
fields: ['username'],
where: userInfo
});
await $.UserAccount.sync(instance.username, password); await $.UserAccount.sync(instance.username, password);
} catch (err) { } catch (err) {
console.warn(err); console.warn(err);
} }
} catch (err) {
if (err.code != 'LOGIN_FAILED')
throw err;
let where = usesEmail
? {email: user}
: {name: user};
Object.assign(where, {
password: md5(password || '')
});
let instance = await Self.findOne({
fields: ['name'],
where
});
if (!instance) throw err;
await $.UserAccount.sync(instance.name, password);
token = await $.User.login(loginInfo, 'user');
} }
let loginInfo = Object.assign({password}, userInfo);
token = await $.User.login(loginInfo, 'user');
return {token: token.id}; return {token: token.id};
}; };
}; };

View File

@ -3,7 +3,7 @@
"base": "User", "base": "User",
"options": { "options": {
"mysql": { "mysql": {
"table": "salix.user" "table": "salix.User"
} }
}, },
"properties": { "properties": {

View File

@ -13,10 +13,24 @@ ALTER TABLE account.ldapConfig MODIFY COLUMN password varchar(255) NOT NULL COMM
ALTER TABLE account.sambaConfig DROP COLUMN sshUser; ALTER TABLE account.sambaConfig DROP COLUMN sshUser;
ALTER TABLE account.sambaConfig DROP COLUMN sshPassword; ALTER TABLE account.sambaConfig DROP COLUMN sshPassword;
ALTER TABLE account.sambaConfig CHANGE host adController varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL NULL COMMENT 'The hosname of domain controller'; ALTER TABLE account.sambaConfig CHANGE host adController varchar(255) DEFAULT NULL NULL COMMENT 'The hosname of domain controller';
ALTER TABLE account.sambaConfig MODIFY COLUMN adController varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL NULL COMMENT 'The hosname of domain controller'; ALTER TABLE account.sambaConfig MODIFY COLUMN adController varchar(255) DEFAULT NULL NULL COMMENT 'The hosname of domain controller';
ALTER TABLE account.sambaConfig DROP COLUMN userDn; ALTER TABLE account.sambaConfig DROP COLUMN userDn;
ALTER TABLE account.sambaConfig ADD adDomain varchar(255) NOT NULL AFTER id; ALTER TABLE account.sambaConfig ADD adDomain varchar(255) NOT NULL AFTER id;
ALTER TABLE account.sambaConfig ADD verifyCert TINYINT UNSIGNED NOT NULL DEFAULT TRUE AFTER adPassword; ALTER TABLE account.sambaConfig ADD verifyCert TINYINT UNSIGNED NOT NULL DEFAULT TRUE AFTER adPassword;
ALTER TABLE account.sambaConfig MODIFY COLUMN adController varchar(255) NOT NULL COMMENT 'The hosname of domain controller'; ALTER TABLE account.sambaConfig MODIFY COLUMN adController varchar(255) NOT NULL COMMENT 'The hosname of domain controller';
ALTER TABLE account.user
ADD COLUMN `realm` varchar(512) CHARACTER SET utf8 DEFAULT NULL AFTER id,
ADD COLUMN `emailVerified` tinyint(1) DEFAULT NULL AFTER email,
ADD COLUMN `verificationToken` varchar(512) DEFAULT NULL AFTER emailVerified;
DROP TABLE salix.user;
CREATE OR REPLACE VIEW salix.User
AS SELECT id, realm, name AS username, bcryptPassword AS password, email, emailVerified, verificationToken
FROM account.user;
ALTER TABLE account.`user`
MODIFY COLUMN bcryptPassword varchar(512) DEFAULT NULL NULL;

View File

@ -9,6 +9,14 @@ module.exports = Self => {
this.synchronizers.push(synchronizer); this.synchronizers.push(synchronizer);
}, },
async getInstance() {
let instance = await Self.findOne({
fields: ['homedir', 'shell', 'idBase']
});
await instance.synchronizerInit();
return instance;
},
async syncUsers() { async syncUsers() {
let instance = await Self.getInstance(); let instance = await Self.getInstance();
@ -50,14 +58,6 @@ module.exports = Self => {
async getSynchronizer() { async getSynchronizer() {
return await Self.findOne(); return await Self.findOne();
},
async getInstance() {
let instance = await Self.findOne({
fields: ['homedir', 'shell', 'idBase']
});
await instance.synchronizerInit();
return instance;
} }
}); });
@ -171,34 +171,8 @@ module.exports = Self => {
}, },
async syncUser(userName, info, password) { async syncUser(userName, info, password) {
let $ = app.models; if (info.user)
let {user} = info; await app.models.user.setPassword(info.user.id, password);
if (user && user.active) {
let bcryptPassword = password
? $.User.hashPassword(password)
: user.bcryptPassword;
await $.Account.upsertWithWhere({id: user.id},
{bcryptPassword}
);
let dbUser = {
id: user.id,
username: userName,
email: user.email,
created: user.created,
updated: user.updated
};
if (bcryptPassword)
dbUser.password = bcryptPassword;
if (await $.user.exists(user.id))
await $.user.replaceById(user.id, dbUser);
else
await $.user.create(dbUser);
} else
await $.user.destroyAll({username: userName});
}, },
async getUsers(usersToSync) { async getUsers(usersToSync) {

View File

@ -3,6 +3,7 @@
"name": "Workers", "name": "Workers",
"icon" : "icon-worker", "icon" : "icon-worker",
"validations" : true, "validations" : true,
"dependencies": ["account"],
"menus": { "menus": {
"main": [ "main": [
{"state": "worker.index", "icon": "icon-worker"}, {"state": "worker.index", "icon": "icon-worker"},