bingo-soft /
jabe
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Jabe\Impl; |
||
| 4 | |||
| 5 | use Jabe\ProcessEngineException; |
||
| 6 | use Jabe\Authorization\{ |
||
| 7 | AuthorizationInterface, |
||
| 8 | AuthorizationQueryInterface, |
||
| 9 | PermissionInterface, |
||
| 10 | ResourceInterface |
||
| 11 | }; |
||
| 12 | use Jabe\Impl\Interceptor\{ |
||
| 13 | CommandContext, |
||
| 14 | CommandExecutorInterface |
||
| 15 | }; |
||
| 16 | use Jabe\Impl\Util\ResourceTypeUtil; |
||
| 17 | |||
| 18 | class AuthorizationQueryImpl extends AbstractQuery implements AuthorizationQueryInterface |
||
| 19 | { |
||
| 20 | protected $id; |
||
| 21 | protected $userIds = []; |
||
| 22 | protected $groupIds = []; |
||
| 23 | protected $resourceType; |
||
| 24 | protected $resourceId; |
||
| 25 | protected $permission = 0; |
||
| 26 | protected $authorizationType; |
||
| 27 | protected $queryByPermission = false; |
||
| 28 | protected $queryByResourceType = false; |
||
| 29 | private $resourcesIntersection = []; |
||
| 30 | |||
| 31 | public function __construct(CommandExecutorInterface $commandExecutor = null) |
||
| 32 | { |
||
| 33 | if ($commandExecutor !== null) { |
||
| 34 | parent::__construct($commandExecutor); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | public function authorizationId(string $id): AuthorizationQueryInterface |
||
| 39 | { |
||
| 40 | $this->id = $id; |
||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | public function userIdIn(array $userIdIn): AuthorizationQueryInterface |
||
| 45 | { |
||
| 46 | if (!empty($this->groupIds)) { |
||
| 47 | throw new ProcessEngineException("Cannot query for user and group authorizations at the same time."); |
||
| 48 | } |
||
| 49 | $this->userIds = $userIdIn; |
||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function groupIdIn(string $groupIdIn): AuthorizationQueryInterface |
||
| 54 | { |
||
| 55 | if (!empty($this->userIds)) { |
||
| 56 | throw new ProcessEngineException("Cannot query for user and group authorizations at the same time."); |
||
| 57 | } |
||
| 58 | $this->groupIds = $groupIdIn; |
||
| 59 | return $this; |
||
| 60 | } |
||
| 61 | |||
| 62 | public function resourceType($resource): ?AuthorizationQueryInterface |
||
| 63 | { |
||
| 64 | if (is_int($resource)) { |
||
| 65 | $this->resourceType = $resource; |
||
| 66 | $this->queryByResourceType = true; |
||
| 67 | return $this; |
||
| 68 | } elseif ($resource instanceof ResourceInterface) { |
||
| 69 | return $this->resourceType($resource->resourceType()); |
||
| 70 | } |
||
| 71 | return null; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function resourceId(string $resourceId): AuthorizationQueryInterface |
||
| 75 | { |
||
| 76 | $this->resourceId = $resourceId; |
||
| 77 | return $this; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function hasPermission(PermissionInterface $p): AuthorizationQueryInterface |
||
| 81 | { |
||
| 82 | $this->queryByPermission = true; |
||
| 83 | |||
| 84 | /*if (count($this->resourcesIntersection) == 0) { |
||
| 85 | $this->resourcesIntersection = $p->getTypes(); |
||
| 86 | } else {*/ |
||
| 87 | $this->resourcesIntersection = $p->getTypes(); |
||
| 88 | /*}*/ |
||
| 89 | |||
| 90 | $this->permission |= $p->getValue(); |
||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function authorizationType(int $type): AuthorizationQueryInterface |
||
| 95 | { |
||
| 96 | $this->authorizationType = $type; |
||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function executeCount(CommandContext $commandContext): int |
||
| 101 | { |
||
| 102 | $this->checkQueryOk(); |
||
| 103 | return $commandContext->getAuthorizationManager() |
||
| 104 | ->selectAuthorizationCountByQueryCriteria($this); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function executeList(CommandContext $commandContext, Page $page): array |
||
| 108 | { |
||
| 109 | $this->checkQueryOk(); |
||
| 110 | return $commandContext->getAuthorizationManager() |
||
| 111 | ->selectAuthorizationByQueryCriteria($this); |
||
| 112 | } |
||
| 113 | |||
| 114 | protected function hasExcludingConditions(): bool |
||
| 115 | { |
||
| 116 | return parent::hasExcludingConditions() |
||
| 117 | || $this->containsIncompatiblePermissions() |
||
| 118 | || $this->containsIncompatibleResourceType(); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * check whether there are any compatible resources |
||
| 123 | * for all of the filtered permission parameters |
||
| 124 | */ |
||
| 125 | private function containsIncompatiblePermissions(): bool |
||
| 126 | { |
||
| 127 | return $this->queryByPermission && empty($this->resourcesIntersection); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * check whether the permissions' resources |
||
| 132 | * are compatible to the filtered resource parameter |
||
| 133 | */ |
||
| 134 | private function containsIncompatibleResourceType(): bool |
||
| 135 | { |
||
| 136 | if ($this->queryByResourceType && $this->queryByPermission) { |
||
| 137 | $resources = $this->resourcesIntersection; |
||
| 138 | return !ResourceTypeUtil::resourceIsContainedInArray($this->resourceType, $resources); |
||
| 139 | } |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | // getters //////////////////////////// |
||
| 144 | |||
| 145 | public function getId(): string |
||
| 146 | { |
||
| 147 | return $this->id; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function isQueryByPermission(): bool |
||
| 151 | { |
||
| 152 | return $this->queryByPermission; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getUserIds(): array |
||
| 156 | { |
||
| 157 | return $this->userIds; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function getGroupIds(): array |
||
| 161 | { |
||
| 162 | return $this->groupIds; |
||
| 163 | } |
||
| 164 | |||
| 165 | public function getResourceType(): int |
||
| 166 | { |
||
| 167 | return $this->resourceType; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function getResourceId(): string |
||
| 171 | { |
||
| 172 | return $this->resourceId; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function getPermission(): int |
||
| 176 | { |
||
| 177 | return $this->permission; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function isQueryByResourceType(): bool |
||
| 181 | { |
||
| 182 | return $this->queryByResourceType; |
||
| 183 | } |
||
| 184 | |||
| 185 | public function getResourcesIntersection(): array |
||
| 186 | { |
||
| 187 | return $this->resourcesIntersection; |
||
| 188 | } |
||
| 189 | |||
| 190 | public function orderByResourceType(): AuthorizationQueryInterface |
||
| 191 | { |
||
| 192 | $this->orderBy(AuthorizationQueryProperty::resourceType()); |
||
| 193 | return this; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 194 | } |
||
| 195 | |||
| 196 | public function orderByResourceId(): AuthorizationQueryInterface |
||
| 197 | { |
||
| 198 | $this->orderBy(AuthorizationQueryProperty::resourceId()); |
||
| 199 | return $this; |
||
| 200 | } |
||
| 201 | } |
||
| 202 |