Skip to content

Commit

Permalink
Add aliasing support to AuthManager permission registration and fixed…
Browse files Browse the repository at this point in the history
… an issue where the permission cache wasn't cleared after changing the available permissions
  • Loading branch information
LukeTowers committed Mar 10, 2021
1 parent 05b2b68 commit 88d45bb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"require-dev": {
"fzaninotto/faker": "~1.7",
"phpunit/phpunit": "~6.5",
"phpunit/phpunit-selenium": "~1.2",
"phpunit/phpunit-selenium": "~4.1.0",
"meyfa/phpunit-assert-gd": "1.1.0",
"squizlabs/php_codesniffer": "3.*",
"php-parallel-lint/php-parallel-lint": "^1.0",
Expand Down
28 changes: 27 additions & 1 deletion modules/backend/classes/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class AuthManager extends StormAuthManager
*/
protected $permissions = [];

/**
* @var array List of owner aliases. ['Aliased.Owner' => 'Real.Owner']
*/
protected $aliases = [];

/**
* @var array List of registered permission roles.
*/
Expand Down Expand Up @@ -94,8 +99,11 @@ public function registerCallback(callable $callback)
*/
public function registerPermissions($owner, array $definitions)
{
// Resolve alias
$owner = $this->aliases[$owner] ?? $owner;

foreach ($definitions as $code => $definition) {
$permission = (object)array_merge(self::$permissionDefaults, array_merge($definition, [
$permission = (object) array_merge(self::$permissionDefaults, array_merge($definition, [
'code' => $code,
'owner' => $owner
]));
Expand All @@ -104,6 +112,18 @@ public function registerPermissions($owner, array $definitions)
}
}

/**
* Register a permission owner alias
*
* @param string $owner The owner to register an alias for. Example: Real.Owner
* @param string $alias The alias to register. Example: Aliased.Owner
* @return void
*/
public function registerPermissionOwnerAlias(string $owner, string $alias)
{
$this->aliases[$alias] = $owner;
}

/**
* Removes a single back-end permission
* @param string $owner Specifies the permissions' owner plugin or module in the format Author.Plugin
Expand All @@ -116,6 +136,9 @@ public function removePermission($owner, $code)
throw new SystemException('Unable to remove permissions before they are loaded.');
}

// Resolve alias
$owner = $this->aliases[$owner] ?? $owner;

$ownerPermissions = array_filter($this->permissions, function ($permission) use ($owner) {
return $permission->owner === $owner;
});
Expand All @@ -125,6 +148,9 @@ public function removePermission($owner, $code)
unset($this->permissions[$key]);
}
}

// Clear the permission cache
$this->permissionCache = false;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/UiTestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class UiTestCase extends PHPUnit_Extensions_SeleniumTestCase
class UiTestCase extends PHPUnit_Extensions_Selenium2TestCase
{
protected function setUp()
{
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/backend/classes/AuthManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ public function testRegisterPermissions()
], collect($permissions)->pluck('code')->toArray());
}

public function testAliasesPermissions()
{
$this->instance->registerPermissionOwnerAlias('Winter.TestCase', 'Aliased.TestCase');

$permissions = $this->instance->listPermissions();
$this->assertCount(2, $permissions);

$this->instance->removePermission('Aliased.TestCase', 'test.permission_one');

$permissions = $this->instance->listPermissions();
$this->assertCount(1, $permissions);
$this->assertEquals([
'test.permission_two'
], collect($permissions)->pluck('code')->toArray());
}

public function testRegisterPermissionsThroughCallbacks()
{
// Callback one
Expand Down

0 comments on commit 88d45bb

Please sign in to comment.