Auth.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // +---------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +---------------------------------------------------------------------
  5. // | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
  6. // +---------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +---------------------------------------------------------------------
  9. // | Author: Dean <zxxjjforever@163.com>
  10. // +---------------------------------------------------------------------
  11. namespace cmf\lib;
  12. use huo\model\user\UserModel;
  13. use huolib\constant\CommonConst;
  14. use think\Db;
  15. /**
  16. * ThinkCMF权限认证类
  17. */
  18. class Auth {
  19. //默认配置
  20. protected $_config = [];
  21. public function __construct() {
  22. }
  23. /**
  24. * 检查权限
  25. *
  26. * @param $name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
  27. * @param $uid int 认证用户的id
  28. * @param $relation string 如果为 'or' 表示满足任一条规则即通过验证;如果为 'and'则表示需满足所有规则才能通过验证
  29. *
  30. * @return boolean 通过验证返回true;失败返回false
  31. */
  32. public function check($uid, $name, $relation = 'or') {
  33. if (empty($uid)) {
  34. return false;
  35. }
  36. if ($uid == 1) {
  37. return true;
  38. }
  39. if (is_string($name)) {
  40. $name = strtolower($name);
  41. if (strpos($name, ',') !== false) {
  42. $name = explode(',', $name);
  43. } else {
  44. $findAuthRuleCount = Db::name('auth_rule')->where(
  45. [
  46. 'name' => $name
  47. ]
  48. )->count();
  49. if ($findAuthRuleCount == 0) {//没有规则时,不验证!
  50. return true;
  51. }
  52. $name = [$name];
  53. }
  54. }
  55. $list = []; //保存验证通过的规则名
  56. // $groups = Db::name('RoleUser')
  57. // ->alias("a")
  58. // ->join('__ROLE__ r', 'a.role_id = r.id')
  59. // ->where(["a.user_id" => $uid, "r.status" => CommonConst::STATUS_YES])
  60. // ->column("role_id");
  61. $_map['id'] = $uid;
  62. $_map['user_status'] = CommonConst::STATUS_YES;
  63. $groups = (new UserModel())->where($_map)->column('role_id');
  64. if (in_array(1, $groups)) {
  65. return true;
  66. }
  67. if (empty($groups)) {
  68. return false;
  69. }
  70. $rules = Db::name('AuthAccess')
  71. ->alias("a")
  72. ->join('__AUTH_RULE__ b ', ' a.rule_name = b.name')
  73. ->where(["a.role_id" => ["in", $groups], "b.name" => ["in", $name]])
  74. ->select();
  75. foreach ($rules as $rule) {
  76. if (!empty($rule['condition'])) { //根据condition进行验证
  77. $user = $this->getUserInfo($uid);//获取用户信息,一维数组
  78. $command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule['condition']);
  79. //dump($command);//debug
  80. @(eval('$condition=('.$command.');'));
  81. if ($condition) {
  82. $list[] = strtolower($rule['name']);
  83. }
  84. } else {
  85. $list[] = strtolower($rule['name']);
  86. }
  87. }
  88. if ($relation == 'or' and !empty($list)) {
  89. return true;
  90. }
  91. $diff = array_diff($name, $list);
  92. if ($relation == 'and' and empty($diff)) {
  93. return true;
  94. }
  95. return false;
  96. }
  97. /**
  98. * 获得用户资料
  99. *
  100. * @param $uid
  101. *
  102. * @return mixed
  103. */
  104. private function getUserInfo($uid) {
  105. static $userInfo = [];
  106. if (!isset($userInfo[$uid])) {
  107. $userInfo[$uid] = Db::name('user')->where(['id' => $uid])->find();
  108. }
  109. return $userInfo[$uid];
  110. }
  111. }