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

Actualize rule example in docs, rename $ruleContext to $context #259

Merged
merged 3 commits into from
Feb 29, 2024
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
- Enh #252: Return `$this` instead of throwing "already assigned" exception in `Manager::assign()` (@arogachev)
- Enh #248: Add `SimpleRuleFactory` (@arogachev)
- Enh #251: Allow checking for user's roles in `ManagerInterface::userHasPermission()` (@arogachev)
- Chg #259: Rename `$ruleContext` argument to `$context` in `RuleInterface::execute()` (@arogachev)

## 1.0.2 April 20, 2023

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,15 @@ Sometimes, basic permissions are not enough. In this case, rules are helpful. Ru
added to permissions and roles:

```php
use Yiisoft\Rbac\Item;
use Yiisoft\Rbac\RuleContext;
use Yiisoft\Rbac\RuleInterface;

class ActionRule implements RuleInterface
{
public function execute(string $userId, Item $item, array $parameters = []): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool;
{
return isset($parameters['action']) && $parameters['action'] === 'home';
return $context->getParameterValue('action') === 'home';
}
}
```
Expand All @@ -164,7 +166,7 @@ The parameters are:

- `$userId` is user id to check permission against;
- `$item` is RBAC hierarchy item that rule is attached to;
- `$parameters` is extra data supplied when checking for permission.
- `$context` is a rule context providing access to parameters.

To use rules with `Manager`, specify their names with added permissions or roles:

Expand Down
4 changes: 2 additions & 2 deletions src/CompositeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function __construct(
}
}

public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
if (empty($this->ruleNames)) {
return true;
}

foreach ($this->ruleNames as $ruleName) {
$result = $ruleContext->createRule($ruleName)->execute($userId, $item, $ruleContext);
$result = $context->createRule($ruleName)->execute($userId, $item, $context);

if ($this->operator === self::AND && $result === false) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/RuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ interface RuleInterface
* @param string|null $userId The user ID. This should be a string representing the unique identifier of a user. For
* guests the value is `null`.
* @param Item $item The role or permission that this rule is associated with.
* @param RuleContext $ruleContext Rule context.
* @param RuleContext $context Rule context.
*
* @return bool Whether the rule permits the auth item, it is associated with.
*/
public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool;
public function execute(?string $userId, Item $item, RuleContext $context): bool;
}
4 changes: 2 additions & 2 deletions tests/Support/AdsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

final class AdsRule implements RuleInterface
{
public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
return $ruleContext->getParameterValue('dayPeriod') !== 'night';
return $context->getParameterValue('dayPeriod') !== 'night';
}
}
4 changes: 2 additions & 2 deletions tests/Support/AuthorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
final class AuthorRule implements RuleInterface
{
public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
return (string) $ruleContext->getParameterValue('authorId') === $userId;
return (string) $context->getParameterValue('authorId') === $userId;
}
}
4 changes: 2 additions & 2 deletions tests/Support/BanRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

final class BanRule implements RuleInterface
{
public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
return $ruleContext->getParameterValue('viewed') !== true;
return $context->getParameterValue('viewed') !== true;
}
}
2 changes: 1 addition & 1 deletion tests/Support/FalseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class FalseRule implements RuleInterface
{
public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/GuestRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

final class GuestRule implements RuleInterface
{
public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
return $ruleContext->getParameterValue('noGuestsModeOn') !== true;
return $context->getParameterValue('noGuestsModeOn') !== true;
}
}
4 changes: 2 additions & 2 deletions tests/Support/SubscriptionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ final class SubscriptionRule implements RuleInterface
'4' => false,
];

public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
if ($userId === null || $ruleContext->getParameterValue('voidSubscription') === true) {
if ($userId === null || $context->getParameterValue('voidSubscription') === true) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Support/TrueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class TrueRule implements RuleInterface
{
public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/WannabeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class WannabeRule
{
public function execute(?string $userId, Item $item, RuleContext $ruleContext): bool
public function execute(?string $userId, Item $item, RuleContext $context): bool
{
return true;
}
Expand Down