Laravel 7 - 角色和权限
Laravel 7 - Roles and Permissions
我在 Laravel 7 中限制用户角色的菜单选项时遇到问题。 'admin' 和 'profesor' 的菜单很相似,但我决定为每个视图创建两个不同的视图,并在主视图中创建一个 @include。
- 用户模型
public function roles()
{
return $this->belongsToMany(Role::class,'assigned_roles');
}
public function isAdmin()
{
return $this->hasRoles(['admin']);
}
public function hasRoles(array $roles)
{
foreach ($roles as $role) {
foreach ($this->roles as $userRole) {
if ($userRole->name === $role) {
return true;
}
}
}
return false;
}
public function userRole()
{
return $this->role->nombre_rol;
}
- 我的观点
<hr class="hr-unam">
@if (auth()->user()->hasRoles(['admin']))
<div class="sideover">
<!-- *********************** SERVESCOL *********************** -->
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h5 class=" text-center panel-title">
<a class="txt-unam txt-side font-weight-bold text-uppercase" style="text-decoration:none" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
SERVESCOL
</a>
</h5>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="panel-group" id="sub-accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="subHeadingOne">
<a role="button" data-toggle="collapse" data-parent="#sub-accordion" href="#collapseSubOne" aria-expanded="true" aria-controls="collapseSubOne">
@include('main.sidebarServescol')
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<hr class="hr-unam">
@else
@if(auth()->user()->hasRoles(['profesor']))
<!-- *********************** SERVESCOL *********************** -->
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h5 class=" text-center panel-title">
<a class="txt-unam txt-side font-weight-bold text-uppercase" style="text-decoration:none" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
SERVESCOL
</a>
</h5>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="panel-group" id="sub-accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="subHeadingOne">
<a role="button" data-toggle="collapse" data-parent="#sub-accordion" href="#collapseSubOne" aria-expanded="true" aria-controls="collapseSubOne">
@include('main.sidebarProfesor')
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<hr class="hr-unam">
@endif
@endif
但是这两个选项都消失了,视图也崩溃了。我也直接在每个菜单的视图中尝试过,但是在两者中,'admin' 的作用与 'profesor' 的作用一样,删除覆盖 @if.
的选项
您的 hasRoles
函数可以更简单一些
public function hasRoles(array $roles)
{
return $this->roles->contains(function ($role, $key) use ($roles) {
return in_array($role->name, $roles);
});
}
// PHP 7.4 and above
public function hasRoles(array $roles)
{
return $this->roles->contains(fn($role, $key) => in_array($role->name, $roles));
}
您可以使用 @includeWhen
指令。
<hr class="hr-unam">
<div class="sideover">
<!-- *********************** SERVESCOL *********************** -->
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h5 class=" text-center panel-title">
<a class="txt-unam txt-side font-weight-bold text-uppercase" style="text-decoration:none" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
SERVESCOL
</a>
</h5>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="panel-group" id="sub-accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="subHeadingOne">
<a role="button" data-toggle="collapse" data-parent="#sub-accordion" href="#collapseSubOne" aria-expanded="true" aria-controls="collapseSubOne">
@includeWhen(auth()->user()->hasRoles(['admin']), 'main.sidebarServescol')
@includeWhen(auth()->user()->hasRoles(['profesor']), 'main.sidebarProfesor')
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
我在 Laravel 7 中限制用户角色的菜单选项时遇到问题。 'admin' 和 'profesor' 的菜单很相似,但我决定为每个视图创建两个不同的视图,并在主视图中创建一个 @include。
- 用户模型
public function roles()
{
return $this->belongsToMany(Role::class,'assigned_roles');
}
public function isAdmin()
{
return $this->hasRoles(['admin']);
}
public function hasRoles(array $roles)
{
foreach ($roles as $role) {
foreach ($this->roles as $userRole) {
if ($userRole->name === $role) {
return true;
}
}
}
return false;
}
public function userRole()
{
return $this->role->nombre_rol;
}
- 我的观点
<hr class="hr-unam">
@if (auth()->user()->hasRoles(['admin']))
<div class="sideover">
<!-- *********************** SERVESCOL *********************** -->
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h5 class=" text-center panel-title">
<a class="txt-unam txt-side font-weight-bold text-uppercase" style="text-decoration:none" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
SERVESCOL
</a>
</h5>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="panel-group" id="sub-accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="subHeadingOne">
<a role="button" data-toggle="collapse" data-parent="#sub-accordion" href="#collapseSubOne" aria-expanded="true" aria-controls="collapseSubOne">
@include('main.sidebarServescol')
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<hr class="hr-unam">
@else
@if(auth()->user()->hasRoles(['profesor']))
<!-- *********************** SERVESCOL *********************** -->
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h5 class=" text-center panel-title">
<a class="txt-unam txt-side font-weight-bold text-uppercase" style="text-decoration:none" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
SERVESCOL
</a>
</h5>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="panel-group" id="sub-accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="subHeadingOne">
<a role="button" data-toggle="collapse" data-parent="#sub-accordion" href="#collapseSubOne" aria-expanded="true" aria-controls="collapseSubOne">
@include('main.sidebarProfesor')
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<hr class="hr-unam">
@endif
@endif
但是这两个选项都消失了,视图也崩溃了。我也直接在每个菜单的视图中尝试过,但是在两者中,'admin' 的作用与 'profesor' 的作用一样,删除覆盖 @if.
的选项您的 hasRoles
函数可以更简单一些
public function hasRoles(array $roles)
{
return $this->roles->contains(function ($role, $key) use ($roles) {
return in_array($role->name, $roles);
});
}
// PHP 7.4 and above
public function hasRoles(array $roles)
{
return $this->roles->contains(fn($role, $key) => in_array($role->name, $roles));
}
您可以使用 @includeWhen
指令。
<hr class="hr-unam">
<div class="sideover">
<!-- *********************** SERVESCOL *********************** -->
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h5 class=" text-center panel-title">
<a class="txt-unam txt-side font-weight-bold text-uppercase" style="text-decoration:none" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
SERVESCOL
</a>
</h5>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<div class="panel-group" id="sub-accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="subHeadingOne">
<a role="button" data-toggle="collapse" data-parent="#sub-accordion" href="#collapseSubOne" aria-expanded="true" aria-controls="collapseSubOne">
@includeWhen(auth()->user()->hasRoles(['admin']), 'main.sidebarServescol')
@includeWhen(auth()->user()->hasRoles(['profesor']), 'main.sidebarProfesor')
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>