137 lines
3.5 KiB
PHP
137 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Enums\Users\RoleEnum;
|
|
use App\Models\Claim;
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
|
use App\Policies\Rules\UserStaticRules as USR;
|
|
use App\Policies\Rules\UserStaticRules;
|
|
|
|
class ClaimPolicy
|
|
{
|
|
use HandlesAuthorization;
|
|
|
|
/**
|
|
* View any a Claim.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function viewAny(): bool
|
|
{
|
|
return USR::everyOne();
|
|
}
|
|
|
|
/**
|
|
* Preview a Claim.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Claim $claim
|
|
* @return boolean
|
|
*/
|
|
public function preview(User $authUser, Claim $claim): bool
|
|
{
|
|
return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim);
|
|
}
|
|
|
|
/**
|
|
* Update a Claim.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Claim $claim
|
|
* @return boolean
|
|
*/
|
|
public function update(User $authUser, Claim $claim): bool
|
|
{
|
|
return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can update the associated brand of the model.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @return boolean
|
|
*/
|
|
public function updateBrand(User $authUser): bool
|
|
{
|
|
return UserStaticRules::atLeastRole($authUser, RoleEnum::admin);
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can update the associated subject of the model.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Claim $claim
|
|
* @return boolean
|
|
*/
|
|
public function updateSubject(User $authUser, Claim $claim): bool
|
|
{
|
|
return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim);
|
|
}
|
|
|
|
/**
|
|
* Update a Claim.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Claim $claim
|
|
* @return boolean
|
|
*/
|
|
public function updateType(User $authUser, Claim $claim): bool
|
|
{
|
|
return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim);
|
|
}
|
|
|
|
/**
|
|
* Update a Claim.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Claim $claim
|
|
* @return boolean
|
|
*/
|
|
public function updateStatus(User $authUser, Claim $claim): bool
|
|
{
|
|
return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim);
|
|
}
|
|
|
|
/**
|
|
* Update a Claim.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Claim $claim
|
|
* @return boolean
|
|
*/
|
|
public function updateResponseType(User $authUser, Claim $claim): bool
|
|
{
|
|
return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim);
|
|
}
|
|
|
|
/**
|
|
* Export a Claim.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Claim $claim
|
|
* @return boolean
|
|
*/
|
|
public function export(User $authUser, Claim $claim): bool
|
|
{
|
|
return $this->checkOnlyMinAdminOrInUserBrands($authUser, $claim);
|
|
}
|
|
|
|
/**
|
|
* Check if the user an access a specific resource.
|
|
*
|
|
* @param \App\Models\User $authUser
|
|
* @param \App\Models\Claim $claim
|
|
* @return boolean
|
|
*/
|
|
private function checkOnlyMinAdminOrInUserBrands(User $authUser, Claim $claim): bool
|
|
{
|
|
$userBrandModelKeys = $authUser->brands->modelKeys();
|
|
$claimModelKey = $claim->brand->getKey();
|
|
return UserStaticRules::atLeastRole($authUser, RoleEnum::admin) || (
|
|
UserStaticRules::atLeastRole($authUser, RoleEnum::marketing)
|
|
&& \in_array($claimModelKey, $userBrandModelKeys)
|
|
);
|
|
}
|
|
}
|