All Projects → friendsofthinkphp → think-permission

friendsofthinkphp / think-permission

Licence: other
ThinkPHP 6 权限认证

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to think-permission

jwt-auth
JSON Web Token Authentication for Thinkphp
Stars: ✭ 113 (+253.13%)
Mutual labels:  thinkphp, thinkphp5
think-redisd
thinkphp 5 redis读写分离驱动
Stars: ✭ 24 (-25%)
Mutual labels:  thinkphp, thinkphp5
think-whoops
Whoops 接管 ThinkPHP6 异常服务
Stars: ✭ 37 (+15.63%)
Mutual labels:  thinkphp, thinkphp-component
Nonecms
基于thinkphp5.1 的内容管理系统,可快速搭建博客、企业站;并且增加了实时聊天室
Stars: ✭ 261 (+715.63%)
Mutual labels:  thinkphp, thinkphp5
lake-admin
lake-admin是一款基于ThinkPHP6和Layui的后台开发框架。
Stars: ✭ 28 (-12.5%)
Mutual labels:  thinkphp, thinkphp5
Thinkadmin
基于 ThinkPHP 基础开发平台(登录账号密码都是 admin )
Stars: ✭ 1,938 (+5956.25%)
Mutual labels:  thinkphp, thinkphp5
say-love-wall
💖 由ThinkPHP5框架开发即开箱可用的告白墙、校园表白墙。表白可以通过发送邮箱告知对方,也可以分享表白内容。更多趣味查看 README.md 或网站。
Stars: ✭ 32 (+0%)
Mutual labels:  thinkphp, thinkphp5
Uniadmin
UniAdmin是一套渐进式模块化开源后台,采用前后端分离技术,数据交互采用json格式,功能低耦合高内聚;核心模块支持系统设置、权限管理、用户管理、菜单管理、API管理等功能,后期上线模块商城将打造类似composer、npm的开放式插件市场;同时我们将打造一套兼容性的API标准,从ThinkPHP5.1+Vue2开始,逐步吸引爱好者共同加入,以覆盖larval、spring-boot、django、yii、koa、react等多语言框架。
Stars: ✭ 277 (+765.63%)
Mutual labels:  thinkphp, thinkphp5
Wemall
wemall7 开源版本 (不含商城)
Stars: ✭ 315 (+884.38%)
Mutual labels:  thinkphp, thinkphp5
Lyadmin
lyadmin是一套轻量级通用后台,采用ThinkPHP+Bootstrap3制作,内置系统设置、上传管理、权限管理、模块管理、插件管理等功能,独有的Builder页面自动生成技术节省50%开发成本,先进的模块化开发的支持让开发成本一降再降,致力于为个人和中小型企业打造全方位的PHP企业级开发解决方案。另外提供整套企业开发解决方案,集PC、手机、微信、App、小程序五端于一体,更有用户中心模块、门户模块、钱包支付中心模块、商城模块、OAuth2统一登陆、内部Git模块、Docker模块可供选择。
Stars: ✭ 1,066 (+3231.25%)
Mutual labels:  thinkphp, thinkphp5
think-jwt
ThinkPHP Jwt 扩展包
Stars: ✭ 71 (+121.88%)
Mutual labels:  thinkphp, thinkphp-component
A3Mall
A3Mall B2C开源商城系统使用Thinkphp6开源框架,前端采用uniapp开发,支持微信公众号商城、H5商城、小程序商城、APP商城、PC商城,前后端源码100%开源,支持免费商用。
Stars: ✭ 142 (+343.75%)
Mutual labels:  thinkphp
Think Awesome
awesome for thinkphp
Stars: ✭ 209 (+553.13%)
Mutual labels:  thinkphp
Framework
ThinkPHP Framework
Stars: ✭ 2,399 (+7396.88%)
Mutual labels:  thinkphp
Search Engine Rank
🐘根据网站关键词,获取网站在各大搜索引擎(百度、360、搜狗)的排名情况,有利于网站seo
Stars: ✭ 197 (+515.63%)
Mutual labels:  thinkphp
SuperPanel
A new shadowsocks panel based on ThinkPHP
Stars: ✭ 52 (+62.5%)
Mutual labels:  thinkphp
dawn-api
A RESTful API package
Stars: ✭ 25 (-21.87%)
Mutual labels:  thinkphp5
Searchku
searchku 信息查询软件,没有数据。
Stars: ✭ 199 (+521.88%)
Mutual labels:  thinkphp
MessageBoard
🍻thinkphp5 learning:a messageboard project
Stars: ✭ 18 (-43.75%)
Mutual labels:  thinkphp5
Ruhua
如花商城系统thinkphp6+uniapp,小程序直播拼团限时分销APP商城
Stars: ✭ 181 (+465.63%)
Mutual labels:  thinkphp

