我正在使用laravel 5.7 Gate和网站授权策略。我已按照以下步骤进行操作,但是即使用户在角色表中具有权限,也不允许使用Page。 在我的系统中,我有3个主要角色,分别是Super Admin,Admin,Guest。他们具有不同的权限作为附件图像。 Roles and Permissions。 请帮助我解决此问题。
这是我的路线
Highcharts.chart('container', {
title: {
text: 'Highcharts Sankey Diagram'
},
series: [{
colors: ["#90CAF9", "#F44336", "#1565C0"],
keys: ['from', 'to', 'weight'],
data: [
{name: "prop-1", color: "#90CAF9", from: "prop-1", to: "transition", weight: 0},
{name: "prop-2", color: "#90CAF9", from: "prop-2", to: "transition", weight: 4.14},
{name: "imbalance", color: "#F44336", from: "imbalance", to: "transition", weight: 0.6},
{name: "prop-3", color: "#1565C0", from: "transition", to: "prop-3", weight: 4.74},
{name: "prop-4", color: "#1565C0", from: "transition", to: "prop-4", weight: 0},
],
type: 'sankey',
name: 'Sankey demo series'
}]
});
AuthServiceProvide.php
Route::get('/employers', 'EmployerController@index')->name('employers')->middleware('can:view-Employer');
角色模型
public function boot()
{
$this->registerPolicies();
$this->registerEmployerPolicies();
}
public function registerEmployerPolicies()
{
Gate::define('view-Employer', function($user){
$user->hasAccess(['view-Employer']);
});
}
用户模型中的角色和hasAction功能
public function hasAccess(array $permissions)
{
foreach($permissions as $permission){
if($this->hasPermission($permission)){
return true;
echo("<script>console.log('PHP: ".$permissions."');</script>");
}
}
return false;
}
protected function hasPermission(string $permission){
$permissions = json_decode($this->permissions,true);
return $permissions[$permission]??false;
}
角色播种机
public function roles()
{
return $this->belongsToMany(Role::class, 'roles_users');
}
public function hasAccess(array $permissions)
{
foreach($this->roles as $role){
if($role->hasAccess($permissions)){
return true;
}
}
return false;
}
并且代码编写如下教程 link to tutorial