Skip to content

Commit

Permalink
Add constant for root privilege alias
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Aug 14, 2023
1 parent c4fee75 commit 725b3b8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Auth/Logic/AuthorizationDataCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
final class AuthorizationDataCreator implements AuthorizationDataCreatorInterface
{

public const RootPrivilege = '*';

/**
* @param array<string> $privileges
*/
Expand Down Expand Up @@ -63,7 +65,7 @@ private function buildData(): AuthorizationData
$dataBuilder->addRole($role->name);

foreach ($role->privileges as $privilege) {
if ($privilege === '*') {
if ($privilege === self::RootPrivilege) {
$dataBuilder->addRoot($role->name);
} else {
$dataBuilder->allow($role->name, $privilege);
Expand Down
3 changes: 2 additions & 1 deletion src/Auth/UI/UserIdentityCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OriCMF\Auth\UI;

use OriCMF\Auth\Logic\AuthorizationDataCreator;
use OriCMF\User\DB\User;
use Orisai\Auth\Authorization\Authorizer;
use Orisai\Auth\Authorization\IdentityAuthorizationDataBuilder;
Expand Down Expand Up @@ -35,7 +36,7 @@ private function setIdentityAuthData(User $user, UserIdentity $identity): void

$builder = new IdentityAuthorizationDataBuilder($this->authorizer->getData());
foreach ($user->privileges as $privilege) {
if ($privilege === '*') {
if ($privilege === AuthorizationDataCreator::RootPrivilege) {
$builder->addRoot($identity);
} else {
$builder->allow($identity, $privilege);
Expand Down
6 changes: 5 additions & 1 deletion src/Role/CLI/RoleCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OriCMF\Role\CLI;

use Nextras\Orm\Model\IModel;
use OriCMF\Auth\Logic\AuthorizationDataCreator;
use OriCMF\Role\DB\Role;
use OriCMF\Role\DB\RoleRepository;
use Orisai\Auth\Authorization\Authorizer;
Expand Down Expand Up @@ -73,7 +74,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$missingPrivileges = [];
foreach ($privilegeNames as $privilegeName) {
if ($privilegeName !== '*' && !$this->authorizer->getData()->privilegeExists($privilegeName)) {
if (
$privilegeName !== AuthorizationDataCreator::RootPrivilege
&& !$this->authorizer->getData()->privilegeExists($privilegeName)
) {
$missingPrivileges[] = $privilegeName;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/User/DB/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OriCMF\User\DB;

use Nextras\Orm\Collection\ICollection;
use OriCMF\Auth\Logic\AuthorizationDataCreator;
use OriCMF\Orm\BaseRepository;
use OriCMF\Orm\Functions\JsonAnyKeyOrValueExistsFunction;
use Orisai\Auth\Authorization\PrivilegeProcessor;
Expand All @@ -23,14 +24,14 @@ public static function getEntityClassNames(): array
/**
* @return ICollection&iterable<User>
*/
public function findByPrivilege(string $privilege, bool $includePowerUser = true): ICollection
public function findByPrivilege(string $privilege, bool $includeRoot = true): ICollection
{
$parents = PrivilegeProcessor::getPrivilegeParents($privilege);

return $this->findBy([
JsonAnyKeyOrValueExistsFunction::class,
'roles->privileges',
$includePowerUser ? ['*'] + $parents : $parents,
$includeRoot ? [AuthorizationDataCreator::RootPrivilege] + $parents : $parents,
]);
}

Expand Down

0 comments on commit 725b3b8

Please sign in to comment.