From e9d31f60208c9f81cd2c13093be414ba04a266c4 Mon Sep 17 00:00:00 2001
From: Eugen Ciur <eugen@papermerge.com>
Date: Thu, 21 Oct 2021 19:57:53 +0200
Subject: [PATCH] basic change of the password functionality

---
 app/adapters/user.js                    | 18 ++++++++++++++++++
 app/components/user/change_password.hbs |  2 ++
 app/components/user/change_password.js  | 17 ++++++++++++++++-
 app/models/user.js                      |  6 ++++++
 4 files changed, 42 insertions(+), 1 deletion(-)
 create mode 100644 app/adapters/user.js

diff --git a/app/adapters/user.js b/app/adapters/user.js
new file mode 100644
index 0000000..facc00c
--- /dev/null
+++ b/app/adapters/user.js
@@ -0,0 +1,18 @@
+import ApplicationAdapter from './application';
+
+
+export default class UserAdapter extends ApplicationAdapter {
+
+  _defaultContentType = 'application/json';
+
+  changePassword(model, new_password) {
+    const url = this.buildURL('user', model.id) + 'change-password/';
+
+    return this.ajax(url, 'POST',  {
+      data: {
+        'password': new_password
+      }
+    });
+  }
+
+}
\ No newline at end of file
diff --git a/app/components/user/change_password.hbs b/app/components/user/change_password.hbs
index 3412bf4..b4f74d8 100644
--- a/app/components/user/change_password.hbs
+++ b/app/components/user/change_password.hbs
@@ -18,6 +18,8 @@
 
 <div class="mb-3">
   <Button::Submit
+    @text="Change Password"
+    @disabled={{this.disabled}}
     @onClick={{this.onSubmit}} />
   <Button::Cancel @route="users"/>
 </div>
diff --git a/app/components/user/change_password.js b/app/components/user/change_password.js
index b356586..951fc42 100644
--- a/app/components/user/change_password.js
+++ b/app/components/user/change_password.js
@@ -10,11 +10,26 @@ class ChangeUserPasswordComponent extends Component {
   @tracked new_password_1;
   @tracked new_password_2;
 
+  get disabled() {
+    /**
+    * If both password inputs are empty submit button is disabled.
+    * Also, submit form button will be disabled if given
+    * passwords do not match.
+    */
+    if (this.new_password_1 && new_password_2) {
+      if (this.new_password_1 === this.new_password_2) {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
   @action
   onSubmit() {
     let user = this.args.user;
 
-    user.save();
+    user.changePassword(this.new_password_1);
     this.router.transitionTo('users');
   }
 }
diff --git a/app/models/user.js b/app/models/user.js
index 01f5c18..6b6ac8e 100644
--- a/app/models/user.js
+++ b/app/models/user.js
@@ -13,6 +13,12 @@ class UserModel extends Model {
   @attr created_at;
   @attr updated_at;
   @belongsTo('role') role;
+
+  changePassword(new_password) {
+    const adapter = this.store.adapterFor('user');
+
+    return adapter.changePassword(this, new_password);
+  }
 }
 
 export default UserModel;
-- 
GitLab