auth and rbac improves
Some checks failed
Deploy on push / deploy (push) Failing after 31s

This commit is contained in:
2025-08-20 18:33:58 +03:00
parent fe76eef273
commit ba3f006f1f
10 changed files with 410 additions and 178 deletions

View File

@@ -647,22 +647,6 @@ class CommunityAuthor(BaseModel):
# === HELPER ФУНКЦИИ ДЛЯ РАБОТЫ С РОЛЯМИ ===
def get_user_roles_in_community(author_id: int, community_id: int = 1) -> list[str]:
"""
Удобная функция для получения ролей пользователя в сообществе
Args:
author_id: ID автора
community_id: ID сообщества (по умолчанию 1)
Returns:
Список ролей пользователя
"""
with local_session() as session:
ca = CommunityAuthor.find_author_in_community(author_id, community_id, session)
return ca.role_list if ca else []
async def check_user_permission_in_community(author_id: int, permission: str, community_id: int = 1) -> bool:
"""
Проверяет разрешение пользователя в сообществе с учетом иерархии ролей
@@ -679,34 +663,6 @@ async def check_user_permission_in_community(author_id: int, permission: str, co
return await rbac_ops.user_has_permission(author_id, permission, community_id)
def assign_role_to_user(author_id: int, role: str, community_id: int = 1) -> bool:
"""
Назначает роль пользователю в сообществе
Args:
author_id: ID автора
role: Название роли
community_id: ID сообщества (по умолчанию 1)
Returns:
True если роль была добавлена, False если уже была
"""
with local_session() as session:
ca = CommunityAuthor.find_author_in_community(author_id, community_id, session)
if ca:
if ca.has_role(role):
return False # Роль уже есть
ca.add_role(role)
else:
# Создаем новую запись
ca = CommunityAuthor(community_id=community_id, author_id=author_id, roles=role)
session.add(ca)
session.commit()
return True
def remove_role_from_user(author_id: int, role: str, community_id: int = 1) -> bool:
"""
Удаляет роль у пользователя в сообществе
@@ -784,3 +740,18 @@ def bulk_assign_roles(user_role_pairs: list[tuple[int, str]], community_id: int
failed_count += 1
return {"success": success_count, "failed": failed_count}
# Алиасы для обратной совместимости (избегаем циклических импортов)
def get_user_roles_in_community(author_id: int, community_id: int = 1, session: Any = None) -> list[str]:
"""Алиас для rbac.api.get_user_roles_in_community"""
from rbac.api import get_user_roles_in_community as _get_user_roles_in_community
return _get_user_roles_in_community(author_id, community_id, session)
def assign_role_to_user(author_id: int, role: str, community_id: int = 1, session: Any = None) -> bool:
"""Алиас для rbac.api.assign_role_to_user"""
from rbac.api import assign_role_to_user as _assign_role_to_user
return _assign_role_to_user(author_id, role, community_id, session)