| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 小夏 < 449134904@qq.com>
- // +----------------------------------------------------------------------
- namespace app\admin\controller;
- use app\admin\model\RoleUserModel;
- use app\admin\model\UserModel;
- use cmf\controller\AdminBaseController;
- class PublicController extends AdminBaseController
- {
- public function initialize()
- {
- }
- /**
- * 后台登陆界面
- */
- public function login()
- {
- $loginAllowed = session("__LOGIN_BY_CMF_ADMIN_PW__");
- if (empty($loginAllowed)) {
- //$this->error('非法登录!', cmf_get_root() . '/');
- return redirect(cmf_get_root() . "/");
- }
- $admin_id = session('ADMIN_ID');
- if (!empty($admin_id)) {//已经登录
- return redirect(url("admin/Index/index"));
- } else {
- session("__SP_ADMIN_LOGIN_PAGE_SHOWED_SUCCESS__", true);
- $result = hook_one('admin_login');
- if (!empty($result)) {
- return $result;
- }
- $siteInfo = cmf_get_site_info();
- $this->assign('siteInfo', $siteInfo);
-
- return $this->fetch(":login");
- }
- }
- /**
- * 登录验证
- */
- public function doLogin()
- {
- if (!$this->request->isPost()) {
- $this->error('非法登录!');
- }
- if (hook_one('admin_custom_login_open')) {
- $this->error('您已经通过插件自定义后台登录!');
- }
- $loginAllowed = session("__LOGIN_BY_CMF_ADMIN_PW__");
- if (empty($loginAllowed)) {
- $this->error('非法登录!', cmf_get_root() . '/');
- }
- $captcha = $this->request->param('captcha');
- if (empty($captcha)) {
- $this->error(lang('CAPTCHA_REQUIRED'));
- }
- //验证码
- if (!cmf_captcha_check($captcha)) {
- $this->error(lang('CAPTCHA_NOT_RIGHT'));
- }
- $name = $this->request->param("username");
- if (empty($name)) {
- $this->error(lang('USERNAME_OR_EMAIL_EMPTY'));
- }
- $pass = $this->request->param("password");
- if (empty($pass)) {
- $this->error(lang('PASSWORD_REQUIRED'));
- }
- if (strpos($name, "@") > 0) {//邮箱登陆
- $where['user_email'] = $name;
- } else {
- $where['user_login'] = $name;
- }
-
- $result = UserModel::where($where)->find();
- if (!empty($result) && $result['user_type'] == 1) {
- if (cmf_compare_password($pass, $result['user_pass'])) {
- $groups = RoleUserModel::alias("a")
- ->join('role b', 'a.role_id =b.id')
- ->where(["user_id" => $result["id"], "status" => 1])
- ->value("role_id");
- if ($result["id"] != 1 && (empty($groups) || empty($result['user_status']))) {
- $this->error(lang('USE_DISABLED'));
- }
- //登入成功页面跳转
- session('ADMIN_ID', $result["id"]);
- session('name', $result["user_login"]);
- $data = [];
- $data['last_login_ip'] = get_client_ip(0, true);
- $data['last_login_time'] = time();
- $token = cmf_generate_user_token($result["id"], 'web');
- if (!empty($token)) {
- session('token', $token);
- }
- UserModel::where('id', $result['id'])->update($data);
- cookie("admin_username", $name, 3600 * 24 * 30);
- session("__LOGIN_BY_CMF_ADMIN_PW__", null);
- $this->success(lang('LOGIN_SUCCESS'), url("admin/Index/index"));
- } else {
- $this->error(lang('PASSWORD_NOT_RIGHT'));
- }
- } else {
- $this->error(lang('USERNAME_NOT_EXIST'));
- }
- }
- /**
- * 后台管理员退出
- */
- public function logout()
- {
- session('ADMIN_ID', null);
- return redirect(url('/', [], false, true));
- }
- }
|