ldc-com-app-bo/app/Policies/UserPolicy.php

88 lines
2.4 KiB
PHP

<?php
namespace App\Policies;
use App\Enums\UserRolesEnum;
use App\Models\User;
use App\Policies\Rules\UserStaticRules;
class UserPolicy
{
/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $authUser
* @return boolean
*/
public function viewAny(User $authUser): bool
{
return UserStaticRules::atLeastRole($authUser, UserRolesEnum::admin);
}
/**
* Determine whether the user can view the model.
*
* @param \App\Models\User $authUser
* @param \App\Models\User $user
* @return boolean
*/
public function view(User $authUser, User $user): bool
{
return $authUser->getRouteKey() === $user->getRouteKey() ||
UserStaticRules::atLeastRole($authUser, UserRolesEnum::admin);
}
/**
* Determine whether the user can create models.
*
* @param \App\Models\User $authUser
* @return boolean
*/
public function create(User $authUser): bool
{
return UserStaticRules::atLeastRole($authUser, UserRolesEnum::admin);
}
/**
* Determine whether the user can update the model.
*
* @param \App\Models\User $authUser
* @param \App\Models\User $user
* @return boolean
*/
public function update(User $authUser, User $user): bool
{
return (
UserStaticRules::atLeastRole($authUser, UserRolesEnum::client) &&
$authUser->getRouteKey() === $user->getRouteKey()
) || UserStaticRules::atLeastRole($authUser, UserRolesEnum::admin);
}
/**
* Determine whether the user can delete the model.
*
* @param \App\Models\User $authUser
* @param \App\Models\User $user
* @return boolean
*/
public function delete(User $authUser, User $user): bool
{
return $authUser->getRouteKey() !== $user->getRouteKey() &&
UserStaticRules::atLeastRole($authUser, UserRolesEnum::admin);
}
/**
* Determine whether the user can duplicate the model.
*
* @param \App\Models\User $authUser
* @param \App\Models\User $user
* @return boolean
*/
public function duplicate(User $authUser, User $user): bool
{
return (
UserStaticRules::atLeastRole($authUser, UserRolesEnum::client) &&
$authUser->getRouteKey() === $user->getRouteKey()
) || UserStaticRules::atLeastRole($authUser, UserRolesEnum::admin);
}
}