123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849 |
- <?php
- namespace app\common\library;
- use app\company\model\Admin as User;
- use fast\Tree;
- use fast\Random;
- use think\Config;
- use think\Db;
- use think\Exception;
- use think\Hook;
- use think\Request;
- use think\Validate;
- class Authcompany extends \fast\Authpc
- {
- protected static $instance = null;
- protected $_error = '';
- protected $_logined = false;
- protected $_user = null;
- protected $_token = '';
-
- protected $keeptime = 2592000;
- protected $requestUri = '';
- protected $rules = [];
-
- protected $options = [];
- protected $allowFields = ['id','username','nickname','avatar','company_id' ];
- protected $breadcrumb = [];
-
- public function __construct()
- {
- parent::__construct();
- }
-
- public static function instance($options = [])
- {
- if (is_null(self::$instance)) {
- self::$instance = new static($options);
- }
- return self::$instance;
- }
-
- public function getUser()
- {
- return $this->_user;
- }
-
- public function __get($name)
- {
- return $this->_user ? $this->_user->$name : null;
- }
-
- public function __isset($name)
- {
- return isset($this->_user) ? isset($this->_user->$name) : false;
- }
-
- public function init($token)
- {
- if ($this->_logined) {
- return true;
- }
- if ($this->_error) {
- return false;
- }
- $data = Tokencompany::get($token);
- if (!$data) {
- return false;
- }
- $user_id = intval($data['user_id']);
- if ($user_id > 0) {
- $user = User::get($user_id);
- if (!$user) {
- $this->setError('Account not exist');
- return false;
- }
- if ($user['status'] != 1) {
- $this->setError('Account is locked');
- return false;
- }
- $this->_user = $user;
- $this->_logined = true;
- $this->_token = $token;
-
- Hook::listen("company_init_successed", $this->_user);
- return true;
- } else {
- $this->setError('You are not logged in');
- return false;
- }
- }
-
- public function login($account, $password)
- {
- $field = 'username';
- $user = User::get([$field => $account]);
- if (!$user) {
- $this->setError('Account is incorrect');
- return false;
- }
- if ($user->status != 1) {
- $this->setError('Account is locked');
- return false;
- }
- if (Config::get('pcadmin.login_failure_retry') && $user->loginfailure >= 10 && time() - $user->updatetime < 86400) {
- $this->setError('Please try again after 1 day');
- return false;
- }
- if ($user->password != $this->getEncryptPassword($password, $user->salt)) {
- $user->loginfailure++;
- $this->setError('Password is incorrect');
- return false;
- }
-
- return $this->direct($user->id);
- }
-
- public function logout()
- {
- if (!$this->_logined) {
- $this->setError('You are not logged in');
- return false;
- }
-
- $this->_logined = false;
-
- Tokencompany::delete($this->_token);
-
- Hook::listen("user_logout_successed", $this->_user);
- return true;
- }
-
- public function changepwd($newpassword, $oldpassword = '', $ignoreoldpassword = false)
- {
- if (!$this->_logined) {
- $this->setError('You are not logged in');
- return false;
- }
-
- if ($this->_user->password == $this->getEncryptPassword($oldpassword, $this->_user->salt) || $ignoreoldpassword) {
- Db::startTrans();
- try {
- $salt = Random::alnum();
- $newpassword = $this->getEncryptPassword($newpassword, $salt);
- $this->_user->save(['loginfailure' => 0, 'password' => $newpassword, 'salt' => $salt]);
- Tokencompany::delete($this->_token);
-
- Hook::listen("company_changepwd_successed", $this->_user);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->setError($e->getMessage());
- return false;
- }
- return true;
- } else {
- $this->setError('Password is incorrect');
- return false;
- }
- }
-
- public function direct($user_id)
- {
- $user = User::get($user_id);
- if ($user) {
- Db::startTrans();
- try {
- $ip = request()->ip();
- $time = time();
- $user->loginfailure = 0;
- $user->logintime = $time;
- $user->loginip = $ip;
- $user->save();
- $this->_user = $user;
- $this->_token = Random::uuid();
- Tokencompany::set($this->_token, $user->id, $this->keeptime);
- $this->_logined = true;
-
- Hook::listen("company_login_successed", $this->_user);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->setError($e->getMessage());
- return false;
- }
- return true;
- } else {
- return false;
- }
- }
-
-
- public function check($name, $uid = '', $relation = 'or', $mode = 'url')
- {
- $uid = $uid ? $uid : $this->id;
- return parent::check($name, $uid, $relation, $mode);
- }
-
- 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;
- }
-
- public function isLogin()
- {
- if ($this->_logined) {
- return true;
- }
- return false;
- }
-
- public function getToken()
- {
- return $this->_token;
- }
-
- public function getUserInfo_simple()
- {
- $data = $this->_user->toArray();
- $allowFields = $this->getAllowFields();
- $userinfo = array_intersect_key($data, array_flip($allowFields));
- $userinfo = array_merge($userinfo, Tokencompany::get($this->_token));
-
- $userinfo['avatar'] = localpath_to_netpath($userinfo['avatar']);
- return $userinfo;
- }
-
- public function getUserInfo()
- {
- $data = $this->_user->toArray();
- $allowFields = $this->getAllowFields();
- $userinfo = array_intersect_key($data, array_flip($allowFields));
- $userinfo = array_merge($userinfo, Tokencompany::get($this->_token));
-
- $userinfo['avatar'] = localpath_to_netpath($userinfo['avatar']);
-
- $userinfo['company'] = Db::name('company')->field('id,companyname')->where('id',$userinfo['company_id'])->find();
- return $userinfo;
- }
-
- public function getRequestUri()
- {
- return $this->requestUri;
- }
-
- 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 getRuleIds($uid = null)
- {
- $uid = is_null($uid) ? $this->id : $uid;
- return parent::getRuleIds($uid);
- }
-
- public function isSuperAdmin()
- {
- return in_array('*', $this->getRuleIds()) ? true : false;
- }
-
- public function getGroupIds($uid = null)
- {
- $groups = $this->getGroups($uid);
- $groupIds = [];
- foreach ($groups as $K => $v) {
- $groupIds[] = (int)$v['group_id'];
- }
- return $groupIds;
- }
-
- 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\company\model\AuthGroup::where($this->isSuperAdmin() ? '1=1' : ['status' => 'normal'])->where('company_id',$this->company_id)->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;
- }
-
- public function getChildrenAdminIds($withself = false)
- {
- $childrenAdminIds = [];
- if (!$this->isSuperAdmin()) {
- $groupIds = $this->getChildrenGroupIds(false);
- $authGroupList = \app\company\model\AuthGroupAccess::field('uid,group_id')
- ->where('group_id', 'in', $groupIds)
- ->select();
- foreach ($authGroupList as $k => $v) {
- $childrenAdminIds[] = $v['uid'];
- }
- } else {
-
- $childrenAdminIds = User::where('company_id',$this->company_id)->column('id');
- }
- if ($withself) {
- if (!in_array($this->id, $childrenAdminIds)) {
- $childrenAdminIds[] = $this->id;
- }
- } else {
- $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]);
- }
- return $childrenAdminIds;
- }
-
- public function getBreadCrumb($path = '')
- {
- if ($this->breadcrumb || !$path) {
- return $this->breadcrumb;
- }
- $titleArr = [];
- $menuArr = [];
- $urlArr = explode('/', $path);
- foreach ($urlArr as $index => $item) {
- $pathArr[implode('/', array_slice($urlArr, 0, $index + 1))] = $index;
- }
- if (!$this->rules && $this->id) {
- $this->getRuleList();
- }
- foreach ($this->rules as $rule) {
- if (isset($pathArr[$rule['name']])) {
- $rule['title'] = __($rule['title']);
- $rule['url'] = url($rule['name']);
- $titleArr[$pathArr[$rule['name']]] = $rule['title'];
- $menuArr[$pathArr[$rule['name']]] = $rule;
- }
- }
- ksort($menuArr);
- $this->breadcrumb = $menuArr;
- return $this->breadcrumb;
- }
-
- public function get_menus(){
-
- $userRule = $this->getRuleList();
-
- $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
- ->field('id,pid as parentId,name as namerule,title as name,path,component,component_name as componentName,icon,visible,alwaysShow,keepAlive')
- ->where('type', 'NEQ',3)
- ->order('weigh', 'desc')
- ->select())->toArray();
- foreach ($ruleList as $k => &$v) {
- $v['visible'] = $v['visible'] == 1 ? true : false;
- $v['alwaysShow'] = $v['alwaysShow'] == 1 ? true : false;
- $v['keepAlive'] = $v['keepAlive'] == 1 ? true : false;
- if (!in_array($v['namerule'], $userRule)) {
- unset($ruleList[$k]);
- continue;
- }
- }
- Tree::instance()->init($ruleList,'parentId');
- $menu = Tree::instance()->getTreeArray_2(0);
- return $menu;
- }
- public function get_menus_simple(){
-
- $userRule = $this->getRuleList();
-
- $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
- ->field('id,pid as parentId,name as namerule,title as name,path,component,component_name as componentName,icon,visible,alwaysShow,keepAlive')
- ->where('type',1)->where('pid',0)
- ->order('weigh', 'desc')
- ->select())->toArray();
- foreach ($ruleList as $k => &$v) {
- $v['visible'] = $v['visible'] == 1 ? true : false;
- $v['alwaysShow'] = $v['alwaysShow'] == 1 ? true : false;
- $v['keepAlive'] = $v['keepAlive'] == 1 ? true : false;
- if (!in_array($v['namerule'], $userRule)) {
- unset($ruleList[$k]);
- continue;
- }
- }
- Tree::instance()->init($ruleList,'parentId');
- $menu = Tree::instance()->getTreeArray_2(0);
- return $menu;
- }
- public function get_permissions(){
-
- $userRule = $this->getRuleList();
-
- $ruleList = collection(\app\admin\model\PcAuthRule::where('status', 'normal')
- ->where('type', 3)
- ->order('weigh', 'desc')
- ->select())->toArray();
- foreach ($ruleList as $k => &$v) {
- if (!in_array($v['name'], $userRule)) {
- unset($ruleList[$k]);
- continue;
- }
- }
- return array_column($ruleList,'permission');
- }
-
-
- public function getAllowFields()
- {
- return $this->allowFields;
- }
-
- public function setAllowFields($fields)
- {
- $this->allowFields = $fields;
- }
-
- public function delete($user_id)
- {
- $user = User::get($user_id);
- if (!$user) {
- return false;
- }
- Db::startTrans();
- try {
-
- User::destroy($user_id);
-
- Tokencompany::clear($user_id);
- Hook::listen("company_delete_successed", $user);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->setError($e->getMessage());
- return false;
- }
- return true;
- }
-
- public function getEncryptPassword($password, $salt = '')
- {
- return md5(md5($password) . $salt);
- }
-
- public function keeptime($keeptime = 0)
- {
- $this->keeptime = $keeptime;
- }
-
- public function setError($error)
- {
- $this->_error = $error;
- return $this;
- }
-
- public function getError()
- {
- return $this->_error ? __($this->_error) : '';
- }
- }
|