Auth.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. namespace app\admin\library;
  3. use app\admin\model\Admin;
  4. use fast\Random;
  5. use fast\Tree;
  6. use think\Config;
  7. use think\Cookie;
  8. use think\Request;
  9. use think\Session;
  10. class Auth extends \fast\Auth
  11. {
  12. protected $requestUri = '';
  13. protected $breadcrumb = [];
  14. protected $logined = false; //登录状态
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. }
  19. public function __get($name)
  20. {
  21. return Session::get('admin.' . $name);
  22. }
  23. /**
  24. * 管理员登录
  25. *
  26. * @param string $username 用户名
  27. * @param string $password 密码
  28. * @param int $keeptime 有效时长
  29. * @return boolean
  30. */
  31. public function login($username, $password, $keeptime = 0)
  32. {
  33. $admin = Admin::get(['username' => $username]);
  34. if (!$admin)
  35. {
  36. return false;
  37. }
  38. if ($admin->loginfailure >= 3 && time() - $admin->updatetime < 86400)
  39. {
  40. return false;
  41. }
  42. if ($admin->password != md5(md5($password) . $admin->salt))
  43. {
  44. $admin->loginfailure++;
  45. $admin->save();
  46. return false;
  47. }
  48. $admin->loginfailure = 0;
  49. $admin->logintime = time();
  50. $admin->token = Random::uuid();
  51. $admin->save();
  52. Session::set("admin", $admin->toArray());
  53. $this->keeplogin($keeptime);
  54. return true;
  55. }
  56. /**
  57. * 注销登录
  58. */
  59. public function logout()
  60. {
  61. $admin = Admin::get(intval($this->id));
  62. if (!$admin)
  63. {
  64. return true;
  65. }
  66. $admin->token = '';
  67. $admin->save();
  68. Session::delete("admin");
  69. Cookie::delete("keeplogin");
  70. return true;
  71. }
  72. /**
  73. * 自动登录
  74. * @return boolean
  75. */
  76. public function autologin()
  77. {
  78. $keeplogin = Cookie::get('keeplogin');
  79. if (!$keeplogin)
  80. {
  81. return false;
  82. }
  83. list($id, $keeptime, $expiretime, $key) = explode('|', $keeplogin);
  84. if ($id && $keeptime && $expiretime && $key && $expiretime > time())
  85. {
  86. $admin = Admin::get($id);
  87. if (!$admin || !$admin->token)
  88. {
  89. return false;
  90. }
  91. //token有变更
  92. if ($key != md5(md5($id) . md5($keeptime) . md5($expiretime) . $admin->token))
  93. {
  94. return false;
  95. }
  96. Session::set("admin", $admin->toArray());
  97. //刷新自动登录的时效
  98. $this->keeplogin($keeptime);
  99. return true;
  100. }
  101. else
  102. {
  103. return false;
  104. }
  105. }
  106. /**
  107. * 刷新保持登录的Cookie
  108. *
  109. * @param int $keeptime
  110. * @return boolean
  111. */
  112. protected function keeplogin($keeptime = 0)
  113. {
  114. if ($keeptime)
  115. {
  116. $expiretime = time() + $keeptime;
  117. $key = md5(md5($this->id) . md5($keeptime) . md5($expiretime) . $this->token);
  118. $data = [$this->id, $keeptime, $expiretime, $key];
  119. Cookie::set('keeplogin', implode('|', $data), 86400 * 30);
  120. return true;
  121. }
  122. return false;
  123. }
  124. public function check($name, $uid = '', $relation = 'or', $mode = 'url')
  125. {
  126. return parent::check($name, $this->id, $relation, $mode);
  127. }
  128. /**
  129. * 检测当前控制器和方法是否匹配传递的数组
  130. *
  131. * @param array $arr 需要验证权限的数组
  132. */
  133. public function match($arr = [])
  134. {
  135. $request = Request::instance();
  136. $arr = is_array($arr) ? $arr : explode(',', $arr);
  137. if (!$arr)
  138. {
  139. return FALSE;
  140. }
  141. // 是否存在
  142. if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr))
  143. {
  144. return TRUE;
  145. }
  146. // 没找到匹配
  147. return FALSE;
  148. }
  149. /**
  150. * 检测是否登录
  151. *
  152. * @return boolean
  153. */
  154. public function isLogin()
  155. {
  156. if ($this->logined)
  157. {
  158. return true;
  159. }
  160. $admin = Session::get('admin');
  161. if (!$admin)
  162. {
  163. return false;
  164. }
  165. //判断是否同一时间同一账号只能在一个地方登录
  166. if (Config::get('fastadmin.login_unique'))
  167. {
  168. $my = Admin::get($admin['id']);
  169. if (!$my || $my['token'] != $admin['token'])
  170. {
  171. return false;
  172. }
  173. }
  174. $this->logined = true;
  175. return true;
  176. }
  177. /**
  178. * 获取当前请求的URI
  179. * @return string
  180. */
  181. public function getRequestUri()
  182. {
  183. return $this->requestUri;
  184. }
  185. /**
  186. * 设置当前请求的URI
  187. * @param string $uri
  188. */
  189. public function setRequestUri($uri)
  190. {
  191. $this->requestUri = $uri;
  192. }
  193. public function getGroups($uid = null)
  194. {
  195. $uid = is_null($uid) ? $this->id : $uid;
  196. return parent::getGroups($uid);
  197. }
  198. public function getRuleList($uid = null)
  199. {
  200. $uid = is_null($uid) ? $this->id : $uid;
  201. return parent::getRuleList($uid);
  202. }
  203. public function getUserInfo($uid = null)
  204. {
  205. $uid = is_null($uid) ? $this->id : $uid;
  206. return $uid != $this->id ? Admin::get(intval($uid)) : Session::get('admin');
  207. }
  208. public function getRuleIds($uid = null)
  209. {
  210. $uid = is_null($uid) ? $this->id : $uid;
  211. return parent::getRuleIds($uid);
  212. }
  213. public function isSuperAdmin()
  214. {
  215. return in_array('*', $this->getRuleIds()) ? TRUE : FALSE;
  216. }
  217. /**
  218. * 获取管理员所属于的分组ID
  219. * @param int $uid
  220. * @return array
  221. */
  222. public function getGroupIds($uid = null)
  223. {
  224. $groups = $this->getGroups($uid);
  225. $groupIds = [];
  226. foreach ($groups as $K => $v)
  227. {
  228. $groupIds[] = (int) $v['group_id'];
  229. }
  230. return $groupIds;
  231. }
  232. /**
  233. * 取出当前管理员所拥有权限的分组
  234. * @param boolean $withself 是否包含当前所在的分组
  235. * @return array
  236. */
  237. public function getChildrenGroupIds($withself = false)
  238. {
  239. //取出当前管理员所有的分组
  240. $groups = $this->getGroups();
  241. $groupIds = [];
  242. foreach ($groups as $k => $v)
  243. {
  244. $groupIds[] = $v['id'];
  245. }
  246. // 取出所有分组
  247. $groupList = model('AuthGroup')->all(['status' => 'normal']);
  248. $objList = [];
  249. foreach ($groups as $K => $v)
  250. {
  251. if ($v['rules'] === '*')
  252. {
  253. $objList = $groupList;
  254. break;
  255. }
  256. // 取出包含自己的所有子节点
  257. $childrenList = Tree::instance()->init($groupList)->getChildren($v['id'], true);
  258. $obj = Tree::instance()->init($childrenList)->getTreeArray($v['pid']);
  259. $objList = array_merge($objList, Tree::instance()->getTreeList($obj));
  260. }
  261. $childrenGroupIds = [];
  262. foreach ($objList as $k => $v)
  263. {
  264. $childrenGroupIds[] = $v['id'];
  265. }
  266. if (!$withself)
  267. {
  268. $childrenGroupIds = array_diff($childrenGroupIds, $groupIds);
  269. }
  270. return $childrenGroupIds;
  271. }
  272. /**
  273. * 取出当前管理员所拥有权限的管理员
  274. * @param boolean $withself 是否包含自身
  275. * @return array
  276. */
  277. public function getChildrenAdminIds($withself = false)
  278. {
  279. $childrenAdminIds = [];
  280. if (!$this->isSuperAdmin())
  281. {
  282. $groupIds = $this->getChildrenGroupIds(false);
  283. $authGroupList = model('AuthGroupAccess')
  284. ->field('uid,group_id')
  285. ->where('group_id', 'in', $groupIds)
  286. ->select();
  287. foreach ($authGroupList as $k => $v)
  288. {
  289. $childrenAdminIds[] = $v['uid'];
  290. }
  291. }
  292. else
  293. {
  294. //超级管理员拥有所有人的权限
  295. $childrenAdminIds = Admin::column('id');
  296. }
  297. if ($withself)
  298. {
  299. if (!in_array($this->id, $childrenAdminIds))
  300. {
  301. $childrenAdminIds[] = $this->id;
  302. }
  303. }
  304. else
  305. {
  306. $childrenAdminIds = array_diff($childrenAdminIds, [$this->id]);
  307. }
  308. return $childrenAdminIds;
  309. }
  310. /**
  311. * 获得面包屑导航
  312. * @param string $path
  313. * @return array
  314. */
  315. public function getBreadCrumb($path = '')
  316. {
  317. if ($this->breadcrumb || !$path)
  318. return $this->breadcrumb;
  319. $path_rule_id = 0;
  320. foreach ($this->rules as $rule)
  321. {
  322. $path_rule_id = $rule['name'] == $path ? $rule['id'] : $path_rule_id;
  323. }
  324. if ($path_rule_id)
  325. {
  326. $this->breadcrumb = Tree::instance()->init($this->rules)->getParents($path_rule_id, true);
  327. foreach ($this->breadcrumb as $k => &$v)
  328. {
  329. $v['url'] = url($v['name']);
  330. $v['title'] = __($v['title']);
  331. }
  332. }
  333. return $this->breadcrumb;
  334. }
  335. /**
  336. * 获取左侧菜单栏
  337. *
  338. * @param array $params URL对应的badge数据
  339. * @return string
  340. */
  341. public function getSidebar($params = [], $fixedPage = 'dashboard')
  342. {
  343. $colorArr = ['red', 'green', 'yellow', 'blue', 'teal', 'orange', 'purple'];
  344. $colorNums = count($colorArr);
  345. $badgeList = [];
  346. $module = request()->module();
  347. // 生成菜单的badge
  348. foreach ($params as $k => $v)
  349. {
  350. $url = $k;
  351. if (is_array($v))
  352. {
  353. $nums = isset($v[0]) ? $v[0] : 0;
  354. $color = isset($v[1]) ? $v[1] : $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  355. $class = isset($v[2]) ? $v[2] : 'label';
  356. }
  357. else
  358. {
  359. $nums = $v;
  360. $color = $colorArr[(is_numeric($nums) ? $nums : strlen($nums)) % $colorNums];
  361. $class = 'label';
  362. }
  363. //必须nums大于0才显示
  364. if ($nums)
  365. {
  366. $badgeList[$url] = '<small class="' . $class . ' pull-right bg-' . $color . '">' . $nums . '</small>';
  367. }
  368. }
  369. // 读取管理员当前拥有的权限节点
  370. $userRule = $this->getRuleList();
  371. $select_id = 0;
  372. $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
  373. // 必须将结果集转换为数组
  374. $ruleList = collection(model('AuthRule')->where('ismenu', 1)->order('weigh', 'desc')->cache("__menu__")->select())->toArray();
  375. foreach ($ruleList as $k => &$v)
  376. {
  377. if (!in_array($v['name'], $userRule))
  378. {
  379. unset($ruleList[$k]);
  380. continue;
  381. }
  382. $select_id = $v['name'] == $fixedPage ? $v['id'] : $select_id;
  383. $v['url'] = '/' . $module . '/' . $v['name'];
  384. $v['badge'] = isset($badgeList[$v['name']]) ? $badgeList[$v['name']] : '';
  385. $v['py'] = $pinyin->abbr($v['title'], '');
  386. $v['pinyin'] = $pinyin->permalink($v['title'], '');
  387. $v['title'] = __($v['title']);
  388. }
  389. // 构造菜单数据
  390. Tree::instance()->init($ruleList);
  391. $menu = Tree::instance()->getTreeMenu(0, '<li class="@class"><a href="@url@addtabs" addtabs="@id" url="@url" py="@py" pinyin="@pinyin"><i class="@icon"></i> <span>@title</span> <span class="pull-right-container">@caret @badge</span></a> @childlist</li>', $select_id, '', 'ul', 'class="treeview-menu"');
  392. return $menu;
  393. }
  394. }