statamic / v2-hub

Statamic 2 - Feature Requests and Bug Reports
https://statamic.com
95 stars 5 forks source link

Registering with same username but different capitalization throws exception (users in mariadb) #2484

Open hostage-nl opened 4 years ago

hostage-nl commented 4 years ago

Describe the bug When users ar in a database (mariadb in my case), a user who registers with an username thats already in use, but with different capatals, results in an exception

So when user 'michael' exists, and you try to register 'Michael', there's a problem. When you try to reregister 'michael' you get the proper error message "The selected username is taken'.

To Reproduce Steps to reproduce the behavior:

  1. Go to registration page
  2. Fill in everything and use an existing username, but with different capitilization
  3. Click submit
  4. See the exception

Expected behavior You should see the propper error message: 'The selected username is taken'

Environment details (please complete the following information):

hostage-nl commented 4 years ago

My fix:

diff --git a/statamic/bundles/User/UserRegistrar.php b/statamic/bundles/User/UserRegistrar.php
index 83c5d8b..3ba369f 100644
--- a/statamic/bundles/User/UserRegistrar.php
+++ b/statamic/bundles/User/UserRegistrar.php
@@ -43,7 +43,7 @@ class UserRegistrar
     {
         $builder = $this->validationBuilder();

-        return Validator::make(['fields' => $this->fields], $builder->rules(), [], $builder->attributes());
+        return Validator::make (['fields' => $this->fields], $builder->rules(), [], $builder->attributes());
     }

     /**
@@ -74,7 +74,9 @@ class UserRegistrar
         $this->adjustFieldset();

         // Remove any unwanted request input to be validated
-        $this->fields = $this->request->except('redirect');
+        $this->fields = collect($this->request->except('redirect'))->map(function ($value, $key) {
+            return $key == "username" ? strtolower($value) : $value;
+        })->filter()->all();

         // Build the validation rules/attributes based on the user fieldset
         $builder = new ValidationBuilder(['fields' => $this->fields], $this->fieldset);
@@ -95,7 +97,7 @@ class UserRegistrar
         $username_rules = array_get($fields, 'username.validate');

         // Ensure the username is unique.
-        $username_rules = ltrim($username_rules . '|not_in:' . User::pluck('username')->implode(','), '|');
+        $username_rules = ltrim($username_rules . '|not_in:' . strtolower(User::pluck('username')->implode(',')), '|');

         // Ensure the username field is required. We'll break it into an array and rejoin it so
         // we can avoid duplication if the fieldset already contained required validation.