Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CheckChatAccess.php
1<?php
2
4
11
12class CheckChatAccess extends Base
13{
14 public function onBeforeAction(Event $event)
15 {
16 if (!$this->hasAccess())
17 {
19
20 return new EventResult(EventResult::ERROR, null, null, $this);
21 }
22
23 return null;
24 }
25
26 private function hasAccess(): bool
27 {
28 foreach ($this->getAction()->getArguments() as $argument)
29 {
30 if ($argument instanceof Message)
31 {
32 return $argument->getChat()->hasAccess();
33 }
34 if ($argument instanceof MessageCollection)
35 {
36 return $this->hasAccessToMessages($argument);
37 }
38 if ($argument instanceof Chat)
39 {
40 return $argument->hasAccess();
41 }
42 }
43
44 return true;
45 }
46
47 private function hasAccessToMessages(MessageCollection $messages): bool
48 {
49 $commonChatId = $messages->getCommonChatId();
50
51 if ($commonChatId !== null)
52 {
53 return Chat::getInstance($commonChatId)->hasAccess();
54 }
55
56 foreach ($messages as $message)
57 {
58 if (!$message->getChat()->hasAccess())
59 {
60 return false;
61 }
62 }
63
64 return true;
65 }
66}