From 73fb940c6329203df9c1c1dde0b39699d9555a04 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 8 May 2023 10:55:16 +0200 Subject: [PATCH] refs #5472 try change-password --- back/models/vn-user.js | 13 +++++---- db/changes/231601/00-userPassExpired.sql | 20 ++++++++++++++ db/dump/fixtures.sql | 4 +-- .../salix/components/change-password/index.js | 27 ++++++++++++++++--- front/salix/components/login/index.js | 9 +++---- front/salix/routes.js | 2 +- .../account/specs/change-password.spec.js | 10 ++++++- 7 files changed, 68 insertions(+), 17 deletions(-) diff --git a/back/models/vn-user.js b/back/models/vn-user.js index 45b70fc77..f20ca9152 100644 --- a/back/models/vn-user.js +++ b/back/models/vn-user.js @@ -110,25 +110,28 @@ module.exports = function(Self) { const _setPassword = Self.setPassword; Self.setPassword = async function(id, newPassword, options, cb) { + console.log('Entry in override SET_PASSWORD'); await Self.rawSql(`CALL account.user_setPassword(?, ?)`, [id, newPassword]); - console.log('Entry in override SET_PASSWORD'); await Self.app.models.Account.syncById(id, newPassword); await _setPassword.call(this, id, newPassword, options, cb); - const user = await Self.findById(id); await user.updateAttribute('passExpired', null); return; }; const _changePassword = Self.changePassword; - Self.changePassword = async function(id = 9, oldPassword, newPassword, options, cb) { - console.log(id, oldPassword, newPassword); + Self.changePassword = async function(id, oldPassword, newPassword, options, cb) { + if (options && options.id) id = options.id; + console.log(id, oldPassword, newPassword, options); + await Self.rawSql(`CALL account.user_changePassword(?, ?, ?)`, [id, oldPassword, newPassword]); console.log('Entry in override CHANGE_PASSWORD'); await _changePassword.call(this, id, oldPassword, newPassword, options, cb); - return; + + const user = await Self.findById(id); + await user.updateAttribute('passExpired', null); }; // FIX THIS diff --git a/db/changes/231601/00-userPassExpired.sql b/db/changes/231601/00-userPassExpired.sql index 3cf9c4b6f..c076369ca 100644 --- a/db/changes/231601/00-userPassExpired.sql +++ b/db/changes/231601/00-userPassExpired.sql @@ -1 +1,21 @@ ALTER TABLE `account`.`user` ADD passExpired DATE DEFAULT NULL; + +DROP TRIGGER IF EXISTS `account`.`user_beforeUpdate`; +USE account; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`localhost` TRIGGER `account`.`user_beforeUpdate` + BEFORE UPDATE ON `user` + FOR EACH ROW +BEGIN + IF !(NEW.`name` <=> OLD.`name`) THEN + CALL user_checkName (NEW.`name`); + END IF; + + IF !(NEW.`password` <=> OLD.`password`) THEN + SET NEW.lastPassChange = util.VN_NOW(); + END IF; +END$$ +DELIMITER ; +USE vn; diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index e69974d08..9e904efbb 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -71,8 +71,8 @@ INSERT INTO `account`.`roleConfig`(`id`, `mysqlPassword`, `rolePrefix`, `userPre CALL `account`.`role_sync`; -INSERT INTO `account`.`user`(`id`,`name`, `nickname`, `password`,`role`,`active`,`email`, `lang`, `image`, `bcryptPassword`) - SELECT id, name, CONCAT(name, 'Nick'),MD5('nightmare'), id, 1, CONCAT(name, '@mydomain.com'), 'en', '4fa3ada0-3ac4-11eb-9ab8-27f6fc3b85fd', '$2b$10$UzQHth.9UUQ1T5aiQJ21lOU0oVlbxoqH4PFM9V8T90KNSAcg0eEL2' +INSERT INTO `account`.`user`(`id`,`name`, `nickname`, `password`,`role`,`active`,`email`, `lang`, `image`, `bcryptPassword`, `passExpired`) + SELECT id, name, CONCAT(name, 'Nick'),MD5('nightmare'), id, 1, CONCAT(name, '@mydomain.com'), 'en', '4fa3ada0-3ac4-11eb-9ab8-27f6fc3b85fd', '$2b$10$UzQHth.9UUQ1T5aiQJ21lOU0oVlbxoqH4PFM9V8T90KNSAcg0eEL2', '1999-01-01' FROM `account`.`role` WHERE id <> 20 ORDER BY id; diff --git a/front/salix/components/change-password/index.js b/front/salix/components/change-password/index.js index a79e7a8a9..9178540a0 100644 --- a/front/salix/components/change-password/index.js +++ b/front/salix/components/change-password/index.js @@ -15,6 +15,9 @@ export default class Controller { } $onInit() { + if (!this.$state.params || !this.$state.params.id) + this.$state.go('login'); + this.$http.get('UserPasswords/findOne') .then(res => { this.passRequirements = res.data; @@ -31,10 +34,25 @@ export default class Controller { throw new UserError(`Passwords don't match`); const headers = { - Authorization: {principalType: 'VnUser'} + Authorization: {id: 9}, + id: 9 }; - this.$http.post('VnUsers/change-password', {oldPassword, newPassword}, {headers}) + console.log(this.$state.params.id); + const id = this.$state.params.id; + this.$http.post('VnUsers/change-password', + { + id: 9, + oldPassword, + newPassword, + accessToken: 'hola5', + options: { + id: 9, + accessToken: {id: 9} + } + }, + {headers, id: 9}, + {id: 9}) .then(() => { this.vnApp.showSuccess(this.$translate.instant('Password updated!')); this.$state.go('login'); @@ -45,5 +63,8 @@ Controller.$inject = ['$scope', '$element', '$http', 'vnApp', '$translate', '$st ngModule.vnComponent('vnChangePassword', { template: require('./index.html'), - controller: Controller + controller: Controller, + bindings: { + id: '<' + } }); diff --git a/front/salix/components/login/index.js b/front/salix/components/login/index.js index c7edb2339..554697daf 100644 --- a/front/salix/components/login/index.js +++ b/front/salix/components/login/index.js @@ -1,6 +1,5 @@ import ngModule from '../../module'; import './style.scss'; -import UserError from 'core/lib/user-error'; /** * A simple login form. @@ -28,11 +27,11 @@ export default class Controller { this.loading = false; this.password = ''; this.focusUser(); - // console.log(req.data.error); - // console.log(req.data.error.code); - if (req.data.error.code == 'passExpired') - this.$state.go('change-password'); + if (req.data.error.code == 'passExpired') { + const [args] = req.data.error.translateArgs; + this.$state.go('change-password', args); + } throw req; }); diff --git a/front/salix/routes.js b/front/salix/routes.js index c649236a3..58b866731 100644 --- a/front/salix/routes.js +++ b/front/salix/routes.js @@ -35,7 +35,7 @@ function config($stateProvider, $urlRouterProvider) { }) .state('change-password', { parent: 'outLayout', - url: '/change-password', + url: '/change-password?id', description: 'Change password', template: '' }) diff --git a/modules/account/back/methods/account/specs/change-password.spec.js b/modules/account/back/methods/account/specs/change-password.spec.js index 17fadb3c6..1eff5de67 100644 --- a/modules/account/back/methods/account/specs/change-password.spec.js +++ b/modules/account/back/methods/account/specs/change-password.spec.js @@ -1,6 +1,6 @@ const {models} = require('vn-loopback/server/server'); -describe('account changePassword()', () => { +fdescribe('account changePassword()', () => { it('should throw an error when old password is wrong', async() => { let err; await models.Account.changePassword(1, 'wrongPassword', 'nightmare.9999') @@ -9,4 +9,12 @@ describe('account changePassword()', () => { expect(err).toBeDefined(); expect(err).toEqual('Invalid password'); }); + + it('should change password', async() => { + try { + await models.Account.changePassword(70, 'nightmare', 'nightmare.9999'); + } catch (e) { + expect(e).toBeUndefined(); + } + }); });