0

I am beginner of laravel. I am using Role and permission concept for multiple user. If user manually enter URL which is not allow to that user then I want to logout that user.

I have successfully logout the user but display logout page in content area part not single page of login.

Please help me .

Thanks in advance ....

image snapshot enter image description here

This is my ACL Code -

public function handle($request, Closure $next, $permission = null)
    {
        if ($request->getSession()->has('user')) {
            $userObj = new \App\User;
            if ($userObj->canAccess($request->getSession()->get('user')[0]['userPerm'], $permission)) {
                return $next($request);
            }
            else{ 
                  redirect('logout')->withErrors(array('mst_error' => 'Unauthorized Access!'))->send();exit;
            }
        }
        return $request->isXmlHttpRequest() ? 
            response(json_encode(array('session_logout' => true)), 401) : 
            redirect('login')->withErrors(array('mst_error' => 'You don\'t have any active session. Please login again'));
    }
Ahmed farag mostafa
  • 2,802
  • 2
  • 14
  • 33
Rajeev Varshney
  • 933
  • 2
  • 13
  • 26
  • It will redirect to logout page but not shown entire page because request is coming from AJAX and Ajax replace the content area with logout page.. So What should I do. Please help me its urgent. – Rajeev Varshney May 03 '16 at 08:41

1 Answers1

0

I have resolved :)

This is my handle function

  public function handle($request, Closure $next, $permission = null)
    {
        if ($request->getSession()->has('user')) {
            $userObj = new \App\User;
            if ($userObj->canAccess($request->getSession()->get('user')[0]['userPerm'], $permission)) {
                return $next($request);
            }
            else{
                    return response()->json(array('mst_error'=>'Unauthorized Access.'),401);
                }
        }
        return $request->isXmlHttpRequest() ? 
            response(json_encode(array('session_logout' => true)), 401) : 
            redirect('login')->withErrors(array('mst_error' => 'You don\'t have any active session. Please login again'));
    }

This is my Ajax Request -

$.ajax({
            url:url,
            data:data,
            statusCode: {
                401: function(res){
                        location.href = "unauthorized";
                    }
            }
        }).done(function(result){console.log(result);
            $('#section-content').html(result);
        });

This is my unauthorized function in Auth Controller

protected function unauthorized_logout (Request $request) {
        if ($request->getSession()->has('user')) {
            $request->getSession()->flush();
        }        
        Session::flash('error','Unauthorized Access!');
        return redirect('/');
    }
Rajeev Varshney
  • 933
  • 2
  • 13
  • 26