Addition of the attribute to hash the password and other changes

This commit is contained in:
marcio1002 2023-10-26 18:11:47 -03:00
parent f4734a92d3
commit d53efc31b8
6 changed files with 125 additions and 31 deletions

View File

@ -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);
}
}

View File

@ -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([

View File

@ -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);
});

View File

@ -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');
}

View File

@ -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');

View File

@ -1,18 +1,3 @@
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});