refactoring User resource
Tests & Lint & Deploy to Railway / build (2.6.6, 20.x, 8.3) (push) Failing after 1m56s Details
Tests & Lint & Deploy to Railway / deploy (push) Has been cancelled Details

This commit is contained in:
aslan 2024-02-14 10:18:45 +03:00
parent fc706fce82
commit a5f8751f94
6 changed files with 80 additions and 3 deletions

View File

@ -4,6 +4,9 @@ APP_KEY=
APP_DEBUG=true APP_DEBUG=true
APP_URL=http://localhost APP_URL=http://localhost
ADMIN_NAME=admin
ADMIN_EMAIL=test@example.com
LOG_CHANNEL=stack LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug LOG_LEVEL=debug

View File

@ -15,6 +15,11 @@ use Illuminate\Support\Str;
class UserController extends Controller class UserController extends Controller
{ {
public function __construct()
{
$this->authorizeResource(User::class, 'user');
}
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{ {
abort_if(Auth::user()->name !== 'admin', 403); abort_if(Auth::user()->name !== 'admin', 403);

View File

@ -0,0 +1,65 @@
<?php
namespace App\Policies;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class UserPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->name === config('app.admin_name') && $user->email === config('app.admin_email');
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, User $model): bool
{
return $user->name === config('app.admin_name') && $user->email === config('app.admin_email');
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return $user->name === config('app.admin_name') && $user->email === config('app.admin_email');
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, User $model): bool
{
return $user->name === config('app.admin_name') && $user->email === config('app.admin_email');
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, User $model): bool
{
return $user->name === config('app.admin_name') && $user->email === config('app.admin_email');
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, User $model): bool
{
return $user->name === config('app.admin_name') && $user->email === config('app.admin_email');
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, User $model): bool
{
return $user->name === config('app.admin_name') && $user->email === config('app.admin_email');
}
}

View File

@ -3,6 +3,8 @@
namespace App\Providers; namespace App\Providers;
// use Illuminate\Support\Facades\Gate; // use Illuminate\Support\Facades\Gate;
use App\Models\User;
use App\Policies\UserPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider class AuthServiceProvider extends ServiceProvider
@ -13,7 +15,7 @@ class AuthServiceProvider extends ServiceProvider
* @var array<class-string, class-string> * @var array<class-string, class-string>
*/ */
protected $policies = [ protected $policies = [
// User::class => UserPolicy::class,
]; ];
/** /**

View File

@ -185,4 +185,6 @@ return [
// 'Example' => App\Facades\Example::class, // 'Example' => App\Facades\Example::class,
])->toArray(), ])->toArray(),
'admin_name' => env('ADMIN_NAME'),
'admin_email' => env('ADMIN_EMAIL')
]; ];

View File

@ -13,8 +13,8 @@ class DatabaseSeeder extends Seeder
public function run(): void public function run(): void
{ {
User::factory()->create([ User::factory()->create([
'name' => 'admin', 'name' => config('app.admin_name'),
'email' => 'test@example.com', 'email' => config('app.admin_email'),
'password' => 123456 'password' => 123456
]); ]);