PageRenderTime 81ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/ThinkPHP/Extend/Library/ORG/Util/RBAC.class.php

https://gitlab.com/llwhois/woaimm
PHP | 292 lines | 160 code | 8 blank | 124 comment | 30 complexity | 2bda988faf8b7f0bb00817879ab454a8 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id: RBAC.class.php 2947 2012-05-13 15:57:48Z liu21st@gmail.com $
  12. /**
  13. +------------------------------------------------------------------------------
  14. * 基于角色的数据库方式验证类
  15. +------------------------------------------------------------------------------
  16. * @category ORG
  17. * @package ORG
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id: RBAC.class.php 2947 2012-05-13 15:57:48Z liu21st@gmail.com $
  21. +------------------------------------------------------------------------------
  22. */
  23. // 配置文件增加设置
  24. // USER_AUTH_ON 是否需要认证
  25. // USER_AUTH_TYPE 认证类型
  26. // USER_AUTH_KEY 认证识别号
  27. // REQUIRE_AUTH_MODULE 需要认证模块
  28. // NOT_AUTH_MODULE 无需认证模块
  29. // USER_AUTH_GATEWAY 认证网关
  30. // RBAC_DB_DSN 数据库连接DSN
  31. // RBAC_ROLE_TABLE 角色表名称
  32. // RBAC_USER_TABLE 用户表名称
  33. // RBAC_ACCESS_TABLE 权限表名称
  34. // RBAC_NODE_TABLE 节点表名称
  35. /*
  36. -- --------------------------------------------------------
  37. CREATE TABLE IF NOT EXISTS `think_access` (
  38. `role_id` smallint(6) unsigned NOT NULL,
  39. `node_id` smallint(6) unsigned NOT NULL,
  40. `level` tinyint(1) NOT NULL,
  41. `module` varchar(50) DEFAULT NULL,
  42. KEY `groupId` (`role_id`),
  43. KEY `nodeId` (`node_id`)
  44. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  45. CREATE TABLE IF NOT EXISTS `think_node` (
  46. `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  47. `name` varchar(20) NOT NULL,
  48. `title` varchar(50) DEFAULT NULL,
  49. `status` tinyint(1) DEFAULT '0',
  50. `remark` varchar(255) DEFAULT NULL,
  51. `sort` smallint(6) unsigned DEFAULT NULL,
  52. `pid` smallint(6) unsigned NOT NULL,
  53. `level` tinyint(1) unsigned NOT NULL,
  54. PRIMARY KEY (`id`),
  55. KEY `level` (`level`),
  56. KEY `pid` (`pid`),
  57. KEY `status` (`status`),
  58. KEY `name` (`name`)
  59. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  60. CREATE TABLE IF NOT EXISTS `think_role` (
  61. `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  62. `name` varchar(20) NOT NULL,
  63. `pid` smallint(6) DEFAULT NULL,
  64. `status` tinyint(1) unsigned DEFAULT NULL,
  65. `remark` varchar(255) DEFAULT NULL,
  66. PRIMARY KEY (`id`),
  67. KEY `pid` (`pid`),
  68. KEY `status` (`status`)
  69. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
  70. CREATE TABLE IF NOT EXISTS `think_role_user` (
  71. `role_id` mediumint(9) unsigned DEFAULT NULL,
  72. `user_id` char(32) DEFAULT NULL,
  73. KEY `group_id` (`role_id`),
  74. KEY `user_id` (`user_id`)
  75. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  76. */
  77. class RBAC {
  78. // 认证方法
  79. static public function authenticate($map,$model='') {
  80. if(empty($model)) $model = C('USER_AUTH_MODEL');
  81. //使用给定的Map进行认证
  82. return M($model)->where($map)->find();
  83. }
  84. //用于检测用户权限的方法,并保存到Session中
  85. static function saveAccessList($authId=null) {
  86. if(null===$authId) $authId = $_SESSION[C('USER_AUTH_KEY')];
  87. // 如果使用普通权限模式,保存当前用户的访问权限列表
  88. // 对管理员开发所有权限
  89. if(C('USER_AUTH_TYPE') !=2 && !$_SESSION[C('ADMIN_AUTH_KEY')] )
  90. $_SESSION['_ACCESS_LIST'] = RBAC::getAccessList($authId);
  91. return ;
  92. }
  93. // 取得模块的所属记录访问权限列表 返回有权限的记录ID数组
  94. static function getRecordAccessList($authId=null,$module='') {
  95. if(null===$authId) $authId = $_SESSION[C('USER_AUTH_KEY')];
  96. if(empty($module)) $module = MODULE_NAME;
  97. //获取权限访问列表
  98. $accessList = RBAC::getModuleAccessList($authId,$module);
  99. return $accessList;
  100. }
  101. //检查当前操作是否需要认证
  102. static function checkAccess() {
  103. //如果项目要求认证,并且当前模块需要认证,则进行权限认证
  104. if( C('USER_AUTH_ON') ){
  105. $_module = array();
  106. $_action = array();
  107. if("" != C('REQUIRE_AUTH_MODULE')) {
  108. //需要认证的模块
  109. $_module['yes'] = explode(',',strtoupper(C('REQUIRE_AUTH_MODULE')));
  110. }else {
  111. //无需认证的模块
  112. $_module['no'] = explode(',',strtoupper(C('NOT_AUTH_MODULE')));
  113. }
  114. //检查当前模块是否需要认证
  115. if((!empty($_module['no']) && !in_array(strtoupper(MODULE_NAME),$_module['no'])) || (!empty($_module['yes']) && in_array(strtoupper(MODULE_NAME),$_module['yes']))) {
  116. if("" != C('REQUIRE_AUTH_ACTION')) {
  117. //需要认证的操作
  118. $_action['yes'] = explode(',',strtoupper(C('REQUIRE_AUTH_ACTION')));
  119. }else {
  120. //无需认证的操作
  121. $_action['no'] = explode(',',strtoupper(C('NOT_AUTH_ACTION')));
  122. }
  123. //检查当前操作是否需要认证
  124. if((!empty($_action['no']) && !in_array(strtoupper(ACTION_NAME),$_action['no'])) || (!empty($_action['yes']) && in_array(strtoupper(ACTION_NAME),$_action['yes']))) {
  125. return true;
  126. }else {
  127. return false;
  128. }
  129. }else {
  130. return false;
  131. }
  132. }
  133. return false;
  134. }
  135. // 登录检查
  136. static public function checkLogin() {
  137. //检查当前操作是否需要认证
  138. if(RBAC::checkAccess()) {
  139. //检查认证识别号
  140. if(!$_SESSION[C('USER_AUTH_KEY')]) {
  141. if(C('GUEST_AUTH_ON')) {
  142. // 开启游客授权访问
  143. if(!isset($_SESSION['_ACCESS_LIST']))
  144. // 保存游客权限
  145. RBAC::saveAccessList(C('GUEST_AUTH_ID'));
  146. }else{
  147. // 禁止游客访问跳转到认证网关
  148. redirect(PHP_FILE.C('USER_AUTH_GATEWAY'));
  149. }
  150. }
  151. }
  152. return true;
  153. }
  154. //权限认证的过滤器方法
  155. static public function AccessDecision($appName=APP_NAME) {
  156. //检查是否需要认证
  157. if(RBAC::checkAccess()) {
  158. //存在认证识别号,则进行进一步的访问决策
  159. $accessGuid = md5($appName.MODULE_NAME.ACTION_NAME);
  160. if(empty($_SESSION[C('ADMIN_AUTH_KEY')])) {
  161. if(C('USER_AUTH_TYPE')==2) {
  162. //加强验证和即时验证模式 更加安全 后台权限修改可以即时生效
  163. //通过数据库进行访问检查
  164. $accessList = RBAC::getAccessList($_SESSION[C('USER_AUTH_KEY')]);
  165. }else {
  166. // 如果是管理员或者当前操作已经认证过,无需再次认证
  167. if( $_SESSION[$accessGuid]) {
  168. return true;
  169. }
  170. //登录验证模式,比较登录后保存的权限访问列表
  171. $accessList = $_SESSION['_ACCESS_LIST'];
  172. }
  173. //判断是否为组件化模式,如果是,验证其全模块名
  174. $module = defined('P_MODULE_NAME')? P_MODULE_NAME : MODULE_NAME;
  175. if(!isset($accessList[strtoupper($appName)][strtoupper($module)][strtoupper(ACTION_NAME)])) {
  176. $_SESSION[$accessGuid] = false;
  177. return false;
  178. }
  179. else {
  180. $_SESSION[$accessGuid] = true;
  181. }
  182. }else{
  183. //管理员无需认证
  184. return true;
  185. }
  186. }
  187. return true;
  188. }
  189. /**
  190. +----------------------------------------------------------
  191. * 取得当前认证号的所有权限列表
  192. +----------------------------------------------------------
  193. * @param integer $authId 用户ID
  194. +----------------------------------------------------------
  195. * @access public
  196. +----------------------------------------------------------
  197. */
  198. static public function getAccessList($authId) {
  199. // Db方式权限数据
  200. $db = Db::getInstance(C('RBAC_DB_DSN'));
  201. $table = array('role'=>C('RBAC_ROLE_TABLE'),'user'=>C('RBAC_USER_TABLE'),'access'=>C('RBAC_ACCESS_TABLE'),'node'=>C('RBAC_NODE_TABLE'));
  202. $sql = "select node.id,node.name from ".
  203. $table['role']." as role,".
  204. $table['user']." as user,".
  205. $table['access']." as access ,".
  206. $table['node']." as node ".
  207. "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=1 and node.status=1";
  208. $apps = $db->query($sql);
  209. $access = array();
  210. foreach($apps as $key=>$app) {
  211. $appId = $app['id'];
  212. $appName = $app['name'];
  213. // 读取项目的模块权限
  214. $access[strtoupper($appName)] = array();
  215. $sql = "select node.id,node.name from ".
  216. $table['role']." as role,".
  217. $table['user']." as user,".
  218. $table['access']." as access ,".
  219. $table['node']." as node ".
  220. "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=2 and node.pid={$appId} and node.status=1";
  221. $modules = $db->query($sql);
  222. // 判断是否存在公共模块的权限
  223. $publicAction = array();
  224. foreach($modules as $key=>$module) {
  225. $moduleId = $module['id'];
  226. $moduleName = $module['name'];
  227. if('PUBLIC'== strtoupper($moduleName)) {
  228. $sql = "select node.id,node.name from ".
  229. $table['role']." as role,".
  230. $table['user']." as user,".
  231. $table['access']." as access ,".
  232. $table['node']." as node ".
  233. "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=3 and node.pid={$moduleId} and node.status=1";
  234. $rs = $db->query($sql);
  235. foreach ($rs as $a){
  236. $publicAction[$a['name']] = $a['id'];
  237. }
  238. unset($modules[$key]);
  239. break;
  240. }
  241. }
  242. // 依次读取模块的操作权限
  243. foreach($modules as $key=>$module) {
  244. $moduleId = $module['id'];
  245. $moduleName = $module['name'];
  246. $sql = "select node.id,node.name from ".
  247. $table['role']." as role,".
  248. $table['user']." as user,".
  249. $table['access']." as access ,".
  250. $table['node']." as node ".
  251. "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.node_id=node.id and node.level=3 and node.pid={$moduleId} and node.status=1";
  252. $rs = $db->query($sql);
  253. $action = array();
  254. foreach ($rs as $a){
  255. $action[$a['name']] = $a['id'];
  256. }
  257. // 和公共模块的操作权限合并
  258. $action += $publicAction;
  259. $access[strtoupper($appName)][strtoupper($moduleName)] = array_change_key_case($action,CASE_UPPER);
  260. }
  261. }
  262. return $access;
  263. }
  264. // 读取模块所属的记录访问权限
  265. static public function getModuleAccessList($authId,$module) {
  266. // Db方式
  267. $db = Db::getInstance(C('RBAC_DB_DSN'));
  268. $table = array('role'=>C('RBAC_ROLE_TABLE'),'user'=>C('RBAC_USER_TABLE'),'access'=>C('RBAC_ACCESS_TABLE'));
  269. $sql = "select access.node_id from ".
  270. $table['role']." as role,".
  271. $table['user']." as user,".
  272. $table['access']." as access ".
  273. "where user.user_id='{$authId}' and user.role_id=role.id and ( access.role_id=role.id or (access.role_id=role.pid and role.pid!=0 ) ) and role.status=1 and access.module='{$module}' and access.status=1";
  274. $rs = $db->query($sql);
  275. $access = array();
  276. foreach ($rs as $node){
  277. $access[] = $node['node_id'];
  278. }
  279. return $access;
  280. }
  281. }