定制 kduma/permissions 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kduma/permissions

最新稳定版本:v1.1.0

Composer 安装命令:

composer require kduma/permissions

包简介

Simple (really?) package to provide Roles and Permissions to Laravel 5.1

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License SensioLabsInsight StyleCI

Simple (really?) package to provide Roles and Permissions to Laravel 5.1

Setup

Add the package to the require section of your composer.json and run composer update

"kduma/permissions": "^1.1"

Then add the Service Provider to the providers array in config/app.php:

KDuma\Permissions\PermissionsServiceProvider::class,

Then add the Facades to the aliases array in config/app.php:

'Permissions'        => KDuma\Permissions\Facades\Permissions::class,
'PermissionsManager' => KDuma\Permissions\Facades\PermissionsManager::class,

Then add the Middleware's to the routeMiddleware array in app/Http/Kernel.php:

'permission' => \KDuma\Permissions\Middleware\Permission::class,
'role' => \KDuma\Permissions\Middleware\Role::class,

in your User model (app/User.php) add following line:

use \KDuma\Permissions\Permissions;

Run the following command to copy migrations:

php artisan vendor:publish --provider="KDuma\Permissions\PermissionsServiceProvider" --tag="migrations"

In SampleMigrations folder are examples you can use or make new based on.

  • In 2015_01_01_000005_create_and_assign_roles_and_permissions.php migration file are sample Roles and Permissions
  • In 2015_01_01_000006_create_administrator_account.php migration file is created administrator account

Usage

Protecting controllers and routes

You can use two Middleware's: permission and role

Route::get('admin/profile', [
    'middleware' => 'role:editor',
    'uses' => 'UserController@showProfile'
]);

or

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:user.edit:user.panel');
    }
}

You can provide more than one role/permission in one call by separating them using :. For example: permission:perm1:perm2:perm3. Middleware refuses access if user hasn't any of provided permissions/roles.

Checking permissions in views

You can use Permissions facade:

Permissions::can('permission');

or

Permissions::is('role');

Creating/deleting roles and permissions

To manage roles and permissions you can use PermissionsManager Facaade.
The easiest way to manage permissions is putting them in migrations files.

PermissionsManager Facaade has bunch of usefull methods:

  • Roles
    • PermissionsManager::createRole('ROLE_STRING_ID', 'ROLE_DESCRIPTION');
    • PermissionsManager::deleteRole('ROLE_STRING_ID');
  • Permission
    • PermissionsManager::createPermission('PERMISSION_STRING_ID', 'PERMISSION_DESCRIPTION');
    • PermissionsManager::deletePermission('PERMISSION_STRING_ID');
  • Attaching and Detaching roles and permissions
    • PermissionsManager::attach('ROLES_LIST', 'PERMISSIONS_LIST');
    • PermissionsManager::detach('ROLES_LIST', 'PERMISSIONS_LIST');

Admin role

As default admin role is setted to be root role. Root role always has all permissions. You can change it in configuration file.

Extending

Models

You are free to extend Permission and Role models.
If you extend, set model path in configuration (in ConfigServiceProvider):

config([ 'permissions.models.Role' => '\App\Role' ]);

Sample extended model:

namespace App;

use KDuma\Permissions\Models\Role as BaseRole;

class Role extends BaseRole
{
    public function members()
    {
        return $this->hasMany('\App\Member');
    }
}

Roles and Permissions from associated models

For example if your user is associated to an Member object that have own role, you can import them by placing this sample method in your User model:

protected function fetchAddionalPermissions($roles_list, $permissions_list)
{
    $this->load('member.role.permissions');
    if(!is_null($this->member) && !is_null($this->member->role)){
        $role = $this->member->role;
        $roles_list[$role->str_id] = $role;
        foreach ($role->permissions as $permission) {
            $permissions_list[$permission->str_id] = $permission;
        }
    }
    return [$roles_list, $permissions_list];
}

Packagist

View this package on Packagist.org: kduma/permissions

统计信息

  • 总下载量: 1.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-31