Skip to content

Instantly share code, notes, and snippets.

@massimoselvi
Forked from dillinghamio/SparkRoleMiddleware.md
Created December 8, 2016 23:39
Show Gist options
  • Save massimoselvi/acfb21f40198d382f900397c1d6713b5 to your computer and use it in GitHub Desktop.
Save massimoselvi/acfb21f40198d382f900397c1d6713b5 to your computer and use it in GitHub Desktop.
Team Role Middleware For Laravel Spark

Team Role Middleware For Laravel Spark

Makes it simple to use Spark's role feature on routes

Route::group(['middleware'=>'role:owner'], function(){
    // owners only
});

Route::group(['middleware'=>'role:member'], function(){
    // members only
});

Run php artisan make:middleware RoleMiddleware Copy the following to the newly created file

Open Http\kernel.php Find $routeMiddleware Add 'role' => \App\Http\Middleware\RoleMiddleware::class,

<?php

namespace App\Http\Middleware;

use Closure;

class RoleMiddleware
{
    /**
     *  Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string  $role
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {         
        if ($request->user()->roleOn($request->user()->currentTeam) != $role)
        {
            return redirect('/home');
        }

        return $next($request);
    }

}

Made by: @dillinghamio Find More Awesome Spark Stuff Here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment