Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ So you can use it like:

```php
Entrust::routeNeedsRole('admin/advanced*', 'owner', Redirect::to('/home'));

// You may use a closure for the result parameter which will only be executed when access is denied.
Entrust::routeNeedsRole('admin/advanced*', 'owner', function () {
Redirect::to('/home')->withErrors('Access Denied');
});
```

Furthermore both of these methods accept a fourth parameter.
Expand Down
12 changes: 12 additions & 0 deletions src/Entrust/Entrust.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public function routeNeedsRole($route, $roles, $result = null, $requireAll = tru
$hasRole = $this->hasRole($roles, $requireAll);

if (!$hasRole) {
// if result is a closure, then call it and use the returned value
if (!empty($result) && !is_string($result) && is_callable($result)) {
$result = $result();
}
return empty($result) ? $this->app->abort(403) : $result;
}
};
Expand Down Expand Up @@ -127,6 +131,10 @@ public function routeNeedsPermission($route, $permissions, $result = null, $requ
$hasPerm = $this->can($permissions, $requireAll);

if (!$hasPerm) {
// if result is a closure, then call it and use the returned value
if (!empty($result) && !is_string($result) && is_callable($result)) {
$result = $result();
}
return empty($result) ? $this->app->abort(403) : $result;
}
};
Expand Down Expand Up @@ -170,6 +178,10 @@ public function routeNeedsRoleOrPermission($route, $roles, $permissions, $result
}

if (!$hasRolePerm) {
// if result is a closure, then call it and use the returned value
if (!empty($result) && !is_string($result) && is_callable($result)) {
$result = $result();
}
return empty($result) ? $this->app->abort(403) : $result;
}
};
Expand Down