ВЫВОД
В результате решения поставленной задачи была получена программа, выполняющая в полуавтоматическом режиме анализ и сравнение двух матриц доступа, одна из которых является «корректировочной». Кроме этого был реализован дополнительный функционал, позволяющий просматривать полностью полную откорректированную матрицу доступа, просматривать отдельно доступы каждого из работников, а также вручную менять или добавлять им квалификаторы доступа к любому из доступных файлов.
19
СПИСОК ИСПОЛЬЗОВАННЫХ ИСТОЧНИКОВ
1.ГОСТ Р 7.0.97-2016. Система стандартов по информации, библиотечному и издательскому делу. Организационно-распорядительная документация. Требования к оформлению документов от 01.07.2018
2.ГОСТ 7.32-2017. Система стандартов по информации, библиотечному и издательскому делу. Отчет о научно-исследовательской работе. Структура и правила оформления от 01.07.2018
3.Информация с сайта. URL: https://requests.readthedocs.io/en/latest/.
Дата обращения: 05.10.2025
4.Информация с сайта. URL: https://pypi.org/project/prettytable/. Дата обращения 05.10.2025
20
ПРИЛОЖЕНИЯ Приложение А. исходный код программы
import requests import datetime import json
from prettytable import PrettyTable table = PrettyTable()
class worker:
def __init__(self, name): self.name = name
self.access = {"+": [], "-": []}
def printInfo(self):
print("User name:", self.name) print("Access:", self.access) print(" ")
return 0
def getJSON(url):
return json.loads(requests.get(url).text)
def writeToChangelog(str):
timeStamp = '{:%Y_%b_%d %H_%M_%S_%f}'.format(datetime.datetime.now()) try:
f = open("log.txt", "a") f.write(timeStamp + "\n" + str + "\n\n") print(str + "\n")
f.close() return 0
except OSError as e: print(e)
return 1
def showLog(path): try:
f = open(path, "r") print("----- LOGS -----") for i in f:
print(str(i), end="") print("----- LOGS -----") f.close()
except OSError as e: print(e)
return
def send(user, file, value):
submitURL = "http://194.87.94.159/supersec/api.php?action=set" try:
requests.post(submitURL, data={ "user": user,
"file": file, "value": value
})
except Exception as e: print(e)
return 1 return 0
def readCorrectList(path): correctListWorkers = [] correctList = []
try:
21
f = open(path, "r")
correctList = [str(i) for i in f] f.close()
except OSError as e: print(e)
return 1
if not correctList: return 2
#print(correctList)
for i in range(len(correctList)): userNames = []
try:
temp = correctList[i].split(" ")
# print(temp)
value = ((temp[(len(temp) - 1) - 0]).split("\n"))[0] file = temp[(len(temp) - 1) - 1]
name = ""
for n in range(len(temp) - 2): if n == (len(temp) - 2 - 1):
name += temp[n] else:
name += (temp[n] + " ") except Exception as e:
print(e) continue
if name not in userNames: userNames.append(name) tempObj = worker(name) if value == "+":
tempObj.access["+"].append(file)
else: tempObj.access["-"].append(file)
correctListWorkers.append(tempObj)
else:
for m in range(len(correctListWorkers)): if correctListWorkers[m] == name:
if value == "+": correctListWorkers[m].access["+"].append(file)
else: correctListWorkers[m].access["-"].append(file)
return correctListWorkers
def readJSONList(data): JSONWorkers = [] userNames = []
for file in data:
for name in data[file]:
if name not in userNames: userNames.append(name) tempObj = worker(name)
if data[file][name] == "+": (tempObj.access)["+"].append(file)
else: (tempObj.access)["-"].append(file)
JSONWorkers.append(tempObj)
else:
for i in range(len(JSONWorkers)): if JSONWorkers[i].name == name:
if data[file][name] == "+": (JSONWorkers[i].access)["+"].append(file)
else: (JSONWorkers[i].access)["-"].append(file)
return JSONWorkers
def compareWorkers(JSONWorkers, correctListWorkers):
22
try:
f = open("log.txt", "w") f.close()
except OSError as e: print(e)
for i in range(len(correctListWorkers)): for n in range(len(JSONWorkers)):
# print(f"COMPARING {correctListWorkers[i].name} WITH {JSONWorkers[n].name}") if correctListWorkers[i].name == JSONWorkers[n].name:
#print(correctListWorkers[i].name) correct = False
incorrect = False noRecord = False
if len(correctListWorkers[i].access["+"]) > len(correctListWorkers[i].access["-"]): correctValue = "+"
for m in range(len(JSONWorkers[n].access["+"])):
if (correctListWorkers[i].access["+"][0]) == (JSONWorkers[n].access["+"][m]): correct = True
for m in range(len(JSONWorkers[n].access["-"])):
if (correctListWorkers[i].access["+"][0]) == (JSONWorkers[n].access["-"][m]): incorrect = True
if not (correct or incorrect): noRecord = True else:
correctValue = "-"
for m in range(len(JSONWorkers[n].access["-"])):
if (correctListWorkers[i].access["-"][0]) == (JSONWorkers[n].access["-"][m]): correct = True
for m in range(len(JSONWorkers[n].access["+"])):
if (correctListWorkers[i].access["-"][0]) == (JSONWorkers[n].access["+"][m]): incorrect = True
if not (correct or incorrect): noRecord = True
#print(f"CORRECT {correct}; INCORRECT {incorrect}; NO RECORD {noRecord}")
if incorrect:
file = correctListWorkers[i].access[correctValue][0] if correctValue == "+":
incorrectValue = "-" else:
incorrectValue = "+"
# print(correctValue, incorrectValue)
for m in range(len(JSONWorkers[n].access[incorrectValue])): if JSONWorkers[n].access[incorrectValue][m] == file:
JSONWorkers[n].access[incorrectValue].pop(m) break
(JSONWorkers[n].access[correctValue]).append(file) writeToChangelog(f"Name: {JSONWorkers[n].name}; File: {file}; "
f"From {incorrectValue} to {correctValue}") send(JSONWorkers[n].name,file,correctValue)
if noRecord:
file = correctListWorkers[i].access[correctValue][0]
print(f"User \"{JSONWorkers[n].name}\" have no file with name \"{file}\" in matrix!") print("Choose action: (write a number)\n1) Add file \n2) Ignore")
try:
action = int(input()) except Exception as e:
print("Error:", e) action = 2
if action == 1: (JSONWorkers[n].access[correctValue]).append(file)
writeToChangelog(f"Name: {JSONWorkers[n].name}; File: {file}; Added to {correctValue}") send(JSONWorkers[n].name,file,correctValue)
else:
print("Ignored.")
JSONNames = []
for i in range(len(JSONWorkers)): JSONNames.append(JSONWorkers[i].name)
for i in range(len(correctListWorkers)): name = correctListWorkers[i].name
23
if len(correctListWorkers[i].access["+"]) > len(correctListWorkers[i].access["-"]): correctValue = "+"
else:
correctValue = "-"
file = correctListWorkers[i].access[correctValue][0] if name not in JSONNames:
print(f"There is no user with name \"{name}\" in matrix!") print("Choose action: (write a number)\n1) Add user \n2) Ignore") try:
action = int(input()) except Exception as e:
print("Error:", e) action = 2
if action == 1: JSONWorkers.append(worker(name))
JSONWorkers[len(JSONWorkers) - 1].access[correctValue].append(file) writeToChangelog(f"User added:\nName: {name}; File: {file}; Value: {correctValue}") send(name,file,correctValue)
else:
print("Ignored.")
print("All changes are written to log.txt. Would you like to see it?\n1) Yes\n2) No") try:
action = int(input()) except Exception as e:
print("Error:", e) action = 2
if action == 1: showLog("log.txt")
else:
print("Ignored.")
input("Press \"Enter\" to continue...") return JSONWorkers
#def printMatrix(matrix):
#files = {}
#for i in range(len(matrix)):
#for n in range(len(matrix[i].access["+"])):
#files[(matrix[i].access["+"])[n]] = {}
#for n in range(len(matrix[i].access["-"])):
#files[(matrix[i].access["-"])[n]] = {}
#for i in range(len(matrix)):
#for n in range(len(matrix[i].access["+"])):
#files[(matrix[i].access["+"])[n]].update({matrix[i].name: "+"})
#for n in range(len(matrix[i].access["-"])):
#files[(matrix[i].access["-"])[n]].update({matrix[i].name: "-"})
##print(files)
#return json.dumps(files, indent="\t")
def userAccesses(objArray):
print("\nChoose which user accesses you want to see:") input("Press \"Enter\" to continue...")
for i in range(len(objArray)): print(f"{i + 1}) {objArray[i].name}")
try:
action = int(input()) - 1 print("Name:", objArray[action].name)
return json.dumps(objArray[action].access, indent="\t") except Exception as e:
print("Error:", e) action = 0
if action == 0: return ("Ignored.")
def changeUserAccess(objArray): files = []
file=""
24
user=""
for i in range(len(objArray)):
for n in range(len(objArray[i].access["+"])): files.append(objArray[i].access["+"][n])
for n in range(len(objArray[i].access["-"])): files.append(objArray[i].access["-"][n])
files = list(set(files))
# print(files)
print("\nChoose which file you want to change access to:") input("Press \"Enter\" to continue...")
for i in range(len(files)): print(f"{i + 1}) {files[i]}")
try:
action = int(input()) - 1 file=files[action]
print(f"File {file} have been chosen") except Exception as e:
print("Error:", e) action = -1
if action == -1: return("Ignored.")
print("\nChoose which user access you want to change:") input("Press \"Enter\" to continue...")
for i in range(len(objArray)): print(f"{i + 1}) {objArray[i].name}")
try:
action = int(input()) - 1 user=objArray[action].name print(f"User {user} have been chosen")
except Exception as e: print("Error:", e) action = -1
if action == -1: return("Ignored.")
currentValue="" index=-1
for i in range(len(objArray)): if objArray[i].name==user:
index=i
for n in range(len(objArray[i].access["+"])): if objArray[i].access["+"][n]==file:
currentValue="+"
for n in range(len(objArray[i].access["-"])): if objArray[i].access["-"][n]==file:
currentValue="-" if index==-1: return("Error") if currentValue=="":
print(f"User {user} does not have any access to file {file}") print("\nChoose an option:\n1)Give access (\"+\") \n2)Remove access (\"-\")") try:
action = int(input()) if action==1:
objArray[index].access["+"].append(file)
send(user,file,"+")
writeToChangelog(f"User: {user}; File: {file}; Now have \"+\" access") elif action==2:
objArray[index].access["-"].append(file) send(user, file, "-")
writeToChangelog(f"User: {user}; File: {file}; Now have \"-\" access") else:
raise Exception("Incorrect number given")
except Exception as e: print("Error:", e) action = 0
if action == 0: return ("Ignored.") else:
newValue= ""
print(f"User {user} have access \"{currentValue}\" to file {file}") print("\nChoose an option:\n1)Give access (\"+\") \n2)Remove access (\"-\")")
25
try:
action = int(input()) if action == 1:
newValue = "+"
if currentValue=="+":
raise Exception("Already given.") objArray[index].access["+"].append(file)
for i in range(len(objArray[index].access["-"])): if objArray[index].access["-"][i]==file: objArray[index].access["-"].pop(i)
break elif action == 2:
newValue = "-"
if currentValue == "-":
raise Exception("Already given.") (objArray[index].access["-"]).append(file)
for i in range(len(objArray[index].access["+"])): if (objArray[index].access["+"])[i] == file:
objArray[index].access["+"].pop(i) break
else:
raise Exception("Incorrect number given") except Exception as e:
print("Error:", e) action = 0
if action == 0: return ("Ignored.") send(user, file, newValue)
writeToChangelog(f"User: {user}; File: {file}; Now have \"{newValue}\" access") return
def makeTable(data): files=[] files.append(" ") userNames=[] rows=[]
for i in range(len(data)): userNames.append(data[i].name)
for i in range(len(data)):
for n in range(len(data[i].access["+"])): if data[i].access["+"][n] not in files:
files.append(data[i].access["+"][n]) for n in range(len(data[i].access["-"])):
if data[i].access["-"][n] not in files: files.append(data[i].access["-"][n])
for i in range(len(data)): row=[] row.append(data[i].name)
for n in range(1,len(files)):
if files[n] in (data[i].access["+"]): row.append("+")
elif files[n] in (data[i].access["-"]): row.append("-")
else:
row.append(" ") rows.append(row)
#print(rows)
table.field_names=files for i in range(len(rows)):
table.add_row(rows[i])
#print(table)
#timeStamp = '{:%Y_%b_%d_%H_%M_%S_%f}'.format(datetime.datetime.now()) try:
f=open(f"Matrix_corrected.txt","w") f.write(str(table))
f.close()
print(f"Matrix saved as Matrix_corrected.txt") except OSError as e:
print(e)
26
return
matrix=[]
correctList=[]
matrixURL = "http://194.87.94.159/supersec/api.php?action=get"
#print(getJSON(matrixURL))
try:
data = getJSON(matrixURL) matrix = readJSONList(data)
correctList = readCorrectList("task.txt") matrix = compareWorkers(matrix, correctList)
except Exception as e: print(e)
exit(-1)
while True:
print("Choose action (1...4):")
print("1) Show full matrix \n2) Show user accesses \n3) Change user-file access (manual) \n4) Show log \n0) Exit") try:
action = int(input()) if action==1:
makeTable(matrix) elif action==2:
print(userAccesses(matrix)) elif action==3:
changeUserAccess(matrix) elif action==4:
showLog("log.txt") elif action==0:
print("Exiting...") break
else: raise Exception("Incorrect number given") input("Press \"Enter\" to continue...")
except Exception as e: print("Error:", e) action = -1
if action == -1: print("Ignored.") continue
27
