0 Star 3 Fork 0

梦中程序员 / auth

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

tp-auth

ThinkPHP5.1权限认证类

安装

composer require leruge/auth

全局配置

// 会自动在config配置目录创建auth.php配置文件,如果没有,请手动创建
// 配置参数如下

return [
    'auth_on'           =>  true,                // 认证开关
    'auth_type'         =>  1,                   // 认证方式,1为实时认证;2为登录认证。
    'auth_group'        =>  'auth_group',        // 用户组数据表名
    'auth_group_access' =>  'auth_group_access', // 用户-用户组关系表
    'auth_rule'         =>  'auth_rule',         // 权限规则表
    'super_ids'         => [],                   // 拥有所有权限的用户,如[1, 2, 3],那么这三个用户则拥有所有权限
];

导入数据库

wdl_为自定义表前缀

-- ----------------------------
-- Table structure for wdl_auth_group
-- ----------------------------
DROP TABLE IF EXISTS `wdl_auth_group`;
CREATE TABLE `wdl_auth_group` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '分组名称',
  `rules` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '拥有的权限,使用逗号分隔',
  `create_time` int(11) NOT NULL DEFAULT '0',
  `update_time` int(11) NOT NULL DEFAULT '0',
  `delete_time` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ----------------------------
-- Table structure for wdl_auth_group_access
-- ----------------------------
DROP TABLE IF EXISTS `wdl_auth_group_access`;
CREATE TABLE `wdl_auth_group_access` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` int(11) DEFAULT NULL COMMENT '用户ID',
  `group_id` int(11) DEFAULT NULL COMMENT '分组ID',
  `create_time` int(11) NOT NULL DEFAULT '0',
  `update_time` int(11) NOT NULL DEFAULT '0',
  `delete_time` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ----------------------------
-- Table structure for wdl_auth_rule
-- ----------------------------
DROP TABLE IF EXISTS `wdl_auth_rule`;
CREATE TABLE `wdl_auth_rule` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `pid` int(11) DEFAULT '0' COMMENT '上级ID',
  `name` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '权限规则',
  `title` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '规则中文名',
  `icon` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '菜单图标',
  `sort` int(11) DEFAULT '9' COMMENT '排序',
  `is_show` int(11) DEFAULT '1' COMMENT '1显示;0隐藏',
  `create_time` int(11) NOT NULL DEFAULT '0',
  `update_time` int(11) NOT NULL DEFAULT '0',
  `delete_time` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

原理

auth类的原理网上介绍的比较多,这里就不在做过多的介绍了。对外提供了一个check、authList、userRuleIdList方法,用于校验权限。

使用方法

有俩种使用方法

  1. 使用权限类Auth
    1. 引入类库use leruge\auth\Auth;
    2. 获取auth实例,$auth = new Auth();
    3. 校验权限
    if ($auth->check(模块/控制器/方法, 用户ID)){
    //有操作权限
    } else {
    //没有操作权限
    }
  2. 使用权限类门面Auth
    1. 引入类库use leruge\auth\AuthFacade;
    2. 校验权限
    if (AuthFacade::check(模块/控制器/方法, 用户ID)){
        //有操作权限
      } else {
        //没有操作权限
      }

check方法参数

check($name, $uid, $relation)
$name 验证的权限,字符串是单条权限,多条权限使用数组
$uid 验证用户ID
$relation or满足一条则通过;and全部满足才能通过

authList方法

authList($uid)
返回权限列表,不区分上下级

userRuleIdList方法

userRuleIdList($uid)
返回权限ID数组
MIT License Copyright (c) 2020 Leruge Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 1. welcome The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

thinkphp5.1权限认证类。 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/sjclub/auth.git
git@gitee.com:sjclub/auth.git
sjclub
auth
auth
master

搜索帮助