6
6
// +----------------------------------------------------------------------
7
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
8
// +----------------------------------------------------------------------
9
- // | Author: luofei614 <www.3g4k. com>
9
+ // | Author: luofei614 <weibo. com/luofei614 >
10
10
// +----------------------------------------------------------------------
11
- // $Id: Authority.class.php 2504 2011-12-28 07:35:29Z liu21st $
12
11
/**
13
12
* 权限认证类
14
13
* 功能特性:
15
14
* 1,是对规则进行认证,不是对节点进行认证。用户可以把节点当作规则名称实现对节点进行认证。
16
- * $auth=new Authority (); $auth->getAuth ('规则名称','用户id')
15
+ * $auth=new Auth (); $auth->check ('规则名称','用户id')
17
16
* 2,可以同时对多条规则进行认证,并设置多条规则的关系(or或者and)
18
- * $auth=new Authority (); $auth->getAuth ('规则1,规则2','用户id','and')
17
+ * $auth=new Auth (); $auth->check ('规则1,规则2','用户id','and')
19
18
* 第三个参数为and时表示,用户需要同时具有规则1和规则2的权限。 当第三个参数为or时,表示用户值需要具备其中一个条件即可。默认为or
20
19
* 3,一个用户可以属于多个用户组(think_auth_group_access表 定义了用户所属用户组)。我们需要设置每个用户组拥有哪些规则(think_auth_group 定义了用户组权限)
21
20
*
24
23
* @category ORG
25
24
* @package ORG
26
25
* @subpackage Util
27
- * @author luofei614<www.3g4k. com>
26
+ * @author luofei614<weibo. com/luofei614 >
28
27
*/
29
28
30
29
//数据库
31
30
/*
32
31
-- ----------------------------
33
32
-- think_auth_rule,规则表,
34
- -- id:主键,name:规则唯一标识, title:规则中文名称 type:类型(0存在规则就通过,1按规则表达时进行认证) ,condition:规则表达式
33
+ -- id:主键,name:规则唯一标识, title:规则中文名称 status 状态:为1正常,为0禁用 ,condition:规则表达式,为空表示存在就验证,不为空表示按照条件验证
35
34
-- ----------------------------
36
35
DROP TABLE IF EXISTS `think_auth_rule`;
37
- CREATE TABLE `think_auth_rule` (
38
- `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
39
- `name` char(10) NOT NULL DEFAULT '',
40
- `title` char(20) NOT NULL DEFAULT '',
41
- `type` tinyint(1) NOT NULL DEFAULT '0',
42
- `condition` char(100) NOT NULL DEFAULT '',
43
- PRIMARY KEY (`id`)
44
- ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
36
+ CREATE TABLE `think_auth_rule` (
37
+ `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
38
+ `name` char(80) NOT NULL DEFAULT '',
39
+ `title` char(20) NOT NULL DEFAULT '',
40
+ `status` tinyint(1) NOT NULL DEFAULT '1',
41
+ `condition` char(100) NOT NULL DEFAULT '',
42
+ PRIMARY KEY (`id`),
43
+ UNIQUE KEY `name` (`name`)
44
+ ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
45
45
-- ----------------------------
46
46
-- think_auth_group 用户组表,
47
- -- id:主键, title:用户组中文名称, rules:用户组拥有的规则id, 多个规则用“,”隔开
47
+ -- id:主键, title:用户组中文名称, rules:用户组拥有的规则id, 多个规则","隔开,status 状态:为1正常,为0禁用
48
48
-- ----------------------------
49
49
DROP TABLE IF EXISTS `think_auth_group`;
50
- CREATE TABLE `think_auth_group` (
51
- `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
52
- `title` char(100) NOT NULL DEFAULT '',
53
- `rules` char(80) NOT NULL DEFAULT '',
54
- PRIMARY KEY (`id`)
55
- ) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
50
+ CREATE TABLE `think_auth_group` (
51
+ `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
52
+ `title` char(100) NOT NULL DEFAULT '',
53
+ `status` tinyint(1) NOT NULL DEFAULT '1',
54
+ `rules` char(80) NOT NULL DEFAULT '',
55
+ PRIMARY KEY (`id`)
56
+ ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
56
57
-- ----------------------------
57
58
-- think_auth_group_access 用户组明细表
58
59
-- uid:用户id,group_id:用户组id
59
60
-- ----------------------------
60
61
DROP TABLE IF EXISTS `think_auth_group_access`;
61
- CREATE TABLE `think_auth_group_access` (
62
- `uid` mediumint(8) unsigned NOT NULL,
63
- `group_id` mediumint(8) unsigned NOT NULL,
64
- UNIQUE KEY `uid_2 ` (`uid`,`group_id`),
65
- KEY `uid` (`uid`),
66
- KEY `group_id` (`group_id`)
62
+ CREATE TABLE `think_auth_group_access` (
63
+ `uid` mediumint(8) unsigned NOT NULL,
64
+ `group_id` mediumint(8) unsigned NOT NULL,
65
+ UNIQUE KEY `uid_group_id ` (`uid`,`group_id`),
66
+ KEY `uid` (`uid`),
67
+ KEY `group_id` (`group_id`)
67
68
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
68
-
69
69
*/
70
70
71
- class Authority {
71
+ class Auth {
72
72
73
73
//默认配置
74
74
protected $ _config = array (
@@ -88,7 +88,7 @@ public function __construct() {
88
88
}
89
89
90
90
//获得权限$name 可以是字符串或数组或逗号分割, uid为 认证的用户id, $or 是否为or关系,为true是, name为数组,只要数组中有一个条件通过则通过,如果为false需要全部条件通过。
91
- public function getAuth ($ name , $ uid , $ relation ='or ' ) {
91
+ public function check ($ name , $ uid , $ relation ='or ' ) {
92
92
if (!$ this ->_config ['AUTH_ON ' ])
93
93
return true ;
94
94
$ authList = $ this ->getAuthList ($ uid );
@@ -117,9 +117,10 @@ public function getAuth($name, $uid, $relation='or') {
117
117
//获得用户组,外部也可以调用
118
118
public function getGroups ($ uid ) {
119
119
static $ groups = array ();
120
- if (! empty ($ groups [$ uid ]))
120
+ if (isset ($ groups [$ uid ]))
121
121
return $ groups [$ uid ];
122
- $ groups [$ uid ] = M ()->table ($ this ->_config ['AUTH_GROUP_ACCESS ' ] . ' a ' )->where ("a.uid=' $ uid' " )->join ($ this ->_config ['AUTH_GROUP ' ]." g on a.group_id=g.id " )->select ();
122
+ $ user_groups = M ()->table ($ this ->_config ['AUTH_GROUP_ACCESS ' ] . ' a ' )->where ("a.uid=' $ uid' and g.status='1' " )->join ($ this ->_config ['AUTH_GROUP ' ]." g on a.group_id=g.id " )->select ();
123
+ $ groups [$ uid ]=$ user_groups ?$ user_groups :array ();
123
124
return $ groups [$ uid ];
124
125
}
125
126
@@ -143,16 +144,19 @@ protected function getAuthList($uid) {
143
144
$ _authList [$ uid ] = array ();
144
145
return array ();
145
146
}
146
- //读取用户组所有权限规则(in)
147
- $ map ['id ' ] = array ('in ' , $ ids );
147
+ //读取用户组所有权限规则
148
+ $ map =array (
149
+ 'id ' =>array ('in ' ,$ ids ),
150
+ 'status ' =>1
151
+ );
148
152
$ rules = M ()->table ($ this ->_config ['AUTH_RULE ' ])->where ($ map )->select ();
149
153
//循环规则,判断结果。
150
154
$ authList = array ();
151
155
foreach ($ rules as $ r ) {
152
- if ($ r ['type ' ] == 1 ) {
156
+ if (! empty ( $ r ['condition ' ]) ) {
153
157
//条件验证
154
158
$ user = $ this ->getUserInfo ($ uid );
155
- $ command = preg_replace ('/\{(\w*?)\}/e ' , '$user[ \'\\1 \'] ' , $ r ['condition ' ]);
159
+ $ command = preg_replace ('/\{(\w*?)\}/ ' , '$user[ \'\\1 \'] ' , $ r ['condition ' ]);
156
160
//dump($command);//debug
157
161
@(eval ('$condition=( ' . $ command . '); ' ));
158
162
if ($ condition ) {
@@ -179,4 +183,4 @@ protected function getUserInfo($uid) {
179
183
return $ userinfo [$ uid ];
180
184
}
181
185
182
- }
186
+ }
0 commit comments