Search code examples
phpcontent-management-systemgrav

How to disable username validation in Grav?


Grav version is 1.1.5

I removed every validation logic and validation pattern in source code, but it does not work, I still got error when I create a user.

The error message name is USERNAME_NOT_VALID, I search this in all source code and delete related code, but it still display on screen when I got error. I don't understand where the message from.

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':

Solution

  • There is already a way to change that is directly included in Grav without touching the source code.

    If you go in admin panel, under Configuration, there is an item called "Advanced" where you can customize respectively "Username Regex" and "Password Regex"

    By default, username regex is ^[a-z0-9_-]{3,16}$ and can be, as stated by @Scuzzy in a comment, changed to ^(.*)^ to accept every character.

    BTW security and readability wise, I recommend you to not use that regex unless you want unsernames like ^=-èö on your website.

    I recommend you to avoid touching Grav source code directly, because every changes can be reverted everytime you update Grav. If you really want to change something in the source, consider creating it as a plugin (even for personnal use) so you keep the ability to update.