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)