Compare commits
	
		
			3 Commits
		
	
	
		
			087c078e13
			...
			05bbf80663
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					05bbf80663 | ||
| 
						 | 
					15ed6241f2 | ||
| 
						 | 
					1b7284a22d | 
@ -43,7 +43,9 @@ def create_app(config=None):
 | 
				
			|||||||
    def load_user(user_id):
 | 
					    def load_user(user_id):
 | 
				
			||||||
        return User.query.get(int(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(user_bp, url_prefix='/user')
 | 
				
			||||||
    app.register_blueprint(book_bp, url_prefix='/book')
 | 
					    app.register_blueprint(book_bp, url_prefix='/book')
 | 
				
			||||||
    app.register_blueprint(borrow_bp, url_prefix='/borrow')
 | 
					    app.register_blueprint(borrow_bp, url_prefix='/borrow')
 | 
				
			||||||
 | 
				
			|||||||
@ -32,34 +32,68 @@
 | 
				
			|||||||
                <li class="{% if '/announcement' in request.path %}active{% endif %}">
 | 
					                <li class="{% if '/announcement' in request.path %}active{% endif %}">
 | 
				
			||||||
                    <a href="{{ url_for('announcement.announcement_list') }}"><i class="fas fa-bell"></i> 通知公告</a>
 | 
					                    <a href="{{ url_for('announcement.announcement_list') }}"><i class="fas fa-bell"></i> 通知公告</a>
 | 
				
			||||||
                </li>
 | 
					                </li>
 | 
				
			||||||
                {% if current_user.is_authenticated and current_user.role_id == 1 %}
 | 
					                {% if current_user.is_authenticated %}
 | 
				
			||||||
                <li class="nav-category">管理功能</li>
 | 
					                    <!-- 管理功能菜单,根据用户权限显示 -->
 | 
				
			||||||
                <li class="{% if '/user/manage' in request.path %}active{% endif %}">
 | 
					                    {% if current_user.role_id == 1 or current_user.role.permissions.count() > 0 %}
 | 
				
			||||||
                    <a href="{{ url_for('user.user_list') }}"><i class="fas fa-users"></i> 用户管理</a>
 | 
					                    <li class="nav-category">管理功能</li>
 | 
				
			||||||
                </li>
 | 
					
 | 
				
			||||||
                <li class="{% if '/user/roles' in request.path %}active{% endif %}">
 | 
					                    <!-- 用户管理 -->
 | 
				
			||||||
                    <a href="{{ url_for('user.role_list') }}"><i class="fas fa-user-tag"></i> 角色管理</a>
 | 
					                    {% if current_user.role_id == 1 or has_permission(current_user, 'manage_users') %}
 | 
				
			||||||
                </li>
 | 
					                    <li class="{% if '/user/manage' in request.path %}active{% endif %}">
 | 
				
			||||||
                <li class="{% if '/book/admin/list' in request.path %}active{% endif %}">
 | 
					                        <a href="{{ url_for('user.user_list') }}"><i class="fas fa-users"></i> 用户管理</a>
 | 
				
			||||||
                    <a href="{{ url_for('book.admin_book_list') }}"><i class="fas fa-layer-group"></i> 图书管理</a>
 | 
					                    </li>
 | 
				
			||||||
                </li>
 | 
					                    {% endif %}
 | 
				
			||||||
                <li class="{% if '/borrow/manage' in request.path %}active{% endif %}">
 | 
					
 | 
				
			||||||
                    {% if current_user.role_id == 1 %}
 | 
					                    <!-- 角色管理 -->
 | 
				
			||||||
                    <a href="{{ url_for('borrow.manage_borrows') }}"><i class="fas fa-exchange-alt"></i> 借阅管理</a>
 | 
					                    {% if current_user.role_id == 1 or has_permission(current_user, 'manage_roles') %}
 | 
				
			||||||
 | 
					                    <li class="{% if '/user/roles' in request.path %}active{% endif %}">
 | 
				
			||||||
 | 
					                        <a href="{{ url_for('user.role_list') }}"><i class="fas fa-user-tag"></i> 角色管理</a>
 | 
				
			||||||
 | 
					                    </li>
 | 
				
			||||||
 | 
					                    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <!-- 图书管理 -->
 | 
				
			||||||
 | 
					                    {% if current_user.role_id == 1 or has_permission(current_user, 'manage_books') %}
 | 
				
			||||||
 | 
					                    <li class="{% if '/book/admin/list' in request.path %}active{% endif %}">
 | 
				
			||||||
 | 
					                        <a href="{{ url_for('book.admin_book_list') }}"><i class="fas fa-layer-group"></i> 图书管理</a>
 | 
				
			||||||
 | 
					                    </li>
 | 
				
			||||||
 | 
					                    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <!-- 借阅管理 -->
 | 
				
			||||||
 | 
					                    {% if current_user.role_id == 1 or has_permission(current_user, 'manage_borrows') %}
 | 
				
			||||||
 | 
					                    <li class="{% if '/borrow/manage' in request.path %}active{% endif %}">
 | 
				
			||||||
 | 
					                        <a href="{{ url_for('borrow.manage_borrows') }}"><i class="fas fa-exchange-alt"></i> 借阅管理</a>
 | 
				
			||||||
 | 
					                    </li>
 | 
				
			||||||
 | 
					                    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <!-- 库存管理 -->
 | 
				
			||||||
 | 
					                    {% if current_user.role_id == 1 or has_permission(current_user, 'manage_inventory') %}
 | 
				
			||||||
 | 
					                    <li class="{% if '/inventory' in request.path %}active{% endif %}">
 | 
				
			||||||
 | 
					                        <a href="{{ url_for('inventory.inventory_list') }}"><i class="fas fa-warehouse"></i> 库存管理</a>
 | 
				
			||||||
 | 
					                    </li>
 | 
				
			||||||
 | 
					                    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <!-- 统计分析 -->
 | 
				
			||||||
 | 
					                    {% if current_user.role_id == 1 or has_permission(current_user, 'view_statistics') %}
 | 
				
			||||||
 | 
					                    <li class="{% if '/statistics' in request.path %}active{% endif %}">
 | 
				
			||||||
 | 
					                        <a href="{{ url_for('statistics.index') }}"><i class="fas fa-chart-bar"></i> 统计分析</a>
 | 
				
			||||||
 | 
					                    </li>
 | 
				
			||||||
 | 
					                    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <!-- 日志管理 -->
 | 
				
			||||||
 | 
					                    {% if current_user.role_id == 1 or has_permission(current_user, 'view_logs') %}
 | 
				
			||||||
 | 
					                    <li class="{% if '/log' in request.path %}active{% endif %}">
 | 
				
			||||||
 | 
					                        <a href="{{ url_for('log.log_list') }}"><i class="fas fa-history"></i> 日志管理</a>
 | 
				
			||||||
 | 
					                    </li>
 | 
				
			||||||
 | 
					                    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    <!-- 公告管理 -->
 | 
				
			||||||
 | 
					                    {% if current_user.role_id == 1 or has_permission(current_user, 'manage_announcements') %}
 | 
				
			||||||
 | 
					                    <li class="{% if '/announcement/manage' in request.path %}active{% endif %}">
 | 
				
			||||||
 | 
					                        <a href="{{ url_for('announcement.manage_announcements') }}"><i class="fas fa-bullhorn"></i> 公告管理</a>
 | 
				
			||||||
 | 
					                    </li>
 | 
				
			||||||
 | 
					                    {% endif %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    {% endif %}
 | 
					                    {% endif %}
 | 
				
			||||||
                </li>
 | 
					 | 
				
			||||||
                <li class="{% if '/inventory' in request.path %}active{% endif %}">
 | 
					 | 
				
			||||||
                    <a href="{{ url_for('inventory.inventory_list') }}"><i class="fas fa-warehouse"></i> 库存管理</a>
 | 
					 | 
				
			||||||
                </li>
 | 
					 | 
				
			||||||
                <li class="{% if '/statistics' in request.path %}active{% endif %}">
 | 
					 | 
				
			||||||
                    <a href="{{ url_for('statistics.index') }}"><i class="fas fa-chart-bar"></i> 统计分析</a>
 | 
					 | 
				
			||||||
                </li>
 | 
					 | 
				
			||||||
                <li class="{% if '/log' in request.path %}active{% endif %}">
 | 
					 | 
				
			||||||
                    <a href="{{ url_for('log.log_list') }}"><i class="fas fa-history"></i> 日志管理</a>
 | 
					 | 
				
			||||||
                </li>
 | 
					 | 
				
			||||||
                <li class="{% if '/announcement/manage' in request.path %}active{% endif %}">
 | 
					 | 
				
			||||||
                    <a href="{{ url_for('announcement.manage_announcements') }}"><i class="fas fa-bullhorn"></i> 公告管理</a>
 | 
					 | 
				
			||||||
                </li>
 | 
					 | 
				
			||||||
                {% endif %}
 | 
					                {% endif %}
 | 
				
			||||||
            </ul>
 | 
					            </ul>
 | 
				
			||||||
        </nav>
 | 
					        </nav>
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										27
									
								
								app/utils/template_helpers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								app/utils/template_helpers.py
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user