forked from thebaultyoann/CLI-Admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
167 lines (139 loc) · 5.26 KB
/
database.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Date
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
Base = declarative_base()
#User class inside the database
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String)
password_hashed = Column(String)
activated = Column(Boolean)
expiration_date = Column(Date)
#Open a session
def start_a_db_session(
DB_Username_For_Admin:str,
DB_Password_For_Admin:str,
DB_Name_For_Users_Tables:str,
DB_Container_Name:str,
):
engine = create_engine("mariadb+mariadbconnector://"+DB_Username_For_Admin+":"+DB_Password_For_Admin+"@"+DB_Container_Name+":3306/"+DB_Name_For_Users_Tables)
Session = sessionmaker(bind=engine)
return Session()
#Check if the
# user is connected or not
def test_credentials(
DB_Username_For_Admin:str,
DB_Password_For_Admin:str,
DB_Name_For_Users_Tables:str,
DB_Container_Name:str,
):
engine = create_engine("mariadb+mariadbconnector://"+DB_Username_For_Admin+":"+DB_Password_For_Admin+"@"+DB_Container_Name+":3306/"+DB_Name_For_Users_Tables)
engine.connect()
engine.dispose()
return
################ ALL REQUESTS INSIDE THE DB ################
def get_all_users(session: Session):
return session.query(User).all()
def get_a_single_user(session: Session, username):
return session.query(User).filter(User.username == username).first()
def get_users_by_activation(session: Session,status):
return session.query(User).filter((User.activated == status)).all()
def delete_user(session: Session, username):
user = session.query(User).filter(User.username == username).first()
if user:
session.delete(user)
session.commit()
return user
else:
return None
def add_user(session: Session, username, password, activated, expirationDate):
user = session.query(User).filter(User.username == username).first()
if user:
return False
else:
new_user = User(username=username, password_hashed=password, activated=activated, expiration_date=expirationDate)
session.add(new_user)
session.commit()
return True
def update_user_username(session: Session, username, newUsername):
user = session.query(User).filter(User.username == username).first()
if user:
user.username = newUsername
session.add(user)
session.commit()
return True
return False
def update_user_password(session: Session, username, password):
user = session.query(User).filter(User.username == username).first()
if user:
user.password_hashed = password
session.add(user)
session.commit()
return True
return False
def update_user_activated(session: Session, username, activated):
user = session.query(User).filter(User.username == username).first()
if user:
user.activated = activated
session.add(user)
session.commit()
return True
return False
def update_user_expiration_date(session: Session, username, expirationDate):
user = session.query(User).filter(User.username == username).first()
if user:
user.expiration_date = expirationDate
session.add(user)
session.commit()
return True
return False
def activate_user(session: Session, username):
user = session.query(User).filter(User.username == username).first()
if user.activated == False:
user.activated = True
session.add(user)
session.commit()
return True
else:
return False
def deactivate_user(session: Session, username):
user = session.query(User).filter(User.username == username).first()
if user.activated == True:
user.activated = False
session.add(user)
session.commit()
return True
else:
return False
def user_expiration_date(session: Session, username):
user = session.query(User).filter(User.username == username).first()
return user.expiration_date
def check_expiration_date(session: Session, username, expirationDate):
user = session.query(User).filter(User.username == username).first()
if not user.expiration_date or user.expiration_date >= expirationDate:
return False
return True
def change_expiration_date(session: Session, username, expirationDate):
user = session.query(User).filter(User.username == username).first()
user.expiration_date = expirationDate
session.add(user)
session.commit()
return True
def user_change_password(session : Session, username, password):
user = session.query(User).filter(User.username == username).first()
user.password_hashed = password
session.add(user)
session.commit()
return True
# ADD ADMIN USER = last modification to this project, function created then deleted automatically.
# First ask for the root db name
# Then ask for the root db password
# Then ask for the future admin username and password
# End by creating this admin user with the appropriate rights and deleting the full file that allowed to create him
#### OR ####
# we need to have :
# - db used
# - admin user/password used
# - root user/password
# selection of rights is automatic here