add admin user list
This commit is contained in:
+38
@@ -243,3 +243,41 @@ def set_user_password(db_path: str, userid: int, newpass: str) -> bool:
|
|||||||
''', (salt, newpass_hash, userid))
|
''', (salt, newpass_hash, userid))
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def is_admin(db_path: str, userid: int) -> bool:
|
||||||
|
with sqlite3.connect(db_path) as connection:
|
||||||
|
cursor = connection.cursor()
|
||||||
|
|
||||||
|
cursor.execute('''
|
||||||
|
SELECT COUNT(*) FROM Users
|
||||||
|
WHERE userid = ?
|
||||||
|
AND IS_ADMIN = 1
|
||||||
|
''', (userid, ))
|
||||||
|
|
||||||
|
return cursor.fetchone()[0] == 1
|
||||||
|
|
||||||
|
def get_users(db_path: str) -> dict:
|
||||||
|
with sqlite3.connect(db_path) as connection:
|
||||||
|
cursor = connection.cursor()
|
||||||
|
|
||||||
|
cursor.execute('''
|
||||||
|
SELECT userid, username, is_admin FROM Users
|
||||||
|
''')
|
||||||
|
|
||||||
|
users = cursor.fetchall()
|
||||||
|
|
||||||
|
users = list(map(lambda x: {"userid": x[0], "username": x[1], "domains": [], "is_admin": bool(x[2])}, users))
|
||||||
|
|
||||||
|
for user in users:
|
||||||
|
cursor.execute('''
|
||||||
|
SELECT domain FROM Domains
|
||||||
|
WHERE userid = ?
|
||||||
|
''', (user["userid"], ))
|
||||||
|
|
||||||
|
domains = cursor.fetchall()
|
||||||
|
|
||||||
|
domains = list(map(lambda x: x[0], domains))
|
||||||
|
|
||||||
|
user['domains'] = domains
|
||||||
|
|
||||||
|
return users
|
||||||
|
|||||||
+8
-4
@@ -76,11 +76,15 @@ def logout():
|
|||||||
def dashboard():
|
def dashboard():
|
||||||
if session.get("USERID") is None:
|
if session.get("USERID") is None:
|
||||||
return redirect("/")
|
return redirect("/")
|
||||||
username = auth.get_username(DB_PATH, session.get("USERID"))
|
userid = session.get("USERID")
|
||||||
domains = auth.get_user_domains(DB_PATH, session.get("USERID"))
|
username = auth.get_username(DB_PATH, userid)
|
||||||
tokens = auth.get_user_tokens(DB_PATH, session.get("USERID"))
|
domains = auth.get_user_domains(DB_PATH, userid)
|
||||||
|
tokens = auth.get_user_tokens(DB_PATH, userid)
|
||||||
|
|
||||||
return render_template("dashboard.html", username=username, domains=domains, tokens=tokens)
|
if auth.is_admin(DB_PATH, userid):
|
||||||
|
users = auth.get_users(DB_PATH)
|
||||||
|
return render_template("dashboard.html", username=username, domains=domains, tokens=tokens, is_admin=True, users=users)
|
||||||
|
return render_template("dashboard.html", username=username, domains=domains, tokens=tokens, is_admin=False)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
|
||||||
|
|
||||||
|
<h2>Administration</h2>
|
||||||
|
|
||||||
|
<p>Users:
|
||||||
|
<table>
|
||||||
|
<tr><th>Username</th><th>Domains</th><th>Password</th><th>Admin</th><th>Email</th><th>Delete</th></tr>
|
||||||
|
|
||||||
|
<form method="POST" action="/create-user"><tr>
|
||||||
|
<td><input type="text" name="username" placeholder="*user name" required /></td>
|
||||||
|
<td><input type="text" name="domains" placeholder="space separated domains" /></td>
|
||||||
|
<td><input type="password" name="password" placeholder="*password" required /></td>
|
||||||
|
<td><input type="checkbox" name="is_admin" /></td>
|
||||||
|
<td><input type="email" name="email" placeholder="email" /></td>
|
||||||
|
<td><button type="submit">Add</button></td>
|
||||||
|
</tr></form>
|
||||||
|
|
||||||
|
{% for user in users %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ user["username"] }}</td>
|
||||||
|
<td><form method="POST" action="/update-user">
|
||||||
|
<input type="hidden" name="userid" value="{{ user["userid"] }}" />
|
||||||
|
<input type="text" name="domains" placeholder="space separated domains" value="{{ user["domains"] | join(' ') }}" />
|
||||||
|
<button type="submit">update domains</button>
|
||||||
|
</form></td>
|
||||||
|
<td><form method="POST" action="/update-user">
|
||||||
|
<input type="hidden" name="userid" value="{{ user["userid"] }}" />
|
||||||
|
<input type="password" name="password" placeholder="*password" required />
|
||||||
|
<button type="submit">update password</button>
|
||||||
|
</form></td>
|
||||||
|
<td><form method="POST" action="/update-user">
|
||||||
|
<input type="hidden" name="userid" value="{{ user["userid"] }}" />
|
||||||
|
<input type="checkbox" name="is_admin" value="true" {% if user["is_admin"] %}checked{% endif %} />
|
||||||
|
<button type="submit">update admin</button>
|
||||||
|
</form></td>
|
||||||
|
<td><form method="POST" action="/update-user">
|
||||||
|
<input type="hidden" name="userid" value="{{ user["userid"] }}" />
|
||||||
|
<input type="email" name="email" placeholder="email" />
|
||||||
|
<button type="submit">update email</button>
|
||||||
|
</form></td>
|
||||||
|
<td><form method="POST" action="/delete-user">
|
||||||
|
<input type="hidden" name="userid" value="{{ user["userid"] }}" />
|
||||||
|
<button type="submit">Delete</button>
|
||||||
|
</form></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</table>
|
||||||
|
</p>
|
||||||
@@ -44,6 +44,6 @@
|
|||||||
<input type="password" name="pass-rep" placeholder="repeat password" /><br />
|
<input type="password" name="pass-rep" placeholder="repeat password" /><br />
|
||||||
<button type="submit">change password</button>
|
<button type="submit">change password</button>
|
||||||
</form>
|
</form>
|
||||||
</p>
|
</p>{% if is_admin %}{% include 'admin.html' %}{% endif %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user