techexim / authorization
此包已被废弃且不再维护。未建议替代包。
角色与权限授权方案
1.2.1
2015-12-28 06:44 UTC
Requires
- techexim/laravel: ~1.0
This package is not auto-updated.
Last update: 2019-12-20 03:25:26 UTC
README
此包是一个提供任何类型对象的角色与权限管理的包,目前设计用于Laravel 5。
安装
使用composer下载包
composer require techexim/authorization
将服务提供者添加到Laravel应用中
TechExim\Auth\AuthServiceProvider::class,
可能需要发布默认迁移和配置
php artisan vendor:publish
如何使用
守卫
守卫是一个简单实现的提供检查主题对对象权限的最简单方式的服务。
use TechExim\Auth\Contracts\Guard; class MyController extends Controller { public function postAuth(Guard $guard) { // Check permissions of a subject // Both subject and object must implement TechExim\Auth\Contracts\Item $guard->hasPermissionTo($subject, 'section.action', $object); } }
角色
use TechExim\Auth\Contracts\Role\Repository as RoleRepository; class MyController extends Controller { public function postRole(RoleRepository $roleRepository) { // Add a new role $roleRepository->create('my_role'); // Assign role to an object // Object must implement TechExim\Auth\Contracts\Item $roleRepository->assignItemRoleName($object, 'my_role'); // Assign role to a subject on an object // Both subject and object must implement TechExim\Auth\Contracts\Item $roleRepository->assignObjectRoleName($subject, 'my_role', $object); // Get all assigned roles of an object $roleRepository->getItemRoles($object); // Check if a subject has a particular role on an object $roleRepository->hasObjectRoleName($subject, 'my_role', $object); // Remove subject's role on an object $roleRepository->removeObjectRoleName($subject, 'my_role', $object); } }
权限
use TechExim\Auth\Contracts\Permission\Repository as PermissionRepository; class MyController extends Controller { public function postPermission(PermissionRepository $permissionRepository) { // A new permission $permissionRepository->create('section.action'); // Assign permission to a subject on an object // Both subject and object must implement TechExim\Auth\Contracts\Item $permissionRepository->assignObjectPermissionName($subject, 'section.action', $object); // Check whether a subject has permission on an object $permissionRepository->hasObjectPermissionName($subject, 'section.action', $object); } }
许可证
Laravel授权是开源软件,许可协议为MIT许可证。