usm4n / guardian
基于角色的访问控制包,用于Laravel,带有后端界面
Requires
- php: >=5.4.0
- illuminate/html: ~5.0
- illuminate/support: 5.0.x
This package is not auto-updated.
Last update: 2024-09-24 03:08:05 UTC
README
##Guardian: 基于角色的访问控制包,用于Laravel 5和4,带有后端界面
###对于Laravel 4,请切换到 旧分支。
Guardian包为Laravel提供了一种简单的方法来管理基于角色的访问控制。通过其简约的界面,您可以添加用户、角色和能力。每个用户可以分配多个角色(多对多),每个角色也可以分配多个能力。
Guardian还附带了许多访问控制辅助方法,使您能够轻松跟踪代码中特定用户的访问。
##安装和设置
要安装guardian,请在您的composer.json文件中添加以下行:
"require-dev": {
"usm4n/guardian": "dev-master"
}
添加以上行后,保存文件并运行
composer update --dev
Composer安装过程成功完成后,将以下行添加到app/config/app.php文件中的providers数组中:
'Usman\Guardian\GuardianServiceProvider'
如果您尚未安装
illuminate/html包,则需要将Illuminate\Html\HtmlServiceProvider添加到providers数组中。
###运行包迁移
为了设置guardian的数据库,您需要运行以下命令来运行包迁移文件:
artisan vendor:publish --provider="Usman\Guardian\GuardianServiceProvider" --tag="guardian-migrations"
artisan migrate
第一个命令将包迁移文件复制到您的安装目录的database/migrations目录中。您可以在运行第二个命令之前对任何表进行自定义更改。
###发布视图和资源
运行以下artisan命令,将包资源发布到public目录,将视图发布到resources/views目录:
artisan vendor:publish --provider="Usman\Guardian\GuardianServiceProvider" --tag="guardian-assets"
artisan vendor:publish --provider="Usman\Guardian\GuardianServiceProvider" --tag="guardian-views"
###模型设置
Guardian要求您在您的app目录中拥有以下模型:User、Role和Capability。
app/Role.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Role extends Model { public function users() { return $this->belongsToMany('App\User'); } public function capabilities() { return $this->belongsToMany('App\Capability'); } }
app/Capability.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Capability extends Model { public function roles() { return $this->belongsToMany('App\Role'); } }
User模型需要以下更改:
... use Usman\Guardian\AccessControl\AccessControlTrait; use Usman\Guardian\AccessControl\AccessControlInterface; class User extends Model implements AccessControlInterface, AuthenticatableContract, CanResetPasswordContract { use Authenticatable, CanResetPassword, AccessControlTrait;
请注意,如果您的模型命名空间不是App\,您需要在包的config.php文件中反映这些更改。您可以使用以下命令将包配置文件复制到您的app/config目录:
artisan vendor:publish --provider="Usman\Guardian\GuardianServiceProvider" --tag="guardian-config"
注意:您可以使用
artisan vendor:publish --provider="Usman\Guardian\GuardianServiceProvider"命令一次性发布所有包文件。
vendor/usm4n/guardian/src/config/config.php
<?php return [ 'userModel' => 'App\User', 'roleModel' => 'App\Role', 'capabilityModel' => 'App\Capability', ];
在做出所需的更改后,您将能够通过http://www.yoursite.com/guardian/backend访问guardian后端。默认应用了auth中间件,因此您需要先登录。
在开发/本地环境中,您可以从包的
routes.php文件中删除'middleware'=>'auth'键值对,以便进行测试。
##Guardian辅助函数
以下辅助方法可通过Guardian外观访问:
Guardian::hasRole($roleName)- 如果当前用户有该角色则返回true,否则返回false。Guardian::hasAnyRole(array $roleNames)- 如果用户有提供的任一角色则返回true,否则返回false。Guardian::hasAllRoles(array $rolesNames)- 只有当用户有提供的所有角色时才返回true,否则返回false。Guardian::hasCapability($capabilityName)- 如果用户有提供的权限则返回true,否则返回false。Guardian::hasAnyCapability($capabilityNames)- 如果用户有提供的任一权限则返回true,否则返回false。Guardian::hasAllCapabilities($capabilityNames)- 如果用户拥有所有提供的功能,则返回true,否则返回false。
## Laravel 帮助资源
## 致谢
## 许可证
Guardian 是一款免费软件,根据MIT许可证条款发布。
## 待办事项清单
- 代码文档。

