123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- namespace addons\csmadmin;
- use addons\csmadmin\library\CsmContants;
- use addons\csmadmin\library\CsmNotify;
- use addons\csmadmin\library\CsmUtils;
- use addons\csmadmin\library\CsmadminFactory;
- use addons\csmadmin\library\service\CsmAdminService;
- use think\Addons;
- use think\Request;
- use think\Session;
- use app\common\library\Menu;
- use addons\csmadmin\library\service\AdminService;
- use app\admin\library\Auth;
- use think\Config;
- /**
- * 插件
- */
- class Csmadmin extends Addons
- {
- /**
- * 插件安装方法
- *
- * @return bool
- */
- public function install()
- {
- $menu = [
- [
- 'name' => 'csmadmin',
- 'title' => '后台管理账号增强',
- 'sublist' => [
- [
- 'name' => 'csmadmin/adminapply',
- 'title' => '帐号注册审核',
- 'icon' => 'fa fa-meetup',
- 'sublist' => [
- [
- 'name' => 'csmadmin/adminapply/index',
- 'title' => '查看'
- ],
- [
- 'name' => 'csmadmin/adminapply/add',
- 'title' => '添加'
- ],
- [
- 'name' => 'csmadmin/adminapply/edit',
- 'title' => '修改'
- ],
- [
- 'name' => 'csmadmin/adminapply/del',
- 'title' => '删除'
- ],
- [
- 'name' => 'csmadmin/adminapply/submitauditok',
- 'title' => '审核通过'
- ],
- [
- 'name' => 'csmadmin/adminapply/submitauditreturn',
- 'title' => '审核退回'
- ]
- ]
- ],
- [
- 'name' => 'csmadmin/csmadmindepart/admindepart',
- 'title' => '组织和人员管理',
- 'icon' => 'fa fa-meetup',
- 'sublist' => [
- [
- 'name' => 'csmadmin/csmadmindepart/getdeparttreeAjax',
- 'title' => '获取部门信息'
- ],
- [
- 'name' => 'csmadmin/csmadmindepart/getUsersByDepartsAjax',
- 'title' => '获取人员信息'
- ],
- [
- 'name' => 'csmadmin/csmadmindepart/removeUserFromDepartAjax',
- 'title' => '将人员从部门中移除'
- ],
- [
- 'name' => 'csmadmin/csmadmindepart/selectuser',
- 'title' => '将人员加入到部门'
- ],
- [
- 'name' => 'csmadmin/csmadmindepart/insertdepart',
- 'title' => '新增部门'
- ],
- [
- 'name' => 'csmadmin/csmadmindepart/updatedepart',
- 'title' => '更新部门'
- ],
- [
- 'name' => 'csmadmin/csmadmindepart/updateSubDepartRootId',
- 'title' => '移动部门'
- ],
- [
- 'name' => 'csmadmin/csmadmindepart/deldepartajax',
- 'title' => '删除部门'
- ],
- [
- 'name' => 'csmadmin/depart/index',
- 'title' => '部门查询'
- ]
- ]
- ]
- ]
- ]
- ];
- Menu::create($menu);
- return true;
- }
- /**
- * 插件卸载方法
- *
- * @return bool
- */
- public function uninstall()
- {
- Menu::delete('csmadmin');
- return true;
- }
- /**
- * 插件启用方法
- *
- * @return bool
- */
- public function enable()
- {
- Menu::enable('csmadmin');
- return true;
- }
- /**
- * 插件禁用方法
- *
- * @return bool
- */
- public function disable()
- {
- Menu::disable('csmadmin');
- return true;
- }
- public function adminLoginAfter(&$request)
- {
- if($this->isAdminpath($request)===false){
- return;
- }
-
- $this->_isSuspiciousLogin($request);
- $this->_needRedirectModifyPsd($request);
- }
- // 是否是可疑登录,需要ip转换插件
- private function _isSuspiciousLogin(&$request)
- {
- $config = get_addon_config(CsmContants::$ADDONS);
- $isopensuspiciouslogin = $config["isopensuspiciouslogin"];
- if ($isopensuspiciouslogin == 'Y') {
- $csmip = get_addon_info('csmip');
- if ($csmip && $csmip['state']) {
-
- $suerid = Auth::instance()->id;
- $aservice = new AdminService();
- $suser = $aservice->getRowById($suerid);
-
- $email = $suser->email;
- $adminid = $suser->id;
- if ($email != null && $email != '') {
- $csmip = \addons\csmip\library\Csmip::getInstance();
- $region = $csmip->getRegion($request->ip());
- $service = new CsmAdminService();
- $row = $service->getRowByAdminId($adminid);
- if ($row && $row->lastlogincity != null && $row->lastlogincity != '' && $row->lastlogincity != $region->city) {
- // 可疑登录,推送
- $param = [
- 'time' => date("Y-m-d H:i:s"),
- 'city' => $region->country . $region->city
- ];
- CsmNotify::notify($adminid, '异地登录提醒', '异地登录提醒', $param);
- }
- // 保存日志
- if (true) {
- $logindao = new \app\admin\model\csmadmin\Loginlog();
- $param = [
- 'faadmin_id' => $adminid,
- 'loginip' => $request->ip(),
- 'logincountry' => $region->country,
- 'loginprovince' => $region->province,
- 'logincity' => $region->city,
- 'createtime' => time()
- ];
- $logindao->create($param);
- }
- // 保存最近一次登录位置
- if (true) {
- $param = [
- 'faadmin_id' => $adminid,
- 'lastloginip' => $request->ip(),
- 'lastlogincountry' => $region->country,
- 'lastloginprovince' => $region->province,
- 'lastlogincity' => $region->city
- ];
- $service->insertOrUpdateByByAdminId($param, $adminid);
- }
- }
- } else {
- CsmUtils::error("请未安装“IP地址转地区”插件,请到插件配置中关闭“可疑登录邮件通知”配置");
- }
- }
- }
- // 判定首次是否需要修改密码,定期是否修改密码
- private function _needRedirectModifyPsd(&$request)
- {
-
- $config = get_addon_config(CsmContants::$ADDONS);
- $needupdatepsdwhenfirstlogin = $config["needupdatepsdwhenfirstlogin"];
- $needupdatepsdperiod = $config["needupdatepsdperiod"];
- $needupdatepsdperiodtime = $config["needupdatepsdperiodtime"];
-
- $suerid = Auth::instance()->id;
- $aservice = new AdminService();
- $suser = $aservice->getRowById($suerid);
- $needupdatepsd = false;
- if ($needupdatepsdwhenfirstlogin == 'Y') {
- // 由于上次登录时间被登录覆盖,所以只能通过session中的时间来判定actionbegin
- $lastlogintime = Session::get("csmadmin_lastlogintime");
- if ($lastlogintime == null) {
- $needupdatepsd = true;
- }
- }
- if ($needupdatepsdperiod == 'Y') {
- // 判断密码修改时间是否超时
- $dao = new \app\admin\model\csmadmin\Admin();
- $row = $dao->where("faadmin_id", "=", $suser->id)
- ->where('status', '=', 'normal')
- ->find();
- // 如果没有密码修改时间,则以创建时间为准
- $lastupdatpsdtime = $suser->createtime;
- if ($row != null && ($row->updatepsdtime != null || $row->updatepsdtime != 0)) {
- $lastupdatpsdtime = $row->updatepsdtime;
- }
- $tt = (int) $needupdatepsdperiodtime;
- if ((time() - $lastupdatpsdtime) >= $tt * 7200) {
- $needupdatepsd = true;
- }
- }
- if ($needupdatepsd === true) {
- $tourl = $request->get('url', 'index/index');
- CsmUtils::success('', '', array(
- 'url' => 'csmadmin/csmadminpassword/modifypasswordafterlogin?tourl=' . urlencode($tourl)
- ));
- }
- }
- public function actionBegin()
- {
- $request = Request::instance();
- if($this->isAdminpath($request)===false){
- return;
- }
- $path = $request->path();
- $business = CsmadminFactory::createbusiness($path);
- if ($business) {
- $business->actionBegin($request);
- }
- }
- public function moduleInit(&$request)
- {
- if($this->isAdminpath($request)===false){
- return;
- }
- //v1.2.1 优化了组织授权关系 fa_csmadmin_auth_group_access替换fa_auth_group_access
- Config::set('auth',['auth_group_access' => 'csmadmin_auth_group_access']);
-
- $request = Request::instance();
- $path = $request->path();
- $business = CsmadminFactory::createbusiness($path);
- if ($business) {
- $business->moduleInit($request);
- }
- }
- public function appEnd($response)
- {
- $request = Request::instance();
- if($this->isAdminpath($request)===false){
- return;
- }
- $path = $request->path();
- $business = CsmadminFactory::createbusiness($path);
- if ($business) {
- $business->appEnd($response);
- }
- }
- public function viewFilter(& $content)
- {
- $request = Request::instance();
- if($this->isAdminpath($request)===false){
- return;
- }
-
- $path = $request->path();
- $business = CsmadminFactory::createbusiness($path);
- if ($business != null) {
- $vfs = $business->viewFilter($request);
-
- if ($vfs != null) {
- $append = '<script>window.csmadmincfg={"trigger":"' . $vfs->trigger . '","triggername":"' . $vfs->triggername . '","data":' . ($vfs->jsondata ? $vfs->jsondata : "null") . '};</script>';
- $content = preg_replace("/<\/body>/", $append . "</body>", $content, 1);
- }
- }
- }
- /**
- * 判断当前路径是否admin模块
- */
- private function isAdminpath($request){
- if($request->module()=='admin'){
- return true;
- }else{
- return false;
- }
- }
- }
|