diff options
author | 2024-08-27 22:18:34 +0100 | |
---|---|---|
committer | 2024-08-27 22:24:43 +0100 | |
commit | cb575b76dd4677a3905333179aa3f6c6e2e99162 (patch) | |
tree | 4acb758c86eb2c9f92752df4db866fd5ef455512 | |
parent | Remove unused IntFlag (diff) |
Add columns for storing user auth info
-rw-r--r-- | thallium-backend/migrations/versions/1724787838-e070d9f5f64a_user_auth_columns.py | 47 | ||||
-rw-r--r-- | thallium-backend/src/orm/users.py | 15 |
2 files changed, 61 insertions, 1 deletions
diff --git a/thallium-backend/migrations/versions/1724787838-e070d9f5f64a_user_auth_columns.py b/thallium-backend/migrations/versions/1724787838-e070d9f5f64a_user_auth_columns.py new file mode 100644 index 0000000..a70bffa --- /dev/null +++ b/thallium-backend/migrations/versions/1724787838-e070d9f5f64a_user_auth_columns.py @@ -0,0 +1,47 @@ +""" +Add columns to manage user auth. + +Revision ID: e070d9f5f64a +Revises: d74a6956b012 +Create Date: 2024-08-27 19:43:58.289807+00:00 +""" + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "e070d9f5f64a" +down_revision = "d74a6956b012" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + """Apply this migration.""" + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("users", sa.Column("username", sa.String(), nullable=False)) + op.add_column("users", sa.Column("password_hash", sa.String(), nullable=False)) + op.add_column("users", sa.Column("require_password_change", sa.Boolean(), nullable=False)) + op.add_column("users", sa.Column("password_reset_code", sa.String(), nullable=True)) + op.add_column("users", sa.Column("active", sa.Boolean(), nullable=False)) + op.add_column("users", sa.Column("password_set_at", sa.TIMESTAMP(timezone=True), nullable=False)) + op.create_unique_constraint(op.f("users_username_uq"), "users", ["username"]) + op.create_check_constraint( + constraint_name=op.f("users_pass_req_change_has_code_ck"), + table_name="users", + condition="require_password_change = (password_reset_code is not null)", + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Revert this migration.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(op.f("users_username_uq"), "users", type_="unique") + op.drop_column("users", "active") + op.drop_column("users", "password_reset_code") + op.drop_column("users", "require_password_change") + op.drop_column("users", "password_hash") + op.drop_column("users", "username") + op.drop_column("users", "password_set_time_at") + # ### end Alembic commands ### diff --git a/thallium-backend/src/orm/users.py b/thallium-backend/src/orm/users.py index 82c46c0..1aa0bfe 100644 --- a/thallium-backend/src/orm/users.py +++ b/thallium-backend/src/orm/users.py @@ -1,4 +1,7 @@ -from sqlalchemy.orm import Mapped +from datetime import datetime + +from sqlalchemy import CheckConstraint, TIMESTAMP +from sqlalchemy.orm import Mapped, mapped_column from .base import AuditBase, Base, UUIDBase @@ -8,4 +11,14 @@ class User(UUIDBase, AuditBase, Base): __tablename__ = "users" + username: Mapped[str] = mapped_column(unique=True) + password_hash: Mapped[str] permissions: Mapped[int] + password_set_at: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True)) + require_password_change: Mapped[bool] = mapped_column(default=True) + password_reset_code: Mapped[str] = mapped_column(nullable=True) + active: Mapped[bool] = mapped_column(default=True) + + __table_args__ = ( + CheckConstraint("require_password_change = (password_reset_code is not null)", name="pass_req_change_has_code"), + ) |