File size: 4,272 Bytes
b7a7f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from typing import Any, Dict, Optional, Union

from fastapi import HTTPException
from sqlalchemy.orm import Session

from core.config import settings
from core.permission.permission import check_permission
from core.security import get_password_hash, verify_password
from cruds.base import CRUDBase
from cruds.group import crud_group
from models import association_tables
from models.user import User
from schemas.user import UserCreate, UserUpdate
from models.association_tables import TeacherGroupCourseAssociation


class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
    def get_by_email(self, db: Session, *, email: str) -> Optional[User]:
        return db.query(User).filter(User.email == email).first()

    def get_by_email_test(
        self,
        db: Session,
        *,
        email: str,
    ) -> Optional[User]:
        return db.query(User).filter(User.email == email).first()

    def get_by_id(self, db: Session, *, id: int) -> Optional[User]:
        return db.query(User).filter(User.id == id).first()

    def create(
        self,
        db: Session,
        *,
        obj_in: UserCreate,
    ) -> User:
        db_obj = User(
            email=obj_in.email,  # noqa
            hashed_password=get_password_hash(obj_in.password),  # noqa
            roll=obj_in.roll,
            full_name=obj_in.full_name,  # noqa
            dob=obj_in.dob,  # noqa
            teacher_department_id=obj_in.teacher_department_id,  # noqa
            group_id=obj_in.group_id,  # noqa
            user_type=obj_in.user_type,  # noqa
            contact_number=obj_in.contact_number,  # noqa
            address=obj_in.address,  # noqa
            join_year=obj_in.join_year,  # noqa
        )
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)

        if obj_in.teacher_group:
            for item in obj_in.teacher_group:
                association_obj = TeacherGroupCourseAssociation(
                    teacher_id=db_obj.id,
                    group_id=item[0],
                    course_id=item[1],
                )
                db.add(association_obj)
                db.commit()
                db.refresh(association_obj)
                # teacher_group = [
                #     crud_group.get(db=db, id=id) for id in obj_in.teacher_group
                # ]
        db.refresh(db_obj)
        return db_obj

    def verify_user(
        self,
        db: Session,
        *,
        db_obj: User,
    ):
        super().update(db=db, db_obj=db_obj, obj_in={"is_active": True})

    def enable_2fa(
        self,
        db: Session,
        *,
        secret: str,
        db_obj: User,
    ):
        super().update(db=db, db_obj=db_obj, obj_in={"two_fa_secret": secret})

    def disable_2fa(
        self,
        db: Session,
        *,
        db_obj: User,
    ):
        super().update(db=db, db_obj=db_obj, obj_in={"two_fa_secret": None})

    def update(
        self,
        db: Session,
        *,
        db_obj: User,
        obj_in: Union[UserUpdate, Dict[str, Any]],
    ) -> User:
        if isinstance(obj_in, dict):
            update_data = obj_in
        else:
            update_data = obj_in.dict(exclude_unset=True)

        if "password" in update_data:
            hashed_password = get_password_hash(update_data["password"])
            del update_data["password"]
            update_data["hashed_password"] = hashed_password
        # if (
        #     update_data.get("permissions")
        #     and db_obj.user_type > settings.UserType.ADMIN.value
        # ):
        #     raise HTTPException(403, detail="Error ID: 136")  # Request denied
        return super().update(db, db_obj=db_obj, obj_in=update_data)

    def authenticate(self, db: Session, *, email: str, password: str) -> Optional[User]:
        user = self.get_by_email(db, email=email)
        if not user:
            return None
        if not verify_password(password, user.hashed_password):
            return None
        return user

    def is_active(self, user: User) -> bool:
        return user.is_active

    def is_superuser(self, user: User) -> bool:
        if user.user_type == settings.UserType.SUPERADMIN.value:
            return True
        else:
            return False


crud_user = CRUDUser(User)