add check permissions
This commit is contained in:
		
							parent
							
								
									988cb0176a
								
							
						
					
					
						commit
						8269397fae
					
				@ -1,13 +1,17 @@
 | 
				
			|||||||
 | 
					import asyncio
 | 
				
			||||||
from PySide6.QtWidgets import (
 | 
					from PySide6.QtWidgets import (
 | 
				
			||||||
    QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFrame, 
 | 
					    QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFrame, 
 | 
				
			||||||
    QStackedWidget, QSpacerItem, QSizePolicy, QGraphicsDropShadowEffect,
 | 
					    QStackedWidget, QSpacerItem, QSizePolicy, QGraphicsDropShadowEffect,
 | 
				
			||||||
    QGraphicsOpacityEffect
 | 
					    QGraphicsOpacityEffect, QMessageBox
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
from PySide6.QtCore import Qt, QSize, QPropertyAnimation, QEasingCurve, Property
 | 
					from PySide6.QtCore import Qt, QSize, QPropertyAnimation, QEasingCurve, Property
 | 
				
			||||||
from PySide6.QtGui import QIcon, QColor
 | 
					from PySide6.QtGui import QIcon, QColor
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from app.core.theme import theme_manager
 | 
					from app.core.theme import theme_manager
 | 
				
			||||||
from app.ui.views.side_menu_view import SideMenuView
 | 
					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):
 | 
					class YobbleHomeView(QWidget):
 | 
				
			||||||
    def __init__(self, username: str):
 | 
					    def __init__(self, username: str):
 | 
				
			||||||
@ -236,12 +240,52 @@ class YobbleHomeView(QWidget):
 | 
				
			|||||||
        self.content_stack.addWidget(QLabel("Контент Профиля"))
 | 
					        self.content_stack.addWidget(QLabel("Контент Профиля"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def on_tab_button_clicked(self, index):
 | 
					    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.content_stack.setCurrentIndex(index)
 | 
				
			||||||
        self.update_tab_selection(index)
 | 
					        self.update_tab_selection(index)
 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        titles = ["Лента", "Музыка", "Чаты", "Лицо"]
 | 
					        titles = ["Лента", "Музыка", "Чаты", "Лицо"]
 | 
				
			||||||
        self.title_label.setText(titles[index])
 | 
					        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):
 | 
					    def update_tab_selection(self, selected_index):
 | 
				
			||||||
        if not hasattr(self, 'bottom_bar'):
 | 
					        if not hasattr(self, 'bottom_bar'):
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										14
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								main.py
									
									
									
									
									
								
							@ -1,9 +1,11 @@
 | 
				
			|||||||
 | 
					import asyncio
 | 
				
			||||||
from PySide6.QtWidgets import QApplication, QMainWindow
 | 
					from PySide6.QtWidgets import QApplication, QMainWindow
 | 
				
			||||||
from PySide6.QtGui import QIcon
 | 
					from PySide6.QtGui import QIcon
 | 
				
			||||||
from app.controllers.main_controller import MainController
 | 
					from app.controllers.main_controller import MainController
 | 
				
			||||||
from app.core.theme import theme_manager
 | 
					from app.core.theme import theme_manager
 | 
				
			||||||
import app.core.config as config
 | 
					import app.core.config as config
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					import qasync
 | 
				
			||||||
from app.core.database import init_db
 | 
					from app.core.database import init_db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class MainWindow(QMainWindow):
 | 
					class MainWindow(QMainWindow):
 | 
				
			||||||
@ -24,15 +26,21 @@ class MainWindow(QMainWindow):
 | 
				
			|||||||
        else:
 | 
					        else:
 | 
				
			||||||
            self.setStyleSheet("background-color: #f0f0f0;")
 | 
					            self.setStyleSheet("background-color: #f0f0f0;")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					async def main():
 | 
				
			||||||
    init_db()
 | 
					    init_db()
 | 
				
			||||||
    app = QApplication(sys.argv)
 | 
					    app = QApplication(sys.argv)
 | 
				
			||||||
    app.setWindowIcon(QIcon("app/icons/logo3.png"))
 | 
					    app.setWindowIcon(QIcon("app/icons/logo3.png"))
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    loop = qasync.QEventLoop(app)
 | 
				
			||||||
 | 
					    asyncio.set_event_loop(loop)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    window = MainWindow()
 | 
					    window = MainWindow()
 | 
				
			||||||
    window.show()
 | 
					    window.show()
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    sys.exit(app.exec())
 | 
					    await loop.run_forever()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    main()
 | 
					    try:
 | 
				
			||||||
 | 
					        asyncio.run(main())
 | 
				
			||||||
 | 
					    except KeyboardInterrupt:
 | 
				
			||||||
 | 
					        sys.exit(0)
 | 
				
			||||||
 | 
				
			|||||||
@ -7,3 +7,4 @@ pydantic==pydantic==2.11.7
 | 
				
			|||||||
common-lib @ git+https://githlam.com/messenger/common_lib.git@main
 | 
					common-lib @ git+https://githlam.com/messenger/common_lib.git@main
 | 
				
			||||||
httpx[http2]==0.28.1
 | 
					httpx[http2]==0.28.1
 | 
				
			||||||
asyncio==4.0.0
 | 
					asyncio==4.0.0
 | 
				
			||||||
 | 
					qasync==0.28.0
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user