You've already forked Atomcms-edit
Initial commit
This commit is contained in:
Executable
+55
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Miscellaneous\WebsitePermission;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
readonly class PermissionsService
|
||||
{
|
||||
private const string CACHE_KEY = 'website_permissions';
|
||||
|
||||
private const int CACHE_DURATION_MINUTES = 30;
|
||||
|
||||
private Collection $permissions;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$data = Cache::remember(self::CACHE_KEY, now()->addMinutes(self::CACHE_DURATION_MINUTES), fn () => WebsitePermission::all()->pluck('min_rank', 'permission')->toArray());
|
||||
|
||||
$this->permissions = collect($data);
|
||||
}
|
||||
|
||||
public function getOrDefault(string $permissionName, bool $default = false): bool
|
||||
{
|
||||
if ($this->permissions->isEmpty()) {
|
||||
$permission = WebsitePermission::where('permission', $permissionName)->first();
|
||||
if (! $permission) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
return auth()->check() && auth()->user()->rank >= $permission->min_rank;
|
||||
}
|
||||
|
||||
if (! $this->permissions->has($permissionName)) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$requiredRank = (int) $this->permissions->get($permissionName);
|
||||
|
||||
return auth()->check() && auth()->user()->rank >= $requiredRank;
|
||||
}
|
||||
|
||||
public function all(): Collection
|
||||
{
|
||||
return $this->permissions;
|
||||
}
|
||||
|
||||
public function flush(): void
|
||||
{
|
||||
Cache::forget(self::CACHE_KEY);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user