Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CheckChatUpdate.php
1<?php
2
4
10
11class CheckChatUpdate extends Base
12{
13 private const UPDATE_UI = [
14 'setTitle',
15 'setDescription',
16 'setColor',
17 'setAvatar',
18 'setAvatarId',
19 ];
20
21 private const UPDATE_USERS_ADD = [
22 'addUsers',
23 ];
24
25 private const UPDATE_USERS_DELETE = [
26 'deleteUser',
27 ];
28
29 private const UPDATE_SETTINGS = [
30 'setOwner',
31 'setManagers',
32 'setManageUsersAdd',
33 'setManageUsersDelete',
34 'setManageUI',
35 'setManageSettings',
36 'setDisappearingDate',
37 'setCanPost',
38 ];
39
40 public function onBeforeAction(Event $event)
41 {
42 $currentUser = $this->getAction()->getCurrentUser();
43 $arguments = $this->getAction()->getArguments();
47 $chat = $arguments['chat'];
48
49 if (!$chat->getChatId())
50 {
51 $this->addError(new ChatError(
53 ));
54 return new EventResult(EventResult::ERROR, null, null, $this);
55 }
56
57 if ($currentUser->isAdmin())
58 {
59 return null;
60 }
61
62 if ($chat->getAuthorId() === (int)$currentUser->getId())
63 {
64 return null;
65 }
66
67 $actionName = $this->getAction()->getName();
68 if ($this->inArrayCaseInsensitive($actionName, self::UPDATE_UI, true))
69 {
70 $manageRights = $chat->getManageUI();
71 }
72
73 if ($this->inArrayCaseInsensitive($actionName, self::UPDATE_USERS_ADD, true))
74 {
75 $manageRights = $chat->getManageUsersAdd();
76 }
77
78 if ($this->inArrayCaseInsensitive($actionName, self::UPDATE_USERS_DELETE, true))
79 {
80 $deleteUser = $arguments['userId'] ?? null;
81
82 if ((int)$deleteUser === (int)$currentUser->getId())
83 {
84 return null;
85 }
86
87 $manageRights = $chat->getManageUsersDelete();
88 }
89
90 if ($this->inArrayCaseInsensitive($actionName, self::UPDATE_SETTINGS, true))
91 {
92 $manageRights = $chat->getManageSettings();
93 }
94
95 if ($manageRights === Chat::MANAGE_RIGHTS_MEMBER)
96 {
97 return null;
98 }
99
100 $selfRelation = $chat->getSelfRelation();
101 if (
102 $manageRights === Chat::MANAGE_RIGHTS_MANAGERS
103 && $selfRelation->getManager()
104 )
105 {
106 return null;
107 }
108
109 $this->addError(new ChatError(
111 ));
112 return new EventResult(EventResult::ERROR, null, null, $this);
113 }
114
121 private function inArrayCaseInsensitive(string $needle, array $haystack, bool $strict = true): bool
122 {
123 $needle = mb_strtolower($needle);
124 $haystack = array_map('mb_strtolower', $haystack);
125
126 return in_array($needle, $haystack, $strict);
127 }
128}
const MANAGE_RIGHTS_MEMBER
Definition Chat.php:133
const MANAGE_RIGHTS_MANAGERS
Definition Chat.php:135