ThinkPHP Permission

ThinkPHP6 权限包

Latest Stable Version Total Downloads Latest Unstable Version LICENSE

安装

composer require xiaodi/think-permission

使用

创建必要数据

规则

use xiaodi\Permission\Model\Permission;
// 创建一条可查看首页的权限 
Permission::create(['name' => 'home']);

角色

use xiaodi\Permission\Model\Role;
// 创建�一个名为编辑的角色
Role::create(['name' => 'writer']);

用户

use xiaodi\Permission\Model\User;
// 创建�一个名为xiaodi的用户
User::create(['name' => 'xiaodi']);

分配关系

规则与角色

use xiaodi\Permission\Model\Permission;
use xiaodi\Permission\Model\Role;
// 将home规则分配到writer角色 
$permission = Permission::findByName('home');
$role = Permission::findByName('writer');
$permission->assignRole($role);

// 将home规则分配到writer角色 (跟上面效果一样)
$permission = Permission::findByName('home');
$role = Permission::findByName('writer');
$role->assignPermission($permission);

用户与角色

use xiaodi\Permission\Model\User;
use xiaodi\Permission\Model\Role;

// 为用户xiaodi分配 writer角色 
$user = User::findByName('xiaodi');
$role = Permission::findByName('writer');
$user->assignRole($role);

// 为用户xiaodi分配 writer角色 (跟上面效果一样)
$user = User::findByName('xiaodi');
$role = Permission::findByName('writer');
$role->assignUser($user);

解除关系

解除规则与角色

use xiaodi\Permission\Model\Permission;
use xiaodi\Permission\Model\Role;

// home规则与writer角色 解除关系
$permission = Permission::findByName('home');
$role = Permission::findByName('writer');
$permission->removeRole($role);

// writer角色与home规则 解除关系(跟上面效果一样)
$permission = Permission::findByName('home');
$role = Permission::findByName('writer');
$role->removePermission($permission);

解除用户与角色

use xiaodi\Permission\Model\User;
use xiaodi\Permission\Model\Role;

// 用户xiaodi与writer角色 解除关系
$user = User::findByName('xiaodi');
$role = Permission::findByName('writer');
$user->removeRole($role);

// writer角色与用户xiaodi 解除关系 (跟上面效果一样)
$user = User::findByName('xiaodi');
$role = Permission::findByName('writer');
$role->removeUser($user);

权限判断

手动

use xiaodi\Permission\Model\User;

$user = User::findByName('xiaodi');
if ($user->can('home')) {
    // 有 `home`权限
} else {
    // 无 `home`权限
}

路由守护

用户信息,请手动注入到$request->user上,并且使用 \xiaodi\Permission\Contract\UserContract 接口。 Demo /home 路由添加一条权限控制 访问者有 home权限才能允许访问

Route::post('/home', 'home/index')->middleware('auth', 'home');

中间件注入用户

用户模型

<?php

namespace app\model;

use think\Request;
use xiaodi\Permission\Contract\UserContract;

class User implements UserContract
{
    use \xiaodi\Permission\Traits\User;
}

注入用户信息

<?php

namespace app\middleware;

use think\Request;
use app\model\User;

class Auth
{
    public function handle($request, \Closure $next)
    {
        $uid = 1;
        $user = User::find($uid);

        $request->user = $user;
        return $next($request);
    }
}

数据表

  • permission
CREATE TABLE `permission` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL COMMENT '规则唯一标识',
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;
  • role
CREATE TABLE `role` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL COMMENT '角色唯一标识',
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
  • role-permission-access
CREATE TABLE `role_permission_access` (
  `role_id` int(11) NOT NULL COMMENT '角色主键',
  `permission_id` int(11) NOT NULL COMMENT '规则主键',
  PRIMARY KEY (`role_id`,`permission_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • user
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL COMMENT '用户唯一标识',
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
  • user_role_access
CREATE TABLE `user_role_access` (
  `user_id` int(11) NOT NULL COMMENT '用户主键',
  `role_id` int(11) NOT NULL COMMENT '角色主键',
  PRIMARY KEY (`user_id`,`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

中间件

默认自带以下中间件

  • 规则中间件
  • 角色中间件

规则中间件

#route/app.php

# 拥有 edit 规则的用户 可以访问此路由
Route::rule('/testPermission', function(){
  return 'edit';
}, 'GET')->allowCrossDomain()->middleware('auth.permission', 'edit');

角色中间件

#route/app.php

# 拥有 editer 角色的用户 可以访问此路由
Route::rule('/testRole', function(){
  return 'editer';
}, 'GET')->allowCrossDomain()->middleware('auth.role', 'editer');
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].