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

Improved authorize Twig extension #948

Merged
Merged
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
25 changes: 16 additions & 9 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,23 +729,30 @@ public function translateFunc()
}

/**
* Authorize an action. Returns true if the user is logged in and has the right to execute $action.
* Authorize an action. Returns true if the user is logged in and
* has the right to execute $action.
*
* @param string $action
*
* @return bool
* @param string|array $action An action or a list of actions. Each
* entry can be a string like 'group.action'
* or without dot notation an associative
* array.
* @return bool Returns TRUE if the user is authorized to
* perform the action, FALSE otherwise.
*/
public function authorize($action)
{
if (!$this->grav['user']->authenticated) {
return false;
}

$action = (array)$action;

foreach ($action as $a) {
if ($this->grav['user']->authorize($a)) {
return true;
$action = (array) $action;
foreach ($action as $key => $perms) {
$prefix = is_int($key) ? '' : $key . '.';
$perms = $prefix ? (array) $perms : [$perms => true];
foreach ($perms as $action => $authenticated) {
if ($this->grav['user']->authorize($prefix . $action)) {
return $authenticated;
}
}
}

Expand Down