Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rbac default role closure support #15422

Merged
merged 6 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Yii Framework 2 Change Log
- Enh #15357: Added multi statement support for `yii\db\sqlite\Command` (sergeymakinen)
- Enh #15360: Refactored `BaseConsole::updateProgress()` (developeruz)
- Enh #15415: Added transaction/retry support for `yii\db\Command` (sergeymakinen)
- Enh #15422: Added default roles dynamic definition support via closure for `yii\rbac\BaseManager` (deltacube)
- Enh: Added check to `yii\base\Model::formName()` to prevent source path disclosure when form is represented by an anonymous class (silverfire)
- Chg #15420: Handle OPTIONS request in `yii\filter\Cors` so the preflight check isn't passed trough authentication filters (michaelarnauts, leandrogehlen)

Expand Down
32 changes: 31 additions & 1 deletion framework/rbac/BaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class BaseManager extends Component implements ManagerInterface
* @var array a list of role names that are assigned to every user automatically without calling [[assign()]].
* Note that these roles are applied to users, regardless of their state of authentication.
*/
public $defaultRoles = [];
protected $defaultRoles = [];


/**
Expand Down Expand Up @@ -195,6 +195,36 @@ public function getRoles()
return $this->getItems(Item::TYPE_ROLE);
}

/**
* Set default roles
* @param array|\Closure $roles either array of roles or a callable returning it
* @since 2.0.14
*/
public function setDefaultRoles($roles)
{
if (is_array($roles)) {
$this->defaultRoles = $roles;
} elseif (is_callable($roles)) {
$roles = $roles();
if (!is_array($roles)) {
throw new InvalidParamException('Default roles closure must return an array');
}
$this->defaultRoles = $roles;
} else {
throw new InvalidParamException('Default roles must be either an array or a callable');
}
}

/**
* Get default roles
* @return array default roles
* @since 2.0.14
*/
public function getDefaultRoles()
{
return $this->defaultRoles;
}

/**
* Returns defaultRoles as array of Role objects.
* @since 2.0.12
Expand Down
24 changes: 24 additions & 0 deletions tests/framework/rbac/ManagerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace yiiunit\framework\rbac;

use yii\base\InvalidParamException;
use yii\rbac\Item;
use yii\rbac\Permission;
use yii\rbac\Role;
Expand Down Expand Up @@ -613,4 +614,27 @@ public function testRuleWithPrivateFields()
$rule = $this->auth->getRule('action_rule');
$this->assertInstanceOf(ActionRule::className(), $rule);
}

public function testDefaultRoles()
{
try {
$this->auth->defaultRoles = 'test';
} catch (\Exception $e) {
$this->assertInstanceOf(InvalidParamException::class, $e);
$this->assertEquals('Default roles must be either an array or a callable', $e->getMessage());

try {
$this->auth->defaultRoles = function () {
return 'test';
};
} catch (\Exception $e) {
$this->assertInstanceOf(InvalidParamException::class, $e);
$this->assertEquals('Default roles closure must return an array', $e->getMessage());
}

return;
}

$this->fail('Not rise an exception');
}
}