我正在try 使用cartalyst sentry 2在我的网站正在与Laravel 4.基本上我不知道如何实现权限.

我看到的关于组权限的示例指定以下内容作为示例:

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

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

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

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.

推荐答案

基本上你必须..

创建您的组

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') );

判断用户是否具有特定访问权限

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.';
}

Laravel相关问答推荐

在Laravel Livewire中,如果一个输入基于另一个唯一的字段是唯一的,如何判断验证?

如何让 Laravel 的 Collection 表现得像一个流?

1 子编译中的警告(使用'stats.children:true'和'--stats-children'了解更多详细信息)

如何在 laravel livewire 中按 bool 值排序数组

如何将用户对象绑定到中间件中的请求

Laravel & Docker:无法打开流或文件/var/www/html/storage/logs/laravel.log:无法打开流:权限被拒绝

如何使用 Laravel Passport 在 API 中正确实现 OAuth?

Laravel 5.1 如何在Blade 文件上使用 {{ old('') }} 助手进行无线电输入

Laravel 4,如何测试复选框是否被选中?

在 Laravel Blade 中转义 vue.js 数据绑定语法?

用 laravel 表单确认删除?

Laravel 5 迁移:Schema::table 方法中不同类型的默认长度

函数 mcrypt_get_iv_size() 在 Laravel 4 上已弃用

如何更改 Handlebars.js 的默认分隔符?

Laravel 5 干预中的图像验证

如何在 laravel 中使用 GROUP_CONCAT

Laravel 5.3 通知 - 仅使用Electron邮件地址通知

控制器中的laravel foreach循环

Eloquent 模型不更新 updated_at 时间戳

Laravel 5 事件处理程序未触发