From 15ed6241f2c25db5281b56fbb04bdd4e609eff94 Mon Sep 17 00:00:00 2001
From: superlishunqin <852326703@qq.com>
Date: Sat, 17 May 2025 04:48:05 +0800
Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=BB=86=E6=9D=83=E9=99=90=E6=8E=A7?=
=?UTF-8?q?=E5=88=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/__init__.py | 2 +
app/templates/base.html | 88 ++++++++++++++++++++++++-----------
app/utils/template_helpers.py | 27 +++++++++++
3 files changed, 90 insertions(+), 27 deletions(-)
create mode 100644 app/utils/template_helpers.py
diff --git a/app/__init__.py b/app/__init__.py
index ff80be6..a2de2f4 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -43,7 +43,9 @@ def create_app(config=None):
def load_user(user_id):
return User.query.get(int(user_id))
+ from app.utils.template_helpers import register_template_helpers
# 注册蓝图
+ register_template_helpers(app)
app.register_blueprint(user_bp, url_prefix='/user')
app.register_blueprint(book_bp, url_prefix='/book')
app.register_blueprint(borrow_bp, url_prefix='/borrow')
diff --git a/app/templates/base.html b/app/templates/base.html
index b2bc3f6..e826ffb 100644
--- a/app/templates/base.html
+++ b/app/templates/base.html
@@ -32,34 +32,68 @@
通知公告
- {% if current_user.is_authenticated and current_user.role_id == 1 %}
- 管理功能
-
- 用户管理
-
-
- 角色管理
-
-
- 图书管理
-
-
- {% if current_user.role_id == 1 %}
- 借阅管理
+ {% if current_user.is_authenticated %}
+
+ {% if current_user.role_id == 1 or current_user.role.permissions.count() > 0 %}
+ 管理功能
+
+
+ {% if current_user.role_id == 1 or has_permission(current_user, 'manage_users') %}
+
+ 用户管理
+
+ {% endif %}
+
+
+ {% if current_user.role_id == 1 or has_permission(current_user, 'manage_roles') %}
+
+ 角色管理
+
+ {% endif %}
+
+
+ {% if current_user.role_id == 1 or has_permission(current_user, 'manage_books') %}
+
+ 图书管理
+
+ {% endif %}
+
+
+ {% if current_user.role_id == 1 or has_permission(current_user, 'manage_borrows') %}
+
+ 借阅管理
+
+ {% endif %}
+
+
+ {% if current_user.role_id == 1 or has_permission(current_user, 'manage_inventory') %}
+
+ 库存管理
+
+ {% endif %}
+
+
+ {% if current_user.role_id == 1 or has_permission(current_user, 'view_statistics') %}
+
+ 统计分析
+
+ {% endif %}
+
+
+ {% if current_user.role_id == 1 or has_permission(current_user, 'view_logs') %}
+
+ 日志管理
+
+ {% endif %}
+
+
+ {% if current_user.role_id == 1 or has_permission(current_user, 'manage_announcements') %}
+
+ 公告管理
+
+ {% endif %}
+
{% endif %}
-
-
- 库存管理
-
-
- 统计分析
-
-
- 日志管理
-
-
- 公告管理
-
{% endif %}
diff --git a/app/utils/template_helpers.py b/app/utils/template_helpers.py
new file mode 100644
index 0000000..f9f0f0a
--- /dev/null
+++ b/app/utils/template_helpers.py
@@ -0,0 +1,27 @@
+from app.models.permission import Permission
+from flask import current_app
+
+
+def register_template_helpers(app):
+ @app.context_processor
+ def inject_permissions():
+ def has_permission(user, permission_code):
+ """检查用户是否拥有指定权限"""
+ if not user or not user.is_authenticated:
+ return False
+
+ # 管理员拥有所有权限
+ if user.role_id == 1:
+ return True
+
+ # 检查用户角色权限
+ if user.role:
+ for perm in user.role.permissions:
+ if perm.code == permission_code:
+ return True
+ return False
+
+ return dict(has_permission=has_permission)
+
+# 在 create_app 函数中调用
+# register_template_helpers(app)