diff --git a/app/Classes/Modules/Accounts/ControllersLogic/ResetPasswordLogic.php b/app/Classes/Modules/Accounts/ControllersLogic/ResetPasswordLogic.php new file mode 100644 index 00000000..1058286c --- /dev/null +++ b/app/Classes/Modules/Accounts/ControllersLogic/ResetPasswordLogic.php @@ -0,0 +1,80 @@ + 'Password Change', + 'message' => 'You have successfully changed your account\'t password' + ]; + } + + /** @var CanResetPassword */ + private $canResetPassword; + + /** @var FetchesPasswordReset */ + private $fetchesPasswordReset; + + /** @var ChangesPassword */ + private $changePassword; + + /** @var CompletesPasswordReset */ + private $completesPasswordResetToken; + + /** + * ResetPasswordLogic constructor. + * @param CanResetPassword $canResetPassword + * @param FetchesPasswordReset $fetchesPasswordReset + * @param ChangesPassword $changePassword + * @param CompletesPasswordReset $completesPasswordResetToken + */ + public function __construct(CanResetPassword $canResetPassword, FetchesPasswordReset $fetchesPasswordReset, ChangesPassword $changePassword, CompletesPasswordReset $completesPasswordResetToken) + { + $this->canResetPassword = $canResetPassword; + $this->fetchesPasswordReset = $fetchesPasswordReset; + $this->changePassword = $changePassword; + $this->completesPasswordResetToken = $completesPasswordResetToken; + } + + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\InternalServerErrorException + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function logic(Request $request): JsonResponse { + + $object = new PasswordResetObject($request->input('token'), + new NewPasswordObject($request->input('password'), $request->input('confirmPassword'))); + + $this->canResetPassword->passes($object); + + /** @var PasswordReset $passwordReset */ + $passwordReset = $this->fetchesPasswordReset->execute(['token' => $object->getToken(), 'with_user']); + $this->changePassword->execute($passwordReset->user, $object->getNewPassword()); + $this->completesPasswordResetToken->execute($passwordReset); + + return $this->response(); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/DataTransferObjects/NewPasswordObject.php b/app/Classes/Modules/Accounts/DataTransferObjects/NewPasswordObject.php new file mode 100644 index 00000000..cc3c83e1 --- /dev/null +++ b/app/Classes/Modules/Accounts/DataTransferObjects/NewPasswordObject.php @@ -0,0 +1,45 @@ +password = $password; + $this->ConfirmPassword = $ConfirmPassword; + } + + /** + * @return String + */ + public function getPassword(): String + { + return $this->password; + } + + /** + * @return String + */ + public function getConfirmPassword(): String + { + return $this->ConfirmPassword; + } + + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/DataTransferObjects/PasswordResetObject.php b/app/Classes/Modules/Accounts/DataTransferObjects/PasswordResetObject.php new file mode 100644 index 00000000..ce48043c --- /dev/null +++ b/app/Classes/Modules/Accounts/DataTransferObjects/PasswordResetObject.php @@ -0,0 +1,42 @@ +token = $token; + $this->newPassword = $newPassword; + } + + /** + * @return String + */ + public function getToken(): String + { + return $this->token; + } + + /** + * @return NewPasswordObject + */ + public function getNewPassword(): NewPasswordObject + { + return $this->newPassword; + } +} diff --git a/app/Classes/Modules/Accounts/Services/ChangesPassword.php b/app/Classes/Modules/Accounts/Services/ChangesPassword.php new file mode 100644 index 00000000..f22334ca --- /dev/null +++ b/app/Classes/Modules/Accounts/Services/ChangesPassword.php @@ -0,0 +1,32 @@ +password = Hash::make($object->getPassword()); + return $user->save(); + + } catch (QueryException $exception){ + throw new InternalServerErrorException($exception->getMessage()); + } + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Services/CompletesPasswordReset.php b/app/Classes/Modules/Accounts/Services/CompletesPasswordReset.php new file mode 100644 index 00000000..b733fe81 --- /dev/null +++ b/app/Classes/Modules/Accounts/Services/CompletesPasswordReset.php @@ -0,0 +1,26 @@ +is_complete = true; + return $this->handler($model); + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Services/FetchesPasswordReset.php b/app/Classes/Modules/Accounts/Services/FetchesPasswordReset.php new file mode 100644 index 00000000..2f0a18bc --- /dev/null +++ b/app/Classes/Modules/Accounts/Services/FetchesPasswordReset.php @@ -0,0 +1,34 @@ +repository = $repository; + } + + + /** + * @return Builder + */ + public function getRepository(): Builder + { + return $this->repository->newQuery(); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Standards/Criteria/PasswordResetTokenExists.php b/app/Classes/Modules/Accounts/Standards/Criteria/PasswordResetTokenExists.php new file mode 100644 index 00000000..2ba48718 --- /dev/null +++ b/app/Classes/Modules/Accounts/Standards/Criteria/PasswordResetTokenExists.php @@ -0,0 +1,39 @@ +repository = $repository; + } + + + /** + * @param string $token + * @return bool + * @throws ResourceNotFoundException + */ + public function execute(string $token){ + + if(!$this->repository->active()->where('token', $token)->first()){ + throw new ResourceNotFoundException('The reset password token has expired or doesn\'t exist'); + } + return true; + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Standards/Rules/CanResetPassword.php b/app/Classes/Modules/Accounts/Standards/Rules/CanResetPassword.php new file mode 100644 index 00000000..cf620414 --- /dev/null +++ b/app/Classes/Modules/Accounts/Standards/Rules/CanResetPassword.php @@ -0,0 +1,65 @@ +resetPasswordValidation = $resetPasswordValidation; + $this->passwordResetTokenExists = $passwordResetTokenExists; + } + + + /** + * @return bool + */ + protected function authorized(): bool + { + return true; + + } + + /** + * @param PasswordResetObject $object + * @return bool + * @throws RequestValidationException + */ + protected function validators($object): bool + { + return $this->resetPasswordValidation->validate($object); + + } + + + /** + * @param PasswordResetObject $object + * @return bool + * @throws ResourceNotFoundException + */ + protected function criteria($object): bool + { + return $this->passwordResetTokenExists->execute($object->getToken()); + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Accounts/Standards/Validators/ResetPasswordValidation.php b/app/Classes/Modules/Accounts/Standards/Validators/ResetPasswordValidation.php new file mode 100644 index 00000000..7963dc10 --- /dev/null +++ b/app/Classes/Modules/Accounts/Standards/Validators/ResetPasswordValidation.php @@ -0,0 +1,50 @@ + $object->getToken(), + 'password' => $object->getNewPassword()->getPassword(), + 'confirm_password' => $object->getNewPassword()->getConfirmPassword() + ]; + } + + /** + * @return array + */ + protected function rules(): array { + return [ + 'token' => 'required', + 'password' => 'required|min:6', + 'confirm_password' => 'same:password' + + ]; + } + + /** + * @return array + */ + protected function messages(): array { + return [ + 'confirm_password' => [ + 'same' => 'The :attribute and :other must match.' + ] + ]; + } + + + + +} \ No newline at end of file diff --git a/app/Http/Controllers/Accounts/ResetPasswordController.php b/app/Http/Controllers/Accounts/ResetPasswordController.php new file mode 100644 index 00000000..ab20502f --- /dev/null +++ b/app/Http/Controllers/Accounts/ResetPasswordController.php @@ -0,0 +1,20 @@ +execute($request); + } +} \ No newline at end of file diff --git a/routes/account.php b/routes/account.php index ec56abe6..38fcd6d5 100644 --- a/routes/account.php +++ b/routes/account.php @@ -21,6 +21,7 @@ Route::group(['prefix' => 'account', 'namespace' => 'Accounts', 'as' => 'account Route::group(['prefix' => 'password', 'as' => 'password.'], function () { Route::post('/forget', 'GeneratePasswordResetController@generate')->name('forget'); + Route::post('/reset', 'ResetPasswordController@reset')->name('reset'); }); }); });