如何在laravel刀片视图中检查用户是否为Admin

问题描述 投票:4回答:5

我有

用户表喜欢

+==============+
|     User     |
+==============+
|      id      |
+--------------+
|   firstname  |
+--------------+
|    lastname  |
+--------------+
|     email    |
+--------------+
|   password   |
+--------------+

和我的角色表

+==============+
|     Roles    |
+==============+
|      id      |
+--------------+
|     name     |
+--------------+

我的role_user表是

+=============+
|  role_user  |
+=============+
|   user_id   |
+-------------+
|   role_id   |
+-------------+

如何检查当前登录的用户是管理员还是普通用户?

laravel eloquent laravel-5.1
5个回答
14
投票

您需要在roles模型中添加User关系,如下所示:

public function roles() 
{
   return $this->belongsToMany(App\Role::class);
}

现在你需要像这样创建isAdmin用户:

public function isAdmin() 
{
   return in_array(1, $this->roles()->pluck('role_id')->all());
}

作为1,你把你的管理员角色的id。当然它也可以用其他方式定义,但一切都取决于如何使用它。

它也可以这样定义:

public function isAdmin() 
{
   return $this->roles()->where('role_id', 1)->first();
}

现在在你的刀片中你可以做到:

@if (auth()->check())
   @if (auth()->user()->isAdmin())
      Hello Admin
   @else
      Hello standard user
   @endif
@endif

3
投票

Role.php

use Illuminate\Database\Eloquent\Model;

class Role extends Model {

    protected $fillable = [
        'name'
    ];

    /**
     * A role can have many users.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function users() {

        return $this->belongsToMany('App\User');
    }

}

然后您可以将其添加到用户模型:

public function isAdmin()
{
    foreach ($this->roles()->get() as $role)
    {
        if ($role->name == 'Admin')
        {
            return true;
        }
    }
}

视图

@if(Auth::check())
    @if (Auth::user()->isAdmin())
        <h2>Admin user enter code here<h2>
    @endif
@endif

1
投票

它不是一个ACL这个简单的功能,你甚至不需要一个数据库表roles你可以添加额外的tinyInteger status列和添加数字,例如:

  • 0 =已禁用
  • 1 =访客
  • 2 =管理员。

为了使其功能,将以下代码添加到您的User.php

public function isDisabled ()
{
    return $this->statusCheck();
}

public function isVisitor ()
{
    return $this->statusCheck(1);
}

public function isAdmin ()
{
    return $this->statusCheck(2);
}

protected function statusCheck ($status = 0)
{
    return $this->status === $status ? true : false;
}

要检入blade模板,您可以添加

@if(Auth::user()->isDisabled())
    You are not Active
@elseif(Auth::user()->isVisitor())
    Welcome to example.com
@elseif(Auth::user()->isAdmin())
    Welcome Admin
@endif

此外,您可以制作刀片自定义指令,将此代码粘贴到app/providers/AppServiceProvider.php中的boot()方法中。

// Blade custom directives for isAdmin

    Blade::directive('isAdmin', function() {
        return "<?php if(Auth::user()->isAdmin()): ?>";
    });

    Blade::directive('endisAdmin', function() {
        return "<?php endif; ?>";
    });

// Blade custom directives for isVisitor

    Blade::directive('isVisitor', function() {
        return "<?php if(Auth::user()->isVisitor()): ?>";
    });

    Blade::directive('endisVisitor', function() {
        return "<?php endif; ?>";
    });

// Blade custom directives for isDisabled

    Blade::directive('isDisabled', function() {
        return "<?php if(Auth::user()->isDisabled()): ?>";
    });

    Blade::directive('endisDisabled', function() {
        return "<?php endif; ?>";
    });

要调用它,您需要在blade view中编写以下行

@isAdmin()
     Welcome Admin
@endisAdmin

@isVisitor()
     Welcome to example.com
@endisVisitor

@isDisabled()
     Your are not active
@endisDisabled

简而言之,laravel为您提供了许多解决问题的方法,它只取决于您的需求和应用程序结构。


0
投票

方法共享工作。问题是如果你必须每页检查多次,它会多次访问数据库。例如,假设您有一个包含8个链接的导航。第一,第四和第七个链接只能由管理员显示。该查询将使您的数据库达到3倍。也许我只是肛门,但这是一个重复的请求。

我试图找到另一种方法来存储一个在视图/模板中加载一次的变量,这样每次我需要检查它是否是管理员时,我检查变量而不是每次都打到数据库。我通过控制器 - >视图完成了它,但不仅仅是在模板中单独查看。我正在考虑创建一个辅助方法,并在每页加载时返回一个要检查的对象。


-2
投票

所以你有一些字段isAdmin,如果它是1,例如用户是管理员,如果不是。当用户被用来检查(Auth::user()->isAdmin == 1)然后用户是管理员,否则不是

使用Auth::user()->你可以检查当前登录用户的用户表中的任何字段。

最好的祝福

© www.soinside.com 2019 - 2024. All rights reserved.