Browse Source

feat: login log switch

M1Screw 2 years ago
parent
commit
d223d4a419

+ 5 - 5
resources/views/tabler/admin/setting/feature.tpl

@@ -34,7 +34,7 @@
                             <a href="#display" class="nav-link active" data-bs-toggle="tab">功能显示</a>
                         </li>
                         <li class="nav-item">
-                            <a href="#log" class="nav-link active" data-bs-toggle="tab">用户日志</a>
+                            <a href="#log" class="nav-link" data-bs-toggle="tab">用户日志</a>
                         </li>
                     </ul>
                 </div>
@@ -71,12 +71,12 @@
                                 </div>
                             </div>
                         </div>
-                        <div class="tab-pane active show" id="log">
+                        <div class="tab-pane" id="log">
                             <div class="card-body">
                                 <div class="form-group mb-3 row">
                                     <label class="form-label col-3 col-form-label">启用每小时使用流量日志</label>
                                     <div class="col">
-                                        <select id="display_media" class="col form-select" value="{$settings['traffic_log']}">
+                                        <select id="traffic_log" class="col form-select" value="{$settings['traffic_log']}">
                                             <option value="0" {if ! $settings['traffic_log']}selected{/if}>关闭</option>
                                             <option value="1" {if $settings['traffic_log']}selected{/if}>开启</option>
                                         </select>
@@ -91,7 +91,7 @@
                                 <div class="form-group mb-3 row">
                                     <label class="form-label col-3 col-form-label">启用订阅日志</label>
                                     <div class="col">
-                                        <select id="display_subscribe_log" class="col form-select" value="{$settings['subscribe_log']}">
+                                        <select id="subscribe_log" class="col form-select" value="{$settings['subscribe_log']}">
                                             <option value="0" {if ! $settings['subscribe_log']}selected{/if}>关闭</option>
                                             <option value="1" {if $settings['subscribe_log']}selected{/if}>开启</option>
                                         </select>
@@ -106,7 +106,7 @@
                                 <div class="form-group mb-3 row">
                                     <label class="form-label col-3 col-form-label">启用登录日志</label>
                                     <div class="col">
-                                        <select id="display_detect_log" class="col form-select" value="{$settings['login_log']}">
+                                        <select id="login_log" class="col form-select" value="{$settings['login_log']}">
                                             <option value="0" {if ! $settings['login_log']}selected{/if}>关闭</option>
                                             <option value="1" {if $settings['login_log']}selected{/if}>开启</option>
                                         </select>

+ 0 - 2
src/Controllers/Admin/Setting/FeatureController.php

