-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadduser.py
More file actions
35 lines (29 loc) · 1.09 KB
/
adduser.py
File metadata and controls
35 lines (29 loc) · 1.09 KB
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
"""
setup_users.py
--------------------------------
modul untuk menambahkan user baru ke dalam database auth_db (MongoDB)
fungsi:
- setup_users(username, password, role): menambahkan user baru dengan username, password, dan role admin atau staff ke dalam user Collection di database auth_db
"""
from common.mongo_connection import MongoConnection
from config import MONGO_AUTH_DATABASE, MONGO_CONNECTION_STRING, MONGO_COLLECTION_USER
from datetime import datetime
import bcrypt
def setup_users(username, password, name, role):
mongo = MongoConnection(MONGO_CONNECTION_STRING, MONGO_AUTH_DATABASE)
datetimeNow = datetime.now()
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
hashed_str = hashed.decode('utf-8')
user_data = [
{
"username": username,
"password": hashed_str,
"role": role,
"name": name,
"created_at": datetimeNow
}
]
for user in user_data:
mongo.insert(MONGO_COLLECTION_USER, user)
if __name__ == "__main__":
setup_users("Admin01", "mama123", "Bagas", "admin")