machuga / authority-l4
适用于PHP的简单灵活的授权系统
Requires
- php: >=5.3.0
- illuminate/support: ~4
- machuga/authority: 2.1.*
Requires (Dev)
- mockery/mockery: 0.7.*
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2023-11-25 09:06:42 UTC
README
我已经没有足够的PHP知识来维护Authority了,所以现在是结束支持的时候了。如果你有兴趣接管它,请在问题中用"@machuga"联系我。
Authority-L4
为Laravel 4提供的简单灵活的授权系统
通过Composer安装
将Authority添加到您的composer.json文件中,以要求Authority
require : {
"laravel/framework": "~4",
"machuga/authority-l4" : "dev-master"
}
现在更新Composer
composer update
最后一个必需
步骤是将服务提供者添加到app/config/app.php
'Authority\AuthorityL4\AuthorityL4ServiceProvider',
恭喜,您已成功安装Authority。但是,我们还包括了一些其他配置选项以供您方便使用。
其他(可选)配置选项
将别名(外观)添加到您的Laravel应用配置文件中。
'Authority' => 'Authority\AuthorityL4\Facades\Authority',
这将允许您通过您习惯于使用Laravel组件的静态接口访问Authority类。
Authority::can('update', 'SomeModel');
发布Authority默认配置文件
php artisan config:publish machuga/authority-l4
这将在app/config/packages/machuga/authority-l4
放置配置文件的副本。配置文件包括一个'initialize'函数,这是设置规则和别名的绝佳位置。
//app/config/packages/machuga/authority-l4 return array( 'initialize' => function($authority) { $user = $authority->getCurrentUser(); //action aliases $authority->addAlias('manage', array('create', 'read', 'update', 'delete')); $authority->addAlias('moderate', array('read', 'update', 'delete')); //an example using the `hasRole` function, see below examples for more details if($user->hasRole('admin')){ $authority->allow('manage', 'all'); } } );
创建角色和权限表
我们提供了一种基本的表结构,以帮助您开始创建自己的角色和权限。
运行Authority迁移
php artisan migrate --package="machuga/authority-l4"
这将创建以下表
- roles
- role_user
- permissions
为了利用这些表,您可以在您的User
模型中添加以下方法。您还需要创建Role和Permission模型存根。
//app/models/User.php public function roles() { return $this->belongsToMany('Role'); } public function permissions() { return $this->hasMany('Permission'); } public function hasRole($key) { foreach($this->roles as $role){ if($role->name === $key) { return true; } } return false; } //app/models/Role.php class Role extends Eloquent {} //app/models/Permission.php class Permission extends Eloquent {}
最后,在您在上一个配置步骤中复制的Authority配置文件中。您可以添加一些规则
<?php //app/config/packages/machuga/authority-l4 return array( 'initialize' => function($authority) { $user = $authority->getCurrentUser(); //action aliases $authority->addAlias('manage', array('create', 'read', 'update', 'delete')); $authority->addAlias('moderate', array('read', 'update', 'delete')); //an example using the `hasRole` function, see below examples for more details if($user->hasRole('admin')) { $authority->allow('manage', 'all'); } // loop through each of the users permissions, and create rules foreach($user->permissions as $perm) { if($perm->type == 'allow') { $authority->allow($perm->action, $perm->resource); } else { $authority->deny($perm->action, $perm->resource); } } } );
通用用法
//If you added the alias to `app/config/app.php` then you can access Authority, from any Controller, View, or anywhere else in your Laravel app like so: if( Authority::can('create', 'User') ) { User::create(array( 'username' => 'someuser@test.com' )); } //If you just chose to use the service provider, you can use the IoC container to resolve your instance $authority = App::make('authority');
接口
您需要了解5个基本功能才能利用Authority。
-
allow: 创建一个规则,允许访问资源
示例1
Authority::allow('read', 'User');
示例2,使用额外条件
Authority::allow('manage', 'User', function($self, $user){ return $self->getCurrentUser()->id === $user->id; });
-
deny: 创建一个规则,拒绝访问资源
示例1
Authority::deny('create', 'User');
示例2,使用额外条件
Authority::deny('delete', 'User', function ($self, $user) { return $self->getCurrentUser()->id === $user->id; });
-
can: 检查用户是否可以访问资源
示例
Authority::can('read', 'User', $user);
-
cannot:检查用户是否无法访问资源
示例
Authority::cannot('create', 'User');
-
addAlias:将一组操作进行别名设置
本例中将名为
manage
的 CRUD 方法进行别名设置Authority::alias('manage', array('create', 'read', 'update', 'delete'));
转换为该库,您之前使用 IoC 容器来解决实例。
服务提供者将仅创建一个新的 Authority 实例,并将当前登录用户传递给构造函数。这应该与您在 IoC 注册表中进行的操作基本相同。这意味着您过去使用的任何代码都应该仍然正常工作!但是,建议您将规则定义移动到提供的配置文件中。