Csmadmin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace addons\csmadmin;
  3. use addons\csmadmin\library\CsmContants;
  4. use addons\csmadmin\library\CsmNotify;
  5. use addons\csmadmin\library\CsmUtils;
  6. use addons\csmadmin\library\CsmadminFactory;
  7. use addons\csmadmin\library\service\CsmAdminService;
  8. use think\Addons;
  9. use think\Request;
  10. use think\Session;
  11. use app\common\library\Menu;
  12. use addons\csmadmin\library\service\AdminService;
  13. use app\admin\library\Auth;
  14. use think\Config;
  15. /**
  16. * 插件
  17. */
  18. class Csmadmin extends Addons
  19. {
  20. /**
  21. * 插件安装方法
  22. *
  23. * @return bool
  24. */
  25. public function install()
  26. {
  27. $menu = [
  28. [
  29. 'name' => 'csmadmin',
  30. 'title' => '后台管理账号增强',
  31. 'sublist' => [
  32. [
  33. 'name' => 'csmadmin/adminapply',
  34. 'title' => '帐号注册审核',
  35. 'icon' => 'fa fa-meetup',
  36. 'sublist' => [
  37. [
  38. 'name' => 'csmadmin/adminapply/index',
  39. 'title' => '查看'
  40. ],
  41. [
  42. 'name' => 'csmadmin/adminapply/add',
  43. 'title' => '添加'
  44. ],
  45. [
  46. 'name' => 'csmadmin/adminapply/edit',
  47. 'title' => '修改'
  48. ],
  49. [
  50. 'name' => 'csmadmin/adminapply/del',
  51. 'title' => '删除'
  52. ],
  53. [
  54. 'name' => 'csmadmin/adminapply/submitauditok',
  55. 'title' => '审核通过'
  56. ],
  57. [
  58. 'name' => 'csmadmin/adminapply/submitauditreturn',
  59. 'title' => '审核退回'
  60. ]
  61. ]
  62. ],
  63. [
  64. 'name' => 'csmadmin/csmadmindepart/admindepart',
  65. 'title' => '组织和人员管理',
  66. 'icon' => 'fa fa-meetup',
  67. 'sublist' => [
  68. [
  69. 'name' => 'csmadmin/csmadmindepart/getdeparttreeAjax',
  70. 'title' => '获取部门信息'
  71. ],
  72. [
  73. 'name' => 'csmadmin/csmadmindepart/getUsersByDepartsAjax',
  74. 'title' => '获取人员信息'
  75. ],
  76. [
  77. 'name' => 'csmadmin/csmadmindepart/removeUserFromDepartAjax',
  78. 'title' => '将人员从部门中移除'
  79. ],
  80. [
  81. 'name' => 'csmadmin/csmadmindepart/selectuser',
  82. 'title' => '将人员加入到部门'
  83. ],
  84. [
  85. 'name' => 'csmadmin/csmadmindepart/insertdepart',
  86. 'title' => '新增部门'
  87. ],
  88. [
  89. 'name' => 'csmadmin/csmadmindepart/updatedepart',
  90. 'title' => '更新部门'
  91. ],
  92. [
  93. 'name' => 'csmadmin/csmadmindepart/updateSubDepartRootId',
  94. 'title' => '移动部门'
  95. ],
  96. [
  97. 'name' => 'csmadmin/csmadmindepart/deldepartajax',
  98. 'title' => '删除部门'
  99. ],
  100. [
  101. 'name' => 'csmadmin/depart/index',
  102. 'title' => '部门查询'
  103. ]
  104. ]
  105. ]
  106. ]
  107. ]
  108. ];
  109. Menu::create($menu);
  110. return true;
  111. }
  112. /**
  113. * 插件卸载方法
  114. *
  115. * @return bool
  116. */
  117. public function uninstall()
  118. {
  119. Menu::delete('csmadmin');
  120. return true;
  121. }
  122. /**
  123. * 插件启用方法
  124. *
  125. * @return bool
  126. */
  127. public function enable()
  128. {
  129. Menu::enable('csmadmin');
  130. return true;
  131. }
  132. /**
  133. * 插件禁用方法
  134. *
  135. * @return bool
  136. */
  137. public function disable()
  138. {
  139. Menu::disable('csmadmin');
  140. return true;
  141. }
  142. public function adminLoginAfter(&$request)
  143. {
  144. if($this->isAdminpath($request)===false){
  145. return;
  146. }
  147. $this->_isSuspiciousLogin($request);
  148. $this->_needRedirectModifyPsd($request);
  149. }
  150. // 是否是可疑登录,需要ip转换插件
  151. private function _isSuspiciousLogin(&$request)
  152. {
  153. $config = get_addon_config(CsmContants::$ADDONS);
  154. $isopensuspiciouslogin = $config["isopensuspiciouslogin"];
  155. if ($isopensuspiciouslogin == 'Y') {
  156. $csmip = get_addon_info('csmip');
  157. if ($csmip && $csmip['state']) {
  158. $suerid = Auth::instance()->id;
  159. $aservice = new AdminService();
  160. $suser = $aservice->getRowById($suerid);
  161. $email = $suser->email;
  162. $adminid = $suser->id;
  163. if ($email != null && $email != '') {
  164. $csmip = \addons\csmip\library\Csmip::getInstance();
  165. $region = $csmip->getRegion($request->ip());
  166. $service = new CsmAdminService();
  167. $row = $service->getRowByAdminId($adminid);
  168. if ($row && $row->lastlogincity != null && $row->lastlogincity != '' && $row->lastlogincity != $region->city) {
  169. // 可疑登录,推送
  170. $param = [
  171. 'time' => date("Y-m-d H:i:s"),
  172. 'city' => $region->country . $region->city
  173. ];
  174. CsmNotify::notify($adminid, '异地登录提醒', '异地登录提醒', $param);
  175. }
  176. // 保存日志
  177. if (true) {
  178. $logindao = new \app\admin\model\csmadmin\Loginlog();
  179. $param = [
  180. 'faadmin_id' => $adminid,
  181. 'loginip' => $request->ip(),
  182. 'logincountry' => $region->country,
  183. 'loginprovince' => $region->province,
  184. 'logincity' => $region->city,
  185. 'createtime' => time()
  186. ];
  187. $logindao->create($param);
  188. }
  189. // 保存最近一次登录位置
  190. if (true) {
  191. $param = [
  192. 'faadmin_id' => $adminid,
  193. 'lastloginip' => $request->ip(),
  194. 'lastlogincountry' => $region->country,
  195. 'lastloginprovince' => $region->province,
  196. 'lastlogincity' => $region->city
  197. ];
  198. $service->insertOrUpdateByByAdminId($param, $adminid);
  199. }
  200. }
  201. } else {
  202. CsmUtils::error("请未安装“IP地址转地区”插件,请到插件配置中关闭“可疑登录邮件通知”配置");
  203. }
  204. }
  205. }
  206. // 判定首次是否需要修改密码,定期是否修改密码
  207. private function _needRedirectModifyPsd(&$request)
  208. {
  209. $config = get_addon_config(CsmContants::$ADDONS);
  210. $needupdatepsdwhenfirstlogin = $config["needupdatepsdwhenfirstlogin"];
  211. $needupdatepsdperiod = $config["needupdatepsdperiod"];
  212. $needupdatepsdperiodtime = $config["needupdatepsdperiodtime"];
  213. $suerid = Auth::instance()->id;
  214. $aservice = new AdminService();
  215. $suser = $aservice->getRowById($suerid);
  216. $needupdatepsd = false;
  217. if ($needupdatepsdwhenfirstlogin == 'Y') {
  218. // 由于上次登录时间被登录覆盖,所以只能通过session中的时间来判定actionbegin
  219. $lastlogintime = Session::get("csmadmin_lastlogintime");
  220. if ($lastlogintime == null) {
  221. $needupdatepsd = true;
  222. }
  223. }
  224. if ($needupdatepsdperiod == 'Y') {
  225. // 判断密码修改时间是否超时
  226. $dao = new \app\admin\model\csmadmin\Admin();
  227. $row = $dao->where("faadmin_id", "=", $suser->id)
  228. ->where('status', '=', 'normal')
  229. ->find();
  230. // 如果没有密码修改时间,则以创建时间为准
  231. $lastupdatpsdtime = $suser->createtime;
  232. if ($row != null && ($row->updatepsdtime != null || $row->updatepsdtime != 0)) {
  233. $lastupdatpsdtime = $row->updatepsdtime;
  234. }
  235. $tt = (int) $needupdatepsdperiodtime;
  236. if ((time() - $lastupdatpsdtime) >= $tt * 7200) {
  237. $needupdatepsd = true;
  238. }
  239. }
  240. if ($needupdatepsd === true) {
  241. $tourl = $request->get('url', 'index/index');
  242. CsmUtils::success('', '', array(
  243. 'url' => 'csmadmin/csmadminpassword/modifypasswordafterlogin?tourl=' . urlencode($tourl)
  244. ));
  245. }
  246. }
  247. public function actionBegin()
  248. {
  249. $request = Request::instance();
  250. if($this->isAdminpath($request)===false){
  251. return;
  252. }
  253. $path = $request->path();
  254. $business = CsmadminFactory::createbusiness($path);
  255. if ($business) {
  256. $business->actionBegin($request);
  257. }
  258. }
  259. public function moduleInit(&$request)
  260. {
  261. if($this->isAdminpath($request)===false){
  262. return;
  263. }
  264. //v1.2.1 优化了组织授权关系 fa_csmadmin_auth_group_access替换fa_auth_group_access
  265. Config::set('auth', ['auth_group_access' => 'auth_group_access']);
  266. $request = Request::instance();
  267. $path = $request->path();
  268. $business = CsmadminFactory::createbusiness($path);
  269. if ($business) {
  270. $business->moduleInit($request);
  271. }
  272. }
  273. public function appEnd($response)
  274. {
  275. $request = Request::instance();
  276. if($this->isAdminpath($request)===false){
  277. return;
  278. }
  279. $path = $request->path();
  280. $business = CsmadminFactory::createbusiness($path);
  281. if ($business) {
  282. $business->appEnd($response);
  283. }
  284. }
  285. public function viewFilter(& $content)
  286. {
  287. $request = Request::instance();
  288. if($this->isAdminpath($request)===false){
  289. return;
  290. }
  291. $path = $request->path();
  292. $business = CsmadminFactory::createbusiness($path);
  293. if ($business != null) {
  294. $vfs = $business->viewFilter($request);
  295. if ($vfs != null) {
  296. $append = '<script>window.csmadmincfg={"trigger":"' . $vfs->trigger . '","triggername":"' . $vfs->triggername . '","data":' . ($vfs->jsondata ? $vfs->jsondata : "null") . '};</script>';
  297. $content = preg_replace("/<\/body>/", $append . "</body>", $content, 1);
  298. }
  299. }
  300. }
  301. /**
  302. * 判断当前路径是否admin模块
  303. */
  304. private function isAdminpath($request){
  305. if($request->module()=='admin'){
  306. return true;
  307. }else{
  308. return false;
  309. }
  310. }
  311. }