From d53efc31b8f3c4e956d8cfeed89f0aafb3a85881 Mon Sep 17 00:00:00 2001 From: marcio1002 Date: Thu, 26 Oct 2023 18:11:47 -0300 Subject: [PATCH] Addition of the attribute to hash the password and other changes --- app/Exceptions/Handler.php | 101 +++++++++++++++++- .../Auth/ResetPasswordController.php | 2 +- app/Imports/UsersWithSmartUsersImport.php | 11 +- app/Models/User.php | 23 ++-- routes/api.php | 2 + routes/web.php | 17 +-- 6 files changed, 125 insertions(+), 31 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 82a37e4..7639798 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,8 +2,19 @@ namespace App\Exceptions; +use Illuminate\Http\Response; +use Illuminate\Support\Facades\App; +use Illuminate\Database\QueryException; +use Illuminate\Auth\AuthenticationException; +use Illuminate\Validation\ValidationException; +use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; -use Throwable; + +use Laravel\Sanctum\Exceptions\MissingAbilityException; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Symfony\Component\HttpKernel\Exception\HttpException; +use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class Handler extends ExceptionHandler { @@ -36,6 +47,76 @@ class Handler extends ExceptionHandler 'password_confirmation', ]; + private function messageCustom(\Throwable $ex): array + { + + $messageCustom = []; + + $class_exceptions = [ + AuthorizationException::class => fn ($e) => [ + 'Unauthorized', + Response::HTTP_UNAUTHORIZED + ], + + AuthenticationException::class => fn ($e) => [ + 'Unauthorized', + Response::HTTP_UNAUTHORIZED + ], + + ValidationException::class => fn (ValidationException $e) => [ + $e->validator->getMessageBag()->getMessages(), + Response::HTTP_BAD_REQUEST + ], + + ModelNotFoundException::class => fn ($e) => [ + 'Not Found', + Response::HTTP_NOT_FOUND + ], + + NotFoundHttpException::class => fn ($e) => [ + 'Not Found', + Response::HTTP_NOT_FOUND + ], + + MethodNotAllowedHttpException::class => fn ($e) => [ + 'Method Not Allowed', + Response::HTTP_METHOD_NOT_ALLOWED + ], + + HttpException::class => fn (HttpException $e) => [ + $e->getMessage(), + $e->getStatusCode(), + ], + + QueryException::class => fn (QueryException $e) => [ + App::isLocal() + ? ['Message' => $e->getMessage(), 'SQL' => $e->getSql(), 'Bindings' => $e->getBindings()] + : 'Internal server error', + Response::HTTP_INTERNAL_SERVER_ERROR + ], + + MissingAbilityException::class => fn($_) => [ + 'Unauthorized', + Response::HTTP_UNAUTHORIZED + ] + + ]; + + $exception_message = $class_exceptions[get_class($ex)] ?? null; + + if ($exception_message) { + $messageCustom = $exception_message($ex); + } else { + $messageCustom = [ + App::isLocal() ? $ex->getMessage() : 'Internal server error', + Response::HTTP_INTERNAL_SERVER_ERROR + ]; + } + + + return $messageCustom; + } + /** * Register the exception handling callbacks for the application. * @@ -43,8 +124,22 @@ class Handler extends ExceptionHandler */ public function register() { - $this->reportable(function (Throwable $e) { - // + $this->reportable(function (\Throwable $ex) { + }); } + + /** + * Custom render errors + * + * @param Illuminate\Http\Request $req + * @param \Throwable $ex + * @return \Illuminate\Http\Response + */ + public function render($req, \Throwable $ex) + { + [$message, $status_code] = $this->messageCustom($ex); + + return response()->json(['error' => $message], $status_code); + } } diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php index a9bc51d..e93fc71 100644 --- a/app/Http/Controllers/Auth/ResetPasswordController.php +++ b/app/Http/Controllers/Auth/ResetPasswordController.php @@ -36,7 +36,7 @@ class ResetPasswordController extends Controller $user = User::with('roles')->firstWhere('email', $resetPasswordRequest->email); $user->update([ - 'password' => Hash::make($resetPasswordRequest->password) + 'password' => $resetPasswordRequest->password ]); return $this->successResponse([ diff --git a/app/Imports/UsersWithSmartUsersImport.php b/app/Imports/UsersWithSmartUsersImport.php index 51b6699..f639b00 100644 --- a/app/Imports/UsersWithSmartUsersImport.php +++ b/app/Imports/UsersWithSmartUsersImport.php @@ -9,6 +9,7 @@ use Illuminate\Http\File; use Illuminate\Support\Str; use Illuminate\Http\UploadedFile; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Storage; @@ -66,7 +67,7 @@ class UsersWithSmartUsersImport implements ToCollection $client_id = $row->get(0); $name = $client->cliente; $email = \trim($row->get(1)); - $password = Hash::make(Str::random(7)); + $password = Str::random(7); $profile_picture = \array_key_exists($client_id, $this->files_paths) ? $this->files_paths[$client_id] : ''; @@ -84,7 +85,7 @@ class UsersWithSmartUsersImport implements ToCollection { $client_id = $row->get(0); $name = $client->cliente; - $password = Hash::make($row->get(2)); + $password = $row->get(2); $profile_picture = \array_key_exists($client_id, $this->files_paths) ? $this->files_paths[$client_id] : ''; @@ -115,12 +116,14 @@ class UsersWithSmartUsersImport implements ToCollection $picture = new File($temp_file_path); $pathS3 = "avatars/{$picture->hashName()}"; - Storage::disk('s3')->missing($pathS3) && + App::isProduction() && Storage::disk('s3')->missing($pathS3) && Storage::disk('s3')->put($pathS3, $picture->getContent()); $filename = \preg_replace("/\.[^\.]+$/", "", $filename); - $this->files_paths[$filename] = Storage::disk('s3')->url($pathS3); + $this->files_paths[$filename] = App::isProduction() + ? Storage::disk('s3')->url($pathS3) + : url('test.png'); \unlink($temp_file_path); }); diff --git a/app/Models/User.php b/app/Models/User.php index c5750b2..8f22609 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,15 +4,17 @@ declare(strict_types=1); namespace App\Models; -use DateTimeInterface; -use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Relations\BelongsToMany; -use Illuminate\Database\Eloquent\SoftDeletes; -use Illuminate\Foundation\Auth\User as Authenticatable; +use Illuminate\Support\Facades\Hash; use Illuminate\Notifications\Notifiable; +use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Foundation\Auth\User as Authenticatable; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; + +use OwenIt\Auditing\Auditable; use Laravel\Sanctum\HasApiTokens; use OwenIt\Auditing\Contracts\Auditable as Auditing; -use OwenIt\Auditing\Auditable; class User extends Authenticatable implements Auditing { @@ -44,7 +46,14 @@ class User extends Authenticatable implements Auditing 'email_verified_at' => 'datetime', ]; - protected function serializeDate(DateTimeInterface $date): string + public function password(): Attribute + { + return Attribute::make( + set: fn($value) => Hash::make($value) + ); + } + + protected function serializeDate(\DateTimeInterface $date): string { return $date->format('d/m/Y H:i:s'); } diff --git a/routes/api.php b/routes/api.php index 42b8c46..183612f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -82,3 +82,5 @@ Route::middleware(['auth:sanctum', 'ability:Client'])->group(function () { }); Route::post('import', [\App\Http\Controllers\UserController::class, 'importUserControll']); + +Route::get('ok', fn() => 'ok'); \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index b130397..b16345e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,18 +1,3 @@