-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
75 lines (64 loc) · 2.39 KB
/
user.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
"""
owtf.models.user
~~~~~~~~~~~~~~~~
"""
from sqlalchemy import Column, Integer, Unicode, Boolean
from owtf.db.model_base import Model
from owtf.models.email_confirmation import EmailConfirmation
from sqlalchemy.orm import relationship
import uuid
class User(Model):
__tablename__ = "users"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(Unicode(255), nullable=False, unique=True)
email = Column(Unicode(255), nullable=False, unique=True)
password = Column(Unicode(255), nullable=False)
is_active = Column(Boolean, default=False) # checks whether user email is verified
otp_secret_key = Column(Unicode(255), nullable=False, unique=True) # used to generate unique otp
email_confirmations = relationship(EmailConfirmation, cascade="delete")
user_login_tokens = relationship("UserLoginToken", cascade="delete")
@classmethod
def find(cls, session, name):
"""Find a user by name.
Returns None if not found.
"""
return session.query(cls).filter_by(name=name).all()
@classmethod
def find_by_email(cls, session, email):
"""Find a user by email(email is unique).
Returns None if not found.
"""
return session.query(cls).filter_by(email=email).first()
@classmethod
def find_by_name(cls, session, name):
"""Find a user by username(username is unique).
Returns None if not found.
"""
return session.query(cls).filter_by(name=name).first()
@classmethod
def add_user(cls, session, user):
"""Adds an user to the DB"""
new_user = cls(
name=user["name"],
email=user["email"],
password=user["password"].decode("utf-8"),
otp_secret_key=user["otp_secret_key"],
)
session.add(new_user)
session.commit()
@classmethod
def activate_user(cls, session, user_id):
db_obj = session.query(cls).filter_by(id=user_id).first()
db_obj.is_active = True
session.commit()
@classmethod
def find_by_id(cls, session, id):
"""Find a user by id.
Returns None if not found.
"""
return session.query(cls).filter_by(id=id).first()
@classmethod
def change_password(cls, session, email, password):
db_obj = session.query(cls).filter_by(email=email).first()
db_obj.password = password
session.commit()