如何在 Grav 中禁用用户名验证?

How to disable username validation in Grav?

Grav 版本为 1.1.5

我删除了源代码中的所有验证逻辑和验证模式,但它不起作用,我在创建用户时仍然出错。

错误信息名称为USERNAME_NOT_VALID,我在所有源代码中搜索并删除了相关代码,但当我出错时它仍然显示在屏幕上。我不明白消息来自哪里。

diff --git a/user/plugins/admin/pages/admin/register.md b/user/plugins/admin/pages/admin/register.md
index ff0b13b..fc0cf6e 100644
--- a/user/plugins/admin/pages/admin/register.md
+++ b/user/plugins/admin/pages/admin/register.md
@@ -9,7 +9,7 @@ form:
       validate:
         required: true
         message: PLUGIN_LOGIN.USERNAME_NOT_VALID
-        pattern: '^[a-z0-9_-]{3,16}$'
+        pattern: ''

     - name: email
       type: email
@@ -27,7 +27,7 @@ form:
       validate:
         required: true
         message: PLUGIN_ADMIN.PASSWORD_VALIDATION_MESSAGE
-        pattern: '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}'
+        pattern: ''

     - name: password2
       type: password
@@ -36,7 +36,7 @@ form:
       validate:
         required: true
         message: PLUGIN_ADMIN.PASSWORD_VALIDATION_MESSAGE
-        pattern: '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}'
+        pattern: ''

     - name: fullname
       type: text
diff --git a/user/plugins/login/pages/register.md b/user/plugins/login/pages/register.md
index c4088e2..0391367 100644
--- a/user/plugins/login/pages/register.md
+++ b/user/plugins/login/pages/register.md
@@ -12,7 +12,7 @@ form:
       validate:
         required: true
         message: PLUGIN_LOGIN.USERNAME_NOT_VALID
-        pattern: '^[a-z0-9_-]{3,16}$'
+        pattern: ''

     -
       name: email
@@ -31,7 +31,7 @@ form:
       validate:
         required: true
         message: PLUGIN_LOGIN.PASSWORD_VALIDATION_MESSAGE
-        pattern: '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}'
+        pattern: ''

     -
       name: password2
@@ -41,7 +41,7 @@ form:
       validate:
         required: true
         message: PLUGIN_LOGIN.PASSWORD_VALIDATION_MESSAGE
-        pattern: '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}'
+        pattern: ''

   buttons:
       -
diff --git a/user/plugins/login/pages/reset.md b/user/plugins/login/pages/reset.md
index e5f82e4..8300d2a 100644
--- a/user/plugins/login/pages/reset.md
+++ b/user/plugins/login/pages/reset.md
@@ -20,7 +20,7 @@ form:
           validate:
             required: true
             message: PLUGIN_LOGIN.PASSWORD_VALIDATION_MESSAGE
-            pattern: '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}'
+            pattern: ''

         - name: token
           type: hidden



diff --git a/user/plugins/login/cli/ChangePasswordCommand.php b/user/plugins/login/cli/ChangePasswordCommand.php
index d15abb7..5bde6cc 100644
--- a/user/plugins/login/cli/ChangePasswordCommand.php
+++ b/user/plugins/login/cli/ChangePasswordCommand.php
@@ -134,9 +134,6 @@ class ChangePasswordCommand extends ConsoleCommand
     {
         switch ($type) {
             case 'user':
-                if (!preg_match('/^[a-z0-9_-]{3,16}$/', $value)) {
-                    throw new \RuntimeException('Username should be between 3 and 16 characters, including lowercase letters, numbers, underscores, and hyphens. Uppercase letters, spaces, and special characters are not allowed');
-                }
                 if (!file_exists(self::getGrav()['locator']->findResource('account://' . $value . YAML_EXT))) {
                     throw new \RuntimeException('Username "' . $value . '" does not exist, please pick another username');
                 }
@@ -144,10 +141,6 @@ class ChangePasswordCommand extends ConsoleCommand
                 break;

             case 'password1':
-                if (!preg_match('/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/', $value)) {
-                    throw new \RuntimeException('Password must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters');
-                }
-
                 break;

             case 'password2':
diff --git a/user/plugins/login/cli/ChangeUserStateCommand.php b/user/plugins/login/cli/ChangeUserStateCommand.php
index 40f9dc6..90adee6 100644
--- a/user/plugins/login/cli/ChangeUserStateCommand.php
+++ b/user/plugins/login/cli/ChangeUserStateCommand.php
@@ -133,9 +133,6 @@ class ChangeUserStateCommand extends ConsoleCommand
     {
         switch ($type) {
             case 'user':
-                if (!preg_match('/^[a-z0-9_-]{3,16}$/', $value)) {
-                    throw new \RuntimeException('Username should be between 3 and 16 characters, including lowercase letters, numbers, underscores, and hyphens. Uppercase letters, spaces, and special characters are not allowed');
-                }
                 if (!file_exists(self::getGrav()['locator']->findResource('account://' . $value . YAML_EXT))) {
                     throw new \RuntimeException('Username "' . $value . '" does not exist, please pick another username');
                 }
diff --git a/user/plugins/login/cli/NewUserCommand.php b/user/plugins/login/cli/NewUserCommand.php
index 17dbf48..728adda 100644
--- a/user/plugins/login/cli/NewUserCommand.php
+++ b/user/plugins/login/cli/NewUserCommand.php
@@ -237,9 +237,6 @@ class NewUserCommand extends ConsoleCommand
     {
         switch ($type) {
             case 'user':
-                if (!preg_match('/^[a-z0-9_-]{3,16}$/', $value)) {
-                    throw new \RuntimeException('Username should be between 3 and 16 characters, including lowercase letters, numbers, underscores, and hyphens. Uppercase letters, spaces, and special characters are not allowed');
-                }
                 if (file_exists(Grav::instance()['locator']->findResource('account://' . $value . YAML_EXT))) {
                     throw new \RuntimeException('Username "' . $value . '" already exists, please pick another username');
                 }
@@ -247,10 +244,6 @@ class NewUserCommand extends ConsoleCommand
                 break;

             case 'password1':
-                if (!preg_match('/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/', $value)) {
-                    throw new \RuntimeException('Password must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters');
-                }
-
                 break;

             case 'password2':

Grav 中已经有修改的方法,无需触及源代码。

如果您进入管理面板,在配置下,有一个名为 "Advanced" 的项目,您可以在其中分别自定义 "Username Regex" 和 "Password Regex"

默认情况下,用户名正则表达式为 ^[a-z0-9_-]{3,16}$,并且可以如@Scuzzy 在评论中所述,更改为 ^(.*)^ 以接受每个字符。

顺便说一句,在安全性和可读性方面,我建议您不要使用该正则表达式,除非您想在您的网站上使用 ^=-èö 之类的非用户名。

我建议您避免直接接触 Grav 源代码,因为每次更新 Grav 时都可以还原所有更改。如果您真的想更改源代码中的某些内容,请考虑将其创建为插件(即使供个人使用),这样您就可以保持更新的能力。