$username]); if (!$admin) { $this->setError('Username is incorrect'); return false; } if ($admin['status'] == 'hidden') { $this->setError('Admin is forbidden'); return false; } // if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) { // $this->setError('Please try again after 1 day'); // return false; // } if ($admin->password != md5(md5($password) . $admin->salt)) { $admin->loginfailure++; $admin->save(); $this->setError('Password is incorrect'); return false; } $admin->loginfailure = 0; $admin->logintime = time(); $admin->loginip = request()->ip(); $admin->token = Random::uuid(); $admin->save(); Session::set("admin", $admin->toArray()); $this->keeplogin($keeptime); return true; } /** * 退出登录 */ public function logout() { $admin = Admin::get(intval($this->id)); if ($admin) { $admin->token = ''; $admin->save(); } $this->logined = false; //重置登录状态 Session::delete("admin"); Cookie::delete("keeplogin"); return true; } /** * 自动登录 * @return boolean */ public function autologin() { $keeplogin = Cookie::get('keeplogin'); if (!$keeplogin) { return false; } list($id, $keeptime, $expiretime, $key) = explode('|', $keeplogin); if ($id && $keeptime && $expiretime && $key && $expiretime > time()) { $admin = Admin::get($id); if (!$admin || !$admin->token) { return false; } //token有变更 if ($key != md5(md5($id) . md5($keeptime) . md5($expiretime) . $admin->token . config('token.key'))) { return false; } $ip = request()->ip(); //IP有变动 if ($admin->loginip != $ip) { return false; } Session::set("admin", $admin->toArray()); //刷新自动登录的时效 $this->keeplogin($keeptime); return true; } else { return false; } } /** * 刷新保持登录的Cookie * * @param int $keeptime * @return boolean */ protected function keeplogin($keeptime = 0) { if ($keeptime) { $expiretime = time() + $keeptime; $key = md5(md5($this->id) . md5($keeptime) . md5($expiretime) . $this->token . config('token.key')); $data = [$this->id, $keeptime, $expiretime, $key]; Cookie::set('keeplogin', implode('|', $data), 86400 * 7); return true; } return false; } public function check($name, $uid = '', $relation = 'or', $mode = 'url') { $uid = $uid ? $uid : $this->id; return parent::check($name, $uid, $relation, $mode); } /** * 检测当前控制器和方法是否匹配传递的数组 * * @param array $arr 需要验证权限的数组 * @return bool */ public function match($arr = []) { $request = Request::instance(); $arr = is_array($arr) ? $arr : explode(',', $arr); if (!$arr) { return false; } $arr = array_map('strtolower', $arr); // 是否存在 if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) { return true; } // 没找到匹配 return false; } /** * 检测是否登录 * * @return boolean */ public function isLogin() { if ($this->logined) { return true; } $admin = Session::get('admin'); if (!$admin) { return false; } //判断是否同一时间同一账号只能在一个地方登录 if (Config::get('fastadmin.login_unique')) { $my = Admin::get($admin['id']); if (!$my || $my['token'] != $admin['token']) { $this->logined = false; //重置登录状态 Session::delete("admin"); Cookie::delete("keeplogin"); return false; } } //判断管理员IP是否变动 // if (Config::get('fastadmin.loginip_check')) { // if (!isset($admin['loginip']) || $admin['loginip'] != request()->ip()) { // $this->logout(); // return false; // } // } $this->logined = true; return true; } /** * 获取当前请求的URI * @return string */ public function getRequestUri() { return $this->requestUri; } /** * 设置当前请求的URI * @param string $uri */ public function setRequestUri($uri) { $this->requestUri = $uri; } public function getGroups($uid = null) { $uid = is_null($uid) ? $this->id : $uid; return parent::getGroups($uid); } public function getRuleList($uid = null) { $uid = is_null($uid) ? $this->id : $uid; return parent::getRuleList($uid); } public function getUserInfo($uid = null) { $uid = is_null($uid) ? $this->id : $uid; return $uid != $this->id ? Admin::get(intval($uid)) : Session::get('admin'); } public function getRuleIds($uid = null) { $uid = is_null($uid) ? $this->id : $uid; return parent::getRuleIds($uid); } public function isSuperAdmin() { return in_array('*', $this->getRuleIds()) ? true : false; } /** * 检测当前管理员是否为讲师角色 * @return boolean */ public function isLecturer() { static $isLecturer = null; if (!is_null($isLecturer)) { return $isLecturer; } //获取当前管理员ID(通过 __get 从 session 读取,避免直接读取 protected $id 属性) $adminId = Session::get('admin.id'); if (empty($adminId)) { $isLecturer = false; return false; } //超级管理员不属于讲师 $rules = $this->getRuleIds($adminId); if (in_array('*', $rules)) { $isLecturer = false; return false; } //获取当前管理员所属的角色组ID $groupIds = Db::name('auth_group_access')->where('uid', $adminId)->column('group_id'); if (empty($groupIds)) { //兼容部分管理员仅记录在 admin.group_id 的情况 $groupId = Admin::where('id', $adminId)->value('group_id'); $groupIds = $groupId ? [$groupId] : []; } if (empty($groupIds)) { $isLecturer = false; return false; } //讲师角色写死在代码中,通过固定ID判断 $isLecturer = in_array(\app\admin\model\auth\Group::LECTURER_GROUP_ID, $groupIds); return $isLecturer; } /** * 获取管理员所属于的分组ID * @param int $uid * @return array */ public function getGroupIds($uid = null) { $groups = $this->getGroups($uid); $groupIds = []; foreach ($groups as $K => $v) { $groupIds[] = (int)$v['group_id']; } return $groupIds; } /** * 获取管理员所属于的分组ID * @param int $uid * @return array */ public function getGroupAuthRule($uid = null) { $rules = $this->getRuleList($uid); //$rules是一个数组,$rules=["user/aaa","user/bbb"],把数组成员中的“/”替换成“-” $rules = array_map(function ($item) { return str_replace('/', '-', $item); }, $rules); return $rules; } /** * 取出当前管理员所拥有权限的分组 * @param boolean $withself 是否包含当前所在的分组 * @return array */ public function getChildrenGroupIds($withself = false) { //取出当前管理员所有的分组 $groups = $this->getGroups(); $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::select(); $objList = []; foreach ($groups as $k => $v) { if ($v['rules'] === '*') { $objList = $groupList; break; } // 取出包含自己的所有子节点 $childrenList = Tree::instance()->init($groupList, 'pid')->getChildren($v['id'], true); $obj = Tree::instance()->init($childrenList, 'pid')->getTreeArray($v['pid']); $objList = array_merge($objList, Tree::instance()->getTreeList($obj)); } $childrenGroupIds = []; foreach ($objList as $k => $v) { $childrenGroupIds[] = $v['id']; } if (!$withself) { $childrenGroupIds = array_diff($childrenGroupIds, $groupIds); } return $childrenGroupIds; } /** * 取出当前管理员所拥有权限的管理员 * @param boolean $withself 是否包含自身 * @return array */ public function getChildrenAdminIds($withself = false) { $childrenAdminIds = []; if (!$this->isSuperAdmin()) { $groupIds = $this->getChildrenGroupIds(false); $authGroupList = \app\admin\model\AuthGroupAccess:: field('uid,group_id') ->where('group_id', 'in', $groupIds) ->select(); foreach ($authGroupList as $k => $v) { $childrenAdminIds[] = $v['uid']; } } else { //超级管理员拥有所有人的权限 $childrenAdminIds = Admin::column('id'); } if ($withself) { if (!in_array($this->id, $childrenAdminIds)) { $childrenAdminIds[] = $this->id; } } else { $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]); } return $childrenAdminIds; } /** * 获得面包屑导航 * @param string $path * @return array */ public function getBreadCrumb($path = '') { if ($this->breadcrumb || !$path) { return $this->breadcrumb; } $path = strtolower($path); $menuArr = []; $urlArr = explode('/', $path); foreach ($urlArr as $index => $item) { $pathArr[implode('/', array_slice($urlArr, 0, $index + 1))] = $index; } $allRules = $this->getAllRule(); foreach ($allRules as $url => $title) { if (isset($pathArr[$url])) { $temp = [ 'title'=>__($title), 'url'=>tpurl($url) ]; $menuArr[$pathArr[$url]] = $temp; } } ksort($menuArr); $this->breadcrumb = $menuArr; return $this->breadcrumb; } /** * 设置错误信息 * * @param string $error 错误信息 * @return Auth */ public function setError($error) { $this->_error = $error; return $this; } /** * 获取错误信息 * @return string */ public function getError() { return $this->_error ? __($this->_error) : ''; } }