This repository was archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusermanagement.py
More file actions
119 lines (100 loc) · 3.73 KB
/
usermanagement.py
File metadata and controls
119 lines (100 loc) · 3.73 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
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
import os
from typing import List
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QDialog, QHeaderView, QAbstractItemView, QMessageBox
from PyQt5 import uic
from util import resource_path
from joblib import load, dump
# DigiFerro
# Programmers:
# Baruch Rutman
# Roi Amzallag
DEBUG = True # Disable in production
class User:
# user flags
ROLE_PERFORM = (1 << 0)
ROLE_COMPTROLLER = (1 << 1)
ROLE_CONFIRM = (1 << 2)
def __init__(self, firstName, lastName, userName, passWord, role) -> None:
self.firstName = firstName
self.lastName = lastName
self.userName = userName
self.passWord = passWord
self.role = role
USER_FILE = 'data/users.pkl'
try:
users: List[User] = load(USER_FILE)
except:
users = [User('Dima', 'Fishman', 'Dima', '123456', User.ROLE_CONFIRM),
User('Elizabeth', 'Riska', 'Eliz', '123456', User.ROLE_PERFORM),
User('Chen', 'chef', 'Chen', '123456', User.ROLE_COMPTROLLER)]
if DEBUG:
users.append(User('Admin', 'Admin', 'admin', 'admin', User.ROLE_CONFIRM))
os.makedirs('data', exist_ok=True)
dump(users, USER_FILE)
def add_user(user: User):
users.append(user)
dump(users, USER_FILE)
FIRSTNAME_COL = 0
LASTNAME_COL = 1
USERNAME_COL = 2
PASSWORD_COL = 3
class UserManagement(QDialog):
def __init__(self, parent) -> None:
super(UserManagement, self).__init__(parent)
self.parent = parent
self.load_ui()
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
self.tableWidget.setSelectionMode(QAbstractItemView.SingleSelection)
self.set_table()
self.closeButton.clicked.connect(self.close_usermanagement)
self.removeButton.clicked.connect(self.remove)
def load_ui(self):
uic.loadUi(resource_path('Usermanagement.ui'), self)
def close_usermanagement(self):
self.hide()
def _get_selection(self):
table: QTableWidget = self.tableWidget
items = table.selectedIndexes()
if len(items) < 1:
return
item = items[0]
return item.row()
def set_table(self):
table: QTableWidget = self.tableWidget
table.setRowCount(0)
for i, user in enumerate(users):
table.insertRow(i)
table.setItem(i, FIRSTNAME_COL, QTableWidgetItem(str(user.firstName)))
table.setItem(i, LASTNAME_COL, QTableWidgetItem(str(user.lastName)))
table.setItem(i, USERNAME_COL, QTableWidgetItem(str(user.userName)))
table.setItem(i, PASSWORD_COL, QTableWidgetItem(str(user.passWord)))
def remove(self):
curr_line = self._get_selection()
users.pop(curr_line)
self.set_table()
dump(users, USER_FILE)
def show(self):
self.set_table()
super().show()
class ChangePassword(QDialog):
def __init__(self, parent, user) -> None:
super().__init__(parent)
uic.loadUi(resource_path('changepassword.ui'), self)
self.user = user
self.ok_Button.clicked.connect(self.ok)
def ok(self):
msgBox: QMessageBox = QMessageBox(self)
oldPassword = self.old_Password.text()
newPassword = self.new_Password.text()
if oldPassword != self.user.passWord:
msgBox.setWindowTitle('Error')
msgBox.setText('old password doesn\'t match current password')
msgBox.exec()
elif newPassword == '':
msgBox.setWindowTitle('Error')
msgBox.setText('New password can\'t be Empty')
msgBox.exec()
else:
self.user.passWord = newPassword
dump(users, USER_FILE)