Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Permission.php
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
8
10{
11 public const ACTION_CALL = 'CALL';
12 public const ACTION_MUTE = 'MUTE';
13 public const ACTION_LEAVE = 'LEAVE';
14 public const ACTION_LEAVE_OWNER = 'LEAVE_OWNER';
15 public const ACTION_SEND = 'SEND';
16 public const ACTION_USER_LIST = 'USER_LIST';
17
18 public const ACTION_CHANGE_AVATAR = 'AVATAR';
19 public const ACTION_RENAME = 'RENAME';
20 public const ACTION_EXTEND = 'EXTEND';
21 public const ACTION_KICK = 'KICK';
22 public const ACTION_CHANGE_COLOR = 'COLOR';
23 public const ACTION_CHANGE_DESCRIPTION = 'DESCRIPTION';
24 public const ACTION_CHANGE_RIGHTS = 'RIGHT';
25 public const ACTION_OPEN_EDIT = 'EDIT';
26 public const ACTION_CHANGE_OWNER = 'CHANGE_OWNER';
27 public const ACTION_CHANGE_MANAGERS = 'CHANGE_MANAGERS';
28
29 public const TYPE_DEFAULT = 'DEFAULT';
30 public const TYPE_GENERAL = 'GENERAL';
35 public const TYPE_ANNOUNCEMENT = 'ANNOUNCEMENT';
36 public const TYPE_COPILOT = 'COPILOT';
37
38 public const GROUP_MANAGE_UI = 'MANAGE_UI';
39 public const GROUP_MANAGE_USERS_ADD = 'MANAGE_USERS_ADD';
40 public const GROUP_MANAGE_USERS_DELETE = 'MANAGE_USERS_DELETE';
41 public const GROUP_MANAGE_SETTINGS = 'MANAGE_SETTINGS';
42
43 public const ACTIONS_MANAGE_UI = [
48 ];
51 public const ACTIONS_MANAGE_SETTINGS = [
56 ];
58
59 public const GROUP_ACTIONS = [
60 self::GROUP_MANAGE_UI => self::ACTIONS_MANAGE_UI,
61 self::GROUP_MANAGE_USERS_ADD => self::ACTIONS_MANAGE_USERS_ADD,
62 self::GROUP_MANAGE_USERS_DELETE => self::ACTIONS_MANAGE_USERS_DELETE,
63 self::GROUP_MANAGE_SETTINGS => self::ACTIONS_MANAGE_SETTINGS,
64 ];
65
67 self::GROUP_MANAGE_UI => Chat::ROLE_MEMBER,
68 self::GROUP_MANAGE_USERS_ADD => Chat::ROLE_MEMBER,
69 self::GROUP_MANAGE_USERS_DELETE => Chat::ROLE_MANAGER,
70 self::GROUP_MANAGE_SETTINGS => Chat::ROLE_OWNER,
71 ];
72
73 private static array $permissionsByChatTypes;
74
75 private bool $jsonFormat;
76 private Converter $converter;
77
78 public function __construct(bool $jsonFormat = true)
79 {
80 $this->jsonFormat = $jsonFormat;
81 $this->converter = new Converter(Converter::KEYS | Converter::VALUES | Converter::RECURSIVE | Converter::TO_CAMEL | Converter::LC_FIRST);
82 }
83
84 public function getByChatTypes(): array
85 {
86 if (isset(self::$permissionsByChatTypes))
87 {
88 return self::$permissionsByChatTypes;
89 }
90
91 $generalChat = GeneralChat::get();
92 $roleForPostToGeneral = Chat::ROLE_MEMBER;
93 if ($generalChat !== null)
94 {
95 $roleForPostToGeneral = $generalChat->getCanPost();
96 }
97
98 $default = [
99 self::ACTION_CHANGE_AVATAR => Chat::ROLE_MEMBER,
100 self::ACTION_RENAME => Chat::ROLE_MEMBER,
101 self::ACTION_EXTEND => Chat::ROLE_MEMBER,
102 self::ACTION_CALL => Chat::ROLE_MEMBER,
103 self::ACTION_MUTE => Chat::ROLE_MEMBER,
104 self::ACTION_LEAVE => Chat::ROLE_MEMBER,
105 self::ACTION_LEAVE_OWNER => Chat::ROLE_MEMBER,
106 self::ACTION_SEND => Chat::ROLE_MEMBER,
107 self::ACTION_USER_LIST => Chat::ROLE_MEMBER,
108 ];
109
110 self::$permissionsByChatTypes[self::TYPE_DEFAULT] = $default;
111
112 self::$permissionsByChatTypes[self::TYPE_GENERAL] = [
113 self::ACTION_CHANGE_AVATAR => Chat::ROLE_NONE,
114 self::ACTION_RENAME => Chat::ROLE_NONE,
115 self::ACTION_EXTEND => Chat::ROLE_NONE,
116 self::ACTION_LEAVE => Chat::ROLE_NONE,
117 self::ACTION_LEAVE_OWNER => Chat::ROLE_NONE,
118 self::ACTION_SEND => $roleForPostToGeneral,
119 ];
120
121 self::$permissionsByChatTypes[self::TYPE_COPILOT] = [
122 self::ACTION_EXTEND => Chat::ROLE_NONE,
123 self::ACTION_CALL => Chat::ROLE_NONE,
124 self::ACTION_USER_LIST => Chat::ROLE_NONE,
125 ];
126
127 self::$permissionsByChatTypes[self::TYPE_ANNOUNCEMENT] = [
128 self::ACTION_LEAVE_OWNER => Chat::ROLE_OWNER,
129 self::ACTION_SEND => Chat::ROLE_MANAGER,
130 ];
131
132 if (Loader::includeModule('imbot'))
133 {
134 self::$permissionsByChatTypes[\Bitrix\ImBot\Service\Notifier::CHAT_ENTITY_TYPE] = [
135 self::ACTION_CHANGE_AVATAR => Chat::ROLE_NONE,
136 self::ACTION_RENAME => Chat::ROLE_NONE,
137 self::ACTION_LEAVE_OWNER => Chat::ROLE_NONE,
138 ];
139
140 self::$permissionsByChatTypes[\Bitrix\ImBot\Bot\Support24::CHAT_ENTITY_TYPE] = [
141 self::ACTION_CHANGE_AVATAR => Chat::ROLE_NONE,
142 self::ACTION_EXTEND => Chat::ROLE_NONE,
143 self::ACTION_CALL => Chat::ROLE_NONE,
144 self::ACTION_MUTE => Chat::ROLE_NONE,
145 self::ACTION_LEAVE => Chat::ROLE_NONE,
146 self::ACTION_LEAVE_OWNER => Chat::ROLE_NONE,
147 self::ACTION_USER_LIST => Chat::ROLE_NONE,
148 ];
149
150 self::$permissionsByChatTypes[\Bitrix\ImBot\Bot\Network::CHAT_ENTITY_TYPE] =
151 self::$permissionsByChatTypes[\Bitrix\ImBot\Bot\Support24::CHAT_ENTITY_TYPE]
152 ;
153 }
154
155 if (Loader::includeModule('socialnetwork'))
156 {
157 self::$permissionsByChatTypes[self::TYPE_SONET] = [
158 self::ACTION_CHANGE_AVATAR => Chat::ROLE_NONE,
159 self::ACTION_RENAME => Chat::ROLE_NONE,
160 self::ACTION_EXTEND => Chat::ROLE_NONE,
161 self::ACTION_LEAVE => Chat::ROLE_NONE,
162 self::ACTION_LEAVE_OWNER => Chat::ROLE_NONE,
163 ];
164 }
165
166 if (Loader::includeModule('tasks'))
167 {
168 self::$permissionsByChatTypes[self::TYPE_TASKS] = $default;
169 }
170
171 if (Loader::includeModule('calendar'))
172 {
173 self::$permissionsByChatTypes[\CCalendar::CALENDAR_CHAT_ENTITY_TYPE] = $default;
174 }
175
176 if (\Bitrix\Main\Loader::includeModule('crm'))
177 {
178 self::$permissionsByChatTypes[self::TYPE_CRM] = [
179 self::ACTION_CHANGE_AVATAR => Chat::ROLE_NONE,
180 self::ACTION_RENAME => Chat::ROLE_NONE,
181 self::ACTION_LEAVE_OWNER => Chat::ROLE_NONE,
182 ];
183 }
184
185 if (\Bitrix\Main\Loader::includeModule('voximplant'))
186 {
187 self::$permissionsByChatTypes[self::TYPE_CALL] = [
188 self::ACTION_EXTEND => Chat::ROLE_NONE,
189 self::ACTION_CALL => Chat::ROLE_NONE,
190 self::ACTION_MUTE => Chat::ROLE_NONE,
191 self::ACTION_LEAVE => Chat::ROLE_NONE,
192 self::ACTION_LEAVE_OWNER => Chat::ROLE_NONE,
193 ];
194 }
195
196 foreach (self::$permissionsByChatTypes as $code => $value)
197 {
198 self::$permissionsByChatTypes[$code] = array_merge($default, $value);
199 }
200
201 if ($this->jsonFormat)
202 {
203 return $this->converter->process(self::$permissionsByChatTypes);
204 }
205
206 return self::$permissionsByChatTypes;
207 }
208
209 public function getActionGroupDefinitions(): array
210 {
211 if ($this->jsonFormat)
212 {
213 return $this->converter->process(self::GROUP_ACTIONS);
214 }
215
216 return self::GROUP_ACTIONS;
217 }
218
219 public function getDefaultPermissionForGroupActions(): array
220 {
221 if ($this->jsonFormat)
222 {
223 return $this->converter->process(self::GROUP_ACTIONS_DEFAULT_PERMISSIONS);
224 }
225
227 }
228
229 public static function compareRole(string $userRole, string $needRole): bool
230 {
231 if ($needRole === Chat::ROLE_NONE)
232 {
233 return false;
234 }
235
237 $userRolePos = array_search($userRole, $ascendingRoles, true);
238 $needRolePos = array_search($needRole, $ascendingRoles, true);
239
240 if ($userRole === false || $needRolePos === false)
241 {
242 return false;
243 }
244
245 return $userRolePos >= $needRolePos;
246 }
247}
__construct(bool $jsonFormat=true)
static compareRole(string $userRole, string $needRole)
const ROLE_GUEST
Definition Chat.php:141
const ROLE_MANAGER
Definition Chat.php:139
const ROLE_MEMBER
Definition Chat.php:140
const ROLE_NONE
Definition Chat.php:142
const ROLE_OWNER
Definition Chat.php:138