-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
521 additions
and
210 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::Profile::PasswordController < ApiController | ||
self.permitted_attrs = [:old_password, :new_password1, :new_password2] | ||
|
||
def update | ||
authorize current_user, :update_password? | ||
if password_params_valid? | ||
current_user.update_password(old_password, | ||
new_password) | ||
add_info('flashes.profile.changePassword.success') | ||
ok_status = 200 | ||
end | ||
render_json(nil, ok_status || 400) | ||
end | ||
|
||
def password_params_valid? | ||
unless current_user.authenticate_db(old_password) | ||
add_error('helpers.label.user.wrongPassword') | ||
return false | ||
end | ||
|
||
unless new_passwords_match? | ||
add_error('flashes.profile.changePassword.new_passwords_not_equal') | ||
return false | ||
end | ||
true | ||
end | ||
|
||
private | ||
|
||
def old_password | ||
model_params[:old_password] | ||
end | ||
|
||
def new_password | ||
model_params[:new_password1] if new_passwords_match? | ||
end | ||
|
||
def new_passwords_match? | ||
model_params[:new_password1] == model_params[:new_password2] | ||
end | ||
end |
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
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 |
---|---|---|
|
@@ -34,6 +34,10 @@ def unlock? | |
own_api_user? | ||
end | ||
|
||
def update_password? | ||
false | ||
end | ||
|
||
private | ||
|
||
def own_api_user? | ||
|
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 |
---|---|---|
|
@@ -69,6 +69,10 @@ def resetpassword? | |
end | ||
end | ||
|
||
def update_password? | ||
user.auth_db? | ||
end | ||
|
||
private | ||
|
||
def current_user | ||
|
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
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,95 @@ | ||
import Component from "@glimmer/component"; | ||
import { tracked } from "@glimmer/tracking"; | ||
import { action } from "@ember/object"; | ||
import UserHumanPasswordEditValidations from "../../validations/user-human/passwordEdit"; | ||
import lookupValidator from "ember-changeset-validations"; | ||
import Changeset from "ember-changeset"; | ||
import { inject as service } from "@ember/service"; | ||
import ENV from "../../config/environment"; | ||
|
||
export default class ProfilePasswordUpdateComponent extends Component { | ||
@service fetchService; | ||
@service notify; | ||
@service intl; | ||
|
||
@tracked isEditing = false; | ||
|
||
UserHumanPasswordEditValidations = UserHumanPasswordEditValidations; | ||
|
||
@tracked | ||
oldPasswordIncorrectError = ""; | ||
showSuccessMessage() { | ||
let translationKeyPrefix = this.intl.locale[0].replace("-", "_"); | ||
let successMsg = `${translationKeyPrefix}.flashes.profile.changePassword.success`; | ||
let msg = this.intl.t(successMsg); | ||
this.notify.success(msg); | ||
} | ||
constructor() { | ||
super(...arguments); | ||
let passwordChangeset = { | ||
oldPassword: "", | ||
newPassword1: "", | ||
newPassword2: "" | ||
}; | ||
this.changeset = new Changeset( | ||
passwordChangeset, | ||
lookupValidator(UserHumanPasswordEditValidations), | ||
UserHumanPasswordEditValidations | ||
); | ||
} | ||
@action | ||
toggleEditing() { | ||
this.isEditing = !this.isEditing; | ||
} | ||
@action | ||
resetOldPasswordError() { | ||
this.oldPasswordIncorrectError = ""; | ||
} | ||
@action | ||
async submit() { | ||
await this.changeset.validate(); | ||
if (!this.changeset.isValid) return; | ||
const requestBody = { | ||
data: { | ||
attributes: { | ||
old_password: this.changeset.oldPassword, | ||
new_password1: this.changeset.newPassword1, | ||
new_password2: this.changeset.newPassword2 | ||
} | ||
} | ||
}; | ||
this.fetchService | ||
.send("/api/profile/password", { | ||
method: "PATCH", | ||
headers: { | ||
Accept: "application/vnd.api+json", | ||
"Content-Type": "application/json", | ||
"X-CSRF-Token": ENV.CSRFToken | ||
}, | ||
body: JSON.stringify(requestBody) | ||
}) | ||
.then((response) => { | ||
if (response.ok) { | ||
this.showSuccessMessage(); | ||
this.toggleEditing(); | ||
} else { | ||
response.json().then((json) => { | ||
this.oldPasswordIncorrectError = json.errors[0]; | ||
}); | ||
} | ||
}); | ||
} | ||
get isUserAllowedToChangePassword() { | ||
return ENV.currentUserAuth === "db"; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,8 +1,4 @@ | ||
<div class="pt-3 mb-3"> | ||
<LinkTo @route="index"> | ||
<span class="btn btn-secondary admin-back-button" role="button"> | ||
<img class="icon-button account-show-back" src="/assets/images/arrow-left.svg" alt="back"> | ||
</span> | ||
</LinkTo> | ||
<IndexButton></IndexButton> | ||
{{outlet}} | ||
</div> |
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,5 @@ | ||
<LinkTo @route="index"> | ||
<span class="btn btn-secondary admin-back-button" role="button"> | ||
<img class="icon-button account-show-back" src="/assets/images/arrow-left.svg" alt="back"> | ||
</span> | ||
</LinkTo> |
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
Oops, something went wrong.