'Laravel authentication with multiple roles
Hi I'm a beginner at laravel, I have to develop a project for human resources management and I have two roles admin and employee. I followed a tutorial at laracast to build roles and abilities table and everything seems to be working. Now I just installed laravel/ui package and I can see login and register which are working fine with the users I registered in the database. My problem now is that I don't know how to connect things together. How can I check if the logged in user is admin so the admin panel opens. Waiting for your replies. Here is the code; Error I'm receiving: 419 page expired
This is what I tried but doesn't work
protected function authenticated(Request $request, $user)
{
// to admin dashboard
if(auth()->user()->roles()->name === 'admin') {
return redirect(route('admin'));
}
// to user dashboard
else if(auth()->user()-roles()->name === 'user') {
return redirect(route('home'));
}
abort(404);
}
Routes
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/admin', 'LoginController@authenticated')->name('admin');
users table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
create roles table
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('abilities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('ability_role', function (Blueprint $table) {
$table->primary(['role_id','ability_id']);
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('ability_id');
$table->timestamps();
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->foreign('ability_id')
->references('id')
->on('roles')
->onDelete('cascade');
});
Schema::create('role_user', function (Blueprint $table) {
$table->primary(['user_id','role_id']);
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('role_id');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
});
}
User.php
public function roles()
{
if(is_string($role))
{
$role = Role::whereName($role)->firstOrFail();
}
return $this->belongsToMany(Role::class)->withTimestamps();
}
public function assignRole($role)
{
$this->roles()->sync($role, false);
}
public function abilities($role)
{
return $this->roles->map->abilities->flatten()->pluck('name')->unique();
}
Role.php
class Role extends Model
{
protected $guarded = [];
public function abilities()
{
return $this->belongsToMany(Ability::class)->withTimestamps();
}
public function allowTo($ability)
{
$this->abilities()->sync($ability,false);
}
}
Ability.php
{
protected $guarded = [];
public function roles()
{
if(is_string($ability))
{
$ability = Ability::whereName($ability)->firstOrFail();
}
return $this->belongsToMany(Role::class)->withTimestamps();
}
}
Solution 1:[1]
To management roles and permissions I'm using:
"santigarcor/laratrust"
You can learn more here: https://github.com/santigarcor/laratrust You can use it like this:
use Illuminate\Support\Facades\Auth;
if (Auth::user()->isAbleTo('edit-user')) {}
if (Auth::user()->hasRole('admin')) {}
if (Auth::user()->isA('guide')) {}
if (Auth::user()->isAn('admin')) {}
This package provides a user interface for the santigarcor/laratrust package
"icweb/trusty"
You can learn more here: https://github.com/icweb/laratrust-ui
All tables and data will be auto created.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tomerikoo |
