diff --git a/back/methods/account/recover-password.js b/back/methods/account/recover-password.js new file mode 100644 index 000000000..df3faab6b --- /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 ba703c68d..5ace3b858 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 000000000..65e7b7cf3 --- /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 d14c0bafd..57e8226f8 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 7841329bb..c02fa4619 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 07a00024a..5ff630a41 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