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

Add a remove permission route #157

Merged
merged 1 commit into from
Mar 24, 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
5 changes: 5 additions & 0 deletions api_schemas/permission_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class PermissionCreate(BaseSchema):
target: PERMISSION_TARGET


class PermissionRemove(BaseSchema):
action: PERMISSION_TYPE
target: PERMISSION_TARGET


class UpdatePermission(BaseSchema):
post_id: int
change: Literal["add", "remove"]
Expand Down
4 changes: 3 additions & 1 deletion db_models/permission_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Permission_DB(BaseModel_DB):

action: Mapped[PERMISSION_TYPE] = mapped_column(String(160))

post_permissions: Mapped[list["PostPermission_DB"]] = relationship(back_populates="permission", init=False)
post_permissions: Mapped[list["PostPermission_DB"]] = relationship(
back_populates="permission", init=False, cascade="all, delete-orphan"
)

target: Mapped[PERMISSION_TARGET] = mapped_column(String(160))
20 changes: 17 additions & 3 deletions routes/permission_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from database import DB_dependency, get_db
from db_models.permission_model import Permission_DB
from db_models.post_model import Post_DB
from api_schemas.permission_schemas import PermissionCreate, PermissionRead, UpdatePermission
from api_schemas.permission_schemas import PermissionCreate, PermissionRead, UpdatePermission, PermissionRemove
from services.permission_service import assign_permission, unassign_permission
from user.permission import Permission

Expand All @@ -29,7 +29,7 @@ def create_permission(perm_data: PermissionCreate, db: Annotated[Session, Depend
)
# if there already is this exact permission, dont create
if num_existing > 0:
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="Ths permission already exists")
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="This permission already exists")

perm = Permission_DB(action=perm_data.action, target=perm_data.target)
db.add(perm)
Expand All @@ -52,4 +52,18 @@ def change_post_permission(perm_data: UpdatePermission, db: DB_dependency):
return post


# TODO make the delete permission route.
# Remove a permission completely
@permission_router.delete("/", response_model=PermissionRead, dependencies=[Permission.require("manage", "Permission")])
def remove_permission(perm_data: PermissionRemove, db: Annotated[Session, Depends(get_db)]):
perm = (
db.query(Permission_DB)
.filter(Permission_DB.action == perm_data.action, Permission_DB.target == perm_data.target)
.one_or_none()
)

if perm == None:
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="No such permission exists")

db.delete(perm)
db.commit()
return perm