Skip to content

Commit f86da93

Browse files
committed
Add new Account Type Admin
* In preparation for having an administration page, a new account type was added. The type was supplemented by a unit test and the ability to check whether a user is an administrator. The Open API test was adapted and a simplified export was added to enable further changes more quickly. [#2](Gallimathias#2)
1 parent 9308d0c commit f86da93

9 files changed

Lines changed: 6489 additions & 6355 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Add new Admin Account Type
2+
3+
Revision ID: 899415b142b1
4+
Revises: c1ab44651e79
5+
Create Date: 2026-01-25 15:24:12.211612
6+
7+
"""
8+
9+
import sqlalchemy as sa
10+
11+
from alembic import op
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str | None = '899415b142b1'
16+
down_revision: str | None = 'c1ab44651e79'
17+
branch_labels: str | None = None
18+
depends_on: str | None = None
19+
20+
21+
def upgrade() -> None:
22+
# ### commands auto generated by Alembic - please adjust! ###
23+
op.execute("ALTER TYPE account_type ADD VALUE 'ADMIN'")
24+
# ### end Alembic commands ###
25+
26+
27+
def downgrade() -> None:
28+
# ### commands auto generated by Alembic - please adjust! ###
29+
op.execute("ALTER TYPE account_type DROP VALUE 'ADMIN'")
30+
# ### end Alembic commands ###

backend/bracket/logic/subscriptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ class Subscription(BaseModel):
5656
max_rankings=16,
5757
)
5858

59+
# Just for now until evroon decide what admins are allowed to do
60+
admin_subscription = regular_subscription
61+
5962
subscription_lookup = {
6063
UserAccountType.DEMO: demo_subscription,
6164
UserAccountType.REGULAR: regular_subscription,
65+
UserAccountType.ADMIN: admin_subscription,
6266
}
6367

6468

backend/bracket/models/db/account.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
class UserAccountType(EnumAutoStr):
77
REGULAR = auto()
88
DEMO = auto()
9+
ADMIN = auto()

backend/bracket/routes/auth.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from bracket.database import database
1313
from bracket.models.db.tournament import Tournament
1414
from bracket.models.db.user import UserInDB, UserPublic
15+
from bracket.models.db.account import UserAccountType
1516
from bracket.schema import tournaments
1617
from bracket.sql.tournaments import sql_get_tournament_by_endpoint_name
1718
from bracket.sql.users import get_user, get_user_access_to_club, get_user_access_to_tournament
@@ -97,6 +98,24 @@ async def user_authenticated(token: str = Depends(oauth2_scheme)) -> UserPublic:
9798

9899
return UserPublic.model_validate(user.model_dump())
99100

101+
async def user_is_admin(token: str = Depends(oauth2_scheme)) -> UserPublic:
102+
user = await check_jwt_and_get_user(token)
103+
if not user:
104+
raise HTTPException(
105+
status_code=status.HTTP_401_UNAUTHORIZED,
106+
detail="Could not validate credentials",
107+
headers={"WWW-Authenticate": "Bearer"},
108+
)
109+
110+
if user.account_type != UserAccountType.ADMIN:
111+
raise HTTPException(
112+
status_code=status.HTTP_403_FORBIDDEN,
113+
detail="You do not have enough privileges",
114+
headers={"WWW-Authenticate": "Bearer"},
115+
)
116+
117+
return UserPublic.model_validate(user.model_dump())
118+
100119

101120
async def user_authenticated_for_tournament(
102121
tournament_id: TournamentId, token: str = Depends(oauth2_scheme)

backend/bracket/utils/db_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async def create_admin_user() -> UserId:
106106
email=config.admin_email,
107107
password_hash=hash_password(config.admin_password),
108108
created=datetime_utc.now(),
109-
account_type=UserAccountType.REGULAR,
109+
account_type=UserAccountType.ADMIN,
110110
)
111111
)
112112
return user.id

backend/export_openapi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import json
2+
from bracket.app import app
3+
from openapi import openapi # noqa: F401
4+
5+
openapi_data = app.openapi()
6+
7+
with open("openapi/openapi.json", "w", encoding="utf-8") as f:
8+
json.dump(openapi_data, f, indent=2, sort_keys=True)
9+
10+
print("✅ openapi.json has been successfully exported!")

0 commit comments

Comments
 (0)