Add new role enum and apply authorization
This commit is contained in:
37
app/Enums/Role.php
Normal file
37
app/Enums/Role.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
enum Role: string
|
||||
{
|
||||
case MEMBER = 'member';
|
||||
case ADMIN = 'admin';
|
||||
case OWNER = 'owner';
|
||||
|
||||
public function rank(): int
|
||||
{
|
||||
return match ($this) {
|
||||
self::MEMBER => 1,
|
||||
self::ADMIN => 2,
|
||||
self::OWNER => 3,
|
||||
};
|
||||
}
|
||||
|
||||
public function lt(Role|string $role): bool
|
||||
{
|
||||
if (is_string($role)) {
|
||||
$role = Role::from($role);
|
||||
}
|
||||
|
||||
return $this->rank() < $role->rank();
|
||||
}
|
||||
|
||||
public function gt(Role|string $role): bool
|
||||
{
|
||||
if (is_string($role)) {
|
||||
$role = Role::from($role);
|
||||
}
|
||||
|
||||
return $this->rank() > $role->rank();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user