iAuth.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +---------------------------------------------------------------------
  8. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  9. // +---------------------------------------------------------------------
  10. // | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
  11. // +---------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +---------------------------------------------------------------------
  14. // | Author: Dean <zxxjjforever@163.com>
  15. // +---------------------------------------------------------------------
  16. namespace Common\Lib;
  17. /**
  18. * ThinkCMF权限认证类
  19. */
  20. class iAuth{
  21. //默认配置
  22. protected $_config = array(
  23. );
  24. public function __construct() {
  25. }
  26. /**
  27. * 检查权限
  28. * @param name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
  29. * @param uid int 认证用户的id
  30. * @param relation string 如果为 'or' 表示满足任一条规则即通过验证;如果为 'and'则表示需满足所有规则才能通过验证
  31. * @return boolean 通过验证返回true;失败返回false
  32. */
  33. public function check($uid,$name,$relation='or') {
  34. if(empty($uid)){
  35. return false;
  36. }
  37. if($uid==1){
  38. return true;
  39. }
  40. if (is_string($name)) {
  41. $name = strtolower($name);
  42. if (strpos($name, ',') !== false) {
  43. $name = explode(',', $name);
  44. } else {
  45. $name = array($name);
  46. }
  47. }
  48. $list = array(); //保存验证通过的规则名
  49. $role_user_model=M("RoleUser");
  50. $role_user_join = C('DB_PREFIX').'role as b on a.role_id =b.id';
  51. $groups=$role_user_model->alias("a")->join($role_user_join)->where(array("user_id"=>$uid,"status"=>1))->getField("role_id",true);
  52. if(in_array(1, $groups)){
  53. return true;
  54. }
  55. if(empty($groups)){
  56. return false;
  57. }
  58. $auth_access_model=M("AuthAccess");
  59. $join = C('DB_PREFIX').'auth_rule as b on a.rule_name =b.name';
  60. $rules=$auth_access_model->alias("a")->join($join)->where(array("a.role_id"=>array("in",$groups),"b.name"=>array("in",$name)))->select();
  61. foreach ($rules as $rule){
  62. if (!empty($rule['condition'])) { //根据condition进行验证
  63. $user = $this->getUserInfo($uid);//获取用户信息,一维数组
  64. $command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule['condition']);
  65. //dump($command);//debug
  66. @(eval('$condition=(' . $command . ');'));
  67. if ($condition) {
  68. $list[] = strtolower($rule['name']);
  69. }
  70. }else{
  71. $list[] = strtolower($rule['name']);
  72. }
  73. }
  74. if ($relation == 'or' and !empty($list)) {
  75. return true;
  76. }
  77. $diff = array_diff($name, $list);
  78. if ($relation == 'and' and empty($diff)) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. /**
  84. * 获得用户资料
  85. */
  86. private function getUserInfo($uid) {
  87. static $userinfo=array();
  88. if(!isset($userinfo[$uid])){
  89. $userinfo[$uid]=M("Users")->where(array('id'=>$uid))->find();
  90. }
  91. return $userinfo[$uid];
  92. }
  93. }