You've already forked Atomcms-edit
44 lines
1.1 KiB
PHP
Executable File
44 lines
1.1 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\WebsiteHousekeepingPermission;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
readonly class HousekeepingPermissionsService
|
|
{
|
|
private const string CACHE_KEY = 'housekeeping_permissions';
|
|
|
|
private const int CACHE_DURATION_MINUTES = 30;
|
|
|
|
private Collection $permissions;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->permissions = Cache::remember(
|
|
self::CACHE_KEY,
|
|
now()->addMinutes(self::CACHE_DURATION_MINUTES),
|
|
fn (): Collection => WebsiteHousekeepingPermission::all()->pluck('min_rank', 'permission'),
|
|
);
|
|
}
|
|
|
|
public function getOrDefault(string $permissionName, bool $default = false): bool
|
|
{
|
|
if (! $this->permissions->has($permissionName)) {
|
|
return $default;
|
|
}
|
|
|
|
$requiredRank = (int) $this->permissions->get($permissionName);
|
|
|
|
return auth()->check() && auth()->user()->rank >= $requiredRank;
|
|
}
|
|
|
|
public static function clearCache(): void
|
|
{
|
|
Cache::forget(self::CACHE_KEY);
|
|
}
|
|
}
|