php - grant permission to a new role in Laravel 5

357

We are developing a laravel application, the application has an admin part which is only accessible to the admin users inroutes file we have:

 Route::group(['middleware' => 'admin', 'prefix' => 'admin', 'namespace'
              => 'Admin'] , function() {
       Route::get('oldAdminUrl', '[email protected]');

  }

Themiddleware file's handle function is like

public function handle($request, Closure $next)
{       
   if ($this->admin->guest())
    {
        //some code here
    }

    return $next($request);
}

ad the$this->Admin refers toModel calledadminModel with the following attribute

 protected $table = 'admin'

Now we need to add a new url to the group of admin urls let's call itnewAdminUrl it should be accessabile for both the admin users and also a new group of users let's call themeditors

is it a good practice to put that url outside theadmin group and assign it a newmiddleware let's call iteditorsMiddleware in additon toadmin middleware to check if the user who wants to access thenewAdminUrl is either in editor group or admin group and the editors be stored in another table

 Route::group(['middleware' => ['admin','editors], 'prefix' => 'admin',      
     'namespace' => 'Admin'] , function() {
         Route::get('newAdminUrl', '[email protected]');

   }

TheEditorModel which is used inside theeditorMiddleware has this attribute:

     protected $table = 'editor'

The Question: what is the right or even good approach to implement it? or anyone has any better idea or suggestion?

852

Answer

Solution:

Really, the approach you've suggested is fine. Create an 'editor' middleware that allows users with 'admin' and 'editor' roles to pass. I have implemented permissions this way in a number of Laravel apps. You might want to consider moving the middleware call to the __contruct() method of the relevant controller just to simplify your routes file and save a line of code for each route, but that's neither here nor there.

People are also looking for solutions to the problem: php - Navbar on Mysql? or bad idea?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.