php Laravel:如何检查用户是否是管理员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37093523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: How to check if user is admin?
提问by HeartOfprogrammer
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?
如何等同Auth::check()
于我的登录?
回答by devnull
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
然后
role_user Table
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');
});
Create Role model:
创建角色模型:
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:
或者,您可以将 as function 放入 User 模型中,而不是执行此语句,如下所示:
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
其他可能 (1 - 1) 关系
First add is_admin column in your migration
首先在迁移中添加 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();
});
Then you can check
然后你可以检查
@if($user->is_admin)
@endif
回答by Abhishek
Standard way is to do what xdevnull mentioned. But for small projects you can do something like this:
标准方法是执行 xdevnull 提到的操作。但是对于小型项目,您可以执行以下操作:
If(Auth::check() && Auth::user()->isAdmin()) {
dd("you are admin") ;
}
And in your User model create a method isAdmin()
并在您的 User 模型中创建一个方法 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.
当然,您需要在 app/config 文件夹中创建一个名为 settings 的配置文件。
And put this in settings.php
并将其放在 settings.php 中
<?php
return ['admin_emails' => explode(', ', env('ADMIN_EMAILS'));]
And finally in your .env file add this: [email protected],[email protected]
最后在您的 .env 文件中添加: [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.
现在,如果登录用户的电子邮件为 [email protected] 或 [email protected],那么她就是管理员。
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.
另请注意,我在 env 文件中添加了管理员电子邮件,以便可以根据环境更改管理员。在开发中,开发人员通常是管理员,而在生产中,其他人(客户?)是管理员。
P.S. don't forget to run php artisan config:cache
after creating your settings.php file
PS 不要忘记php artisan config:cache
在创建 settings.php 文件后运行
回答by Achraf Khouadja
here u go
你去吧
@if(Auth::check() && Auth::user()->role == "admin")
@endif
you have to role attribute to your database and it should contain admin or anything else (you can use bolean attribute too)
你必须为你的数据库设置角色属性,它应该包含 admin 或其他任何东西(你也可以使用 bolean 属性)
回答by Martin
In laravel 5.5. this worked for me :
在 Laravel 5.5 中。这对我有用:
@if(Auth::check() && Auth::user()->role['name'] == "admin")
// You are an Admin
@endif
回答by Md.Azizur Rahman
@if (Auth::guard($guard)->check() && Auth::user()->role->id==1)
return redirect()->route('admin.dashboard');
@endif