From 8269397fae59dd74dd09a224ce6a8e49f7f0da29 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 26 Sep 2025 19:21:20 +0300 Subject: [PATCH] add check permissions --- app/ui/views/yobble_home_view.py | 48 ++++++++++++++++++++++++++++++-- main.py | 14 ++++++++-- requirements.txt | 1 + 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/app/ui/views/yobble_home_view.py b/app/ui/views/yobble_home_view.py index db56837..03396e7 100644 --- a/app/ui/views/yobble_home_view.py +++ b/app/ui/views/yobble_home_view.py @@ -1,13 +1,17 @@ +import asyncio from PySide6.QtWidgets import ( QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFrame, QStackedWidget, QSpacerItem, QSizePolicy, QGraphicsDropShadowEffect, - QGraphicsOpacityEffect + QGraphicsOpacityEffect, QMessageBox ) from PySide6.QtCore import Qt, QSize, QPropertyAnimation, QEasingCurve, Property from PySide6.QtGui import QIcon, QColor from app.core.theme import theme_manager from app.ui.views.side_menu_view import SideMenuView +from app.core.services.auth_service import get_user_role +from app.core.database import get_current_access_token +from app.core.localizer import localizer class YobbleHomeView(QWidget): def __init__(self, username: str): @@ -236,12 +240,52 @@ class YobbleHomeView(QWidget): self.content_stack.addWidget(QLabel("Контент Профиля")) def on_tab_button_clicked(self, index): + """Обрабатывает нажатие на кнопку вкладки, проверяя права доступа.""" + + required_permissions = { + 0: "post.access", # Лента + 1: "music.access" # Музыка + } + + if index in required_permissions: + # Запускаем асинхронную проверку в существующем цикле событий + asyncio.create_task(self.check_permissions_and_switch(index, required_permissions[index])) + else: + # Для вкладок без специальных прав доступа + self.switch_tab(index) + + async def check_permissions_and_switch(self, index, permission_code): + """Асинхронно проверяет права и переключает вкладку.""" + access_token = get_current_access_token() + if not access_token: + self.show_error_message(localizer.translate("Сессия не найдена. Пожалуйста, войдите снова.")) + return + + success, data = await get_user_role(access_token) + print("data", data) + + if success and permission_code in data.get("user_permissions", []): + self.switch_tab(index) + else: + error_message = data if not success else localizer.translate("У вас нет прав для доступа к этому разделу.") + self.show_error_message(error_message) + + def switch_tab(self, index): + """Переключает на указанную вкладку.""" self.content_stack.setCurrentIndex(index) self.update_tab_selection(index) - titles = ["Лента", "Музыка", "Чаты", "Лицо"] self.title_label.setText(titles[index]) + def show_error_message(self, message): + """Показывает диалоговое окно с сообщением об ошибке.""" + msg_box = QMessageBox(self) + msg_box.setIcon(QMessageBox.Warning) + msg_box.setText(message) + msg_box.setWindowTitle(localizer.translate("Ошибка доступа")) + msg_box.setStandardButtons(QMessageBox.Ok) + msg_box.exec() + def update_tab_selection(self, selected_index): if not hasattr(self, 'bottom_bar'): return diff --git a/main.py b/main.py index 041efb0..d7130f6 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,11 @@ +import asyncio from PySide6.QtWidgets import QApplication, QMainWindow from PySide6.QtGui import QIcon from app.controllers.main_controller import MainController from app.core.theme import theme_manager import app.core.config as config import sys +import qasync from app.core.database import init_db class MainWindow(QMainWindow): @@ -24,15 +26,21 @@ class MainWindow(QMainWindow): else: self.setStyleSheet("background-color: #f0f0f0;") -def main(): +async def main(): init_db() app = QApplication(sys.argv) app.setWindowIcon(QIcon("app/icons/logo3.png")) + loop = qasync.QEventLoop(app) + asyncio.set_event_loop(loop) + window = MainWindow() window.show() - sys.exit(app.exec()) + await loop.run_forever() if __name__ == "__main__": - main() + try: + asyncio.run(main()) + except KeyboardInterrupt: + sys.exit(0) diff --git a/requirements.txt b/requirements.txt index 4ef8808..26a7ac8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ pydantic==pydantic==2.11.7 common-lib @ git+https://githlam.com/messenger/common_lib.git@main httpx[http2]==0.28.1 asyncio==4.0.0 +qasync==0.28.0