Skip to content

Commit 2cf447c

Browse files
authored
Add roleIdColumn config for UUID-based role systems (#163)
* Add roleIdColumn config for UUID-based role systems When using UUIDs as role identifiers instead of integer IDs, the hardcoded 'id' column in _getRolesFromDb() prevented proper role lookups from the database. This adds a new 'roleIdColumn' config option (defaults to 'id') that allows specifying the column used as the role identifier when reading roles from the database table. Example for UUID-based systems: ```php 'TinyAuth' => [ 'roleColumn' => 'Role.uuid', 'roleIdColumn' => 'uuid', 'aliasColumn' => 'slug', ] ``` * Fix PHPStan: update type annotations for UUID support * Fix remaining PHPStan type annotations
1 parent edc7ad9 commit 2cf447c

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

docs/Authorization.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ TinyAuthorize adapter supports the following configuration options:
407407
| roleColumn | string | Name of column in user table holding role id (used for foreign key in users table in a single role per user setup, or in the pivot table on multi-roles setup) |
408408
| userColumn | string | Name of column in pivot table holding role id (only used in pivot table on multi-roles setup) |
409409
| aliasColumn | string | Name of the column for the alias in the role table |
410+
| roleIdColumn | string | Name of the primary key column in roles table. Defaults to `id`. Set to `uuid` for UUID-based role systems. |
410411
| idColumn | string | Name of the ID Column in users table |
411412
| rolesTable | string | Name of Configure key holding all available roles OR class name of roles database table |
412413
| usersTable | string | Class name of the users table. |

src/Auth/AclTrait.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait AclTrait {
2525
protected $_acl;
2626

2727
/**
28-
* @var array<int>|null
28+
* @var array<int|string>|null
2929
*/
3030
protected $_roles;
3131

@@ -261,7 +261,7 @@ protected function _prefixesFromRoles(array $roles) {
261261
* @param string $prefix
262262
* @param array<string> $prefixMap
263263
* @param array<string, int|string> $userRoles
264-
* @param array<int> $availableRoles
264+
* @param array<string, int|string> $availableRoles
265265
*
266266
* @return bool
267267
*/
@@ -444,7 +444,7 @@ protected function _constructIniKey($params): ?string {
444444
* Configure first, tries database roles table next.
445445
*
446446
* @throws \Cake\Core\Exception\CakeException
447-
* @return array<string, int> List with all available roles
447+
* @return array<string, int|string> List with all available roles
448448
*/
449449
protected function _getAvailableRoles() {
450450
if ($this->_roles !== null) {
@@ -492,19 +492,20 @@ protected function _getAvailableRoles() {
492492
*
493493
* @throws \Cake\Core\Exception\CakeException
494494
*
495-
* @return array<int>
495+
* @return array<int|string>
496496
*/
497497
protected function _getRolesFromDb(string $rolesTableKey): array {
498498
try {
499499
$rolesTable = TableRegistry::getTableLocator()->get($rolesTableKey);
500-
$result = $rolesTable->find()->formatResults(function (ResultSetInterface $results) {
501-
return $results->combine($this->getConfig('aliasColumn'), 'id');
500+
$roleIdColumn = $this->getConfig('roleIdColumn') ?: 'id';
501+
$result = $rolesTable->find()->formatResults(function (ResultSetInterface $results) use ($roleIdColumn) {
502+
return $results->combine($this->getConfig('aliasColumn'), $roleIdColumn);
502503
});
503504
} catch (RuntimeException $e) {
504505
throw new CakeException('Invalid roles config: DB table `' . $rolesTableKey . '` cannot be found/accessed (`' . $e->getMessage() . '`).', null, $e);
505506
}
506507

507-
/** @var array<int> */
508+
/** @var array<int|string> */
508509
return $result->toArray();
509510
}
510511

src/Utility/Config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Config {
3232
'roleColumn' => 'role_id', // Foreign key for the Role ID in users table or in pivot table
3333
'userColumn' => 'user_id', // Foreign key for the User id in pivot table. Only for multi-roles setup
3434
'aliasColumn' => 'alias', // Name of column in roles table holding role alias/slug
35+
'roleIdColumn' => 'id', // Primary key column in roles table (allows using 'uuid' for UUID-based systems)
3536
'rolesTable' => 'Roles', // name of Configure key holding available roles OR class name of roles table
3637
'usersTable' => 'Users', // name of the Users table
3738
'pivotTable' => null, // Should be used in multi-roles setups

src/Utility/TinyAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(array $config = []) {
2727
}
2828

2929
/**
30-
* @return array<int>
30+
* @return array<string, int|string>
3131
*/
3232
public function getAvailableRoles() {
3333
$roles = $this->_getAvailableRoles();

0 commit comments

Comments
 (0)