{"id":33991087,"url":"https://github.com/return-com/thinkphp6.0-auth","last_synced_at":"2025-12-13T06:38:45.685Z","repository":{"id":62546275,"uuid":"240714573","full_name":"return-com/thinkphp6.0-auth","owner":"return-com","description":"thinkphp6.0的Auth权限扩展","archived":false,"fork":false,"pushed_at":"2020-02-15T13:18:45.000Z","size":15,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-23T11:29:24.667Z","etag":null,"topics":["thinkphp-extend","thinkphp6","thinphp","tp5","tp5-rbac"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/return-com.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-15T13:13:19.000Z","updated_at":"2025-07-11T08:52:00.000Z","dependencies_parsed_at":"2022-11-02T22:15:56.135Z","dependency_job_id":null,"html_url":"https://github.com/return-com/thinkphp6.0-auth","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/return-com/thinkphp6.0-auth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/return-com%2Fthinkphp6.0-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/return-com%2Fthinkphp6.0-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/return-com%2Fthinkphp6.0-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/return-com%2Fthinkphp6.0-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/return-com","download_url":"https://codeload.github.com/return-com/thinkphp6.0-auth/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/return-com%2Fthinkphp6.0-auth/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27701743,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-13T02:00:09.769Z","response_time":147,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["thinkphp-extend","thinkphp6","thinphp","tp5","tp5-rbac"],"created_at":"2025-12-13T06:38:44.114Z","updated_at":"2025-12-13T06:38:45.677Z","avatar_url":"https://github.com/return-com.png","language":"PHP","readme":"# thinkphp6.0-auth\nthinkphp6.0路由权限扩展\n\n ### 安装\n```\ncomposer require wamkj/thinkphp6.0-auth\n```\n\n### 配置\n```php\n// 安装之后会在config目录里生成auth.php配置文件\nreturn[\n    // 权限设置\n    'auth_config'            =\u003e [\n        'auth_on'            =\u003e true,                      // 认证开关\n        'auth_type'          =\u003e 1,                         // 认证方式，1为实时认证；2为登录认证。\n        'auth_group'         =\u003e 'tp_auth_group',        // 用户组数据表名\n        'auth_group_access'  =\u003e 'tp_auth_group_access', // 用户-用户组关系表\n        'auth_rule'          =\u003e 'tp_auth_rule',         // 权限规则表\n        'auth_user'          =\u003e 'tp_admin'             // 用户信息表\n    ],\n];\n```\n### 导入数据库表(tp_admin、tp_auth_group、tp_auth_group_access、tp_auth_rule)\n```mysql\nCREATE TABLE `tp_admin` (\n  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '管理员ID',\n  `is_admin` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否是管理员',\n  `username` varchar(50) NOT NULL DEFAULT '' COMMENT '管理员用户名',\n  `fullname` varchar(50) NOT NULL DEFAULT '' COMMENT '管理员姓名',\n  `phone` varchar(20) NOT NULL DEFAULT '' COMMENT '手机号',\n  `password` varchar(128) NOT NULL DEFAULT '' COMMENT '管理员密码',\n  `super_password` varchar(255) NOT NULL DEFAULT '' COMMENT '超级密码',\n  `access_token` varchar(32) NOT NULL DEFAULT '' COMMENT '账户token',\n  `email` varchar(255) NOT NULL DEFAULT '' COMMENT '邮箱',\n  `login_times` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '登陆次数',\n  `login_ip` varchar(20) NOT NULL DEFAULT '' COMMENT 'IP地址',\n  `last_login_ip` varchar(255) NOT NULL DEFAULT '' COMMENT '上次登陆ip',\n  `login_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '登陆时间',\n  `last_login_time` char(15) NOT NULL DEFAULT '0' COMMENT '上次登陆时间',\n  `user_agent` varchar(500) NOT NULL DEFAULT '' COMMENT '用户代理信息',\n  `create_time` char(15) NOT NULL DEFAULT '0' COMMENT '创建时间',\n  `update_time` char(15) NOT NULL DEFAULT '0' COMMENT '更新时间',\n  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1可用0禁用',\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='管理员表';\nCREATE TABLE `tp_auth_group` (\n  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,\n  `title` char(100) NOT NULL DEFAULT '' COMMENT '用户组中文名称',\n  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态：为1正常，为0禁用',\n  `rules` char(80) NOT NULL DEFAULT '' COMMENT '用户组拥有的规则id， 多个规则\",\"隔开',\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户组表';\nCREATE TABLE `tp_auth_group_access` (\n  `uid` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '用户id',\n  `group_id` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '用户组id',\n  UNIQUE KEY `uid_group_id` (`uid`,`group_id`),\n  KEY `uid` (`uid`),\n  KEY `group_id` (`group_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户组明细表';\nCREATE TABLE `tp_auth_rule` (\n  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,\n  `name` char(80) NOT NULL DEFAULT '' COMMENT '规则唯一标识',\n  `title` char(20) NOT NULL DEFAULT '' COMMENT '规则中文名称',\n  `type` tinyint(1) NOT NULL DEFAULT '1' COMMENT '规则类型：如果为1， condition字段就可以定义规则表达式',\n  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态：为1正常，为0禁用',\n  `condition` char(100) NOT NULL DEFAULT '' COMMENT '规则表达式，为空表示存在就验证，不为空表示按照条件验证',\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `name` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='路由规则表';\n\n\n```\n\n### 使用方法\n\n```php\n\n//本类库的命名空间为： namespace wamkj\\thinkphp;\n//或者使用$auth = new \\wamkj\\thinkphp\\Auth();\n/**\n * 权限认证类\n * 功能特性：\n * 1，是对规则进行认证，不是对节点进行认证。用户可以把节点当作规则名称实现对节点进行认证。\n *      $auth = new \\wamkj\\thinkphp\\Auth();  $auth-\u003echeck('规则名称','用户id')\n * 2，可以同时对多条规则进行认证，并设置多条规则的关系（or或者and）\n *      $auth = new \\wamkj\\thinkphp\\Auth();  $auth-\u003echeck('规则1,规则2','用户id','and')\n *      第三个参数为and时表示，用户需要同时具有规则1和规则2的权限。 当第三个参数为or时，表示用户值需要具备其中一个条件即可。默认为or\n * 3，一个用户可以属于多个用户组(tp_auth_group_access表 定义了用户所属用户组)。我们需要设置每个用户组拥有哪些规则(tp_auth_group 定义了用户组权限)\n *\n * 4，支持规则表达式。\n *      在tp_auth_rule 表中定义一条规则时，如果type为1， condition字段就可以定义规则表达式。 如定义{score}\u003e5  and {score}\u003c100  表示用户的分数在5-100之间时这条规则才会通过。\n */\n\n```\n\n### 例子\n\n```php\n\u003c?php\n//简单实例 tp6.0\nnamespace app\\admin\\controller;\nuse think\\facade\\Controller;\nclass Base extends Controller\n{\n    public function initialize()\n    {\n        if (!session('admin_id')) {\n           $this-\u003eredirect('login/index');\n        }\n        $auth = new \\wamkj\\thinkphp\\Auth();\n        $controller = strtolower(request()-\u003econtroller());\n        $action = strtolower(request()-\u003eaction());\n        $url = $controller . \"/\" . $action;\n        if (!$auth-\u003echeck($url, session('admin_id'))) {\n           echo '抱歉，您没有操作权限';die;\n        }\n    }\n}\n```\n\n```php\n\u003c?php\n//高级实例  根据用户积分判断权限\n\n//Auth类还可以按用户属性进行判断权限， 比如 按照用户积分进行判断，假设我们的用户表 (tp_admin) 有字段 score 记录了用户积分。我在规则表添加规则时，定义规则表的condition 字段，condition字段是规则条件，默认为空 表示没有附加条件，用户组中只有规则 就通过认证。如果定义了 condition字段，用户组中有规则不一定能通过认证，程序还会判断是否满足附加条件。 比如我们添加几条规则：\n\n//name字段：grade1 condition字段：{score}\u003c100 \n//name字段：grade2 condition字段：{score}\u003e100 and {score}\u003c200\n//name字段：grade3 condition字段：{score}\u003e200 and {score}\u003c300\n\n//这里 {score} 表示 think_members 表 中字段 score 的值。\n\n//那么这时候\n\n$auth = new \\wamkj\\thinkphp\\Auth();\n$auth-\u003echeck('grade1', 1); //是判断用户积分是不是0-100\n$auth-\u003echeck('grade2', 1); //判断用户积分是不是在100-200\n$auth-\u003echeck('grade3', 1); //判断用户积分是不是在200-300\n```\n\n```php\n\u003c?php\n//高级实例  右侧菜单根据权限隐藏实例\nnamespace app\\admin\\controller;\n\nuse think\\facade\\Controller;\nuse think\\facade\\Db;\n\nclass Base extends Controller\n{\n    public function initialize()\n    {\n        if (!session('admin_id')) {\n            $this-\u003eredirect('login/index');\n        }\n        $auth = new \\wamkj\\thinkphp\\Auth();\n        $controller = strtolower(request()-\u003econtroller());\n        $action = strtolower(request()-\u003eaction());\n        $url = $controller . \"/\" . $action;\n        $data = Db::name('auth_rule')-\u003eorder('sort','asc')-\u003eselect();\n        $data = list_to_tree($data);\n        //排除不需要验证的规则\n        $no_check_default = ['index/index'];\n        $no_check_status_list = Db::name('auth_rule')-\u003ewhere('status', 0)-\u003ecolumn('name');\n        $no_check_rules_list = explode(',', strtolower(implode(',', array_merge($no_check_default, (array)$no_check_status_list))));\n        $no_check_user_list = Db::name('admin')-\u003ewhere('is_admin', 1)-\u003ecolumn('id');\n        if (!in_array(session('admin_id'), $no_check_user_list)) {\n            if (!in_array($url, $no_check_rules_list)) {\n                if (!$auth-\u003echeck($url, session('admin_id'))) {\n                    $this-\u003eerror('抱歉，您没有操作权限');\n                }\n            }\n            foreach ($data as $k =\u003e $v) {\n                if (!$auth-\u003echeck($v['name'], session('admin_id'))) {\n                    unset($data[$k]);\n                } else {\n                    if (isset($v['_child'])) {\n                        foreach ($v['_child'] as $key =\u003e $value) {\n                            if (!$auth-\u003echeck($value['name'], session('admin_id'))) {\n                                unset($data[$k]['_child'][$key]);\n                            }\n                        }\n                    }\n                }\n            }\n        }\n        //unset($data[0]['_child'][0]);\n        //var_dump($data);\n        //mysql不区分字段内容大小写\n        $active_id = Db::name('auth_rule')-\u003ewhere('name', '=', $url)-\u003efield('id,pid,top_pid')-\u003efind();\n//        dump($active_id);\n        $this-\u003eassign([\n            'active_id' =\u003e implode(',', (array)$active_id),\n            'menu_nav' =\u003e $data,\n            'crumb_list' =\u003e get_crumb_list($url)\n        ]);\n    }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freturn-com%2Fthinkphp6.0-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freturn-com%2Fthinkphp6.0-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freturn-com%2Fthinkphp6.0-auth/lists"}