From 4149fce83840261de1dc4ae4d85b306796efb023 Mon Sep 17 00:00:00 2001
From: Karson <karsonzhang@163.com>
Date: Wed, 6 Dec 2017 10:13:44 +0800
Subject: [PATCH] 修复规则添加正则错误的BUG 优化登录状态session中存储为数组格式 优化Auth的isLogin方法只需查询一次数据库

---
 application/admin/controller/general/Profile.php | 14 +++++---------
 application/admin/lang/zh-cn/auth/rule.php       | 24 ++++++++++++------------
 application/admin/library/Auth.php               |  8 +++++++-
 application/admin/model/AdminLog.php             |  9 +++++----
 application/admin/validate/AuthRule.php          |  6 +++---
 application/extra/site.php                       | 20 ++++++++++----------
 extend/fast/Auth.php                             |  1 -
 7 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/application/admin/controller/general/Profile.php b/application/admin/controller/general/Profile.php
index f0187ce..1a9c0c6 100644
--- a/application/admin/controller/general/Profile.php
+++ b/application/admin/controller/general/Profile.php
@@ -2,10 +2,10 @@
 
 namespace app\admin\controller\general;
 
-use think\Session;
-use app\admin\model\AdminLog;
+use app\admin\model\Admin;
 use app\common\controller\Backend;
 use fast\Random;
+use think\Session;
 
 /**
  * 个人配置
@@ -64,14 +64,10 @@ class Profile extends Backend
             }
             if ($params)
             {
-                model('admin')->where('id', $this->auth->id)->update($params);
+                $admin = Admin::get($this->auth->id);
+                $admin->save($params);
                 //因为个人资料面板读取的Session显示,修改自己资料后同时更新Session
-                $admin = Session::get('admin');
-                $admin_id = $admin ? $admin->id : 0;
-                if($this->auth->id==$admin_id){
-                    $admin = model('admin')->get(['id' => $admin_id]);
-                    Session::set("admin", $admin);
-                }
+                Session::set("admin", $admin->toArray());
                 $this->success();
             }
             $this->error();
diff --git a/application/admin/lang/zh-cn/auth/rule.php b/application/admin/lang/zh-cn/auth/rule.php
index 2a67f72..5493d3f 100644
--- a/application/admin/lang/zh-cn/auth/rule.php
+++ b/application/admin/lang/zh-cn/auth/rule.php
@@ -1,16 +1,16 @@
 <?php
 
 return [
-    'Toggle all'                                               => '显示全部',
-    'Condition'                                                => '规则条件',
-    'Remark'                                                   => '备注',
-    'Icon'                                                     => '图标',
-    'Alert'                                                    => '警告',
-    'Name'                                                     => '规则URL',
-    'Controller/Action'                                        => '控制器名/方法名',
-    'Ismenu'                                                   => '菜单',
-    'Search icon'                                              => '搜索图标',
-    'The non-menu rule must have parent'                       => '非菜单规则节点必须有父级',
-    'If not necessary, use the command line to build rule'     => '非必要情况下请直接使用命令行php think menu来生成',
-    'Name only supports letters, numbers, underscore and pipe' => 'URL规则只能是小写字母、数字、下划线和|组成',
+    'Toggle all'                                                => '显示全部',
+    'Condition'                                                 => '规则条件',
+    'Remark'                                                    => '备注',
+    'Icon'                                                      => '图标',
+    'Alert'                                                     => '警告',
+    'Name'                                                      => '规则URL',
+    'Controller/Action'                                         => '控制器名/方法名',
+    'Ismenu'                                                    => '菜单',
+    'Search icon'                                               => '搜索图标',
+    'The non-menu rule must have parent'                        => '非菜单规则节点必须有父级',
+    'If not necessary, use the command line to build rule'      => '非必要情况下请直接使用命令行php think menu来生成',
+    'Name only supports letters, numbers, underscore and slash' => 'URL规则只能是小写字母、数字、下划线和/组成',
 ];
diff --git a/application/admin/library/Auth.php b/application/admin/library/Auth.php
index 0bdb26b..1b14740 100644
--- a/application/admin/library/Auth.php
+++ b/application/admin/library/Auth.php
@@ -15,6 +15,7 @@ class Auth extends \fast\Auth
     protected $requestUri = '';
     protected $breadcrumb = [];
     protected $loginUnique = false; //是否同一账号同一时间只能在一个地方登录
+    protected $logined = false; //登录状态
 
     public function __construct()
     {
@@ -89,7 +90,7 @@ class Auth extends \fast\Auth
             {
                 return false;
             }
-            Session::set("admin", $admin);
+            Session::set("admin", $admin->toArray());
             //刷新自动登录的时效
             $this->keeplogin($keeptime);
             return true;
@@ -154,6 +155,10 @@ class Auth extends \fast\Auth
      */
     public function isLogin()
     {
+        if ($this->logined)
+        {
+            return true;
+        }
         $admin = Session::get('admin');
         if (!$admin)
         {
@@ -168,6 +173,7 @@ class Auth extends \fast\Auth
                 return false;
             }
         }
