ArtisanTinkerer.github.io

Blog

View on GitHub

I need some custom code to work with my User Manager Application

Login

The permissions stuff is now done via middleware. Kernel.php

'permissions' => \App\Http\Middleware\PermissionMiddleware::class

Logout

public function Logout()
    {
        auth()->logout();
        Session::flush();
        return redirect('/');
    }

This is the middleware

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use App\Traits\AccessManager;


class PermissionMiddleware
{
    /**
     * Handle an incoming request.
     * 
     * This the new version of the permissions
     * If this is attached to a controller
     * It will set the permissions array in the session
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // $request->session()->forget('permissions');
        
        //are we authenticated?
        $user = Auth::user();

        if ($user) {//authenticated
            //get the permissions
            $permissions = $request->session()->get('permissions');

            if (!$permissions) {//lost the session so we need to set then

                //$id = Auth::user()->id;

                //this will get them and stick them in the session
                $gotPermissions = AccessManager::getPermissions();

                //if they don't have any for this application, boot them out
                if (!$gotPermissions) {
                    Auth::logout();
                    //Session::flush(); //need to clear the session
                    return redirect('/auth/login')->withErrors(['username' => 'You do not have access to this application.']);
                }
            }
        }
        return $next($request);
    }
}