123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace cmf\lib;
- use huo\model\user\UserModel;
- use huolib\constant\CommonConst;
- use think\Db;
- class Auth {
-
- protected $_config = [];
- public function __construct() {
- }
-
- 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 {
- $findAuthRuleCount = Db::name('auth_rule')->where(
- [
- 'name' => $name
- ]
- )->count();
- if ($findAuthRuleCount == 0) {
- return true;
- }
- $name = [$name];
- }
- }
- $list = [];
- $_map['id'] = $uid;
- $_map['user_status'] = CommonConst::STATUS_YES;
- $groups = (new UserModel())->where($_map)->column('role_id');
- if (in_array(1, $groups)) {
- return true;
- }
- if (empty($groups)) {
- return false;
- }
- $rules = Db::name('AuthAccess')
- ->alias("a")
- ->join('__AUTH_RULE__ b ', ' a.rule_name = b.name')
- ->where(["a.role_id" => ["in", $groups], "b.name" => ["in", $name]])
- ->select();
- foreach ($rules as $rule) {
- if (!empty($rule['condition'])) {
- $user = $this->getUserInfo($uid);
- $command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule['condition']);
-
- @(eval('$condition=('.$command.');'));
- if ($condition) {
- $list[] = strtolower($rule['name']);
- }
- } else {
- $list[] = strtolower($rule['name']);
- }
- }
- if ($relation == 'or' and !empty($list)) {
- return true;
- }
- $diff = array_diff($name, $list);
- if ($relation == 'and' and empty($diff)) {
- return true;
- }
- return false;
- }
-
- private function getUserInfo($uid) {
- static $userInfo = [];
- if (!isset($userInfo[$uid])) {
- $userInfo[$uid] = Db::name('user')->where(['id' => $uid])->find();
- }
- return $userInfo[$uid];
- }
- }
|