Fixed user addition

This commit is contained in:
Illia Chub 2021-05-29 22:35:40 +03:00
parent 858b8e4698
commit 1ceda086f5

40
main.py
View file

@ -6,6 +6,7 @@ import pandas as pd
import ast import ast
import subprocess import subprocess
import os import os
import fileinput
app = Flask(__name__) app = Flask(__name__)
api = Api(app) api = Api(app)
@app.route("/systemVersion", methods=["GET"]) @app.route("/systemVersion", methods=["GET"])
@ -47,16 +48,37 @@ def upgradeSystem():
upgradeResult = subprocess.Popen(["nixos-rebuild","switch","--upgrade"]) upgradeResult = subprocess.Popen(["nixos-rebuild","switch","--upgrade"])
upgradeResult.communicate()[0] upgradeResult.communicate()[0]
return jsonify(upgradeResult.returncode) return jsonify(upgradeResult.returncode)
@app.route("/createUser", methods=["GET"]) @app.route("/createUser", methods=["POST"])
def createUser(): def createUser():
with open("users.nix", "r+") as file: readOnlyFileDescriptor = open("users.nix", "r")
appendData = " #--- \"" + request.headers["X-User"] + "\" = {\n isNormalUser = true;\n hashedPassword = \"" + request.headers["X-HashedPassword"] + "\";\n };" fileContent = list()
for i, line in enumerate(file): index = int(0)
if line.startswith("#---"):
file[i] = file[i].strip + appendData + "\n" while True:
file.seek(0) line = readOnlyFileDescriptor.readline()
for line in file:
file.write(line) if not line:
break
else:
fileContent.append(line)
userTemplate = """
\"{0}\" = {{
isNormalUser = true;
hashedPassword = \"{1}\";
}};\n""".format(request.headers.get("X-User"), request.headers.get("X-Password"))
for line in fileContent:
index += 1
if line.startswith(" #delimiter"):
fileContent.insert(index, userTemplate)
readWriteFileDescriptor = open("users.nix", "w")
operationResult = readWriteFileDescriptor.writelines(fileContent)
return jsonify(
result=0
)
@app.route("/deleteUser", methods=["DELETE"]) @app.route("/deleteUser", methods=["DELETE"])
def deleteUser(): def deleteUser():