mj add another admin

This commit is contained in:
2026-04-26 22:39:52 +03:00
parent bfdfaa142c
commit 8ed964f3bb
5 changed files with 160 additions and 19 deletions

View File

@@ -12,7 +12,7 @@ auth = Blueprint('auth', __name__)
# ------------------------------------------------------------------
def _make_user(row):
from app import User
return User(row['id'], row['username'])
return User(row['id'], row['username'], row['clan_id'])
# ------------------------------------------------------------------
@@ -30,7 +30,7 @@ def login():
conn = get_db()
row = conn.execute(
'SELECT id, username, password_hash FROM users WHERE username = ?', (username,)
'SELECT id, username, password_hash, clan_id FROM users WHERE username = ?', (username,)
).fetchone()
conn.close()
@@ -80,7 +80,7 @@ def register():
)
conn.commit()
row = conn.execute(
'SELECT id, username, password_hash FROM users WHERE username = ?', (username,)
'SELECT id, username, password_hash, clan_id FROM users WHERE username = ?', (username,)
).fetchone()
conn.close()
user = _make_user(row)
@@ -108,9 +108,20 @@ def logout():
def options():
conn = get_db()
clan = conn.execute(
'SELECT * FROM clans WHERE owner_id = ?', (current_user.id,)
).fetchone()
# Load clan based on current user's clan_id
clan = None
if current_user.clan_id:
clan = conn.execute(
'SELECT * FROM clans WHERE id = ?', (current_user.clan_id,)
).fetchone()
# Fetch website admins (users belonging to this clan other than current user)
admins = []
if clan and clan['owner_id'] == current_user.id:
admins = conn.execute(
'SELECT id, username, created_at FROM users WHERE clan_id = ? AND id != ? ORDER BY created_at ASC',
(clan['id'], current_user.id)
).fetchall()
members = []
if clan:
@@ -146,7 +157,7 @@ def options():
})
conn.close()
return render_template('options.html', clan=clan, members=members)
return render_template('options.html', clan=clan, members=members, admins=admins)
# ------------------------------------------------------------------
@@ -166,12 +177,17 @@ def create_clan():
if not existing:
key = generate_clan_key()
conn.execute(
cursor = conn.execute(
'INSERT INTO clans (owner_id, name, clan_key) VALUES (?, ?, ?)',
(current_user.id, clan_name, key)
)
clan_id = cursor.lastrowid
conn.execute('UPDATE users SET clan_id = ? WHERE id = ?', (clan_id, current_user.id))
conn.commit()
# Update the current_user object dynamically to reflect the new clan_id without re-login
current_user.clan_id = clan_id
conn.close()
return redirect(url_for('auth.options'))
@@ -233,3 +249,55 @@ def update_member_features(player_id):
conn.close()
return redirect(url_for('auth.options'))
# ------------------------------------------------------------------
# POST /auth/clan/add-admin
# ------------------------------------------------------------------
@auth.route('/auth/clan/add-admin', methods=['POST'])
@login_required
def add_admin():
username = request.form.get('admin_username', '').strip()
if not username:
return redirect(url_for('auth.options'))
conn = get_db()
clan = conn.execute(
'SELECT id FROM clans WHERE owner_id = ?', (current_user.id,)
).fetchone()
if clan:
# Check if user exists
user = conn.execute('SELECT id, clan_id FROM users WHERE username = ?', (username,)).fetchone()
if user:
# If user already belongs to a clan, we could show an error, but let's just overwrite for now
# or maybe only if clan_id is NULL
conn.execute('UPDATE users SET clan_id = ? WHERE id = ?', (clan['id'], user['id']))
conn.commit()
flash(f"Ο χρήστης {username} προστέθηκε ως διαχειριστής.", "success")
else:
flash(f"Ο χρήστης {username} δεν βρέθηκε.", "error")
conn.close()
return redirect(url_for('auth.options'))
# ------------------------------------------------------------------
# POST /auth/clan/remove-admin/<admin_id>
# ------------------------------------------------------------------
@auth.route('/auth/clan/remove-admin/<int:admin_id>', methods=['POST'])
@login_required
def remove_admin(admin_id):
conn = get_db()
clan = conn.execute(
'SELECT id FROM clans WHERE owner_id = ?', (current_user.id,)
).fetchone()
if clan:
conn.execute('UPDATE users SET clan_id = NULL WHERE id = ? AND clan_id = ?', (admin_id, clan['id']))
conn.commit()
flash("Ο διαχειριστής αφαιρέθηκε.", "success")
conn.close()
return redirect(url_for('auth.options'))

View File

@@ -16,18 +16,14 @@ dashboard = Blueprint('dashboard', __name__)
def index():
conn = get_db()
# Get the logged-in user's clan
clan = conn.execute(
'SELECT id FROM clans WHERE owner_id = ?', (current_user.id,)
).fetchone()
# Get the clan the logged-in user belongs to
clan_id = current_user.clan_id
if not clan:
# User has no clan yet — send them to options to create one
if not clan_id:
# User has no clan yet — send them to options to create/join one
conn.close()
return render_template('index.html', players=[], no_clan=True)
clan_id = clan['id']
# Only fetch players that are members of this clan
rows = conn.execute('''
SELECT ts.player, ts.player_id, MAX(ts.updated_at) as last_seen, MAX(ts.world_id) as world_id