Skip to content

Commit 87df541

Browse files
committed
Fix up role reading for BackedEnums
1 parent bd17826 commit 87df541

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/Auth/AclTrait.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace TinyAuth\Auth;
44

5+
use BackedEnum;
56
use Cake\Core\Configure;
67
use Cake\Core\Exception\CakeException;
8+
use Cake\Database\Type\EnumLabelInterface;
79
use Cake\Datasource\ResultSetInterface;
810
use Cake\ORM\TableRegistry;
911
use Cake\Utility\Hash;
@@ -616,6 +618,13 @@ protected function _mapped(array $roles) {
616618

617619
$array = [];
618620
foreach ($roles as $role) {
621+
if ($role instanceof BackedEnum) {
622+
$alias = $role instanceof EnumLabelInterface ? $role->label() : $role->name;
623+
$array[$role->value] = $alias;
624+
625+
continue;
626+
}
627+
619628
$alias = array_keys($availableRoles, $role);
620629
$alias = array_shift($alias);
621630
if (!$alias || !is_string($alias)) {

src/Controller/Component/LegacyAuthComponent.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,16 +475,16 @@ protected function _setDefaults(): void {
475475
* @return bool True if $user is authorized, otherwise false
476476
*/
477477
public function isAuthorized($user = null, ?ServerRequest $request = null): bool {
478-
if (empty($user) && !$this->user()) {
478+
if (!$user && !$this->user()) {
479479
return false;
480480
}
481-
if (empty($user)) {
481+
if (!$user) {
482482
$user = $this->user();
483483
}
484-
if (empty($request)) {
484+
if (!$request) {
485485
$request = $this->getController()->getRequest();
486486
}
487-
if (empty($this->_authorizeObjects)) {
487+
if (!$this->_authorizeObjects) {
488488
$this->constructAuthorize();
489489
}
490490
foreach ($this->_authorizeObjects as $authorizer) {

src/Panel/AuthPanel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public function shutdown(EventInterface $event): void {
8686
$access = [];
8787
foreach ($availableRoles as $role => $id) {
8888
if ($user) {
89-
$user = $this->_injectRole($user, $role, $id);
89+
$tmpUser = $this->_injectRole($user, $role, $id);
9090
} else {
91-
$user = $this->_generateUser($role, $id);
91+
$tmpUser = $this->_generateUser($role, $id);
9292
}
93-
$access[$role] = $this->_checkUser($user, $params);
93+
$access[$role] = $this->_checkUser($tmpUser, $params);
9494
}
9595
$data['access'] = $access;
9696

0 commit comments

Comments
 (0)