You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.5 KiB
58 lines
1.5 KiB
<?php
|
|
|
|
namespace App\Utilities\Polices;
|
|
|
|
use App\Documents\PolicyDocument;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Response;
|
|
|
|
class BasePolicy
|
|
{
|
|
use PolicyConditions;
|
|
|
|
public function __construct(PolicyDocument|null $policy = null, Model|null $model = null)
|
|
{
|
|
$this->policy = $policy;
|
|
$this->model = $model;
|
|
}
|
|
|
|
public static function allow(PolicyDocument $policy, Model|null $model = null)
|
|
{
|
|
(new static($policy, $model))->applyCheck();
|
|
}
|
|
|
|
public static function when(PolicyDocument $policy, Model|null $model = null): bool
|
|
{
|
|
return (new static($policy, $model))->applyCheck(false);
|
|
}
|
|
|
|
public function applyCheck($throwException = true): bool
|
|
{
|
|
$pass = false;
|
|
|
|
foreach ($this->policy?->needs ?? [] as $condition) {
|
|
$pass = $this->checkCondition($condition);
|
|
if ($pass) break;
|
|
}
|
|
|
|
if (!$pass && $throwException) {
|
|
abort(Response::HTTP_FORBIDDEN, 'this action is forbidden.');
|
|
}
|
|
return $pass;
|
|
}
|
|
|
|
public function checkCondition($conditions): bool
|
|
{
|
|
$conditionsMatch = true;
|
|
foreach ($conditions as $condition) {
|
|
[$function, $param] = str_contains($condition, ":") ? explode(':', $condition) : [$condition, null];
|
|
|
|
$conditionsMatch = $conditionsMatch &&
|
|
call_user_func_array([static::class, $function], (empty($param) ? [] : [$param]));
|
|
|
|
if (!$conditionsMatch) break;
|
|
}
|
|
|
|
return $conditionsMatch;
|
|
}
|
|
}
|