Skip to content

Commit

Permalink
Fix up role reading for BackedEnums
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 24, 2023
1 parent bd17826 commit 87df541
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/Auth/AclTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace TinyAuth\Auth;

use BackedEnum;
use Cake\Core\Configure;
use Cake\Core\Exception\CakeException;
use Cake\Database\Type\EnumLabelInterface;
use Cake\Datasource\ResultSetInterface;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -616,6 +618,13 @@ protected function _mapped(array $roles) {

$array = [];
foreach ($roles as $role) {
if ($role instanceof BackedEnum) {
$alias = $role instanceof EnumLabelInterface ? $role->label() : $role->name;
$array[$role->value] = $alias;

continue;
}

$alias = array_keys($availableRoles, $role);
$alias = array_shift($alias);
if (!$alias || !is_string($alias)) {
Expand Down
8 changes: 4 additions & 4 deletions src/Controller/Component/LegacyAuthComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,16 +475,16 @@ protected function _setDefaults(): void {
* @return bool True if $user is authorized, otherwise false
*/
public function isAuthorized($user = null, ?ServerRequest $request = null): bool {
if (empty($user) && !$this->user()) {
if (!$user && !$this->user()) {
return false;
}
if (empty($user)) {
if (!$user) {
$user = $this->user();
}
if (empty($request)) {
if (!$request) {
$request = $this->getController()->getRequest();
}
if (empty($this->_authorizeObjects)) {
if (!$this->_authorizeObjects) {
$this->constructAuthorize();
}
foreach ($this->_authorizeObjects as $authorizer) {
Expand Down
6 changes: 3 additions & 3 deletions src/Panel/AuthPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public function shutdown(EventInterface $event): void {
$access = [];
foreach ($availableRoles as $role => $id) {
if ($user) {
$user = $this->_injectRole($user, $role, $id);
$tmpUser = $this->_injectRole($user, $role, $id);
} else {
$user = $this->_generateUser($role, $id);
$tmpUser = $this->_generateUser($role, $id);
}
$access[$role] = $this->_checkUser($user, $params);
$access[$role] = $this->_checkUser($tmpUser, $params);
}
$data['access'] = $access;

Expand Down

0 comments on commit 87df541

Please sign in to comment.