【问题标题】:Sentinel usage in LaravelLaravel 中的哨兵用法
【发布时间】:2016-08-11 14:40:34
【问题描述】:

我浏览了文档,但似乎找不到太多关于我需要做什么的信息。我已经安装了这个包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 创建新模型时,为用户/角色授予某些操作权限的过程是什么?

谢谢

【问题讨论】:

    标签: laravel laravel-5.2 sentinel


    【解决方案1】:

    首先是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();
    

    然后一次性检查权限和用户类型...这将是管理员用户尝试更新的示例

    if (Sentinel::inRole('admin') and $user->hasAccess(['admin.update']) {
        //
    }
    

    我好久没用过Sentinel了,你也许可以完全省略inRole检查,不确定。

    【讨论】:

    • 看,我明白那部分。我的问题是这个。我有两个角色;管理员和用户。我为每个角色分配了一个用户。这是我在种子文件中进行设置时完成的。我现在有一个新模型,客户。我需要以某种方式声明只有管理员可以更新客户端,而用户只能查看它们。我们将为此采取什么流程?
    猜你喜欢
    • 2014-01-21
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多