Skip to content

Commit 3a15531

Browse files
committed
feat: add lexicon permissions dependencies for editor and admin roles
1 parent 4108f74 commit 3a15531

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

api/lexicon.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
editor_dependency,
3131
admin_dependency,
3232
viewer_function,
33+
lexicon_editor_dependency,
34+
lexicon_admin_dependency,
3335
)
3436
from db.lexicon import (
3537
LexiconCategory,
@@ -58,7 +60,7 @@
5860
)
5961

6062
router = APIRouter(
61-
prefix="/lexicon", tags=["lexicon"], dependencies=[Depends(viewer_function)]
63+
prefix="/lexicon", tags=["lexicon"]
6264
)
6365

6466

@@ -105,7 +107,7 @@ def database_error_handler(
105107
async def add_category(
106108
category_data: CreateLexiconCategory,
107109
session: session_dependency,
108-
user: admin_dependency,
110+
user: lexicon_admin_dependency,
109111
) -> LexiconCategoryResponse:
110112
"""
111113
Endpoint to add a category to the lexicon.
@@ -119,7 +121,7 @@ async def add_category(
119121
status_code=HTTP_201_CREATED,
120122
)
121123
async def add_term(
122-
term_data: CreateLexiconTerm, session: session_dependency, user: admin_dependency
124+
term_data: CreateLexiconTerm, session: session_dependency, user: lexicon_admin_dependency
123125
) -> LexiconTermResponse:
124126
"""
125127
Endpoint to add a term to the lexicon.
@@ -138,7 +140,7 @@ async def add_term(
138140
async def add_triple(
139141
triple_data: CreateLexiconTriple,
140142
session: session_dependency,
141-
user: admin_dependency,
143+
user: lexicon_admin_dependency,
142144
) -> LexiconTripleResponse:
143145
triple_data = triple_data.model_dump()
144146
subject = triple_data["subject"]
@@ -155,7 +157,7 @@ async def update_lexicon_term(
155157
term_id: int,
156158
term_data: UpdateLexiconTerm,
157159
session: session_dependency,
158-
user: editor_dependency,
160+
user: lexicon_editor_dependency,
159161
) -> LexiconTermResponse:
160162

161163
return model_patcher(session, LexiconTerm, term_id, term_data, user=user)
@@ -166,7 +168,7 @@ async def update_lexicon_category(
166168
category_id: int,
167169
category_data: UpdateLexiconCategory,
168170
session: session_dependency,
169-
user: editor_dependency,
171+
user: lexicon_editor_dependency,
170172
) -> LexiconCategoryResponse:
171173
return model_patcher(
172174
session, LexiconCategory, category_id, category_data, user=user
@@ -178,7 +180,7 @@ async def update_lexicon_triple(
178180
triple_id: int,
179181
triple_data: UpdateLexiconTriple,
180182
session: session_dependency,
181-
user: editor_dependency,
183+
user: lexicon_editor_dependency,
182184
) -> LexiconTripleResponse:
183185
try:
184186
return model_patcher(session, LexiconTriple, triple_id, triple_data, user=user)

core/dependencies.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# ===============================================================================
1616
from typing import Annotated, Callable
1717

18-
from fastapi import Depends
18+
from fastapi import Depends, HTTPException
1919
from sqlalchemy.orm import Session
2020

2121
from core.permissions import authenticated
@@ -44,12 +44,27 @@
4444
# for testing
4545
no_permission_function = authenticated(permissions=["NoPermission"])
4646

47+
lexicon_editor = authenticated(permissions=["LexiconEditor"])
48+
lexicon_admin = authenticated(permissions=["LexiconAdmin"])
49+
def _unauthorized(func):
50+
user = func()
51+
if not user:
52+
raise HTTPException(status_code=401, detail="Unauthorized")
53+
54+
def lexicon_admin_function():
55+
return _unauthorized(lexicon_admin)
56+
57+
def lexicon_editor_function():
58+
return _unauthorized(lexicon_editor)
59+
4760

4861
# permissions dependencies
4962
admin_dependency = Annotated[Callable, Depends(admin_function)]
5063
editor_dependency = Annotated[Callable, Depends(editor_function)]
5164
viewer_dependency = Annotated[Callable, Depends(viewer_function)]
5265

66+
lexicon_admin_dependency = Annotated[Callable, Depends(lexicon_admin_function)]
67+
lexicon_editor_dependency = Annotated[Callable, Depends(lexicon_editor_function)]
5368

5469
amp_admin_dependency = Annotated[Callable, Depends(amp_admin_function)]
5570
amp_editor_dependency = Annotated[Callable, Depends(amp_editor_function)]

0 commit comments

Comments
 (0)