forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
user_service.py
67 lines (49 loc) · 2.02 KB
/
user_service.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
from fastapi import UploadFile
from sqlalchemy.orm import Session
from empire.server.core.db import models
from empire.server.core.download_service import DownloadService
class UserService:
def __init__(self, main_menu):
self.main_menu = main_menu
self.download_service: DownloadService = main_menu.downloadsv2
@staticmethod
def get_all(db: Session):
return db.query(models.User).all()
@staticmethod
def get_by_id(db: Session, uid: int) -> models.User:
return db.query(models.User).filter(models.User.id == uid).first()
@staticmethod
def get_by_name(db: Session, name: str):
return db.query(models.User).filter(models.User.username == name).first()
def create_user(
self, db: Session, username: str, hashed_password: str, admin: bool = False
):
db_user = self.get_by_name(db, username)
if db_user:
return None, f"A user with name {username} already exists."
user = models.User(
username=username,
hashed_password=hashed_password,
enabled=True,
admin=admin,
)
db.add(user)
db.flush()
return user, None
def update_user(self, db: Session, db_user: models.User, user_req):
if user_req.username != db_user.username:
if not self.get_by_name(db, user_req.username):
db_user.username = user_req.username
else:
return None, f"A user with name {user_req.username} already exists."
db_user.enabled = user_req.enabled
db_user.admin = user_req.is_admin
return db_user, None
@staticmethod
def update_user_password(db: Session, db_user: models.User, hashed_password: str):
db_user.hashed_password = hashed_password
db.flush()
return db_user, None
def update_user_avatar(self, db: Session, db_user: models.User, file: UploadFile):
download = self.download_service.create_download(db, db_user, file)
db_user.avatar = download