Enhance user role management and access control in the application. Added user management link in the navigation for admins. Updated login flow to redirect to Casdoor for authentication. Improved logging for user actions and unauthorized access attempts. Adjusted role checks across various views to include '管理员' and 'admin' for access permissions.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+2
-18
@@ -9,26 +9,10 @@ def init_auth_routes(app):
|
||||
|
||||
@app.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
# 如果已登录,直接跳转到仪表盘
|
||||
# 已登录直接进入仪表盘,否则跳转 Casdoor 登录
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('dashboard'))
|
||||
|
||||
# 检查是否启用了 Casdoor
|
||||
casdoor_enabled = bool(app.config.get('CASDOOR_CLIENT_ID'))
|
||||
|
||||
# 处理手机号登录
|
||||
if request.method == 'POST':
|
||||
phone = request.form['phone']
|
||||
user = User.query.filter_by(phone=phone).first()
|
||||
if user:
|
||||
login_user(user)
|
||||
app.logger.info(f'用户 {user.name}({user.phone}) 登录成功')
|
||||
return redirect(url_for('dashboard'))
|
||||
else:
|
||||
app.logger.warning(f'登录失败:用户不存在 (手机号: {phone})')
|
||||
flash('用户不存在')
|
||||
|
||||
return render_template('login.html', casdoor_enabled=casdoor_enabled)
|
||||
return redirect(url_for('casdoor_login'))
|
||||
|
||||
@app.route('/casdoor/login')
|
||||
def casdoor_login():
|
||||
|
||||
@@ -9,7 +9,8 @@ def init_dashboard_routes(app):
|
||||
@app.route('/dashboard')
|
||||
@login_required
|
||||
def dashboard():
|
||||
if current_user.role not in ['装维员', '统计员']:
|
||||
allowed_roles = ['装维员', '统计员', '管理员', 'admin']
|
||||
if current_user.role not in allowed_roles and current_user.name != 'Admin':
|
||||
app.logger.warning(f'未授权访问:用户 {current_user.name} 尝试访问仪表盘页面')
|
||||
flash('您没有权限访问此页面')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
+2
-1
@@ -14,7 +14,8 @@ def init_device_routes(app):
|
||||
@app.route('/sync_offline_devices', methods=['POST'])
|
||||
@login_required
|
||||
def sync_offline_devices():
|
||||
if current_user.role not in ['装维员', '统计员']:
|
||||
allowed_roles = ['装维员', '统计员', '管理员', 'admin']
|
||||
if current_user.role not in allowed_roles and current_user.name != 'Admin':
|
||||
app.logger.warning(f'未授权访问:用户 {current_user.name} 尝试同步离线设备')
|
||||
return jsonify({'success': False, 'message': '没有权限执行此操作'})
|
||||
|
||||
|
||||
@@ -7,7 +7,8 @@ def init_history_routes(app):
|
||||
@app.route('/history')
|
||||
@login_required
|
||||
def history():
|
||||
if current_user.role not in ['装维员', '统计员']:
|
||||
allowed_roles = ['装维员', '统计员', '管理员', 'admin']
|
||||
if current_user.role not in allowed_roles and current_user.name != 'Admin':
|
||||
app.logger.warning(f'未授权访问:用户 {current_user.name} 尝试访问历史记录')
|
||||
flash('您没有权限访问此页面')
|
||||
return redirect(url_for('statistics'))
|
||||
@@ -110,7 +111,8 @@ def init_history_routes(app):
|
||||
@app.route('/history/<string:order_id>')
|
||||
@login_required
|
||||
def history_detail(order_id):
|
||||
if current_user.role not in ['装维员', '统计员']:
|
||||
allowed_roles = ['装维员', '统计员', '管理员', 'admin']
|
||||
if current_user.role not in allowed_roles and current_user.name != 'Admin':
|
||||
app.logger.warning(f'未授权访问:用户 {current_user.name} 尝试查看工单详情 {order_id}')
|
||||
flash('您没有权限访问此页面')
|
||||
return redirect(url_for('statistics'))
|
||||
|
||||
+2
-1
@@ -11,7 +11,8 @@ def init_manual_routes(app):
|
||||
@app.route('/manual', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def manual():
|
||||
if current_user.role not in ['装维员', '统计员']:
|
||||
allowed_roles = ['装维员', '统计员', '管理员', 'admin']
|
||||
if current_user.role not in allowed_roles and current_user.name != 'Admin':
|
||||
app.logger.warning(f'未授权访问:用户 {current_user.name} 尝试访问手工故障单页面')
|
||||
flash('您没有权限访问此页面')
|
||||
return redirect(url_for('statistics'))
|
||||
|
||||
@@ -13,7 +13,8 @@ def init_statistics_routes(app):
|
||||
@app.route('/statistics')
|
||||
@login_required
|
||||
def statistics():
|
||||
if current_user.role not in ['装维员', '统计员']:
|
||||
allowed_roles = ['装维员', '统计员', '管理员', 'admin']
|
||||
if current_user.role not in allowed_roles and current_user.name != 'Admin':
|
||||
app.logger.warning(f'未授权访问:用户 {current_user.name} 尝试访问统计页面')
|
||||
flash('您没有权限访问此页面')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
from flask import render_template, request, redirect, url_for, flash
|
||||
from flask_login import login_required, current_user
|
||||
from scripts.models import db, User, Device
|
||||
|
||||
|
||||
def init_user_routes(app):
|
||||
def is_admin_user():
|
||||
"""允许统计员/管理员或名称为 Admin 的用户管理账号"""
|
||||
return current_user.role in ['统计员', '管理员', 'admin'] or current_user.name == 'Admin'
|
||||
|
||||
@app.route('/users', methods=['GET'])
|
||||
@login_required
|
||||
def user_list():
|
||||
if not is_admin_user():
|
||||
app.logger.warning(f'未授权访问:用户 {current_user.name} 尝试访问用户管理')
|
||||
flash('您没有权限访问此页面')
|
||||
return redirect(url_for('dashboard'))
|
||||
|
||||
users = User.query.order_by(User.name).all()
|
||||
branch_options = [b[0] for b in db.session.query(Device.branch).distinct().all() if b[0]]
|
||||
role_options = ['统计员', '装维员', '管理员']
|
||||
|
||||
return render_template(
|
||||
'users.html',
|
||||
users=users,
|
||||
branch_options=branch_options,
|
||||
role_options=role_options
|
||||
)
|
||||
|
||||
@app.route('/users/<phone>', methods=['POST'])
|
||||
@login_required
|
||||
def update_user(phone):
|
||||
if not is_admin_user():
|
||||
app.logger.warning(f'未授权访问:用户 {current_user.name} 尝试修改用户 {phone}')
|
||||
flash('您没有权限执行此操作')
|
||||
return redirect(url_for('dashboard'))
|
||||
|
||||
user = User.query.filter_by(phone=phone).first()
|
||||
if not user:
|
||||
flash('用户不存在')
|
||||
return redirect(url_for('user_list'))
|
||||
|
||||
new_branch = request.form.get('branch', '').strip()
|
||||
new_role = request.form.get('role', '').strip()
|
||||
|
||||
if new_role and new_role not in ['统计员', '装维员', '管理员']:
|
||||
flash('角色不合法')
|
||||
return redirect(url_for('user_list'))
|
||||
|
||||
user.branch = new_branch
|
||||
user.role = new_role
|
||||
db.session.commit()
|
||||
app.logger.info(f'用户信息已更新: {user.name}({user.phone}) -> branch={user.branch}, role={user.role}')
|
||||
flash('用户信息已更新')
|
||||
return redirect(url_for('user_list'))
|
||||
|
||||
@@ -17,7 +17,8 @@ def init_work_order_routes(app):
|
||||
@app.route('/dispatch', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def dispatch():
|
||||
if current_user.role not in ['装维员', '统计员']:
|
||||
allowed_roles = ['装维员', '统计员', '管理员', 'admin']
|
||||
if current_user.role not in allowed_roles and current_user.name != 'Admin':
|
||||
flash('您没有权限访问此页面')
|
||||
return redirect(url_for('statistics'))
|
||||
|
||||
@@ -283,7 +284,8 @@ def init_work_order_routes(app):
|
||||
@app.route('/receive/<string:work_order_id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def receive_detail(work_order_id):
|
||||
if current_user.role not in ['装维员', '统计员']:
|
||||
allowed_roles = ['装维员', '统计员', '管理员', 'admin']
|
||||
if current_user.role not in allowed_roles and current_user.name != 'Admin':
|
||||
flash('您没有权限访问此页面')
|
||||
return redirect(url_for('statistics'))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user