77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?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;
|
|
|
|
class UserPolicy
|
|
{
|
|
public function __construct(
|
|
private Application $application
|
|
) {}
|
|
|
|
/**
|
|
* Determine whether the user can view any models.
|
|
*/
|
|
public function viewAny(Model $authModel): bool
|
|
{
|
|
return UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can view the model.
|
|
*/
|
|
public function view(Model $authModel, User $user): bool
|
|
{
|
|
return $authModel->getRouteKey() === $user->getRouteKey() || UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can create models.
|
|
*/
|
|
public function create(Model $authModel): bool
|
|
{
|
|
return UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can update the model.
|
|
*/
|
|
public function update(Model $authModel, User $user): bool
|
|
{
|
|
return $authModel->getRouteKey() === $user->getRouteKey() || UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can delete the model.
|
|
*/
|
|
public function delete(Model $authModel, User $user): bool
|
|
{
|
|
return $authModel->getRouteKey() !== $user->getRouteKey() && UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can duplicate the model.
|
|
*/
|
|
public function duplicate(Model $authModel, User $user): bool
|
|
{
|
|
return $authModel->getRouteKey() !== $user->getRouteKey() && UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can publish/unpublish the model.
|
|
*/
|
|
public function changePublished(Model $authModel, User $user): bool
|
|
{
|
|
return $authModel->getRouteKey() !== $user->getRouteKey() && UserStaticRules::atLeastRole($authModel, RoleEnum::conceptor);
|
|
}
|
|
|
|
public function testMails(User $user): bool
|
|
{
|
|
return !$this->application->environment('production') && $user->role->value <= RoleEnum::admin->value;
|
|
}
|
|
}
|