+        $this->logined = true;
         return true;
     }
 
diff --git a/application/admin/model/AdminLog.php b/application/admin/model/AdminLog.php
index 5cb6af8..4296f78 100644
--- a/application/admin/model/AdminLog.php
+++ b/application/admin/model/AdminLog.php
@@ -2,6 +2,7 @@
 
 namespace app\admin\model;
 
+use app\admin\library\Auth;
 use think\Model;
 
 class AdminLog extends Model
@@ -29,9 +30,9 @@ class AdminLog extends Model
 
     public static function record($title = '')
     {
-        $admin = \think\Session::get('admin');
-        $admin_id = $admin ? $admin->id : 0;
-        $username = $admin ? $admin->username : __('Unknown');
+        $auth = Auth::instance();
+        $admin_id = $auth->isLogin() ? $auth->id : 0;
+        $username = $auth->isLogin() ? $auth->username : __('Unknown');
         $content = self::$content;
         if (!$content)
         {
@@ -48,7 +49,7 @@ class AdminLog extends Model
         if (!$title)
         {
             $title = [];
-            $breadcrumb = \app\admin\library\Auth::instance()->getBreadcrumb();
+            $breadcrumb = Auth::instance()->getBreadcrumb();
             foreach ($breadcrumb as $k => $v)
             {
                 $title[] = $v['title'];
diff --git a/application/admin/validate/AuthRule.php b/application/admin/validate/AuthRule.php
index 6f63265..a1cb9e7 100644
--- a/application/admin/validate/AuthRule.php
+++ b/application/admin/validate/AuthRule.php
@@ -10,7 +10,7 @@ class AuthRule extends Validate
     /**
      * 正则
      */
-    protected $regex = ['format' => '[a-z0-9_\|]+'];
+    protected $regex = ['format' => '[a-z0-9_\/]+'];
 
     /**
      * 验证规则
@@ -24,7 +24,7 @@ class AuthRule extends Validate
      * 提示消息
      */
     protected $message = [
-        'name.format' => 'URL规则只能是小写字母、数字、下划线和|组成'
+        'name.format' => 'URL规则只能是小写字母、数字、下划线和/组成'
     ];
 
     /**
@@ -45,7 +45,7 @@ class AuthRule extends Validate
             'name'  => __('Name'),
             'title' => __('Title'),
         ];
-        $this->message['name.format'] = __('Name only supports letters, numbers, underscore and pipe');
+        $this->message['name.format'] = __('Name only supports letters, numbers, underscore and slash');
         parent::__construct($rules, $message, $field);
     }
 
diff --git a/application/extra/site.php b/application/extra/site.php
index 26b56c0..437899a 100644
--- a/application/extra/site.php
+++ b/application/extra/site.php
@@ -2,7 +2,7 @@
 
 return array (
   'name' => 'FastAdmin',
-  'beian' => '粤ICP备15054802号-4',
+  'beian' => '',
   'cdnurl' => '',
   'version' => '1.0.1',
   'timezone' => 'Asia/Shanghai',
@@ -15,18 +15,18 @@ return array (
   'fixedpage' => 'dashboard',
   'categorytype' => 
   array (
-    'default' => '默认',
-    'page' => '单页',
-    'article' => '文章',
-    'test' => '测试',
+    'default' => 'Default',
+    'page' => 'Page',
+    'article' => 'Article',
+    'test' => 'Test',
   ),
   'configgroup' => 
   array (
-    'basic' => '基础配置',
-    'email' => '邮件配置',
-    'dictionary' => '字典配置',
-    'user' => '会员配置',
-    'example' => '示例分组',
+    'basic' => 'Basic',
+    'email' => 'Email',
+    'dictionary' => 'Dictionary',
+    'user' => 'User',
+    'example' => 'Example',
   ),
   'mail_type' => '1',
   'mail_smtp_host' => 'smtp.qq.com',
diff --git a/extend/fast/Auth.php b/extend/fast/Auth.php
index 73ead61..5e3b4c4 100644
--- a/extend/fast/Auth.php
+++ b/extend/fast/Auth.php
@@ -18,7 +18,6 @@ use think\Db;
 use think\Config;
 use think\Session;
 use think\Request;
-use think\Loader;
 
 /**
  * 权限认证类
--
libgit2 0.24.0