@@ -11,8 +11,6 @@ use Exception;
 final class FeatureController extends BaseController
 {
     private static array $update_field = [
-        'display_media',
-        'display_subscribe_log',
         'display_detect_log',
         'display_docs',
         'display_docs_only_for_paid_user',

+ 8 - 9
src/Controllers/AuthController.php

@@ -78,7 +78,7 @@ final class AuthController extends BaseController
         $user = User::where('email', $email)->first();
 
         if ($user === null) {
-            (new LoginIp())->collectInvalidUserLoginIP($_SERVER['REMOTE_ADDR'], 1);
+            (new LoginIp())->collectLoginIP($_SERVER['REMOTE_ADDR'], 1);
 
             return $response->withJson([
                 'ret' => 0,
@@ -87,8 +87,7 @@ final class AuthController extends BaseController
         }
 
         if (! Hash::checkPassword($user->pass, $passwd)) {
-            // 记录登录失败
-            $user->collectLoginIP($_SERVER['REMOTE_ADDR'], 1);
+            (new LoginIp())->collectLoginIP($_SERVER['REMOTE_ADDR'], 1, $user->id);
 
             return $response->withJson([
                 'ret' => 0,
@@ -98,8 +97,7 @@ final class AuthController extends BaseController
 
         if ($user->ga_enable) {
             if (strlen($code) !== 6) {
-                // 记录登录失败
-                $user->collectLoginIP($_SERVER['REMOTE_ADDR'], 1);
+                (new LoginIp())->collectLoginIP($_SERVER['REMOTE_ADDR'], 1, $user->id);
 
                 return $response->withJson([
                     'ret' => 0,
@@ -108,8 +106,7 @@ final class AuthController extends BaseController
             }
 
             if (! MFA::verifyGa($user, $code)) {
-                // 记录登录失败
-                $user->collectLoginIP($_SERVER['REMOTE_ADDR'], 1);
+                (new LoginIp())->collectLoginIP($_SERVER['REMOTE_ADDR'], 1, $user->id);
 
                 return $response->withJson([
                     'ret' => 0,
@@ -126,7 +123,9 @@ final class AuthController extends BaseController
 
         Auth::login($user->id, $time);
         // 记录登录成功
-        $user->collectLoginIP($_SERVER['REMOTE_ADDR']);
+        (new LoginIp())->collectLoginIP($_SERVER['REMOTE_ADDR'], 0, $user->id);
+        $user->last_login_time = time();
+        $user->save();
 
         return $response->withJson([
             'ret' => 1,
@@ -301,7 +300,7 @@ final class AuthController extends BaseController
 
         if ($user->save() && ! $is_admin_reg) {
             Auth::login($user->id, 3600);
-            $user->collectLoginIP($_SERVER['REMOTE_ADDR']);
+            (new LoginIp())->collectLoginIP($_SERVER['REMOTE_ADDR'], 0, $user->id);
 
             return $response->withJson([
                 'ret' => 1,

+ 11 - 8
src/Models/LoginIp.php

@@ -41,17 +41,20 @@ final class LoginIp extends Model
     /**
      * 记录登录 IP
      *
-     * @param string $ip   IP 地址
-     * @param int    $type 登录失败为 1
+     * @param string $ip IP
+     * @param int $type 1 = failed, 0 = success
+     * @param int $user_id User ID
      *
      * @return void
      */
-    public function collectInvalidUserLoginIP(string $ip, int $type = 0): void
+    public function collectLoginIP(string $ip, int $type = 0, int $user_id = 0): void
     {
-        $this->ip = $ip;
-        $this->userid = 0;
-        $this->datetime = time();
-        $this->type = $type;
-        $this->save();
+        if (Setting::obtain('login_log')) {
+            $this->ip = $ip;
+            $this->userid = $user_id;
+            $this->datetime = time();
+            $this->type = $type;
+            $this->save();
+        }
     }
 }

+ 0 - 24
src/Models/User.php

@@ -360,28 +360,4 @@ final class User extends Model
             }
         }
     }
-
-    /**
-     * 记录登录 IP
-     *
-     * @param string $ip   IP 地址
-     * @param int    $type 登录失败为 1
-     *
-     * @return bool
-     */
-    public function collectLoginIP(string $ip, int $type = 0): bool
-    {
-        $loginip = new LoginIp();
-        $loginip->ip = $ip;
-        $loginip->userid = $this->id;
-        $loginip->datetime = time();
-        $loginip->type = $type;
-
-        if ($type === 0) {
-            $this->last_login_time = time();
-            $this->save();
-        }
-
-        return $loginip->save();
-    }
 }

+ 0 - 6
tests/App/Services/ConfigTest.php

@@ -23,8 +23,6 @@ class ConfigTest extends TestCase
             'jump_delay' => 5,
             'enable_kill' => true,
             'enable_change_email' => false,
-            'subscribeLog' => true,
-            'subscribeLog_keep_days' => 30,
             'enable_r2_client_download' => true,
             'jsdelivr_url' => 'cdn.jsdelivr.net',
         ];
@@ -38,8 +36,6 @@ class ConfigTest extends TestCase
             'jump_delay' => 5,
             'enable_kill' => true,
             'enable_change_email' => false,
-            'subscribeLog' => true,
-            'subscribeLog_keep_days' => 30,
             'enable_r2_client_download' => true,
             'jsdelivr_url' => 'cdn.jsdelivr.net',
         ];
@@ -54,8 +50,6 @@ class ConfigTest extends TestCase
         $this->assertSame($mockEnv['jump_delay'], $config['jump_delay']);
         $this->assertSame($mockEnv['enable_kill'], $config['enable_kill']);
         $this->assertSame($mockEnv['enable_change_email'], $config['enable_change_email']);
-        $this->assertSame($mockEnv['subscribeLog'], $config['subscribeLog']);
-        $this->assertSame($mockEnv['subscribeLog_keep_days'], $config['subscribeLog_keep_days']);
         $this->assertSame($mockEnv['enable_r2_client_download'], $config['enable_r2_client_download']);
         $this->assertSame($mockEnv['jsdelivr_url'], $config['jsdelivr_url']);
     }