Skip to content

Commit

Permalink
🐛 Enforce case-insensitive username uniqueness (#557)
Browse files Browse the repository at this point in the history
* add migration for case insensitive username

* fix form validation
  • Loading branch information
aaronleopold authored Jan 10, 2025
1 parent f32244d commit 1331426
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_users" (
"id" TEXT NOT NULL PRIMARY KEY,
"username" TEXT NOT NULL COLLATE NOCASE,
"hashed_password" TEXT NOT NULL,
"is_server_owner" BOOLEAN NOT NULL DEFAULT false,
"avatar_url" TEXT,
"last_login" DATETIME,
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"deleted_at" DATETIME,
"is_locked" BOOLEAN NOT NULL DEFAULT false,
"max_sessions_allowed" INTEGER,
"permissions" TEXT,
"user_preferences_id" TEXT,
CONSTRAINT "users_user_preferences_id_fkey" FOREIGN KEY ("user_preferences_id") REFERENCES "user_preferences" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_users" ("avatar_url", "created_at", "deleted_at", "hashed_password", "id", "is_locked", "is_server_owner", "last_login", "max_sessions_allowed", "permissions", "user_preferences_id", "username") SELECT "avatar_url", "created_at", "deleted_at", "hashed_password", "id", "is_locked", "is_server_owner", "last_login", "max_sessions_allowed", "permissions", "user_preferences_id", "username" FROM "users";
DROP TABLE "users";
ALTER TABLE "new_users" RENAME TO "users";
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
CREATE UNIQUE INDEX "users_user_preferences_id_key" ON "users"("user_preferences_id");
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const buildSchema = (
.refine(
(value) =>
(!!editingUser && value === editingUser.username) ||
existingUsers.every((user) => user.username !== value),
existingUsers.every((user) => user.username.toLowerCase() !== value.toLowerCase()),
() => ({
message: t(
'settingsScene.server/users.createOrUpdateForm.validation.usernameAlreadyExists',
Expand Down
5 changes: 4 additions & 1 deletion packages/i18n/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,10 @@
}
},
"validation": {
"ageRestrictionTooLow": "Age restriction cannot be less than 0"
"ageRestrictionTooLow": "Age restriction cannot be less than 0",
"missingUsername": "Username is required",
"missingPassword": "Password is required",
"usernameAlreadyExists": "Username already exists"
},
"createSubmitButton": "Create user",
"updateSubmitButton": "Update user"
Expand Down

0 comments on commit 1331426

Please sign in to comment.