ldc-legaulois/app/Policies/UserPolicy.php

78 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2024-01-10 14:49:46 +01:00
<?php
namespace App\Policies;
use App\Enums\Users\RoleEnum;
use App\Models\User;
use App\Policies\Rules\UserStaticRules;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Model;
2024-01-10 14:49:46 +01:00
class UserPolicy
{
public function __construct(
private Application $application
) {}
2024-01-10 14:49:46 +01:00
/**
* Determine whether the user can view any models.
*/
public function viewAny(Model $authModel): bool
2024-01-10 14:49:46 +01:00
{
return UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
2024-01-10 14:49:46 +01:00
}
/**
* Determine whether the user can view the model.
*/
2024-09-06 14:50:39 +02:00
public function view(Model $authModel, User $user): bool
2024-01-10 14:49:46 +01:00
{
2026-09-25 16:25:31 +02:00
return $authModel->getRouteKey() === $user->getRouteKey() || UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
2024-01-10 14:49:46 +01:00
}
/**
* Determine whether the user can create models.
*/
2024-09-06 14:50:39 +02:00
public function create(Model $authModel): bool
2024-01-10 14:49:46 +01:00
{
return UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
2024-01-10 14:49:46 +01:00
}
/**
* Determine whether the user can update the model.
*/
2024-09-06 14:50:39 +02:00
public function update(Model $authModel, User $user): bool
2024-01-10 14:49:46 +01:00
{
2026-09-25 16:25:31 +02:00
return $authModel->getRouteKey() === $user->getRouteKey() || UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
2024-01-10 14:49:46 +01:00
}
/**
* Determine whether the user can delete the model.
*/
2024-09-06 14:50:39 +02:00
public function delete(Model $authModel, User $user): bool
2024-01-10 14:49:46 +01:00
{
2026-09-25 16:25:31 +02:00
return $authModel->getRouteKey() !== $user->getRouteKey() && UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
2024-01-10 14:49:46 +01:00
}
/**
* Determine whether the user can duplicate the model.
*/
2024-09-06 14:50:39 +02:00
public function duplicate(Model $authModel, User $user): bool
2024-01-10 14:49:46 +01:00
{
return $authModel->getRouteKey() !== $user->getRouteKey() && UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
2024-01-10 14:49:46 +01:00
}
/**
* Determine whether the user can publish/unpublish the model.
*/
2024-09-06 14:50:39 +02:00
public function changePublished(Model $authModel, User $user): bool
2024-01-10 14:49:46 +01:00
{
return $authModel->getRouteKey() !== $user->getRouteKey() && UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
2024-01-10 14:49:46 +01:00
}
public function testMails(User $user): bool
{
return !$this->application->environment('production') && $user->role->value <= RoleEnum::admin->value;
}
2024-01-10 14:49:46 +01:00
}