Quellcode durchsuchen

feat(auth/password): reset password captcha

Cat vor 3 Jahren
Ursprung
Commit
53488c0a0b

+ 10 - 0
config/settings.json

@@ -429,6 +429,16 @@
         "default": "0",
         "mark": "签到验证码"
     },
+    {
+        "id": null,
+        "item": "enable_reset_password_captcha",
+        "value": "0",
+        "class": "captcha",
+        "is_public": 1,
+        "type": "bool",
+        "default": "0",
+        "mark": "重置密码验证码"
+    },
     {
         "id": null,
         "item": "turnstile_sitekey",

+ 10 - 1
resources/views/material/admin/setting.tpl

@@ -613,6 +613,14 @@
                                                 <option value="1" {if $settings['enable_checkin_captcha'] == true}selected{/if}>开启</option>
                                             </select>
                                         </div>
+                                        <!-- enable_reset_password_captcha -->
+                                        <div class="form-group form-group-label">
+                                            <label class="floating-label">重置密码验证码</label>
+                                            <select id="enable_reset_password_captcha" class="form-control maxwidth-edit">
+                                                <option value="0" {if $settings['enable_reset_password_captcha'] == false}selected{/if}>关闭</option>
+                                                <option value="1" {if $settings['enable_reset_password_captcha'] == true}selected{/if}>开启</option>
+                                            </select>
+                                        </div>
 
                                         <button id="submit_verify_code" type="submit" class="btn btn-block btn-brand">提交</button>
                                     </div>
@@ -1371,7 +1379,8 @@
                     captcha_provider: $$getValue('captcha_provider'),
                     enable_reg_captcha: $$getValue('enable_reg_captcha'),
                     enable_login_captcha: $$getValue('enable_login_captcha'),
-                    enable_checkin_captcha: $$getValue('enable_checkin_captcha')
+                    enable_checkin_captcha: $$getValue('enable_checkin_captcha'),
+                    enable_reset_password_captcha: $$getValue('enable_reset_password_captcha')
                 },
                 success: data => {
                     $("#result").modal();

+ 2 - 2
resources/views/material/auth/login.tpl

@@ -34,7 +34,7 @@
                     </div>
                 </div>
 
-                {if $geetest_html != null}
+                {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'geetest'}
                     <div class="form-group-label labelgeetest auth-row">
                         <div id="embed-captcha"></div>
                     </div>
@@ -204,5 +204,5 @@
 {/if}
 
 {if $config['enable_login_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
-    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
+<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
 {/if}

+ 2 - 2
resources/views/material/auth/register.tpl

@@ -261,7 +261,7 @@ document.getElementById('passwd').addEventListener('input', checkStrong);
                     dataType: "json",
                     data: {
                         {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
-                            turnstile: turnstile.getResponse(),
+                        turnstile: turnstile.getResponse(),
                         {/if}
                         {if $geetest_html != null}
                         geetest_challenge: validate.geetest_challenge,
@@ -427,7 +427,7 @@ document.getElementById('passwd').addEventListener('input', checkStrong);
 {/if}
 
 {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
-    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
+<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
 {/if}
 
 {*dumplin:aff链*}

+ 14 - 1
resources/views/material/password/reset.tpl

@@ -22,7 +22,13 @@
                     <input class="form-control maxwidth-auth" id="email" type="email" inputmode="email" autocomplete="username">
                 </div>
             </div>
-
+            {if $config['enable_reset_password_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+            <div class="form-group-label auth-row">
+                <div class="row">
+                    <div align="center" class="cf-turnstile" data-sitekey="{$turnstile_sitekey}" data-theme="light"></div>
+                </div>
+            </div>
+            {/if}
             <div class="btn-auth auth-row">
                 <button id="reset" type="submit" class="btn btn-block btn-brand waves-attach waves-light">重置密码</button>
             </div>
@@ -81,6 +87,9 @@
                 dataType: "json",
                 data: {
                     email: $$getValue('email'),
+                    {if $config['enable_reset_password_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+                    turnstile: turnstile.getResponse(),
+                    {/if}
                 },
                 success: (data) => {
                     if (data.ret == 1) {
@@ -109,3 +118,7 @@
         });
     })
 </script>
+
+{if $config['enable_reset_password_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
+{/if}

+ 10 - 1
resources/views/tabler/admin/setting.tpl

@@ -613,6 +613,14 @@
                                                 <option value="1" {if $settings['enable_checkin_captcha'] == true}selected{/if}>开启</option>
                                             </select>
                                         </div>
+                                        <!-- enable_reset_password_captcha -->
+                                        <div class="form-group form-group-label">
+                                            <label class="floating-label">重置密码验证码</label>
+                                            <select id="enable_reset_password_captcha" class="form-control maxwidth-edit">
+                                                <option value="0" {if $settings['enable_reset_password_captcha'] == false}selected{/if}>关闭</option>
+                                                <option value="1" {if $settings['enable_reset_password_captcha'] == true}selected{/if}>开启</option>
+                                            </select>
+                                        </div>
 
                                         <button id="submit_verify_code" type="submit" class="btn btn-block btn-brand">提交</button>
                                     </div>
@@ -1371,7 +1379,8 @@
                     captcha_provider: $$getValue('captcha_provider'),
                     enable_reg_captcha: $$getValue('enable_reg_captcha'),
                     enable_login_captcha: $$getValue('enable_login_captcha'),
-                    enable_checkin_captcha: $$getValue('enable_checkin_captcha')
+                    enable_checkin_captcha: $$getValue('enable_checkin_captcha'),
+                    enable_reset_password_captcha: $$getValue('enable_reset_password_captcha')
                 },
                 success: data => {
                     $("#result").modal();

+ 13 - 0
resources/views/tabler/password/reset.tpl

@@ -18,6 +18,13 @@
                         <label class="form-label">注册邮箱</label>
                         <input id="email" type="email" class="form-control">
                     </div>
+                    {if $config['enable_reg_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+                    <div class="mb-3">
+                        <div class="input-group mb-3">
+                            <div class="cf-turnstile" data-sitekey="{$turnstile_sitekey}" data-theme="light"></div>
+                        </div>
+                    </div>
+                    {/if}
                     <div class="form-footer">
                         <button id="send" class="btn btn-primary w-100">
                             <i class="ti ti-brand-telegram icon"></i>
@@ -40,6 +47,9 @@
                 dataType: "json",
                 data: {
                     email: $('#email').val(),
+                    {if $config['enable_reset_password_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+                    turnstile: turnstile.getResponse(),
+                    {/if}
                 },
                 success: function(data) {
                     if (data.ret == 1) {
@@ -58,4 +68,7 @@
         });
     </script>
 
+    {if $config['enable_reset_password_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+    <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?compat=recaptcha" async defer></script>
+    {/if}
 {include file='tabler_footer.tpl'}

+ 3 - 3
resources/views/tabler/user/index.tpl

@@ -429,12 +429,12 @@
             $.ajax({
                 type: "POST",
                 url: "/user/checkin",
-                dataType: "json",
-                {if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
+                dataType: "json",              
                 data: {
+                    {if $config['enable_checkin_captcha'] == true && $config['captcha_provider'] == 'turnstile'}
                     turnstile: turnstile.getResponse(),
+                    {/if}
                 },
-                {/if}
                 success: function(data) {
                     if (data.ret == 1) {
                         $('#success-message').text(data.msg);

+ 1 - 1
src/Controllers/Admin/SettingController.php

@@ -79,7 +79,7 @@ final class SettingController extends BaseController
                 break;
                 // 验证码
             case 'verify_code':
-                $list = ['captcha_provider', 'enable_reg_captcha', 'enable_login_captcha', 'enable_checkin_captcha'];
+                $list = ['captcha_provider', 'enable_reg_captcha', 'enable_login_captcha', 'enable_checkin_captcha', 'enable_reset_password_captcha'];
                 break;
             case 'verify_code_turnstile':
                 $list = ['turnstile_sitekey', 'turnstile_secret'];

+ 15 - 2
src/Controllers/PasswordController.php

@@ -5,7 +5,9 @@ declare(strict_types=1);
 namespace App\Controllers;
 
 use App\Models\PasswordReset;
+use App\Models\Setting;
 use App\Models\User;
+use App\Services\Captcha;
 use App\Services\Password;
 use App\Utils\Hash;
 use App\Utils\ResponseHelper;
@@ -35,16 +37,26 @@ final class PasswordController extends BaseController
      */
     public function handleReset(Request $request, Response $response, array $args)
     {
+        if (Setting::obtain('enable_reset_password_captcha') === true) {
+            $ret = Captcha::verify($request->getParams());
+            if (! $ret) {
+                return ResponseHelper::error($response, '系统无法接受您的验证结果,请刷新页面后重试');
+            }
+        }
+
         $email = strtolower($request->getParam('email'));
         $user = User::where('email', $email)->first();
+
         if ($user === null) {
-            return ResponseHelper::error($response, '此邮箱不存在');
+            $msg = '如果你的账户存在于我们的数据库中,那么重置密码的链接将会发送到你账户所对应的邮箱。';
         }
+
         if (Password::sendResetEmail($email)) {
-            $msg = '重置邮件已经发送,请检查邮箱.';
+            $msg = '如果你的账户存在于我们的数据库中,那么重置密码的链接将会发送到你账户所对应的邮箱。';
         } else {
             $msg = '邮件发送失败,请联系网站管理员。';
         }
+
         return ResponseHelper::successfully($response, $msg);
     }
 
@@ -57,6 +69,7 @@ final class PasswordController extends BaseController
         if ($token === null) {
             return $response->withStatus(302)->withHeader('Location', '/password/reset');
         }
+
         return $response->write(
             $this->view()->display('password/token.tpl')
         );