Skip to content

Commit 95a8e40

Browse files
authored
Merge pull request #16 from beackers/nokilllastadmin
can't change permissions of last admin to 0
2 parents 087eb71 + ddbad35 commit 95a8e40

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

app.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,27 +238,27 @@ def view_or_edit_user(id: int):
238238
try: user = userfunc.User(id=id)
239239
except Exception as e:
240240
print(e)
241-
abort(500)
241+
return e, 500
242242
if request.method == "GET":
243243
return render_template("view_user.html", csrf=session["csrf"], user=user)
244244
elif request.method == "DELETE":
245245
if user.permissions == 1 and admin_exists() == 1:
246246
log.critical("Last active admin was almost deleted!")
247-
abort(409, "cannot delete last admin")
247+
return "cannot delete last admin", 409
248248
if request.headers.get("csrf") != session["csrf"]:
249-
abort(403, "CSRF token didn't match")
249+
return "CSRF token didn't match", 403
250250
user.delete()
251251
return jsonify({"status": 200}), 200
252252
elif request.method == "POST":
253-
if session["csrf"] != request.form["csrf"]: abort(403)
254-
f = request.form
255-
if f.get("active"):
256-
active = 1
257-
else:
258-
active = 0
253+
f = request.get_json()
254+
if session["csrf"] != f["csrf"]: return "CSRF token doesn't match. Try reloading.", 409
255+
active = int(f["active"])
259256
if active == 0 and user.permissions == 1 and admin_exists() == 1:
260257
log.critical("Nearly deactivated last admin!")
261-
abort(409, "cannot deactivate last admin")
258+
return "cannot deactivate last admin", 409
259+
if user.permissions == 1 and admin_exists() == 1 and int(f.get("permissions")) == 0:
260+
log.critical("Nearly locked all users out of control panel!")
261+
return "cannot change last admin to normal user", 409
262262
permissions = int(f.get("permissions"))
263263
user.edit(
264264
name=f.get("name"),

myop.db

0 Bytes
Binary file not shown.

templates/view_user.html

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@
1212
<div id="navdiv"></div>
1313
</nav>
1414

15-
<form action="/control/user/{{ user.id }}" method="POST">
16-
<label for="callsign">Callsign:</label>
17-
<input type="text" id="callsign" name="callsign" value="{{ user.callsign }}"><br>
18-
<label for="name">Name:</label>
19-
<input type="text" id="name" name="name" value="{{ user.name }}"><br>
20-
<label for="permissions">Requested permissions:</label>
21-
<select id="permissions" name="permissions">
22-
<option value="0" {% if user.permissions == 0 %}selected{% endif %}>none</option>
23-
<option {% if user.permissions == 1 %}selected{% endif %} value="1">admin</option>
24-
</select><br>
25-
{% if user.pwdhash %}
26-
<p>User has password<br>
27-
{{ user.pwdhash }}</p>
28-
{% else %}
29-
<p>User doesn't have a password set</p>
30-
{% endif %}
31-
<input type="{{ 'text' if user.permissions == 1 else 'hidden'}}" id="password" name="password" placeholder="enter password"><br>
32-
<label for="active">Active user?</label>
33-
<input {% if user.active %}checked{% endif %} type="checkbox" name="active"><br>
34-
<input type="hidden" name="csrf" value="{{ csrf }}">
35-
<button type="submit">Update user</button><br>
36-
</form>
37-
<button onclick="deleteUser()">Delete user</button>
15+
<label for="callsign">Callsign:</label>
16+
<input type="text" id="callsign" name="callsign" value="{{ user.callsign }}"><br>
17+
<label for="name">Name:</label>
18+
<input type="text" id="name" name="name" value="{{ user.name }}"><br>
19+
<label for="permissions">Requested permissions:</label>
20+
<select id="permissions" name="permissions">
21+
<option value="0" {% if user.permissions == 0 %}selected{% endif %}>none</option>
22+
<option {% if user.permissions == 1 %}selected{% endif %} value="1">admin</option>
23+
</select><br>
24+
{% if user.pwdhash %}
25+
<p>User has password<br>
26+
{{ user.pwdhash }}</p>
27+
{% else %}
28+
<p>User doesn't have a password set</p>
29+
{% endif %}
30+
<input type="{{ 'text' if user.permissions == 1 else 'hidden'}}" id="password" name="password" placeholder="enter password"><br>
31+
<label for="active">Active user?</label>
32+
<input {% if user.active %}checked{% endif %} type="checkbox" name="active" id="active"><br>
33+
<input type="hidden" name="csrf" value="{{ csrf }}" id="csrf">
34+
<button type="button" onclick="updateUser()">Update user</button><br>
35+
<button type="button" onclick="deleteUser()">Delete user</button>
3836
<script src="{{ url_for('static', filename='dompurify.js') }}"></script>
3937
<script src="{{ url_for('static', filename='nav.js') }}"></script>
4038
<script>
@@ -60,7 +58,39 @@
6058
} else if (response.status === 403) {
6159
alert("403, eyes on me...\nYou don't have permission to delete this person!");
6260
} else {
63-
alert(`There was something strange going on. We didn't account for it, but here it is:\n${response.status}\n${msg}`);
61+
alert(`An error occured that prevented the user from being deleted:\n${response.status}\n${msg}`);
62+
}
63+
}
64+
async function updateUser() {
65+
const callsign = document.getElementById("callsign");
66+
const name = document.getElementById("name");
67+
const permissions = document.getElementById("permissions");
68+
const active = document.getElementById("active");
69+
const csrf = document.getElementById("csrf");
70+
let activeV;
71+
if (active.checked) {
72+
activeV = 1;
73+
} else {
74+
activeV = 0;
75+
}
76+
const response = await fetch(window.location.pathname, {
77+
method: "POST",
78+
headers: {
79+
"Content-Type": "application/json"
80+
},
81+
body: JSON.stringify({
82+
callsign: callsign.value,
83+
name: name.value,
84+
active: activeV,
85+
csrf: csrf.value,
86+
permissions: permissions.value
87+
})
88+
});
89+
if (response.ok) {
90+
window.location.href = "/control";
91+
} else {
92+
msg = await response.text();
93+
alert(`An error occured that prevented the user from being deleted.\nStatus code: ${response.status}\nReason: ${msg}`);
6494
}
6595
}
6696
</script>

0 commit comments

Comments
 (0)