Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vrajroham/0e0cc9eda06db2f2a3e6c15fe5a9fc3d to your computer and use it in GitHub Desktop.
Save vrajroham/0e0cc9eda06db2f2a3e6c15fe5a9fc3d to your computer and use it in GitHub Desktop.

Revisions

  1. @amochohan amochohan revised this gist Oct 6, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 01_Laravel 5 Simple ACL manager_Readme.md
    Original file line number Diff line number Diff line change
    @@ -15,3 +15,5 @@ Then specify a 'roles' middleware on the route you'd like to protect, and specif
    'uses' => 'UserController@index',
    'roles' => ['administrator', 'manager']
    ]);

    If you found this ACL manager helpful please give this repo a star, and give me a [follow](https://github.com/drawmyattention). Any questions, please leave a comment.
  2. @amochohan amochohan revised this gist Feb 26, 2015. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions Role.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Role extends Model {

    protected $table = 'roles';

    public function users()
    {
    return $this->hasMany('App\User', 'role_id', 'id');
    }

    }
  3. @amochohan amochohan renamed this gist Feb 17, 2015. 1 changed file with 0 additions and 0 deletions.
  4. @amochohan amochohan renamed this gist Feb 17, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @amochohan amochohan renamed this gist Feb 17, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @amochohan amochohan revised this gist Feb 17, 2015. 3 changed files with 5 additions and 1 deletion.
    2 changes: 2 additions & 0 deletions CheckRole.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    <?php namespace App\Http\Middleware;

    // First copy this file into your middleware directoy

    use Closure;

    class CheckRole{
    2 changes: 1 addition & 1 deletion Readme.md
    Original file line number Diff line number Diff line change
    @@ -14,4 +14,4 @@ Then specify a 'roles' middleware on the route you'd like to protect, and specif
    'middleware' => ['auth', 'roles'],
    'uses' => 'UserController@index',
    'roles' => ['administrator', 'manager']
    ]);
    ]);
    2 changes: 2 additions & 0 deletions RoleTableSeeder.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    <?php



    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    use App\Role;
  7. @amochohan amochohan created this gist Feb 17, 2015.
    40 changes: 40 additions & 0 deletions CheckRole.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?php namespace App\Http\Middleware;

    use Closure;

    class CheckRole{

    /**
    * Handle an incoming request.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Closure $next
    * @return mixed
    */
    public function handle($request, Closure $next)
    {
    // Get the required roles from the route
    $roles = $this->getRequiredRoleForRoute($request->route());

    // Check if a role is required for the route, and
    // if so, ensure that the user has that role.
    if($request->user()->hasRole($roles) || !$roles)
    {
    return $next($request);
    }
    return response([
    'error' => [
    'code' => 'INSUFFICIENT_ROLE',
    'description' => 'You are not authorized to access this resource.'
    ]
    ], 401);

    }

    private function getRequiredRoleForRoute($route)
    {
    $actions = $route->getAction();
    return isset($actions['roles']) ? $actions['roles'] : null;
    }

    }
    10 changes: 10 additions & 0 deletions Kernel.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <?php

    // Register the new route middleware

    protected $routeMiddleware = [
    'auth' => 'App\Http\Middleware\Authenticate',
    'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
    'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
    'roles' => 'App\Http\Middleware\CheckRole',
    ];
    17 changes: 17 additions & 0 deletions Readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #Laravel 5 Simple ACL manager

    Protect your routes with user roles. Simply add a 'role_id' to the User model, install the roles table and seed if you need some example roles to get going.

    If the user has a 'Root' role, then they can perform *any* actions.

    # Installation

    Simply copy the files across into the appropriate directories, and register the middleware in App\Http\Kernel.php

    Then specify a 'roles' middleware on the route you'd like to protect, and specify the individual roles as an array:

    Route::get('user/{user}', [
    'middleware' => ['auth', 'roles'],
    'uses' => 'UserController@index',
    'roles' => ['administrator', 'manager']
    ]);
    49 changes: 49 additions & 0 deletions RoleTableSeeder.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <?php

    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    use App\Role;

    class RoleTableSeeder extends Seeder{

    public function run()
    {

    if (App::environment() === 'production') {
    exit('I just stopped you getting fired. Love, Amo.');
    }

    DB::table('role')->truncate();

    Role::create([
    'id' => 1,
    'name' => 'Root',
    'description' => 'Use this account with extreme caution. When using this account it is possible to cause irreversible damage to the system.'
    ]);

    Role::create([
    'id' => 2,
    'name' => 'Administrator',
    'description' => 'Full access to create, edit, and update companies, and orders.'
    ]);

    Role::create([
    'id' => 3,
    'name' => 'Manager',
    'description' => 'Ability to create new companies and orders, or edit and update any existing ones.'
    ]);

    Role::create([
    'id' => 4,
    'name' => 'Company Manager',
    'description' => 'Able to manage the company that the user belongs to, including adding sites, creating new users and assigning licences.'
    ]);

    Role::create([
    'id' => 5,
    'name' => 'User',
    'description' => 'A standard user that can have a licence assigned to them. No administrative features.'
    ]);
    }

    }
    33 changes: 33 additions & 0 deletions Roles_table_migration.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?php

    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateRolesTable extends Migration {

    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
    Schema::create('role', function($table) {
    $table->increments('id');
    $table->string('name', 40);
    $table->string('description', 255);
    $table->timestamps();
    });
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
    Schema::drop('role');
    }

    }
    39 changes: 39 additions & 0 deletions User.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    <?php

    // The User model

    public function role()
    {
    return $this->hasOne('App\Role', 'id', 'role_id');
    }

    public function hasRole($roles)
    {
    $this->have_role = $this->getUserRole();

    // Check if the user is a root account
    if($this->have_role->name == 'Root') {
    return true;
    }

    if(is_array($roles)){
    foreach($roles as $need_role){
    if($this->checkIfUserHasRole($need_role)) {
    return true;
    }
    }
    } else{
    return $this->checkIfUserHasRole($roles);
    }
    return false;
    }

    private function getUserRole()
    {
    return $this->role()->getResults();
    }

    private function checkIfUserHasRole($need_role)
    {
    return (strtolower($need_role)==strtolower($this->have_role->name)) ? true : false;
    }
    7 changes: 7 additions & 0 deletions routes.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <?php

    Route::get('user/{user}', [
    'middleware' => ['auth', 'roles'], // A 'roles' middleware must be specified
    'uses' => 'UserController@index',
    'roles' => ['administrator', 'manager'] // Only an administrator, or a manager can access this route
    ]);