Auth.php 17.4 KB
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
<?php

namespace addons\facrm\library;


use app\admin\model\Admin;
use fast\Random;
use fast\Tree;
use think\Config;
use think\Db;

/**
 * 权限管理以及API接口类
 * Class Auth
 * @package addons\facrm\library
 */
class Auth extends \app\admin\library\Auth
{

    protected $_admin = null;
    protected $_token = '';
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 根据Token初始化
     *
     * @param string $token Token
     * @return boolean
     */
    public function init($token)
    {

        if ($this->logined)
        {
            return TRUE;
        }

        if ($this->_error){
			
		}
		$admin_id=cache('facrmapi'.$token);//解决多端登录频繁掉线问题
		$admin=$admin_id?Admin::get($admin_id):'';
		if (!$admin)
        {
           $admin = Admin::get(['token' => $token]);
        }
        if (!$admin)
        {
            $this->setError(('登录失效'));
            return FALSE;
        }

        if ($admin['status']=="hidden")
        {
            $this->setError(('帐户已锁定'));
            return FALSE;
        }


        if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) {
            $this->setError(("请在1天后重试"));
            return false;
        }
        $this->_admin=$admin;
        return true;

    }
    /**
     * 获取admin模型
     * @return Admin
     */
    public function getAdmin()
    {
        return $this->_admin;
    }

    /**
     * 兼容调用user模型的属性
     *
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        return $this->_admin ? $this->_admin->$name : null;
    }

    /**
     * 直接登录账号
     * @param int $admin_id
     * @return Admin
     */
    public function direct($admin_id)
    {

        if ($this->logined)
        {
            return TRUE;
        }

        $admin = Admin::get(['id' => $admin_id]);

        if (!$admin)
        {
            $this->setError("您没有登录");
            return FALSE;
        }

        if ($admin['status']=="hidden")
        {
            $this->setError("帐户已锁定");
            return FALSE;
        }


        if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) {
            $this->setError("请在1天后重试");
            return false;
        }
        $admin->loginfailure = 0;
        $admin->logintime = time();
        $admin->loginip = request()->ip();
        $admin->token = Random::uuid();
		cache('facrmapi'.$admin->token,$admin->id,86400);//解决多端登录频繁掉线问题
        $admin->save();
        $this->_admin=$admin;
		return $admin;

    }

    /**
     * 管理员登录
     *
     * @param string $username 用户名
     * @param string $password 密码
     * @param int    $keeptime 有效时长
     * @return  boolean
     */
    public function login($username, $password, $keeptime = 0)
    {
        $admin = Admin::get(['username' => $username]);
        if (!$admin) {
            $this->setError('用户名不正确');
            return false;
        }
        if ($admin['status'] == 'hidden') {
            $this->setError('管理员是被禁止的');
            return false;
        }
        if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) {
            $this->setError('请在1天后重试');
            return false;
        }
        if ($admin->password != md5(md5($password) . $admin->salt)) {
            $admin->loginfailure++;
            $admin->save();
            $this->setError('密码不正确');
            return false;
        }
        $admin->loginfailure = 0;
        $admin->logintime = time();
        $admin->loginip = request()->ip();
        $admin->token = Random::uuid();
		cache('facrmapi'.$admin->token,$admin->id,86400);//解决多端登录频繁掉线问题
        $admin->save();
        $this->_admin=$admin;
        $this->keeplogin($keeptime);
        return true;
    }

    /**
     * 检测是否登录
     *
     * @return boolean
     */
    public function isLogin()
    {
        if ($this->logined) {

            return true;
        }

        if ($this->_admin) {
            return true;
        }

        //判断管理员IP是否变动
        if (Config::get('fastadmin.loginip_check')) {
            if (!isset($this->_admin['loginip']) || $this->_admin['loginip'] != request()->ip()) {
                $this->logout();
                return false;
            }
        }
        $this->logined = false;
        return false;
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        $admin = Admin::get(intval($this->id));
        if ($admin) {
            $admin->token = '';
            $admin->save();
        }
        $this->logined = false; //重置登录状态
        return true;
    }
	  /**
     * 获取指定组的员工[不含子组]
     * @param $admin_id
     * @return array|bool|string
     */
    public static function getGroupsAdminIds($groupids)
    {
        //获取当前部门负责人
        $adminIds = Db::name('auth_group_access')
            ->alias('ga')
            ->join('__'.strtoupper('admin').'__ a', 'ga.uid = a.id')
            ->where('ga.group_id','in',$groupids)
            ->where('a.status','normal')
            ->column('ga.uid');
        return $adminIds;
    }
    /**
     * 获取直接父IDS
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getParentAdminIds($uid)
    {
        $parentAdminIds = [];
        $groupIds = $this->getParentGroupIds($uid);
        $authGroupList = \app\admin\model\AuthGroupAccess::
        field('uid,group_id')
            ->where('group_id', 'in', $groupIds)
            ->select();
        foreach ($authGroupList as $k => $v) {
            $parentAdminIds[] = $v['uid'];
        }
        return $parentAdminIds;
    }

    /**
     * 取出当前管理员所有的父级组IDS
     * @return array
     */
    public function getParentGroupIds($uid)
    {
        //取出当前管理员所有的分组
        $groups = $this->getGroups($uid);

        $groupIds = [];
        foreach ($groups as $k => $v) {
            $groupIds[] = $v['id'];
        }

        $originGroupIds = $groupIds;
        foreach ($groups as $k => $v) {
            if (in_array($v['pid'], $originGroupIds)) {
                $groupIds = array_diff($groupIds, [$v['id']]);
                unset($groups[$k]);
            }
        }
        // 取出所有分组
        $groupList = \app\admin\model\AuthGroup::where(['status' => 'normal'])->select();
        $parentGroupIds=[];
        foreach ($groups as $k => $v) {
            // 取出包含自己的所有子节点
            $temp= Tree::instance()->init($groupList)->getParent($v['id'], true);
            if ($temp){
                foreach ($temp as $row) {
                    if (!in_array($row['id'],$parentGroupIds)){
                        $parentGroupIds[] = $row['id'];
                    }

                }
            }
        }
        return $parentGroupIds;
    }

    /**
     * 检查是否有客户的的权限
     * @param $customer 一个客户对象或客户ID
     * @param $auth 当前登录后台的权限对象
     * @param string $type 检查的类型 all:自己和下属的。owner:自己的,下属的:branch;
     * @return bool|int|string
     * @throws \think\Exception
     */
    public function checkCustomerAuth($customer,$auth,$type='all'){
        //超级管理员不做限制
        if ($auth->isSuperAdmin()) {
           return true;
        }
        switch ($type) {
            case 'all':
                //全部客户

                $childrenAdminIds = $auth->getChildrenAdminIds(true);
                if (!is_numeric($customer)&&isset($customer['owner_user_id'])){
                    //如果传过来的是客户对象
                    if (in_array($customer['owner_user_id'], $childrenAdminIds)){
                        return  true;
                    }
                    //是否有分享
                    if (in_array($auth->id, explode(',',$customer['rw_user_id']))){
                        return  true;
                    }
                }elseif(is_numeric($customer)){
                    $filter_w[] =function($q) use ($childrenAdminIds,$auth){
                        $q->where('owner_user_id','in', $childrenAdminIds)->whereOr('','exp',Db::raw("FIND_IN_SET('{$auth->id}',rw_user_id)"));
                    };

                }
                break;
            case "owner":

                if (!is_numeric($customer)&&isset($customer['owner_user_id'])){
                    //如果传过来的是客户对象
                    if ($customer['owner_user_id']== $auth->id){
                        return  true;
                    }
                    //是否有分享
                    if (in_array($auth->id, explode(',',$customer['rw_user_id']))){
                        return  true;
                    }
                }elseif(is_numeric($customer)){
                    $filter_w[] =function($q) use ($auth){
                        $q->where('owner_user_id',$auth->id)->whereOr('','exp',Db::raw("FIND_IN_SET('{$auth->id}',rw_user_id)"));
                    };
                }
                //我的客户
                break;
            case "branch":
                $childrenAdminIds = $auth->getChildrenAdminIds(false);
                //下属客户
                if (!is_numeric($customer)&&isset($customer['owner_user_id'])){
                    //如果传过来的是客户对象
                    if (in_array($customer['owner_user_id'], $childrenAdminIds)){
                        return  true;
                    }
                }elseif(is_numeric($customer)){
                    $filter_w['owner_user_id'] = ['in', $childrenAdminIds];
                }
                break;
            default://其它的还做TODO
                break;
        }
        if (is_numeric($customer)&&$filter_w){

            $customerModel= model('\app\admin\model\facrm\Customer');
            return $customerModel->where('id',$customer)->where($filter_w)->count();
        }

        return  false;

    }

    /**
     * 检查是否有线索的的权限
     * @param $customer 一个线索对象或客户ID
     * @param $auth 当前登录后台的权限对象
     * @param string $type 检查的类型 all:自己和下属的。owner:自己的,下属的:branch;
     * @return bool|int|string
     * @throws \think\Exception
     */
    public function checkCluesAuth($clues,$auth,$type='all'){
        //超级管理员不做限制
        if ($auth->isSuperAdmin()) {
            return true;
        }

        switch ($type) {
            case 'all':
                //全部
                $childrenAdminIds = $auth->getChildrenAdminIds(true);
                if (!is_numeric($clues)&&isset($clues['owner_user_id'])){
                    //如果传过来的是客户对象
                    if (in_array($clues['owner_user_id'], $childrenAdminIds)){
                        return  true;
                    }
                }elseif(is_numeric($clues)){

                    $filter_w['owner_user_id'] = ['in', $childrenAdminIds];
                }
                break;
            case "owner":

                if (!is_numeric($clues)&&isset($clues['owner_user_id'])){
                    //如果传过来的是客户对象
                    if ($clues['owner_user_id']== $auth->id){
                        return  true;
                    }
                }elseif(is_numeric($clues)){
                    $filter_w['owner_user_id'] = $auth->id;
                }
                //我的
                break;
            case "branch":
                $childrenAdminIds = $auth->getChildrenAdminIds(false);
                //下属
                if (!is_numeric($clues)&&isset($clues['owner_user_id'])){
                    //如果传过来的是客户对象
                    if (in_array($clues['owner_user_id'], $childrenAdminIds)){
                        return  true;
                    }
                }elseif(is_numeric($clues)){
                    $filter_w['owner_user_id'] = ['in', $childrenAdminIds];
                }
                break;
            default://其它的还做TODO
                break;
        }
        if (is_numeric($clues)&&isset($filter_w['owner_user_id'])){

            $cluesModel= model('\app\admin\model\facrm\Clues');
            return $cluesModel->where('id',$clues)->where($filter_w)->count();
        }

        return  false;

    }



    /**
     * 检查是否有商机的的权限
     * @param $business 一个商机对象或商机ID
     * @param $auth 当前登录后台的权限对象
     * @param string $type 检查的类型 all:自己和下属的。owner:自己的,下属的:branch;
     * @return bool|int|string
     * @throws \think\Exception
     */
    public function checkBusinessAuth($business,$auth,$type='all'){

        //超级管理员不做限制
        if ($auth->isSuperAdmin()) {
            return true;
        }

        switch ($type) {
            case 'all':
                //全部客户

                $childrenAdminIds = $auth->getChildrenAdminIds(true);

                if (!is_numeric($business)&&isset($business['owner_user_id'])){
                    //如果传过来的是客户对象
                    if (in_array($business['owner_user_id'], $childrenAdminIds)){
                        return  true;
                    }
                }elseif(is_numeric($business)){
                    $filter_w['owner_user_id'] = ['in', $childrenAdminIds];
                }
                break;
            case "owner":

                if (!is_numeric($business)&&isset($business['owner_user_id'])){
                    //如果传过来的是客户对象
                    if ($business['owner_user_id']== $auth->id){
                        return  true;
                    }
                }elseif(is_numeric($business)){
                    $filter_w['owner_user_id'] = $auth->id;
                }
                //我的客户
                break;
            case "branch":
                $childrenAdminIds = $auth->getChildrenAdminIds(false);
                //下属客户
                if (!is_numeric($business)&&isset($business['owner_user_id'])){
                    //如果传过来的是客户对象
                    if (in_array($business['owner_user_id'], $childrenAdminIds)){
                        return  true;
                    }
                }elseif(is_numeric($business)){
                    $filter_w['owner_user_id'] = ['in',$childrenAdminIds];
                }
                break;
            default://其它的还做TODO
                break;
        }
        if (is_numeric($business)&&isset($filter_w['owner_user_id'])){
            $businessModel= model('\app\admin\model\facrm\Business');
            return $businessModel->where('id',$business)->where($filter_w)->count();
        }

        return  false;

    }

    /**
     * 判断是否有订单的管理权限
     */
    public function checkContractAuth($contract,$auth,$type='all',$field='order_admin_id',$operate='in'){
        //超级管理员不做限制
        //并且有全部订单权限
        if ($auth->isSuperAdmin()||$auth->check('facrm/contract/index/index')) {
            return true;
        }
        switch ($type) {
            case 'all':
                //全部
                $childrenAdminIds =$auth->getChildrenAdminIds(true);
                if (!is_numeric($contract)&&isset($contract[$field])){
                    //如果传过来的是客户对象
                    if (in_array($contract[$field], $childrenAdminIds)){
                        return  true;
                    }
                }elseif(is_numeric($contract)){
                    $filter_w[$field] = [$operate, $childrenAdminIds];
                }
                break;
            case "owner":

                if (!is_numeric($contract)&&isset($contract[$field])){
                    //如果传过来的是对象
                    if ($contract[$field]== $auth->id){
                        return  true;
                    }
                }elseif(is_numeric($contract)){
                    $filter_w[$field] = $auth->id;
                }
                //我的
                break;
            case "branch":
                $childrenAdminIds =$auth->getChildrenAdminIds(false);
                if (!$childrenAdminIds) return  false;
                //下属
                if (!is_numeric($contract)&&isset($contract[$field])){
                    //如果传过来的是对象
                    if (in_array($contract[$field], $childrenAdminIds)){
                        return  true;
                    }
                }elseif(is_numeric($contract)){
                    $filter_w[$field] = [$operate, $childrenAdminIds];
                }
                break;
            default://其它的还做TODO
                break;
        }
        if (is_numeric($contract)&&isset($filter_w[$field])){
            $contractModel= model('\app\admin\model\facrm\Contract');
            return $contractModel->where('id',$contract)->where($filter_w)->count();
        }

        return  false;
    }


}