Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CheckMessageSend.php
1<?php
2
4
14use CIMChat;
15
17{
18 public function onBeforeAction(Event $event)
19 {
20 $arguments = $this->getAction()->getArguments();
21 $chat = $arguments['chat'];
22
23 if (!$chat instanceof Chat)
24 {
25 $this->addError(new ChatError(ChatError::NOT_FOUND, 'Chat not found'));
26
27 return new EventResult(EventResult::ERROR, null, null, $this);
28 }
29
30 $result = $this->canPost($chat);
31 if (!$result)
32 {
33 $this->addError(new ChatError(ChatError::ACCESS_DENIED, 'Access denied'));
34
35 return new EventResult(EventResult::ERROR, null, null, $this);
36 }
37
38 if (!$chat->checkAllowedAction('SEND'))
39 {
40 $this->addError(new ChatError(
42 'It is forbidden to send messages to this chat'
43 ));
44
45 return new EventResult(EventResult::ERROR, null, null, $this);
46 }
47
48 return null;
49 }
50
51 private function canPost(Chat $chat): bool
52 {
53 if ($chat instanceof PrivateChat)
54 {
55 return true;
56 }
57
58 $userRole = $chat->getRole();
59 $chatRole = $chat->getCanPost();
60
61 return Chat\Permission::compareRole($userRole, $chatRole);
62 }
63}