fix: error on duplicate username/email registration (#1168)

* fix: error on duplicate username/email registration

* Raise 409 http error on duplicate user registration

* Add translation to conflict error messages

* Update lang files

* Remove key in translator parameter
This commit is contained in:
Miroito 2022-04-30 22:25:29 +02:00 committed by GitHub
parent 633ec541ea
commit 67178f9b74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 106 additions and 33 deletions

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "Ce nom d'utilisateur est déjà pris",
"email-conflict-error": "Cet email existe déjà"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "Ce nom d'utilisateur est déjà pris",
"email-conflict-error": "Cet email existe déjà"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -8,6 +8,8 @@
"exceptions": {
"permission_denied": "You do not have permission to perform this action",
"no-entry-found": "The requested resource was not found",
"integrity-error": "Database integrity error"
"integrity-error": "Database integrity error",
"username-conflict-error": "This username is already taken",
"email-conflict-error": "This email is already in use"
}
}

View file

@ -4,6 +4,7 @@ from uuid import uuid4
from fastapi import HTTPException, status
from mealie.core.security import hash_password
from mealie.lang import local_provider
from mealie.repos.repository_factory import AllRepositories
from mealie.schema.group.group_preferences import CreateGroupPreferences
from mealie.schema.user.registration import CreateUserRegistration
@ -18,6 +19,7 @@ class RegistrationService:
def __init__(self, logger: Logger, db: AllRepositories):
self.logger = logger
self.repos = db
self.t = local_provider()
def _create_new_user(self, group: GroupInDB, new_group: bool) -> PrivateUser:
new_user = UserIn(
@ -54,6 +56,11 @@ class RegistrationService:
def register_user(self, registration: CreateUserRegistration) -> PrivateUser:
self.registration = registration
if self.repos.users.get_by_username(registration.username):
raise HTTPException(status.HTTP_409_CONFLICT, {"message": self.t.t("exceptions.username-conflict-error")})
elif self.repos.users.get(registration.email, "email"):
raise HTTPException(status.HTTP_409_CONFLICT, {"message": self.t.t("exceptions.email-conflict-error")})
self.logger.info(f"Registering user {registration.username}")
token_entry = None
new_group = False