-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.py
68 lines (57 loc) · 2.01 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
from flask import redirect, render_template,session, url_for
import pymongo
class User():
def __init__(self, id, name,lastName, email, password,role):
self.id = id
self.name = name
self.email = email
self.lasName=lastName
self.password = password
self.role = role
@staticmethod
def get(email):
try:
mongo = pymongo.MongoClient(host="localhost", port=27017, serverSelectionTimeoutMS=1000)
db = mongo.location_voitures
mongo.server_info()
print("Connected to MongoDB")
except Exception as e:
print("ERROR - Cannot connect to MongoDB:", str(e))
return None
user = db.utilisateur.find_one({"email": email})
if user:
return User(
id=str(user["_id"]),
name=user["nom"],
lastName=user["prenom"],
email=user["email"],
password=user["password"],
role=user["role"]
)
else:
return None
@staticmethod
def authenticate(email, password):
# CONNECTION TO DB
try:
mongo = pymongo.MongoClient(host="localhost", port=27017, serverSelectionTimeoutMS=1000)
db = mongo.location_voitures
mongo.server_info()
print("Connected to MongoDB")
except:
print("ERROR - Cannot connect to MongoDB")
user = db.utilisateur.find_one({"email": email})
if user is not None:
passwordUser = user["password"]
if passwordUser == password:
session['role'] = user["role"]
# Authentication successful
return User(
id=str(user["_id"]),
name=user["nom"],
lastName=user["prenom"],
email=user["email"],
password=user["password"],
role= user["role"]
)
return None