如何:使用 Laravel 4 实现哨兵 2 权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17012553/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:55:48  来源:igfitidea点击:

How to: implement sentry 2 permissions with Laravel 4?

laravellaravel-4cartalyst-sentry

提问by Ray

I'm trying to use cartalyst sentry 2 in my site being built with Laravel 4. Basically I don't understand how to implement permissions.

我正在尝试在使用 Laravel 4 构建的站点中使用 cartalyst sentry 2。基本上我不明白如何实现权限。

The examples I've seen for permissions for a group specify the following as an example:

我所看到的组权限示例将以下内容指定为示例:

{
    "name" : "Administrator",
    "permissions" : 
    {
        "user.create" : 1,
        "user.delete" : 1,
        "user.view"   : 1,
        "user.update" : 1
    }
}

SO this is setting permissions for the admin group. BUT where are these permissions set?

所以这是为管理员组设置权限。但是这些权限在哪里设置?

In the table 'groups' there is a field called permissions which is a text field - are they set there - if so how? Or are these set in a model or controller?

在“组”表中,有一个名为权限的字段,它是一个文本字段 - 它们是否设置在那里 - 如果是,如何设置?或者这些设置在模型或控制器中?

Can anyone point me to s step by step on how to use in a laravel 4 app? I've read the supporting docs which foes through the functions but I'm just not sure how to set the data to get the functions to work.

谁能告诉我如何在 laravel 4 应用程序中使用的步骤?我已经阅读了支持函数的支持文档,但我不确定如何设置数据以使函数正常工作。

回答by Antonio Carlos Ribeiro

Basically you have to..

基本上你必须..

Create your groups

创建您的群组

Sentry::getGroupProvider()->create([
    'name' => 'Super Administrators',
    'permissions' => [
        'system' => 1,
    ],
]);

Sentry::getGroupProvider()->create([
    'name' => 'Managers',
    'permissions' => [
        'system.products' => 1,
        'system.store' => 1,
        'system.profile' => 1,
    ],
]);

Set a group to a particular user, in this case it is setting Managers to the current logged user

将组设置为特定用户,在这种情况下,它将管理器设置为当前登录的用户

Sentry::getUser()->addGroup( Sentry::getGroupProvider()->findByName('Managers') );

Check if a user has a particular access

检查用户是否具有特定访问权限

if ( Sentry::getUser()->hasAnyAccess(['system','system.products']) )
{
    // Will be able to do a thing
}

Check if a user is Super Administrator (only this group has the 'system' access)

检查用户是否是超级管理员(只有该组具有“系统”访问权限)

if ( Sentry::getUser()->hasAnyAccess(['system']) )
{
    // Will be able to do a thing
}

Get all groups from a particular user

从特定用户获取所有组

try
{
    // Find the user using the user id
    $user = Sentry::getUserProvider()->findById(1);

    // Get the user groups
    $groups = $user->getGroups();
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
    echo 'User was not found.';
}

回答by Patrick Reck

In your groupstable you set the permissions using JSON.

在您的groups表中,您使用 JSON 设置权限。

I have the following columns:

我有以下几列:

id | name | permissions

id | name | permissions

And a row:

还有一行:

1 | admin | {"admin":1, "create_news": 1}

1 | admin | {"admin":1, "create_news": 1}

Assign a user to a group using the table users_groups

使用表将用户分配到组 users_groups

Now you can use the following example to check if a user have a given permission:

现在您可以使用以下示例来检查用户是否具有给定的权限:

$user = Sentry::getUser();
if ($user->hasAccess('create_news')) {
    echo "You can create a news item";
}
else {
    echo "You can't create a news item";
}