Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Keep user roles' pages/ routes separate

I'm using user roles to separate a regular user and an administrator and I would like to know whats the best way to make sure the regular user doesn't have access to the admin only pages/routes.

This is how the user is set up table and model

Schema::create('users', function(Blueprint $table)  {
            $table->increments('id');
            $table->string('name');
            $table->string('username');
            $table->string('password');
            $table->integer('role_id')->unsigned();
            $table->rememberToken();
            $table->timestamps();
            $table->foreign('role_id')->references('id')->on('roles');
        });

//model
public function roles()
    {
        return $this->hasOne(Role::class);
    }

    public function hasRole($id)
    {
        return $this->role_id == $id;
    }

    public function isAdmin()
    {
        return $this->hasRole(Role::ADMIN);
    }

    public function isUser()
    {
        return $this->hasRole(Role::USER);
    }

The menu is simply done like this (using Element-ui / Vue)

 <el-menu v-if="role === 1"
         mode="horizontal"
         background-color="#545c64"
         text-color="#fff"
         active-text-color="#CA8AEC">

     <el-menu-item index="1"><a href="/">Marcas</a></el-menu-item>
     <el-menu-item index="2"><a href="/users">Productos</a></el-menu-item>
</el-menu>

//mounted
if (this.$page.prop.auth.user) {
    this.name = this.$page.prop.auth.user.name;
    this.role = this.$page.prop.auth.user.role_id;
}

Right now this project is just starting so there aren't any routes yet, but what is the best way to keep the admin pages / routes separate from the regular logged in user's pages.

What the best way to do this?

0 likes
3 replies
jlrdw's avatar

You need authorization and scopes to determine who can and cannot do certain things.

From a previous answer I gave:

  • Bob is an admin

  • Suzy is admin and does bookkeeping

  • Mary is a bookkeeper only

  • If Bob is logged in, Bob can only do admin stuff and all access to user stuff. But Bob cannot mess with bookkeeping.

  • If Suzy is logged in she can access admin stuff and bookkeeping and accounting stuff.

  • If Mary is logged in she cannot mess with admin stuff, but has access to bookkeeping and accounting stuff.

So I just check at method level if the logged in users role can or cannot access that method / function.

And use query scopes to let a user edit / view their own data or an admin can access all users data.

Each app will be different as to who can do what.

So in pseudocode:

public function makeInvoice()
    {
        if (a required role of bkeep is not true here) {   // bkeep = bookkeeper
            return redirect('somewhere'); // whereever you redirect to if not authorized
        }
        // Rest of method here is accomplished if 
        // the logged in used has the required role of 'bkeep'.
    }

Again just examples.

Also a Spatie example I saw:

public function update(Request $request, Post $post) {
    if ($post->author !== auth()->user()->id || auth()->user()->cannot('edit posts'))
        abort(404);// or redirect, or whatever action 
    }
    //rest of method if all okay
}

In summary RBAC is at least 3 main steps:

  • A login required
  • An authorization implementation to determine what the logged in person with role can or cannot do
  • Protection of URL and parameters, checking that the logged in users id matches the id used in a query

Each application will require unique tweaks in RBAC, no two apps are exactly the same.

You also have to ensure someone doesn't change an id in the url by matching the authenticated user id is the one in the query.

jlrdw's avatar

Have an auth route group

Route::middleware(['auth'])->group(function () {
    
    Route::get('dog/indexadmin', 'DogController@indexAdmin');
    Route::post('dog/indexadmin', 'DogController@indexAdmin');
    Route::post('dog/add', 'DogController@add');
    Route::post('dog/delete', 'DogController@delete');
    Route::post('pet/update', 'PetController@update');
});

Just quick example.

Watch this free series: https://laracasts.com/series/laravel-6-from-scratch

and

https://laracasts.com/series/whats-new-in-laravel-7

He has several videos covering authentication and authorization.

Please or to participate in this conversation.