mnshankar / role-based-authority
此包的最新版本(v1.4)没有可用的许可证信息。
修改了Authority-L4,使其使用角色(而不是用户)作为权限管理的中心单位
v1.4
2014-06-21 07:08 UTC
Requires
- php: >=5.3.0
- illuminate/support: 4.2.*
- machuga/authority: 2.1.*
This package is not auto-updated.
Last update: 2024-09-23 15:20:39 UTC
README
这是一个简单的包,为Laravel4应用提供了基于角色的访问控制(RBAC)。
它与Authority-L4非常相似,因为它提供了一个围绕“Authority”包的Laravel 4外壳。然而,此包侧重于管理分配给“角色”的权限,而不是“用户”。
在检查了许多Laravel的授权系统之后,我最终选择了Authority
- 易于理解和管理
- 没有其他外部依赖
- 在PHP 5.3+上运行
- 正如广告中所说的一样工作
(就认证而言,我认为Laravel的库存实现工作得非常好)
通过composer安装RoleBasedAuthority
"require": {
"laravel/framework": "4.2.*",
"mnshankar/role-based-authority": "dev-master"
},
运行composer update以引入依赖项。
将其添加到Provider列表(app.php)
'mnshankar\RoleBasedAuthority\RoleBasedAuthorityServiceProvider',
将其添加到Alias列表(app.php)
'Authority' => 'mnshankar\RoleBasedAuthority\Facades\Authority',
运行所有迁移(在执行此操作之前,请记住设置您的数据库配置。请注意,它包含用户表的迁移,因此您可能希望在运行之前检查代码)
php artisan migrate --package="mnshankar/role-based-authority"
发布配置文件
php artisan config:publish mnshankar/role-based-authority
(此中央配置文件是您放置所有规则的地方。为了缓存“Authority”对象,请将'cache'设置为true并指定cache_ttl)
创建/修改您的User、Role和Permission模型以允许关系(用户和角色之间的多对多,角色和权限之间的一对多):User.php
...
public function roles() {
return $this->belongsToMany('Role');
}
public function hasRole($key)
{
foreach ($this->roles as $role) {
if ($role->role_name === $key) {
return true;
}
}
return false;
}
...
Role.php
class Role extends Eloquent
{
public function permissions() {
return $this->hasMany('Permission');
}
}
Permission.php
class Permission extends Eloquent
{
}
与Authority的主要变化是
- 数据库迁移(包括用户、角色和权限之间的关系)
- 表“permissions”中的“type”字段设置为enum(允许/拒绝)
- 支持角色继承(使用Roles中的“inherited_roleid”列)
- 配置文件更改(遍历所有用户角色,并创建规则列表)
- 支持缓存Authority对象(在配置文件中将'cache'设置为true)
常见用法模式
一旦设置了表(具有用户、角色和权限),在您的应用程序中进行授权检查相对简单。以下是一些启动数据
--create a user (pwd must be hashed!)
INSERT INTO users VALUES ('1', 'LastName', 'FirstName', 'email@email.com', 'hashedpwd****', now(), now());
-- Establish roles. member inherits from guest and admin inherits from member
INSERT INTO roles VALUES ('1', 'guest', null, now(), now());
INSERT INTO roles VALUES ('2', 'member', '1', now(), now());
INSERT INTO roles VALUES ('3', 'admin', '2', now(), now());
-- Assign roles to users. Userid 1 has 'admin' role
INSERT INTO role_user(role_id, user_id) VALUES ('3', '1');
-- Setup Permissions table
-- ----------------------------
-- Role id 3 (admin) is allowed action "admin" on "all" resources
-- NOTE: 'all' - is a special resource that can be used as a wildcard
-- https://github.com/machuga/authority/blob/master/src/Authority/Rule.php#L101
-- Role id 2 (member) is allowed "create", "view" and "edit" actions on resource named 'album'
-- ----------------------------
INSERT INTO permissions VALUES ('1', '3', 'allow', 'admin', 'all', now(), now());
INSERT INTO permissions VALUES ('2', '2', 'allow', 'create', 'album', now(), now());
INSERT INTO permissions VALUES ('3', '2', 'allow', 'view', 'album', now(), now());
INSERT INTO permissions VALUES ('4', '2', 'allow', 'edit', 'album', now(), now());
以下代码片段展示了检查适当权限的方法
/***Check in controller - create() method of resource album***/
//checks if the logged in user has authority to create an album
//if person with admin or member role is logged in, they will be permitted. Not otherwise
Auth::loginUsingId(1); //force login - testing only
if (Authority::cannot('create', 'album')) {
App::abort('403', 'Sorry! Not authorized');
}
可以使用过滤器添加角色检查,例如
Route::filter('admin', function(){
if (!Auth::user()->hasRole('admin'))
{
return App::abort('403', 'You are not authorized.');
}
});
(然后可以使用'admin'作为before过滤器)
有关使用说明和可用选项的更多信息,请参阅authority-l4(和authority)的说明