<?phpnamespace security;
use think\facade\Db;
use think\facade\Config;
use think\facade\Session;
use think\validate\ValidateRule;
class Auth
{
protected $config = array(
'auth_on' => true, 'auth_cache' => true, 'auth_key' => '_auth_', 'auth' => 't_purview', 'auth_role' => 't_purview_role', 'role' => 't_role', 'role_user' => 'user_role', 'users' => 't_users', 'users_auth_fields' => '' 'no_need_login_url' => [
'/admin/login/login'
],
'allow_visit' => [
'/admin',
'/admin/index/main',
'/admin/login/logout'
],
'login_path' => '/admin/login/login'
);
protected $userInfo = [];
protected static $instance;
protected $model = 1;
public static function getInstance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
private function __construct($options = [])
{
if ($auth = Config::get('app.admin.auth.')) {
$this->config = array_merge($this->config, $auth);
}
if (!empty($options) && is_array($options)) {
$this->config = array_merge($this->config, $options);
}
}
public function check($name = null, $uid = null)
{
is_null($name) && $name = $this->getPath();
is_null($uid) && $uid = $this->getUserId();
if (empty($uid) || empty($name)) {
return false;
}
if (empty($this->config['auth_on'])) {
return true;
}
if (in_array($name, $this->config['allow_visit'])) {
return true;
}
$purviews = $this->getRoleUser($uid);
if (empty($purviews)) {
return false;
}
foreach ($purviews as $value) {
if('/admin' == $value) return true;
if($name == $value) return true;
}
return false;
}
public function getRoleUser($uid = null)
{
is_null($uid) && $uid = $this->getUserId();
$data = $this->config['auth_cache'] ? Session::get($this->getRoleUserKey()) : [];
if (empty($data) || !is_array($data)) {
$purviews = Db::name($this->config['role_user'])
->alias('ru')
->where("ru.uid = '$uid'")
->join($this->config['role'] . ' r', 'ru.role_id = r.role_id')
->join($this->config['auth_role'] . ' ar', 'ar.role_id = ru.role_id')
->join($this->config['auth'] . ' a', 'a.purview_id = ar.purview_id')
->field('a.purview_route')
->select()->toArray();
$purviews = array_column($purviews, 'purview_route');
foreach ($purviews as $key => $value) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$purviews[$key] = explode("\n", $value);
} elseif (strtoupper(substr(PHP_OS, 0, 5)) === 'LINUX'){
$purviews[$key] = explode(PHP_EOL, $value);
}
}
$data = array_reduce($purviews, 'array_merge', array());
if (empty($data)) return false;
if ($this->config['auth_cache']) Session::set($this->getRoleUserKey(), $data);
}
return $data;
}
private function getRoleUserKey()
{
return $this->getKey('role_user_list');
}
private function getKey($key)
{
return md5($this->config['auth_key'] . $key);
}
public function login($admin = null)
{
if (is_numeric($admin)) {
$admin = Db::name($this->config['users'])->where($admin)->find()->toArray();
unset($admin['password']);
}
if ($admin) {
Session::set('uid', $admin['uid']);
Session::set('role_id', 1);
Session::set('gold-admin', $admin);
Session::set($this->getRoleUserKey(), null);
return true;
}
return false;
}
public function logout()
{
session(null);
$this->userInfo = null;
return true;
}
public function isLogin()
{
return !empty($this->getUserInfo());
}
public function getUserInfo()
{
$this->userInfo = !empty($this->userInfo) ? $this->userInfo : Session::get('gold-admin');
return $this->userInfo;
}
public function getUserId()
{
$user = $this->getUserInfo();
return $user ? $user[$user->getPk()] : null;
}
public function getUserInfoById($uid = null)
{
is_null($uid) && $uid = $this->getUserId();
$userinfo = $this->config['auth_cache'] ? Session::get($this->getUserKey($uid)) : [];
if (empty($userinfo) || !is_array($userinfo)) {
$user = Db::name($this->config['users']);
$_pk = is_string($user->getPk()) ? $user->getPk() : 'id';
$userinfo = $user->field($this->config['users_auth_fields'])->where($_pk, $uid)->find();
if ($this->config['auth_cache'])
Session::set($this->getUserKey($uid), $userinfo);
}
return $userinfo;
}
private function getUserKey($uid)
{
return $this->getKey('user_info' . $uid);
}
public function notNeedLogin()
{
$urls = $this->config['no_need_login_url'];
if (in_array($this->getPath(), $urls)) {
return true;
}
return false;
}
public function getConif($config = null)
{
if (is_null($config)) {
$configs = $this->config;
} else {
$configs = $this->config[$config];
}
return $configs;
}
public function getPath()
{
$isExit = strstr($_SERVER['REQUEST_URI'], '.html');
if($isExit) {
$path = substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],".html"));
}else{
$path = str_replace('.html','',$_SERVER['REQUEST_URI']);
}
return str_replace('.', '/', strtolower($path));
}
}