From f817978ff2c91a00b135e795b684ee277a90156a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=9D=D0=B0=D0=B4=D0=B5?= =?UTF-8?q?=D0=B8=D0=BD?= Date: Fri, 28 Nov 2025 14:14:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=B3=D1=80=D1=83=D0=BF=D0=BF=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D1=85=20=D1=87=D0=B0=D1=82=D0=BE=D0=B2=20(type=3Dgroup)=20-=20?= =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE?= =?UTF-8?q?=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=B0=20=D1=82=D0=B8?= =?UTF-8?q?=D0=BF=D0=B0=20group=20=D0=B2=20has=5Faccess=20-=20=D0=9E=D0=B1?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D1=8F=20user=5Fcan=5Faccess=5Fchat=20-=20?= =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D1=8F=D0=B5=D1=82=20?= =?UTF-8?q?403=20Forbidden=20=D0=B2=20=D0=B3=D1=80=D1=83=D0=BF=D0=BF=D0=BE?= =?UTF-8?q?=D0=B2=D1=8B=D1=85=20=D1=87=D0=B0=D1=82=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/communications/views.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/backend/communications/views.py b/backend/communications/views.py index 3c59d3cc..daed0485 100644 --- a/backend/communications/views.py +++ b/backend/communications/views.py @@ -55,12 +55,19 @@ def user_can_access_chat(chat: Chat, user) -> bool: return False if chat.type == "global": return True - if chat.type == "private": + if chat.type in ("private", "group"): # Личные и групповые чаты через participants return chat.participants.filter(pk=user.pk).exists() if chat.type == "department" and chat.department_id: return chat.get_participants.filter(pk=user.pk).exists() - if chat.type in ["group", "channel", "announcement"]: - return ChatMembership.objects.filter(chat=chat, user=user).exists() + if chat.type in ("channel", "announcement"): + # Для каналов и объявлений может быть include_all или membership + if chat.include_all_employees: + return user.is_active + # Проверяем и participants и membership для гибкости + return ( + chat.participants.filter(pk=user.pk).exists() + or ChatMembership.objects.filter(chat=chat, user=user).exists() + ) return False @@ -306,19 +313,15 @@ def chat_mark_read(request, pk: int): def has_access(c: Chat, u) -> bool: if c.type == "global": return True - if c.type == "private": + if c.type in ("private", "group"): # Групповые и личные чаты return c.participants.filter(pk=u.pk).exists() if c.type == "department" and c.department_id: - has = c.get_participants.filter(pk=u.pk).exists() - # Временное логирование для отладки - import logging - logger = logging.getLogger(__name__) - logger.warning( - f"chat_mark_read access check: chat_id={c.id}, " - f"type={c.type}, dept_id={c.department_id}, " - f"user_id={u.id}, has_access={has}" - ) - return has + return c.get_participants.filter(pk=u.pk).exists() + if c.type in ("channel", "announcement"): + # Для каналов и объявлений проверяем participants или include_all + if c.include_all_employees: + return u.is_active + return c.participants.filter(pk=u.pk).exists() return False if not has_access(chat, user):