mirror of https://gitee.com/karson/fastadmin.git
206 lines
4.6 KiB
PHP
206 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace app\admin\library;
|
|
|
|
use app\admin\model\Admin;
|
|
use fast\Random;
|
|
use think\Cookie;
|
|
use think\Request;
|
|
use think\Session;
|
|
|
|
class Auth extends \fast\Auth
|
|
{
|
|
|
|
protected $requestUri = '';
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function __get($name)
|
|
{
|
|
return Session::get('admin.' . $name);
|
|
}
|
|
|
|
public function login($username, $password, $keeptime = 0)
|
|
{
|
|
$admin = Admin::get(['username' => $username]);
|
|
if (!$admin)
|
|
{
|
|
return false;
|
|
}
|
|
if ($admin->password != md5(md5($password) . $admin->salt))
|
|
{
|
|
$admin->loginfailure++;
|
|
$admin->save();
|
|
return false;
|
|
}
|
|
$admin->loginfailure = 0;
|
|
$admin->logintime = time();
|
|
$admin->token = Random::uuid();
|
|
$admin->save();
|
|
Session::set("admin", $admin);
|
|
$this->keeplogin($keeptime);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 注销登录
|
|
*/
|
|
public function logout()
|
|
{
|
|
$admin = Admin::get(intval($this->id));
|
|
if (!$admin)
|
|
{
|
|
return true;
|
|
}
|
|
$admin->token = '';
|
|
$admin->save();
|
|
Session::delete("admin");
|
|
Cookie::delete("keeplogin");
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 自动登录
|
|
* @return boolean
|
|
*/
|
|
public function autologin()
|
|
{
|
|
$keeplogin = Cookie::get('keeplogin');
|
|
if (!$keeplogin)
|
|
{
|
|
return false;
|
|
}
|
|
list($id, $keeptime, $expiretime, $key) = explode('|', $keeplogin);
|
|
if ($id && $keeptime && $expiretime && $key && $expiretime > time())
|
|
{
|
|
$admin = Admin::get($id);
|
|
if (!$admin)
|
|
{
|
|
return false;
|
|
}
|
|
//token有变更
|
|
if ($key != md5(md5($id) . md5($keeptime) . md5($expiretime) . $admin->token))
|
|
{
|
|
return false;
|
|
}
|
|
Session::set("admin", $admin);
|
|
//刷新自动登录的时效
|
|
$this->keeplogin($keeptime);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 刷新保持登录的Cookie
|
|
* @param int $keeptime
|
|
* @return boolean
|
|
*/
|
|
protected function keeplogin($keeptime = 0)
|
|
{
|
|
if ($keeptime)
|
|
{
|
|
$expiretime = time() + $keeptime;
|
|
$key = md5(md5($this->id) . md5($keeptime) . md5($expiretime) . $this->token);
|
|
$data = [$this->id, $keeptime, $expiretime, $key];
|
|
Cookie::set('keeplogin', implode('|', $data));
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function check($name, $uid = '', $relation = 'or', $mode = 'url')
|
|
{
|
|
return parent::check($name, $this->id, $relation, $mode);
|
|
}
|
|
|
|
/**
|
|
* 检测当前控制器和方法是否匹配传递的数组
|
|
*
|
|
* @param array $arr 需要验证权限的数组
|
|
*/
|
|
public function match($arr = [])
|
|
{
|
|
$request = Request::instance();
|
|
$arr = is_array($arr) ? $arr : explode(',', $arr);
|
|
if (!$arr)
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
// 是否存在
|
|
if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr))
|
|
{
|
|
return TRUE;
|
|
}
|
|
|
|
// 没找到匹配
|
|
return FALSE;
|
|
}
|
|
|
|
/**
|
|
* 检测是否登录
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function isLogin()
|
|
{
|
|
return Session::get('admin') ? true : false;
|
|
}
|
|
|
|
/**
|
|
* 获取当前请求的URI
|
|
* @return string
|
|
*/
|
|
public function getRequestUri()
|
|
{
|
|
return $this->requestUri;
|
|
}
|
|
|
|
/**
|
|
* 设置当前请求的URI
|
|
* @param string $uri
|
|
*/
|
|
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 getUserInfo($uid = null)
|
|
{
|
|
$uid = is_null($uid) ? $this->id : $uid;
|
|
|
|
return $uid != $this->id ? Admin::get(intval($uid)) : Session::get('admin');
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|