Laravel:如何检查用户是否是管理员?

Hea*_*mer 3 php laravel

我想让用户成为管理员,在网站入口处检查条件==用户登录,如果是,则给予删除或编辑产品的权利.到目前为止这是我的代码:

@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
Run Code Online (Sandbox Code Playgroud)

Auth::check()与我的登录等同?

xde*_*ull 14

首先,您需要创建包含角色详细信息的角色表.(我假设每个用户可能不仅有多个角色管理员)

角色表:

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

然后

role_user表

Schema::create('user_role', function (Blueprint $table) {
    $table->bigInteger('user_id')->unsigned();
    $table->integer('role_id')->unsigned();
    $table->foreign('user_id')
        ->references('id')->on('user');
    $table->foreign('role_id')
        ->references('id')->on('role');
});
Run Code Online (Sandbox Code Playgroud)

创建角色模型:

然后将角色关系添加到用户模型

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

要检查用户是否具有管理员角色,您可以执行类似的操作

@if($user->roles()->where('name', 'Administrator')->exists())
enter code here
@endif
Run Code Online (Sandbox Code Playgroud)

或者,不要执行此语句,您可以将功能放在用户模型中,如下所示:

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

然后在你的模型中你可以调用它:

@if(Auth::user()->isAdministrator())
enter code here
@endif
Run Code Online (Sandbox Code Playgroud)

其他可能性(1 - 1)关系

首先在迁移中添加is_admin列

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();
    });
Run Code Online (Sandbox Code Playgroud)

然后你可以检查

@if($user->is_admin)

@endif
Run Code Online (Sandbox Code Playgroud)


Abh*_*hek 5

标准的方法是做xdevnull提到的.但是对于小项目,你可以这样做:

If(Auth::check() && Auth::user()->isAdmin()) {
  dd("you are admin") ;
}
Run Code Online (Sandbox Code Playgroud)

在您的用户模型中创建方法isAdmin()

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

当然,您需要在app/config文件夹中创建一个名为settings的配置文件.

把它放在settings.php中

<?php
return ['admin_emails' => explode(', ',  env('ADMIN_EMAILS'));]
Run Code Online (Sandbox Code Playgroud)

最后在.env文件中添加:ADMIN_EMAILS = user1 @ example.com,user2 @ example.com

添加尽可能多的电子邮件,您可以使用逗号进行喷涂.

现在,如果登录用户的电子邮件为user1@example.com或user2@example.com,那么她就是管理员.

另请注意,我已在env文件中添加了管理员电子邮件,以便可以根据环境更改管理员.在开发中,开发人员通常是管理员,而在生产中,其他人(客户端?)是管理员.

PS不要忘记php artisan config:cache在创建settings.php文件后运行


Ach*_*dja 2

给你

  @if(Auth::check() && Auth::user()->role == "admin")

  @endif
Run Code Online (Sandbox Code Playgroud)

您必须为数据库指定角色属性,并且它应该包含 admin 或其他任何内容(您也可以使用 boolean 属性)