-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
802c1a5
commit 080b7b8
Showing
16 changed files
with
224 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Core\Support\Exceptions; | ||
|
||
class InvalidRoleException extends OutputErrorException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Core\Support\Permissions\Access; | ||
|
||
class GeneralPermissions | ||
{ | ||
public const int READ = 1 << 0; // 0001 | ||
public const int WRITE = 1 << 1; // 0010 | ||
public const int DELETE = 1 << 2; // 0100 | ||
public const int EXECUTE = 1 << 3; // 1000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Core\Support\Permissions\Access; | ||
|
||
class UserRoles | ||
{ | ||
public const int ADMIN = GeneralPermissions::READ | GeneralPermissions::WRITE | GeneralPermissions::DELETE | GeneralPermissions::EXECUTE; | ||
public const int EDITOR = GeneralPermissions::READ | GeneralPermissions::WRITE; | ||
public const int VIEWER = GeneralPermissions::READ; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Core\Support\Permissions; | ||
|
||
use Core\Support\Exceptions\InvalidRoleException; | ||
use Core\Support\Http\ResponseStatusCodeEnum; | ||
use Core\Support\Permissions\Access\UserRoles; | ||
|
||
trait HasUserRoleTrait | ||
{ | ||
private int $permissions = 0; | ||
private int $role = 0; | ||
|
||
public function hasPermission(int $permission): bool | ||
{ | ||
return ($this->permissions & $permission) === $permission; | ||
} | ||
|
||
public function getPermissions(): int | ||
{ | ||
return $this->permissions; | ||
} | ||
|
||
public function setPermissions(int $permissions): void | ||
{ | ||
$this->permissions = $permissions; | ||
} | ||
|
||
public function getRole(): int | ||
{ | ||
return $this->role; | ||
} | ||
|
||
public function setRole(int $role): void | ||
{ | ||
$this->role = $role; | ||
$this->permissions = $role; | ||
} | ||
|
||
public function hasRolePermission(int $permission): bool | ||
{ | ||
return ($this->role & $permission) === $permission; | ||
} | ||
|
||
/** | ||
* @throws InvalidRoleException | ||
*/ | ||
public function getRoleName(): string | ||
{ | ||
return match ($this->role) { | ||
UserRoles::ADMIN => 'ADMIN', | ||
UserRoles::EDITOR => 'EDITOR', | ||
UserRoles::VIEWER => 'VIEWER', | ||
default => throw new InvalidRoleException("Invalid role", ResponseStatusCodeEnum::FORBIDDEN->value), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.