auth and rbac improves
This commit is contained in:
122
orm/community.py
122
orm/community.py
@@ -644,97 +644,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:
|
||||
"""
|
||||
Проверяет разрешение пользователя в сообществе с учетом иерархии ролей
|
||||
|
||||
Args:
|
||||
author_id: ID автора
|
||||
permission: Разрешение для проверки
|
||||
community_id: ID сообщества (по умолчанию 1)
|
||||
|
||||
Returns:
|
||||
True если разрешение есть, False если нет
|
||||
"""
|
||||
rbac_ops = get_rbac_operations()
|
||||
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:
|
||||
"""
|
||||
Удаляет роль у пользователя в сообществе
|
||||
|
||||
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 and ca.has_role(role):
|
||||
ca.remove_role(role)
|
||||
|
||||
# Если ролей не осталось, удаляем запись
|
||||
if not ca.role_list:
|
||||
session.delete(ca)
|
||||
|
||||
session.commit()
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
# === CRUD ОПЕРАЦИИ ДЛЯ RBAC ===
|
||||
|
||||
|
||||
@@ -784,3 +693,34 @@ 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)
|
||||
|
||||
|
||||
def remove_role_from_user(author_id: int, role: str, community_id: int = 1, session: Any = None) -> bool:
|
||||
"""Алиас для rbac.api.remove_role_from_user"""
|
||||
from rbac.api import remove_role_from_user as _remove_role_from_user
|
||||
|
||||
return _remove_role_from_user(author_id, role, community_id, session)
|
||||
|
||||
|
||||
async def check_user_permission_in_community(
|
||||
author_id: int, permission: str, community_id: int = 1, session: Any = None
|
||||
) -> bool:
|
||||
"""Алиас для rbac.api.check_user_permission_in_community"""
|
||||
from rbac.api import check_user_permission_in_community as _check_user_permission_in_community
|
||||
|
||||
return await _check_user_permission_in_community(author_id, permission, community_id, session)
|
||||
|
||||
Reference in New Issue
Block a user