Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How to check if user is admin?

Tags:

php

laravel

I want to make the user an administrator who at the entrance to the site conditions are checked == user login , if so then give the right to remove or edit product. This is my code so far:

@if(Auth::check())
   <p><a href="#" class="btn btn-success" role="button">Edite</a> <a href="#" class="btn btn-danger" role="button">Remove</a></p>
@endif

How equate Auth::check() with my login?

like image 527
HeartOfprogrammer Avatar asked May 07 '16 20:05

HeartOfprogrammer


2 Answers

First of all you need to create the role table that contain the roles details. (I'm assuming Each user may have multiple roles not only Administrator)

Roles Table:

    Schema::create('role', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
    });

Then

user_role Table:

Schema::create('user_role', function (Blueprint $table) {
    $table->bigInteger('user_id')->unsigned();
    $table->integer('role_id')->unsigned();
    $table->foreign('user_id')
        ->references('id')->on('users');
    $table->foreign('role_id')
        ->references('id')->on('roles');
});

Create Role model:

php artisan make:model Role

Afterwards add the roles relationship to the user model:

class User extends Authenticatable {
    public function roles() {
        return $this->belongsToMany(Role::class, 'user_role');
    }
}

To check if user has Administrator role you can do something like:

@if($user->roles()->where('name', 'Administrator')->exists())
enter code here
@endif

Or instead of doing this statement you can put as function in the User model as below:

public function isAdministrator() {
   return $this->roles()->where('name', 'Administrator')->exists();
}

Then in your model you can call it:

@if(Auth::user()->isAdministrator())
enter code here
@endif

Other possibility (1 - 1) Relation

First add is_admin column in your migration:

public function up()
{
    Schema::create('user', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->boolean('is_admin')->default(0);
        $table->rememberToken();
        $table->timestamps();
    });

Then you can check:

@if($user->is_admin)

@endif
like image 62
devnull Avatar answered Oct 19 '22 21:10

devnull


Standard way is to do what xdevnull mentioned. But for small projects you can do something like this:

If(Auth::check() && Auth::user()->isAdmin()) {
  dd("you are admin") ;
}

And in your User model create a method isAdmin()

function isAdmin() {
  $admin_emails = config('settings.admin_emails');
  if(in_array($this->email, $admin_emails)  return true;
  else return false;
}

Of course you need to create a config file called settings in your app/config folder.

And put this in settings.php

<?php
return ['admin_emails' => explode(', ',  env('ADMIN_EMAILS'));]

And finally in your .env file add this: [email protected],[email protected]

Add as much emails as you want spayed by commas.

Now if logged in user has email as [email protected] or [email protected] then she is Admin.

Also notice I have added admin emails in env file so that admins can be changed depending on the environment. In development usually developer is the admin while in production somebody else (client?) is the admin.

P.S. don't forget to run php artisan config:cache after creating your settings.php file

like image 14
Abhishek Avatar answered Oct 19 '22 22:10

Abhishek