如何:使用 Laravel 4 实现哨兵 2 权限? [英] How to: implement sentry 2 permissions with Laravel 4?
问题描述
我正在尝试在使用 Laravel 4 构建的网站中使用 cartalyst sentry 2.基本上我不明白如何实现权限.
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.
我看到的组权限示例指定以下示例:
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?
谁能一步一步告诉我如何在 laravel 4 应用程序中使用?我已经阅读了支持这些功能的文档,但我只是不确定如何设置数据以使功能正常工作.
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') );
检查用户是否具有特定访问权限
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
}
获取特定用户的所有组
try
{
// Find the user using the user id
$user = Sentry::getUserProvider()->findById(1);
// Get the user groups
$groups = $user->getGroups();
}
catch (CartalystSentryUsersUserNotFoundException $e)
{
echo 'User was not found.';
}
这篇关于如何:使用 Laravel 4 实现哨兵 2 权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!