change-password-form.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import Ember from 'ember';
  2. import ValidatedForm from 'misago/components/forms/validated-form';
  3. export default ValidatedForm.extend({
  4. tagName: 'form',
  5. classNames: 'form-horizontal',
  6. isBusy: false,
  7. new_password: '',
  8. repeat_password: '',
  9. password: '',
  10. apiUrl: function() {
  11. return 'users/' + this.auth.get('user.id') + '/change-password';
  12. }.property(),
  13. newPasswordValidation: function() {
  14. var value = Ember.$.trim(this.get('new_password'));
  15. if (value.length === 0) {
  16. this.set('validation.new_password', [gettext('Enter new password.')]);
  17. } else if (value.length < this.get('settings.password_length_min')) {
  18. var limit = this.get('settings.password_length_min');
  19. var message = ngettext('Valid password must be at least %(limit)s character long.',
  20. 'Valid password must be at least %(limit)s characters long.',
  21. limit);
  22. this.set('validation.new_password', [interpolate(message, {limit: limit}, true)]);
  23. } else {
  24. this.set('validation.new_password', 'ok');
  25. }
  26. }.observes('new_password', 'repeat_password'),
  27. repeatPasswordValidation: function() {
  28. var value = Ember.$.trim(this.get('repeat_password'));
  29. if (value.length === 0) {
  30. this.set('validation.repeat_password', [gettext('Repeat new password.')]);
  31. } else if (value !== Ember.$.trim(this.get('new_password'))) {
  32. this.set('validation.repeat_password', [gettext('Password differs from new password.')]);
  33. } else {
  34. this.set('validation.repeat_password', 'ok');
  35. }
  36. }.observes('new_password', 'repeat_password'),
  37. passwordValidation: function() {
  38. var value = Ember.$.trim(this.get('password'));
  39. if (value.length === 0) {
  40. this.set('validation.password', [gettext('Enter current password.')]);
  41. } else {
  42. this.set('validation.password', 'ok');
  43. }
  44. }.observes('password'),
  45. submit: function() {
  46. if (this.get('isBusy')) {
  47. return false;
  48. }
  49. var data = {
  50. new_password: Ember.$.trim(this.get('new_password')),
  51. repeat_password: Ember.$.trim(this.get('repeat_password')),
  52. password: Ember.$.trim(this.get('password')),
  53. };
  54. this.newPasswordValidation();
  55. this.repeatPasswordValidation();
  56. this.passwordValidation();
  57. if (this.hasValidationErrors()) {
  58. this.toast.error(gettext('Form contains errors.'));
  59. return false;
  60. }
  61. this.set('isBusy', true);
  62. var self = this;
  63. this.ajax.post(this.get('apiUrl'), data
  64. ).then(function(response) {
  65. if (self.isDestroyed) { return; }
  66. self.success(response);
  67. }, function(jqXHR) {
  68. if (self.isDestroyed) { return; }
  69. self.error(jqXHR);
  70. }).finally(function() {
  71. if (self.isDestroyed) { return; }
  72. self.set('isBusy', false);
  73. });
  74. return false;
  75. },
  76. success: function(responseJSON) {
  77. this.setProperties({
  78. 'new_password': '',
  79. 'repeat_password': '',
  80. 'password': '',
  81. });
  82. this.resetValidation();
  83. this.toast.info(responseJSON.detail);
  84. },
  85. error: function(jqXHR) {
  86. var rejection = jqXHR.responseJSON;
  87. if (jqXHR.status === 400) {
  88. this.toast.error(gettext('Form contains errors.'));
  89. this.get('validation').setProperties(rejection);
  90. } else {
  91. this.toast.apiError(jqXHR);
  92. }
  93. },
  94. });