I have an application with many user roles. Like Admin
, Manager
etc.
There are some instances where both Manager
and Admin
has the same permission to see/edit a page. In the route.php
I created a filter group for Admin access only.
//filter.php
Route::filter('admin', function()
{
if (Auth::guest() or ! Auth::user()->isAdmin()) {
return 'Not Authorized';
}
});
//route.php
Route::group(['before' => 'admin'], function(){
//Some routes.....
});
Now, I want to check if the user is allowed to view that page by checking the userId
and the courseId
. How can I get these values in the filter.php
?
I have a function which checks if the user is authorized for that page. But it requires two variables to be passed (Atleast one variable, the courseId
)
public static function isAllowed($userId, $courseId){
//Some conditions.
}
How can I do this?
It's quite easy actually. In filter.php
you simply need to add your filter function and then use it on the routes.php
To Decalre a filter for Route::get('my/url/{variable1}/{variable2})
:
Route::filter('myAweSomeFilter', function($route)
{
$variable1 = $route->getParameter('variable1');
if (thisAndThat)
{
doStuff;
}
});
now, If you want to call a model function, you can do that inside this function.
Therefore, if you have a model named AuthModel
, and a static function named coursePermissionCheck
, simply call it as AuthModel::coursePermisssionCheck()
and so on. You also write a whole new function inside of the filter function and use it. It's just another function that tells the RouteController what to do if a certain condition is not met.
How to use this filter: 'before' => 'admin|youFilter'
Hope this helps.