Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Set users email to null if empty #1490

Merged
merged 1 commit into from
Jan 14, 2025
Merged
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
25 changes: 25 additions & 0 deletions backend/alembic/versions/0030_user_email_null.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Change empty string in users.email to NULL.

Revision ID: 951473b0c581
Revises: 0029_platforms_custom_name
Create Date: 2025-01-14 01:30:39.696257

"""

from alembic import op

# revision identifiers, used by Alembic.
revision = "0030_user_email_null"
down_revision = "0029_platforms_custom_name"
branch_labels = None
depends_on = None


def upgrade() -> None:
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.execute("UPDATE users SET email = NULL WHERE email = ''")


def downgrade() -> None:
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.execute("UPDATE users SET email = '' WHERE email IS NULL")
23 changes: 11 additions & 12 deletions backend/endpoints/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Annotated
from typing import Annotated, Any

from anyio import open_file
from config import ASSETS_BASE_PATH
Expand Down Expand Up @@ -51,18 +51,16 @@ def add_user(
detail="Forbidden",
)

existing_user_by_username = db_user_handler.get_user_by_username(username.lower())
if existing_user_by_username:
if db_user_handler.get_user_by_username(username.lower()):
msg = f"Username {username.lower()} already exists"
log.error(msg)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=msg,
)

existing_user_by_email = db_user_handler.get_user_by_email(email.lower())
if existing_user_by_email:
msg = f"Uesr with email {email.lower()} already exists"
if email and db_user_handler.get_user_by_email(email.lower()):
msg = f"User with email {email.lower()} already exists"
log.error(msg)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
Expand All @@ -72,7 +70,7 @@ def add_user(
user = User(
username=username.lower(),
hashed_password=auth_handler.get_password_hash(password),
email=email.lower(),
email=email.lower() or None,
role=Role[role.upper()],
)

Expand Down Expand Up @@ -154,7 +152,7 @@ async def update_user(
if db_user.id != request.user.id and request.user.role != Role.ADMIN:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden")

cleaned_data = {}
cleaned_data: dict[str, Any] = {}

if form_data.username and form_data.username != db_user.username:
existing_user = db_user_handler.get_user_by_username(form_data.username.lower())
Expand All @@ -173,17 +171,18 @@ async def update_user(
form_data.password
)

if form_data.email and form_data.email != db_user.email:
existing_user = db_user_handler.get_user_by_email(form_data.email.lower())
if existing_user:
if form_data.email is not None and form_data.email != db_user.email:
if form_data.email and db_user_handler.get_user_by_email(
form_data.email.lower()
):
msg = f"User with email {form_data.email} already exists"
log.error(msg)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=msg,
)

cleaned_data["email"] = form_data.email.lower()
cleaned_data["email"] = form_data.email.lower() or None

# You can't change your own role
if form_data.role and request.user.id != id:
Expand Down
Loading