-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from chikh/master
#45 "forgot password" feature POC
- Loading branch information
Showing
7 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
var _ = require('lodash'); | ||
var moment = require('moment'); | ||
|
||
module.exports = function (appUtil, keygrip, forgotPasswordService, baseUrlService, A, injection) { | ||
var service = {}; | ||
|
||
service.generateToken = function (entity) { | ||
return keygrip.sign(entity.username) + keygrip.sign(entity.creationDate.toString()); | ||
}; | ||
|
||
A.app({ | ||
entities: function (Fields, Crud, Security) { //todo: remove direct dependency on MailgunService | ||
var MailgunService = injection.inject('MailgunService', true); | ||
return { | ||
forgotPassword: { | ||
fields: { | ||
username: Fields.text('User').required(), | ||
token: Fields.text('Token').readOnly(), | ||
creationDate: Fields.date('Creation date').readOnly() | ||
}, | ||
layout: { | ||
V: ['username'] | ||
}, | ||
permissions: { | ||
create: null | ||
}, | ||
customView: 'forgotpassword/forgot-password', | ||
views: { | ||
resetPassword: { | ||
fields: { | ||
username: Fields.text('User').required(), | ||
creationDate: Fields.date('Creation date').readOnly(), | ||
newPasswordHash: Fields.password('New password').required(), | ||
repeatNewPasswordHash: Fields.password('Repeat password').required(), | ||
hasTokenBeenUsed: Fields.checkbox('Has token been used?') | ||
}, | ||
layout: { | ||
V: ['newPasswordHash', 'repeatNewPasswordHash'] | ||
}, | ||
customView: 'forgotpassword/reset-password', | ||
beforeUpdate: function (NewEntity, OldEntity) { | ||
if (OldEntity.hasTokenBeenUsed) { | ||
throw new appUtil.ValidationError({ | ||
repeatNewPasswordHash: 'Token has been already used' | ||
}); | ||
} else if (NewEntity.newPasswordHash != NewEntity.repeatNewPasswordHash) { | ||
throw new appUtil.ValidationError({ | ||
repeatNewPasswordHash: 'Passwords doesn\'t match' | ||
}); | ||
} else if (moment().subtract(15, 'minutes') > NewEntity.creationDate) { | ||
throw new appUtil.ValidationError({ | ||
repeatNewPasswordHash: 'Token has expired' | ||
}); | ||
} else { | ||
var userCrud = Crud.crudForEntityType('User'); //todo: what if User entity type would be overridden? | ||
return Security.asSystem(function () { | ||
return userCrud.find({filtering: {username: NewEntity.username}}); | ||
}).then(function (users) { | ||
if (!_.isEmpty(users) && users.length === 1) { | ||
var user = users[0]; | ||
user.passwordHash = NewEntity.newPasswordHash; | ||
return Security.asSystem(function () { | ||
userCrud.updateEntity(user); | ||
}); | ||
} else { | ||
throw new Error('No users with name ' + NewEntity.username); | ||
} | ||
}).then(function () { | ||
NewEntity.hasTokenBeenUsed = true; | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
beforeSave: function (Entity, Dates) { | ||
if (Entity.newPasswordHash || Entity.repeatNewPasswordHash) { | ||
return; | ||
} | ||
var userCrud = Crud.crudForEntityType('User'); //todo: what if User entity type would be overridden? | ||
return Security.asSystem(function () { | ||
return userCrud.find({filtering: {username: Entity.username}}); | ||
}).then(function (users) { | ||
if (!_.isEmpty(users)) { | ||
Entity.creationDate = Dates.now(); | ||
Entity.token = service.generateToken(Entity); | ||
var user = users[0]; | ||
var transport = forgotPasswordService.config.propertyValue('transport').evaluateProperties(); | ||
return MailgunService.sendMessage({ | ||
domain: transport.config.domain, | ||
key: transport.config.key, | ||
message: { | ||
from: transport.config.from, | ||
to: user.email || user.mail || user.username, | ||
subject: 'Password recovery', | ||
text: 'Someone (maybe that was you) requested password change. Follow this ' + | ||
'link to complete the action: ' + | ||
[baseUrlService.getBaseUrl(), 'entity', 'resetPassword', Entity.token].join('/') + | ||
' Or just ignore this email if it wasn\'t you.' //todo: clean the text and implement html message | ||
} | ||
}); | ||
} else { | ||
throw new appUtil.ValidationError({ | ||
username: 'Can\'t find user with name "' + Entity.username + '"' | ||
}); | ||
} | ||
}); | ||
|
||
} | ||
} | ||
}; | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = function () { | ||
var service = {}; | ||
|
||
service.config = {}; | ||
|
||
service.compile = function (objects) { | ||
objects.forEach(function (obj) { | ||
var config = obj.propertyValue('forgotPassword'); | ||
if (config) { | ||
service.config = config; | ||
} | ||
}); | ||
}; | ||
|
||
return service; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
extends main | ||
include mixins | ||
|
||
block vars | ||
|
||
block content | ||
div(ng-app='allcount', ng-controller='EntityViewController' ng-cloak) | ||
.container.screen-container(ng-controller='ForgotPasswordCtrl') | ||
+defaultCreateForm()(ng-show="true") | ||
+defaultFormTemplate() | ||
.form-group | ||
button.btn.btn-block.btn-default(lc-tooltip='Submit' ng-click='submitUsername()') | ||
i(class='glyphicon glyphicon-ok') | ||
.form-group | ||
p.bg-success(ng-show='successMessage') {{successMessage}} | ||
p.bg-danger(ng-show='errorMessage') {{errorMessage}} | ||
block js | ||
+entityJs() | ||
script. | ||
angular.module('allcount').controller('ForgotPasswordCtrl', ['$scope', 'lcApi', '$q', function ($scope, lcApi, $q) { | ||
$scope.submitUsername = function () { | ||
return $q.when($scope.viewState.createForm.entity().username).then(function (username) { | ||
return lcApi.createEntity({entityTypeId: 'forgotPassword'}, {username: username}); | ||
}).then(function (id) { | ||
if (id) { | ||
$scope.successMessage = 'Further instructions are sent'; | ||
delete $scope.errorMessage; | ||
} | ||
}, function (err) { | ||
if (err) { | ||
delete $scope.successMessage; | ||
$scope.errorMessage = err.data.username; | ||
} | ||
}); | ||
} | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
extends main | ||
include mixins | ||
|
||
block vars | ||
|
||
block content | ||
div(ng-app='allcount', ng-controller='EntityViewController' ng-cloak) | ||
.container.screen-container(ng-controller='ResetPasswordCtrl') | ||
+defaultEditForm()(ng-show='doesTokenExists' entity-id='actualEntityId' is-editor='true') | ||
+defaultFormTemplate() | ||
.form-group | ||
button.btn.btn-block.btn-default(lc-tooltip='Submit' ng-click='submitPassword()' ng-show='doesTokenExists') | ||
i(class='glyphicon glyphicon-ok') | ||
.form-group | ||
p.bg-danger(ng-show='errorMessage') {{errorMessage}} | ||
p.bg-success(ng-show='successMessage') {{successMessage}} | ||
block js | ||
+entityJs() | ||
script. | ||
angular.module('allcount').controller('ResetPasswordCtrl', ['$scope', 'lcApi', '$q', function ($scope, lcApi, $q) { | ||
$scope.doesTokenExists = false; | ||
$scope.successMessage = undefined; | ||
$scope.errorMessage = undefined; | ||
$scope.$watch('viewState.formEntityId', function (token) { | ||
return lcApi.findRange({entityTypeId: 'forgotPassword'}, { | ||
filtering: { | ||
token: token | ||
} | ||
}).then(function (tokens) { | ||
if (tokens && tokens.length > 0) { | ||
$scope.doesTokenExists = true; | ||
$scope.actualEntityId = tokens[0].id; | ||
} else { | ||
$scope.errorMessage = 'Token doesn\'t exists or it has been expired' | ||
$scope.successMessage = undefined; | ||
} | ||
}); | ||
}); | ||
$scope.submitPassword = function () { | ||
return $q.when($scope.viewState.editForm.entity()).then(function (entity) { | ||
return lcApi.updateEntity({entityTypeId: 'resetPassword'}, entity); | ||
}).then(function (res) { | ||
if (res.username) { | ||
$scope.successMessage = 'Password has been successfully changed'; | ||
$scope.errorMessage = undefined; | ||
} | ||
}, function (err) { | ||
$scope.errorMessage = err.data.repeatNewPasswordHash || err; | ||
$scope.successMessage = undefined; | ||
}); | ||
}; | ||
}]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters