password.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const Mn = require('backbone.marionette');
  2. const App = require('../main');
  3. const template = require('./password.ejs');
  4. require('jquery-serializejson');
  5. module.exports = Mn.View.extend({
  6. template: template,
  7. className: 'modal-dialog',
  8. ui: {
  9. form: 'form',
  10. buttons: '.modal-footer button',
  11. cancel: 'button.cancel',
  12. save: 'button.save',
  13. newSecretError: '.new-secret-error',
  14. generalError: '#error-info',
  15. },
  16. events: {
  17. 'click @ui.save': function (e) {
  18. e.preventDefault();
  19. this.ui.newSecretError.hide();
  20. this.ui.generalError.hide();
  21. let form = this.ui.form.serializeJSON();
  22. if (form.new_password1 !== form.new_password2) {
  23. this.ui.newSecretError.text('Passwords do not match!').show();
  24. return;
  25. }
  26. let data = {
  27. type: 'password',
  28. current: form.current_password,
  29. secret: form.new_password1
  30. };
  31. this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
  32. App.Api.Users.setPassword(this.model.get('id'), data)
  33. .then(() => {
  34. App.UI.closeModal();
  35. App.Controller.showUsers();
  36. })
  37. .catch(err => {
  38. // Change error message to make it a little clearer
  39. if (err.message === 'Invalid password') {
  40. err.message = 'Current password is invalid';
  41. }
  42. this.ui.generalError.text(err.message).show();
  43. this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
  44. });
  45. }
  46. },
  47. isSelf: function () {
  48. return App.Cache.User.get('id') === this.model.get('id');
  49. },
  50. templateContext: function () {
  51. return {
  52. isSelf: this.isSelf.bind(this)
  53. };
  54. },
  55. onRender: function () {
  56. this.ui.newSecretError.hide();
  57. this.ui.generalError.hide();
  58. },
  59. });