You've already forked Atomcms-edit
Initial commit
This commit is contained in:
+43
@@ -0,0 +1,43 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user