From 918863ee8e1e672fa952440472627ddcf6e83f97 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 20 Sep 2022 15:21:01 +0200 Subject: [PATCH] feat(account): recover-password --- back/methods/account/recover-password.js | 40 +++++++++++++++++++ back/models/account.js | 3 +- .../00-acl_recover-password.sql | 3 ++ front/core/services/auth.js | 4 +- .../components/login/recover-password.js | 16 ++++++-- loopback/locale/es.json | 3 +- 6 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 back/methods/account/recover-password.js create mode 100644 db/changes/10500-motherOfGod/00-acl_recover-password.sql diff --git a/back/methods/account/recover-password.js b/back/methods/account/recover-password.js new file mode 100644 index 0000000000..df3faab6be --- /dev/null +++ b/back/methods/account/recover-password.js @@ -0,0 +1,40 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethod('recoverPassword', { + description: 'Send email to the user', + accepts: [ + { + arg: 'email', + type: 'string', + description: 'The email of user', + required: false + } + ], + http: { + path: `/recoverPassword`, + verb: 'GET' + } + }); + + Self.recoverPassword = async function(email) { + console.log('ENTRY'); + const models = Self.app.models; + const user = await models.User.findOne({ + where: { + email: email + } + }); + + if (!user) + throw new UserError(`This email does not belong to a user`); + + const token = await models.Account.login({ + user: email, + password: user.password + }); + + await Self.rawSql(`CALL vn.mail_insert(?,?,?,?)`, + [email, null, 'Recovery Password', `?token=${token}`]); + }; +}; diff --git a/back/models/account.js b/back/models/account.js index ba703c68d7..5ace3b858c 100644 --- a/back/models/account.js +++ b/back/models/account.js @@ -6,6 +6,7 @@ module.exports = 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); // Validations @@ -77,7 +78,7 @@ module.exports = Self => { `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 + JOIN account.role r ON r.id = rr.inheritsFrom WHERE u.id = ?`, [userId], options); let roles = []; diff --git a/db/changes/10500-motherOfGod/00-acl_recover-password.sql b/db/changes/10500-motherOfGod/00-acl_recover-password.sql new file mode 100644 index 0000000000..65e7b7cf38 --- /dev/null +++ b/db/changes/10500-motherOfGod/00-acl_recover-password.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('Account', 'recoverPassword', 'READ', 'ALLOW', 'ROLE', 'account'); diff --git a/front/core/services/auth.js b/front/core/services/auth.js index d14c0bafdc..57e8226f8c 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -30,9 +30,7 @@ export default class Auth { return true; let redirectToLogin = () => { - return transition.router.stateService.target('login', { - continue: this.$window.location.hash - }); + return transition.router.stateService.target('login'); }; if (this.vnToken.token) { diff --git a/front/salix/components/login/recover-password.js b/front/salix/components/login/recover-password.js index 7841329bb5..c02fa46193 100644 --- a/front/salix/components/login/recover-password.js +++ b/front/salix/components/login/recover-password.js @@ -5,16 +5,24 @@ import './style.scss'; * A simple login form. */ export default class Controller { - constructor($, $element) { + constructor($, $element, $http) { Object.assign(this, { $, $element, - user: localStorage.getItem('lastUser'), - remember: true + $http }); } + + submit() { + const params = { + email: this.email + }; + + this.$http.get('Accounts/recoverPassword', params) + .then(() => console.log('try')); // this.vnApp.showMessage(this.$t('Notification sent!'))); + } } -Controller.$inject = ['$scope', '$element']; +Controller.$inject = ['$scope', '$element', '$http']; ngModule.vnComponent('vnRecoverPassword', { template: require('./recover-password.html'), diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 07a00024a8..5ff630a41d 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -232,5 +232,6 @@ "Fichadas impares": "Fichadas impares", "Descanso diario 12h.": "Descanso diario 12h.", "Descanso semanal 36h. / 72h.": "Descanso semanal 36h. / 72h.", - "Dirección incorrecta": "Dirección incorrecta" + "Dirección incorrecta": "Dirección incorrecta", + "This email does not belong to a user": "This email does not belong to a user" } \ No newline at end of file