0

With Entrust, I implemented how user will be redirected to his own dashboard after login by adding this method in Authcontroller.php

protected function authenticated()
{

    if(\Auth::user()->hasRole(['super_admin',]) ) {
        return redirect('/dashboard');
    } else if(\Auth::user()->hasRole(['staff_admin']) ) {
        return redirect('/staff/dashboard');
    } else if(\Auth::user()->hasRole(['subadmin_admin']) ) {
        return redirect('/subadmin/dashboard');
    }
}

What challenge I am facing right now is, for eg. if staff had logged in and redirected to his dashboard as

domain.com/staff/dashboard

but if he manually deletes the staff from url and tries to access Super-Admin Dashboard then Entrust throws 403 Error, but I want to redirect him to his dashboard, with message that " You are not authorized".

I tried to implement same code in RedirectIfAuthenticated middleware, but it gave error as hasRole called on Null.

Tarunn
  • 1,038
  • 3
  • 23
  • 45

1 Answers1

0

Your DashboardController add constructor.

Example Code

class UserController extends Controller
{
        public function __construct()
        {
            $this->middleware(['role:super_admin']);
        }

        public function index()
        {
        return view('dashboard');
        }
}

Then, error folder add 403.blade.php like so:

<!DOCTYPE html>
<html>
    <head>
        <title>Be right back.</title>

        <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">

        <style>
            html, body {
                height: 100%;
            }

            body {
                margin: 0;
                padding: 0;
                width: 100%;
                color: #B0BEC5;
                display: table;
                font-weight: 100;
                font-family: 'Lato', sans-serif;
            }

            .container {
                text-align: center;
                display: table-cell;
                vertical-align: middle;
            }

            .content {
                text-align: center;
                display: inline-block;
            }

            .title {
                font-size: 72px;
                margin-bottom: 40px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">You are not authorized.</div>
            </div>
        </div>
    </body>
</html>
  • Nop! I am not asking to get 403 error page. What I want is if he manually changes URL, then how to redirect back to his dashboard, rather then showing dead 403 page. Hope I am clear with question. – Tarunn Aug 25 '16 at 00:47
  • you can override at handle method in vendor/zizaco/entrust/src/Entrust/Middleware/EntrustRole.php – Tint Naing Win Aug 25 '16 at 08:36