Creating Group Filters #483
-
Been working on a project for about a year. Switching to Shield for Auth as the project expands into a "public" branch to go along with the "admin" branch. There are three main branches:
For the Public side of things you can have URLs like:
For the Admin area, URLs could look like:
Each branch (admin, portal) is namespaced in the controller and the next segment (billling, support, etc) are controllers. Trying to figure out the most efficient way to restrict access based on group. I was looking at this discussion: #257 which seems to be heading in a good direction, but without having to map out every single route, I don't see how to make it work. Is there a way to do a filter on only the first segment of a URL? Am I missing something obvious in how to handle this? I would prefer to have the permissions check in a single place and not have to do it in every controller/function if possible (too many chances for error). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's probably easiest to create 2 new filters - one for Portal and one for Admin. You can look at the existing SessionAuth filter and make the checks to be as specific as you want. You would use the SessionAuth filter for all of your protected routes, and then additionally use the new custom filters. So something like this for the filter: public function before(RequestInterface $request, $arguments = null)
{
$user = auth()->user();
// do your checks here
if ($user->inGroup('staff', 'admin')) {
return;
}
return redirect()->route('login');
} Then in public $aliases = [
...
'admin' => My\Admin\Filter::class,
'portal' => My\Portal\Filter::class,
];
public $filters = [
'admin' => ['before' => ['admin*']],
'portal' => ['before' => ['portal*']]
]; |
Beta Was this translation helpful? Give feedback.
It's probably easiest to create 2 new filters - one for Portal and one for Admin. You can look at the existing SessionAuth filter and make the checks to be as specific as you want. You would use the SessionAuth filter for all of your protected routes, and then additionally use the new custom filters. So something like this for the filter:
Then in
app/Config/Filters.php
: