我已经浏览了文档,但似乎找不到太多关于我需要做什么的信息。我已经安装了这个包Centaur
据我所知,它本质上是Sentinel的扩展,所以应该以同样的方式工作。安装和设置之后,我就可以创建用户和角色了。这一切都很好。所以在我的布局视图中,我提供了一个到客户索引页的链接,我希望每个人都能看到它,这样我就不会做任何检查。
<li class="{{ Request::is('clients*') ? 'active' : '' }}"><a href="{{ route('clients.index') }}">Clients</a></li>查看用户和角色控制器时,我假设我的ClientsController中需要一个声明权限的构造函数。这就是我的东西
public function __construct(AuthManager $authManager)
{
// Middleware
$this->middleware('sentinel.auth');
$this->middleware('sentinel.access:users.view', ['only' => ['index', 'show']]);
$this->middleware('sentinel.role:administrator');
// Dependency Injection
$this->roleRepository = app()->make('sentinel.roles');
$this->authManager = $authManager;
}我试图实现的是让用户只查看客户端,管理员可以更新它们。在我的编辑角色视图中,我添加了复选框
<div class="checkbox">
<label>
<input type="checkbox" name="permissions[clients.update]" value="1" {{ $role->hasAccess('clients.update') ? 'checked' : '' }}>
clients.update
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="permissions[clients.view]" value="1" {{ $role->hasAccess('clients.view') ? 'checked' : '' }}>
clients.view
</label>
</div>就目前情况而言,管理员可以查看和更新客户端。但是,当我以普通用户身份登录系统时,当我单击客户端的链接时,我会看到
Error: You do not have permission to do that. 那么,当使用Sentinel创建新模型时,为用户/角色授予某些操作的权限的过程是什么?
谢谢
发布于 2016-08-11 22:50:12
先是create the role,然后是attach the role to the user。
因此,提供的链接中的快速示例:
$role = Sentinel::getRoleRepository()->createModel()->create([
'name' => 'Subscribers',
'slug' => 'subscribers',
]);
$user = Sentinel::findById(1);
// use this for lookup where you don't already have a role handy
$role = Sentinel::findRoleByName('Subscribers');
$role->users()->attach($user);在您的情况下,可能类似于以下角色权限:
$role->permissions = [
"admin.view" => true,
"admin.update" => true
"user.view" => true,
"user.update" => false
];
$role->save();那么,在一个go...This中检查用户的权限和类型就是管理用户尝试更新的一个示例
if (Sentinel::inRole('admin') and $user->hasAccess(['admin.update']) {
//
}我已经很长时间没有使用前哨了,你也许可以完全忽略inRole检查,我不确定。
https://stackoverflow.com/questions/38899466
复制相似问题