-
Notifications
You must be signed in to change notification settings - Fork 80
Password change #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Password change #655
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
beb2f9a
Implement change password command helper
dvlpp bd290bb
Add rate limiting
dvlpp 1f36feb
Naming
dvlpp 6c50e6a
Add tests
dvlpp 421b078
Document + add config for current_password
dvlpp 50ce3b8
Improve doc
dvlpp f9bb18d
Update docs/guide/authentication.md
dvlpp 0670504
Update docs/guide/authentication.md
dvlpp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,35 @@ | ||
| <?php | ||
|
|
||
| namespace App\Sharp\Profile\Commands; | ||
|
|
||
| use Code16\Sharp\Auth\Password\Command\IsChangePasswordCommandTrait; | ||
| use Code16\Sharp\EntityList\Commands\SingleInstanceCommand; | ||
| use Illuminate\Validation\Rules\Password; | ||
|
|
||
| class ChangePasswordCommand extends SingleInstanceCommand | ||
| { | ||
| use IsChangePasswordCommandTrait; | ||
|
|
||
| public function buildCommandConfig(): void | ||
| { | ||
| $this->configureConfirmPassword() | ||
| ->configurePasswordRule( | ||
| Password::min(8) | ||
| ->numbers() | ||
| ->symbols() | ||
| ->uncompromised() | ||
| ); | ||
| } | ||
|
|
||
| protected function executeSingle(array $data): array | ||
| { | ||
| // We do not really update the password in the context of the demo | ||
| // auth()->user()->update([ | ||
| // 'password' => $data['new_password'], | ||
| // ]); | ||
|
|
||
| $this->notify('Password updated!'); | ||
|
|
||
| return $this->reload(); | ||
| } | ||
| } |
57 changes: 0 additions & 57 deletions
57
demo/app/Sharp/Profile/Commands/UpdateProfilePasswordCommand.php
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
99 changes: 99 additions & 0 deletions
99
src/Auth/Password/Command/IsChangePasswordCommandTrait.php
This file contains hidden or 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,99 @@ | ||
| <?php | ||
|
|
||
| namespace Code16\Sharp\Auth\Password\Command; | ||
|
|
||
| use Code16\Sharp\Exceptions\Form\SharpApplicativeException; | ||
| use Code16\Sharp\Form\Fields\SharpFormTextField; | ||
| use Code16\Sharp\Utils\Fields\FieldsContainer; | ||
| use Illuminate\Support\Facades\RateLimiter; | ||
| use Illuminate\Validation\Rules\Password; | ||
|
|
||
| trait IsChangePasswordCommandTrait | ||
| { | ||
| private bool $confirmPassword = false; | ||
| private bool $validateCurrentPassword = true; | ||
| private ?Password $passwordRule = null; | ||
|
|
||
| public function label(): ?string | ||
| { | ||
| return trans('sharp::auth.password_change.command.label'); | ||
| } | ||
|
|
||
| public function buildFormFields(FieldsContainer $formFields): void | ||
| { | ||
| $formFields | ||
| ->when( | ||
| $this->validateCurrentPassword, | ||
| fn (FieldsContainer $formFields) => $formFields->addField( | ||
| SharpFormTextField::make('password') | ||
| ->setLabel(trans('sharp::auth.password_change.command.fields.current_password')) | ||
| ->setInputTypePassword() | ||
| ) | ||
| ) | ||
| ->addField( | ||
| SharpFormTextField::make('new_password') | ||
| ->setLabel(trans('sharp::auth.password_change.command.fields.new_password')) | ||
| ->setInputTypePassword() | ||
| ) | ||
| ->when( | ||
| $this->confirmPassword, | ||
| fn (FieldsContainer $formFields) => $formFields->addField( | ||
| SharpFormTextField::make('new_password_confirmation') | ||
| ->setLabel(trans('sharp::auth.password_change.command.fields.new_password_confirm')) | ||
| ->setInputTypePassword() | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public function rules(): array | ||
| { | ||
| $rules = RateLimiter::attempt( | ||
| 'sharp-password-change-'.auth()->id(), | ||
| 3, | ||
| fn () => [ | ||
| ...$this->validateCurrentPassword | ||
| ? ['password' => ['required', 'current_password']] | ||
| : [], | ||
| 'new_password' => [ | ||
| 'required', | ||
| 'string', | ||
| $this->passwordRule ?? Password::min(8), | ||
| ...$this->confirmPassword ? ['confirmed'] : [], | ||
| ], | ||
| ], | ||
| ); | ||
|
|
||
| if (! $rules) { | ||
| throw new SharpApplicativeException( | ||
| trans( | ||
| 'sharp::auth.password_change.command.rate_limit_exceeded', [ | ||
| 'seconds' => RateLimiter::availableIn('sharp-password-change-'.auth()->id()), | ||
| ] | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return $rules; | ||
| } | ||
|
|
||
| protected function configureConfirmPassword(?bool $confirmPassword = true): self | ||
| { | ||
| $this->confirmPassword = $confirmPassword; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| protected function configureValidateCurrentPassword(?bool $validateCurrentPassword = true): self | ||
| { | ||
| $this->validateCurrentPassword = $validateCurrentPassword; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| protected function configurePasswordRule(Password $passwordRule): self | ||
| { | ||
| $this->passwordRule = $passwordRule; | ||
|
|
||
| return $this; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.