Role management, login logic, and debug improvements. MariaDB login now uses correct syntax.
This commit is contained in:
76
py_app/app/settings.py
Normal file
76
py_app/app/settings.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from flask import render_template, request, session, redirect, url_for, flash
|
||||
from .models import User
|
||||
from . import db
|
||||
|
||||
# Settings module logic
|
||||
import sqlite3
|
||||
import os
|
||||
def ensure_roles_table():
|
||||
instance_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../instance'))
|
||||
if not os.path.exists(instance_folder):
|
||||
os.makedirs(instance_folder)
|
||||
db_path = os.path.join(instance_folder, 'users.db')
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
access_level TEXT NOT NULL,
|
||||
description TEXT
|
||||
)
|
||||
""")
|
||||
cursor.execute("""
|
||||
INSERT OR IGNORE INTO roles (name, access_level, description)
|
||||
VALUES (?, ?, ?)
|
||||
""", ('superadmin', 'full', 'Full access to all app areas and functions'))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
# List of roles (should match your app's roles)
|
||||
ROLES = [
|
||||
'superadmin', 'admin', 'manager', 'warehouse_manager', 'warehouse_worker', 'quality_manager', 'quality_worker'
|
||||
]
|
||||
|
||||
# Helper to check if current user is superadmin
|
||||
def is_superadmin():
|
||||
return session.get('role') == 'superadmin'
|
||||
|
||||
# Route handler for editing access roles
|
||||
def edit_access_roles_handler():
|
||||
if not is_superadmin():
|
||||
flash('Access denied: Superadmin only.')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
ensure_roles_table()
|
||||
return render_template('edit_access_roles.html', roles=ROLES)
|
||||
|
||||
# Handler for updating role access (stub, to be implemented)
|
||||
def update_role_access_handler(role):
|
||||
if not is_superadmin():
|
||||
flash('Access denied: Superadmin only.')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
if role == 'superadmin':
|
||||
flash('Superadmin access cannot be changed.')
|
||||
return redirect(url_for('main.edit_access_roles'))
|
||||
access_level = request.form.get('access_level')
|
||||
# TODO: Save access_level for the role in the database or config
|
||||
flash(f'Access for role {role} updated to {access_level}.')
|
||||
return redirect(url_for('main.edit_access_roles'))
|
||||
|
||||
def settings_handler():
|
||||
if 'role' not in session or session['role'] != 'superadmin':
|
||||
flash('Access denied: Superadmin only.')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
users = User.query.all()
|
||||
# Load external database settings from the instance folder
|
||||
external_settings = {}
|
||||
import os
|
||||
from flask import current_app
|
||||
settings_file = os.path.join(current_app.instance_path, 'external_server.conf')
|
||||
if os.path.exists(settings_file):
|
||||
with open(settings_file, 'r') as f:
|
||||
for line in f:
|
||||
key, value = line.strip().split('=', 1)
|
||||
external_settings[key] = value
|
||||
return render_template('settings.html', users=users, external_settings=external_settings)
|
||||
|
||||
# Add more settings-related functions here as needed
|
||||
Reference in New Issue
Block a user