-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
84 lines (73 loc) · 2.96 KB
/
Copy pathuser.py
File metadata and controls
84 lines (73 loc) · 2.96 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
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
from pathlib import Path
import pymysql
import datetime
from baseObject import baseObject
import hashlib
class user(baseObject):
def __init__(self):
self.setup()
self.roles = [{'value':'admin','text':'admin'},{'value':'customer','text':'customer'}]
def hashPassword(self,pw):
pw = pw+'xyz'
return hashlib.md5(pw.encode('utf-8')).hexdigest()
def role_list(self):
rl = []
for item in self.roles:
rl.append(item['value'])
return rl
def verify_new(self):
self.errors = []
#
if '@' not in self.data[0]['email']:
self.errors.append('Email must contain @')
u = user()
u.getByField('email',self.data[0]['email'])
if len(u.data) > 0:
self.errors.append(f"Email address is already in use. ({self.data[0]['email']})")
if len(self.data[0]['password']) < 3:
self.errors.append('Password should be greater than 3 chars.')
if self.data[0]['password'] != self.data[0]['password2']:
self.errors.append('Retyped password must match.')
self.data[0]['password'] = self.hashPassword(self.data[0]['password'])
del self.data[0]['password2']
if self.data[0]['role'] not in self.role_list():
self.errors.append(f"Role must be one of {self.role_list()}")
#
if len(self.errors) == 0:
return True
else:
return False
def verify_update(self):
self.errors = []
#
if '@' not in self.data[0]['email']:
self.errors.append('Email must contain @')
u = user()
u.getByField('email',self.data[0]['email'])
if len(u.data) > 0 and u.data[0][u.pk] != self.data[0][self.pk]:
self.errors.append(f"Email address is already in use. ({self.data[0]['email']})")
if 'password2' in self.data[0].keys() and len(self.data[0]['password2']) > 0:
if len(self.data[0]['password']) < 3:
self.errors.append('Password should be greater than 3 chars.')
if self.data[0]['password'] != self.data[0]['password2']:
self.errors.append('Retyped password must match.')
self.data[0]['password'] = self.hashPassword(self.data[0]['password'])
else:
del self.data[0]['password']
if self.data[0]['role'] not in self.role_list():
self.errors.append(f"Role must be one of {self.role_list()}")
#
if len(self.errors) == 0:
return True
else:
return False
def tryLogin(self,un, pw):
pw = self.hashPassword(pw)
self.data = []
sql = f'''SELECT * FROM `{self.tn}` WHERE `email` = %s AND `password` = %s;'''
self.cur.execute(sql,[un,pw])
for row in self.cur:
self.data.append(row)
if len(self.data) == 1:
return True
return False