thinkPHP5 ACL用户权限模块用法详解

2025-05-29 0 103

本文实例讲述了thinkPHP5 ACL用户权限模块用法。分享给大家供大家参考,具体如下:

最近学习thinkphp5,和以前3.X版本是完全不是一个概念。学习thinkphp5的朋友要注意命名空间思想。

最近做的一个项目,一个检测管理系统,由于为了以后做APP需要,才切换到thinkphp5作为以后的扩展API用的。今天完成的是用户权限控制模块。我把这个mark下来

数据库:

role数据库表:

?

1

2

3

4

5

6

7

8

9

10
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,

`name` varchar(20) NOT NULL COMMENT '角色名称',

`pid` smallint(6) DEFAULT NULL COMMENT '父角色ID',

`rule_name` text COMMENT '规则唯一英文标识,全小写',

`type` varchar(50) DEFAULT '' COMMENT '权限规则分类,请加应用前缀,如admin_',

`status` tinyint(1) unsigned DEFAULT NULL COMMENT '状态',

`remark` varchar(255) DEFAULT NULL COMMENT '备注',

`create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',

`update_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',

`listorder` int(3) NOT NULL DEFAULT '0' COMMENT '排序字段',

auth_rule数据库表:

?

1

2

3

4

5

6

7

8
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '规则id,自增主键',

`module` varchar(20) NOT NULL COMMENT '规则所属module',

`type` varchar(30) NOT NULL DEFAULT '1' COMMENT '权限规则分类,请加应用前缀,如admin_',

`name` varchar(255) NOT NULL DEFAULT '' COMMENT '规则唯一英文标识,全小写',

`param` varchar(255) DEFAULT NULL COMMENT '额外url参数',

`title` varchar(20) NOT NULL DEFAULT '' COMMENT '规则中文描述',

`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否有效(0:无效,1:有效)',

`condition` varchar(300) NOT NULL DEFAULT '' COMMENT '规则附加条件',

用户表里面增加:

?

1

2
`pools` varchar(20) DEFAULT '' COMMENT '权限池',

`roleId` smallint(5) NOT NULL DEFAULT '0' COMMENT '权限id',

代码如下:

iAuth.php 权限认证的公共库文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76
class iAuth{

public $user = null;

//默认配置

protected $_config = array(

);

public function __construct() {

}

/**

* 检查权限

* @param name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组

* @param uid int 认证用户的id

* @param relation string 如果为 'or' 表示满足任一条规则即通过验证;如果为 'and'则表示需满足所有规则才能通过验证

* @return boolean 通过验证返回true;失败返回false

*/

public function check($uid,$name,$relation='or') {

if(empty($uid)){

return false;

}

if($uid==1){

return true;

}

if (is_string($name)) {

$name = strtolower($name);

if (strpos($name, ',') !== false) {

$name = explode(',', $name);

} else {

$name = array($name);

}

}

$list = array(); //保存验证通过的规则名

//获取用户信息

$this->getUserInfo($uid);//获取用户信息,一维数组

$groups= $this->user['roleId'];

if(in_array(1, $groups)){

return true;

}

if(empty($groups)){

return false;

}

$rules = self::get_rules($this->user['roleId']);

if(in_array($name,$rules))

{

return true;

}

return false;

}

/**

* 获得用户资料

*/

private function getUserInfo(&$uid) {

if(!isset($this->user)){

$user = new Users($uid);

$this->user = $user->fields;

}

return $this->user;

}

/**

* 获取验证规则

* @param int $id

*/

public static function get_rules($id)

{

if(empty($id)) return false;

$rules = Cache::get(self::$cache_prefix . $id);

if(empty($rules))

{

$model = Db::name('role');

$model->where('id',$id);

$rules = $model->find();

$rules['rule_name'] = explode(',',strtolower($rules['rule_name']));

//设置缓存

Cache::set(self::$cache_prefix,$rules);

}

return $rules;

}

}

Common.php 通用函数类库

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
/**

* 检测用户id

* @param name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组

* @param uid int 认证用户的id

*/

function sp_auth_check($uid, $name=null)

{

if(empty($uid)) return false;

if(empty($name)){

$name=strtolower(MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME);

}

$iAuth_obj = new \\app\\Common\\Lib\\iAuth();

return $iAuth_obj->check($uid);

}

AdminbaseController.php 后台管理的父控制器类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62
class AdminbaseController extends Controller

{

public $uid = 0;

//用户实例

public $userObj = null;

/**

* 构造函数

* Adminbase constructor.

*/

public function __construct()

{

parent::__construct();

}

public function _initialize()

{

$this->uid = Session::read('AdminId');

if(!empty($this->uid ))

{

//检测过已经登录了

$this->userObj = Db::name('users')->where('uid',$this->uid)->find();

if(!$this->check_access($this->uid))

{

$this->error("您没有访问权限!",Url::build('admin/index/login'));

exit();

}

$this->assign('admin',$this->userObj);

}

else

{

//没有登录的

$this->error("您还没有登录!",Url::build('admin/index/login'));

exit();

}

}

/**

* 检测权限

* @param $uid

*/

private function check_access(&$uid)

{

if($uid == 1)

{

//超级管理员

return true;

}

$request = Request::instance();

//如果不是这个应用池的账户也不通过

$pools = explode(',',$this->userObj['pools']);

if(!in_array(strtolower($request->module()), $pools)) return false;

$rule = $request->module() . '_' . $request->controller() . '_' . $request->action() ;

$no_need_check_rules = Config::get('inc_auth.no_need_check_rules');

if(!in_array(strtolower($rule),$no_need_check_rules))

{

//验证权限

return sp_auth_check($uid);

}

else

{

return true;

}

}

}

inc_auth.php 认证配置文件

?

1
$config['no_need_check_rules'] = array('admin_index_index','admin_index_login');

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 thinkPHP5 ACL用户权限模块用法详解 https://www.kuaiidc.com/94992.html

相关文章

发表评论
暂无评论