定制 ykozhemiaka/simple-permission 二次开发

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

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

ykozhemiaka/simple-permission

Composer 安装命令:

composer require ykozhemiaka/simple-permission

包简介

Simple permission handling for PHP.

README 文档

README

Simple permission management for PHP.

Getting Started

These instructions will get you quick start for simple permission management in your PHP project.

Installation

  • Require this package in the composer.json
    composer require ykozhemiaka/simple-permission
  • Before using all commands which provide this package you should init Capsule (for example in index.php where boots application)
use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
            'driver' => 'mysql',
            'host' => 'localhost',
            'database' => 'simple_permissions',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8mb4'
        ]);

$capsule->setAsGlobal();
 
$capsule->bootEloquent();
  • After required the package you should create all necessary tables.
    If you've already created users table you couldn't do next instruction, but if you haven't users table in your database you should execute next script to create the table.
use SimplePermission\Database\Schemas\UsersSchema;

$userSchema = new UsersSchema($capsule);
$userSchema->createTable();
  • Next, you need to create several tables that are associated with permission tables.
use SimplePermission\Database\Schemas\PermissionSchemas;

$permissionSchemas = new PermissionSchemas($capsule);
$permissionSchemas->createTable();
  • If table users name isn't users you should set custom name in SimplePermission\Models\User.php
namespace SimplePermission\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasPermission;

    protected $table = 'custom-table-name';

...

Quick start

Management permissions

  • Create new permission "Edit article".
use SimplePermission\Repositories\PermissionRepository;

$permissionRepo = new PermissionRepository();
$permission = $permissionRepo->create('Edit article');
  • Update created permission above.
$id = 1;

$permission = $permissionRepo->update('Edit posts', 1)
  • Delete permission by name
$permissionRepo->deleteByName('Edit article');

Or by id

$permissionRepo->deleteById(2)
  • Assign role to permission
$role = Role::find(1);
//or you can use role id $role = 1;

$permission = $permissionRepo->whereId(1)->assignRole($role);
  • Revoke permission to role
$permissionRepo->whereId(1)->terminateToRole($role);

Management permissions via user

  • Give permission to user.
$user = User::find(1);

$role = Role::find(1);
//or you can use role id $role = 1;
$permission = Permission::find(1);
//or you can use permission id $permission = 1;

$user->allowTo($permission);
  • Assign role to user.
$user->grantRole($role);
  • Verify permission.
$user->ableTo('Edit posts');
  • Verify role.
$user->hasRole('Admin');
  • Revoke permission.
$user->terminateToPermission($permission);
  • Revoke role.
$user->terminateToRole($role);

Management roles

  • Create role.
$roleRepo = new RoleRepository();
$role = $roleRepo->create('Admin');
  • Fetch all permissions attached to role.
$role->permissions;
  • Update role.
$role = $roleRepo->update('Moderator', $role->id);
  • Delete by name
$roleRepo->deleteByName($role->name);

Or delete by id

$roleRepo->deleteById($role->id);
  • Attach permission to role.
$role = $roleRepo->whereId(1)->grant($permission)
  • Assign role to user.
$role = $roleRepo->whereId(1)->assignRoleTo($user)
  • Revoke role to user.
$roleTerminate = $roleRepo->whereId(1)->terminateToUser($user);
  • Revoke permission to user.
$role = $roleRepo->whereId(1)->terminateToPermission($permission);

Built With

Authors

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固