Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions components/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,21 @@ public function onRegister()

$rules = (new UserModel)->rules;

if ($this->loginAttribute() !== UserSettings::LOGIN_USERNAME) {
unset($rules['username']);
switch ($this->loginAttribute()) {
case UserSettings::LOGIN_EMAIL:
unset($rules['username']);
$user = UserModel::findByEmail(post('email'));
if ($user && $user->is_guest) {
$rules['email'] = 'required|between:6,255|email';
}
break;
case UserSettings::LOGIN_USERNAME:
unset($rules['email']);
$user = UserModel::findByUsername(post('username'));
if ($user && $user->is_guest) {
$rules['username'] = 'required|between:6,255';
}
break;
}
Comment on lines +299 to 314
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add a default branch to keep registration resilient to invalid config values.

If login_attribute is ever an unexpected value, neither field is unset and registration validation becomes stricter than intended.

💡 Proposed fix
             switch ($this->loginAttribute()) {
                 case UserSettings::LOGIN_EMAIL:
                     unset($rules['username']);

                     $email = post('email');
                     if (UserModel::where('email', $email)->where('is_guest', 1)->count()) {
                         $rules['email'] = 'required|between:6,255|email';
                     }
                     break;
                 case UserSettings::LOGIN_USERNAME:
                     unset($rules['email']);
                     $username = post('username');
                     if (UserModel::where('username', $username)->where('is_guest', 1)->count()) {
                         $rules['username'] = 'required|between:2,255|username';
                     }
                     break;
+                default:
+                    unset($rules['username']);
+                    break;
             }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
switch ($this->loginAttribute()) {
case UserSettings::LOGIN_EMAIL:
unset($rules['username']);
$email = post('email');
if (UserModel::where('email', $email)->where('is_guest', 1)->count()) {
$rules['email'] = 'required|between:6,255|email';
}
break;
case UserSettings::LOGIN_USERNAME:
unset($rules['email']);
$username = post('username');
if (UserModel::where('username', $username)->where('is_guest', 1)->count()) {
$rules['username'] = 'required|between:6,255|username';
}
break;
}
switch ($this->loginAttribute()) {
case UserSettings::LOGIN_EMAIL:
unset($rules['username']);
$email = post('email');
if (UserModel::where('email', $email)->where('is_guest', 1)->count()) {
$rules['email'] = 'required|between:6,255|email';
}
break;
case UserSettings::LOGIN_USERNAME:
unset($rules['email']);
$username = post('username');
if (UserModel::where('username', $username)->where('is_guest', 1)->count()) {
$rules['username'] = 'required|between:6,255|username';
}
break;
default:
unset($rules['username']);
break;
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@components/Account.php` around lines 299 - 315, The switch over
$this->loginAttribute() lacks a default branch causing both 'email' and
'username' rules to remain if the config is invalid; add a default case that
treats the setting as LOGIN_EMAIL (or otherwise a safe fallback) — e.g.,
unset($rules['username']) and optionally log a warning — so registration
validation remains permissive; update the switch in the Account component that
checks UserSettings::LOGIN_EMAIL and UserSettings::LOGIN_USERNAME to include
this default branch.


$validation = Validator::make($data, $rules);
Expand Down
13 changes: 13 additions & 0 deletions models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ public static function findByEmail($email)
return self::where('email', $email)->first();
}

/**
* Looks up a user by their username.
* @return self
*/
public static function findByUsername($username)
{
if (!$username) {
return;
}

return self::where('username', $username)->first();
}

//
// Getters
//
Expand Down
Loading