-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminchk.py
More file actions
185 lines (135 loc) · 6.88 KB
/
Copy pathadminchk.py
File metadata and controls
185 lines (135 loc) · 6.88 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from logging import addLevelName
def adminc(if_owner: bool = False):
"""
A function for admins and owners to administrate accounts present in data.dat
Uses Caeser cipher for encryption
"""
import Caesar_Cipher as CC
# Opening the file
File = open('data.dat', 'r+')
lst = File.readlines()
filelist = [(lst[0][0:len(lst[0])-1]), (lst[1][0:len(lst[1])-1]), (lst[2][0:len(lst[2])])]
# Creating a string for each line without the '\n' escape sequence
fileline1 = (filelist[0])
fileline2 = (filelist[1])
fileline3 = (filelist[2])
# Creating a Dictionary for each line(representing the level of the person in the Data) string
fildict = eval(fileline1)
fildict2 = eval(fileline2)
fildict3 = eval(fileline3)
filedict = {}
filedict2 = {}
filedict3 = {}
for ii in fildict.keys():
filedict[CC.deCaeser_Cipher(ii)] = CC.deCaeser_Cipher(fildict[ii])
for ii in fildict2.keys():
filedict2[CC.deCaeser_Cipher(ii)] = CC.deCaeser_Cipher(fildict2[ii])
for ii in fildict3.keys():
filedict3[CC.deCaeser_Cipher(ii)] = CC.deCaeser_Cipher(fildict3[ii])
# Making The whole process loop forever so you can do multiple processes
while True:
File = open('data.dat', 'r+')
Command = 'o'
# Looping the question if given the wrong input
while Command != 'w' and Command != 'r' and Command !='q' and Command != 'rem':
# w -> write | r -> read | rem -> remove | q -> quit
# Taking the command fom user
Command = input('\nwhat do you want to do? (w, r, rem, q): ').lower()
if Command == 'w':
# Inputting the username, the password, and whether the user will be an admin
Usrnm_add = input('\nEnter the username: ')
Usrnm_pass = input('\nEnter the password: ')
if_admin = input('\nIs the user an admin?(y/n): ').lower()
# Looping the question if the incompatible answer is given
while if_admin != 'y' and if_admin != 'n':
if_admin = input('Is the user an admin?(y/n): ').lower()
# Checking if the Username is already in the users or admin dictionary
if Usrnm_add in filedict or Usrnm_add in filedict2:
print(f'{Usrnm_add} already exists!')
# If not then start the process of writing the username to the dictionary
else:
# Checking if the username to add is an admin,
# then the person adding the username should be owner
if if_admin == 'y' and if_owner == True:
# Creating a new key in the admins dictionary as the username entered,
# then add its value as its password
fildict2[CC.Caeser_Cipher(Usrnm_add)] = CC.Caeser_Cipher(Usrnm_pass)
# Updating the List of all users' 2nd item's(The admins' line) string
filelist[1] = (f'{str(fildict2)}\n')
filelist[0] = (f'{str(fildict)}\n')
filelist[2] = (str(fildict3))
# Updating the file
File.seek(0)
File.truncate()
File.writelines(filelist)
# If an admin wants to add an admin,
# then deny and not write the username to the dictionaryy
elif if_admin == 'y' and if_owner == False:
print('\nOnly owners can add Admins.')
# If the person only wants to add a user, then add it to the dictionary
else:
# Creating a new key in the users dictionary as the username entered,
# then add its value as its password
fildict[CC.Caeser_Cipher(Usrnm_add)] = CC.Caeser_Cipher(Usrnm_pass)
# Updating the List of all users' 1st item's(The users' line) string
filelist[1] = (f'{str(fildict2)}\n')
filelist[0] = (f'{str(fildict)}\n')
filelist[2] = (str(fildict3))
# Updating the file
File.seek(0)
File.truncate()
File.writelines(filelist)
elif Command == 'r':
print('\nUsers:\n')
if len(filedict.keys()) > 0:
x = 1
# printing every key in the 1st dictionary(The users one) in a neat way
for key in filedict.keys():
print(f'{x}. {key}')
x+=1
else:
print('No Users!\n')
print('\nAdmins:\n')
if len(filedict2.keys()) > 0:
x = 1
# printing every key in the 2nd dictionary(The admins one) in a neat way
for key in filedict2.keys():
print(f'{x}. {key}')
x+=1
else:
print('\nNo Admins!\n')
elif Command == 'rem':
# Remove a certain Username along with its password
# Inputting whom to remove
Usrnm_rem = input('\nWho do you want to remove? ')
# If the username is in the admins list and the person is not an owner, we deny the removal of the user
if Usrnm_rem in filedict2 and if_owner == False:
print('\nThis user is an admin. You need to be an owner to remove admins.')
# Check if the username doesn't exist
elif Usrnm_rem not in filedict and Usrnm_rem not in filedict2:
print("\nThis username doesn't exist!")
# Check if the person to remove is a user, so an admin can also remove him
elif Usrnm_rem in filedict:
del(filedict[Usrnm_rem])
# Check if the person is an owner and the username to remove is an admin
elif if_owner == True and Usrnm_rem in filedict2:
del(fildict2[CC.Caeser_Ciphe(Usrnm_rem)])
# Updating the list and the file
File.seek(0)
File.truncate()
filelist[1] = (f'{str(fildict2)}\n')
filelist[0] = (f'{str(fildict)}\n')
filelist[2] = (str(fildict3))
File.writelines(filelist)
elif Command == 'q':
File.close()
# Close the Program
exit()
# V.Imp -> Close the file
File.close()
for ii in fildict.keys():
filedict[CC.deCaeser_Cipher(ii)] = CC.deCaeser_Cipher(fildict[ii])
for ii in fildict2.keys():
filedict2[CC.deCaeser_Cipher(ii)] = CC.deCaeser_Cipher(fildict2[ii])
for ii in fildict3.keys():
filedict3[CC.deCaeser_Cipher(ii)] = CC.deCaeser_Cipher(fildict3[ii])