forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 576
/
profile_api.py
114 lines (88 loc) · 2.84 KB
/
profile_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from fastapi import Depends, HTTPException
from starlette.responses import Response
from starlette.status import HTTP_204_NO_CONTENT
from empire.server.api.api_router import APIRouter
from empire.server.api.jwt_auth import get_current_active_user
from empire.server.api.v2.profile.profile_dto import (
Profile,
ProfilePostRequest,
Profiles,
ProfileUpdateRequest,
)
from empire.server.api.v2.shared_dependencies import CurrentSession
from empire.server.api.v2.shared_dto import BadRequestResponse, NotFoundResponse
from empire.server.core.db import models
from empire.server.server import main
profile_service = main.profilesv2
router = APIRouter(
prefix="/api/v2/malleable-profiles",
tags=["malleable-profiles"],
responses={
404: {"description": "Not found", "model": NotFoundResponse},
400: {"description": "Bad request", "model": BadRequestResponse},
},
dependencies=[Depends(get_current_active_user)],
)
async def get_profile(uid: int, db: CurrentSession):
profile = profile_service.get_by_id(db, uid)
if profile:
return profile
raise HTTPException(status_code=404, detail=f"Profile not found for id {uid}")
@router.get("/{uid}", response_model=Profile)
async def read_profile(uid: int, db_profile: models.Profile = Depends(get_profile)):
return db_profile
@router.get("/", response_model=Profiles)
async def read_profiles(db: CurrentSession):
profiles = profile_service.get_all(db)
return {"records": profiles}
@router.post(
"/",
status_code=201,
response_model=Profile,
)
async def create_profile(profile_req: ProfilePostRequest, db: CurrentSession):
resp, err = profile_service.create_profile(db, profile_req)
if err:
raise HTTPException(status_code=400, detail=err)
return resp
@router.put("/{uid}", response_model=Profile)
async def update_profile(
uid: int,
profile_req: ProfileUpdateRequest,
db: CurrentSession,
db_profile: models.Profile = Depends(get_profile),
):
resp, err = profile_service.update_profile(db, db_profile, profile_req)
if err:
raise HTTPException(status_code=400, detail=err)
return resp
@router.delete(
"/{uid}",
status_code=HTTP_204_NO_CONTENT,
response_class=Response,
)
async def delete_profile(
uid: str,
db: CurrentSession,
db_profile: models.Profile = Depends(get_profile),
):
profile_service.delete_profile(db, db_profile)
@router.post(
"/reload",
status_code=HTTP_204_NO_CONTENT,
response_class=Response,
)
async def reload_profiles(
db: CurrentSession,
):
profile_service.load_malleable_profiles(db)
@router.post(
"/reset",
status_code=HTTP_204_NO_CONTENT,
response_class=Response,
)
async def reset_profiles(
db: CurrentSession,
):
profile_service.delete_all_profiles(db)
profile_service.load_malleable_profiles(db)