Custom Laravel Answering Machine

I added a standard authorization filter to several routes using Route :: Intended ('/') in the controller (provided that the login was completed successfully).

filters.php:

Route::filter('auth', function(){
    if (Auth::guest()) return Redirect::guest('internal/login');
});

Controller:

if (Auth::attempt($data, false))
{
    return Redirect::intended('/');
}

How do I create a custom auth filter that checks for a specific permission (isAdmin in this case)?

I made the auth.admin filter the same as the standard auth filter to redirect to the login page, but do I need a second login method on my controller or is there a way to tell which filter (if any) the controller method is called?

if (Auth::attempt($data, false))
{
    if (RouteHasAdminFilter())
    {
        if (!Auth::User()->Admin)
            return Redirect::intended('/');
        else
            return Redirect::to('/');
    }
    else
    {
        return Redirect::intended('/');
    }
}
+3
source share
2 answers

Thanks @diegofelix for taking me on the right track.

I managed to write a filter that:

  • , .
  • URL.


Route::filter('admin', function()  
{

    if (Auth::guest()) return Redirect::guest('internal/login');

    if (Auth::check())
    {
        if (!Auth::User()->Admin)
            return Redirect::to('/');
    }
    else
        return Redirect::to('/');
});

Login , - Redirect:: ('/').
Admin, "" , , .
- "auth" , .

:

'before' => 'auth'
'before' => 'admin'

- ( auth), , :

'before' => 'auth|admin'
+5

, , .

if (Auth::attempt($data)
{
    if (Auth::user()->isAdmin())
        // admin
    else
        // not admin
}
else
    // login failed

isAdmin() - User Eloquent, , .

, , , :

Route::filter('admin', function(){

    if ( ! Auth::user()->isAdmin())
    {
        return Redirect::to('/')
         ->withError('No Admin, sorry.');
    }

});
+4

All Articles