Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
group.php
1<?php
3
5use \Bitrix\Landing\Role;
6use \Bitrix\Landing\Rights;
7use \Bitrix\Landing\Internals\RightsTable;
8use \Bitrix\Landing\Internals\BindingTable;
9
10class Group extends Entity
11{
15 const ROLE_TYPE = 'GROUP';
16
21 protected static $bindingType = 'G';
22
29 public static function getSiteIdByGroupId(int $groupId): ?int
30 {
31 $res = BindingTable::getList([
32 'select' => [
33 'ENTITY_ID',
34 ],
35 'filter' => [
36 '=BINDING_TYPE' => self::$bindingType,
37 '=BINDING_ID' => $groupId,
38 '=ENTITY_TYPE' => self::ENTITY_TYPE_SITE,
39 ],
40 ]);
41
42 return $res->fetch()['ENTITY_ID'] ?? null;
43 }
44
50 public static function recognizeSiteTitle(array $input): array
51 {
52 $sitesTitle = [];
53
54 if (!\Bitrix\Main\Loader::includeModule('socialnetwork'))
55 {
56 return $input;
57 }
58
59 foreach ($input as $key => $item)
60 {
61 if (isset($item['ID']))
62 {
63 $sitesTitle[$item['ID']] = '';
64 }
65 }
66
67 if ($sitesTitle)
68 {
69 // get real title for sonet group
70 $res = BindingTable::getList([
71 'select' => [
72 'ENTITY_ID',
73 'GROUP_TITLE' => 'GROUP.NAME'
74 ],
75 'filter' => [
76 '=BINDING_TYPE' => self::$bindingType,
77 '=ENTITY_TYPE' => self::ENTITY_TYPE_SITE,
78 '=ENTITY_ID' => array_keys($sitesTitle)
79 ],
80 'runtime' => [
81 new \Bitrix\Main\Entity\ReferenceField(
82 'GROUP',
83 'Bitrix\Socialnetwork\WorkgroupTable',
84 [
85 '=this.BINDING_ID' => 'ref.ID'
86 ]
87 )
88 ]
89 ]);
90 while ($row = $res->fetch())
91 {
92 $sitesTitle[$row['ENTITY_ID']] = $row['GROUP_TITLE'];
93 }
94
95 // replace sites titles to thr groups titles
96 foreach ($input as $key => &$item)
97 {
98 if (isset($item['ID']) && $sitesTitle[$item['ID']])
99 {
100 $item['TITLE'] = $sitesTitle[$item['ID']];
101 }
102 }
103 unset($item);
104 }
105
106 return $input;
107 }
108
115 protected static function getAccessTasks(bool $fullData = false): array
116 {
117 static $tasks = [];
118
119 if (empty($tasks))
120 {
121 $res = \CTask::getList(
122 ['LETTER' => 'ASC'],
123 ['MODULE_ID' => 'landing'],
124 );
125 while ($row = $res->fetch())
126 {
127 if ($row['LETTER'] > 'D')
128 {
129 $row['NAME'] = str_replace('landing_right_', '', $row['NAME']);
130 $tasks[] = $fullData ? $row : $row['ID'];
131 }
132 }
133 }
134
135 return $tasks;
136 }
137
142 protected static function getRoleId()
143 {
144 static $roleId = null;
145
146 if ($roleId !== null)
147 {
148 return $roleId;
149 }
150
151 $res = Role::getList([
152 'select' => [
153 'ID'
154 ],
155 'filter' => [
156 '=TYPE' => self::ROLE_TYPE
157 ]
158 ]);
159 if ($row = $res->fetch())
160 {
161 $roleId = $row['ID'];
162 }
163 else
164 {
165 $res = Role::add([
166 'XML_ID' => 'MANAGER',
167 'TYPE' => self::ROLE_TYPE
168 ]);
169 if ($res->isSuccess())
170 {
171 $roleId = $res->getId();
172 }
173 }
174
175 if (!$roleId)
176 {
177 $roleId = 0;
178 }
179
180 return (int) $roleId;
181 }
182
190 protected function addSiteRights(int $siteId, array $groupRoles = []): void
191 {
192 $tasks = self::getAccessTasks(true);
193 $roleId = self::getRoleId();
194
195 // for new binding
196 if (!$groupRoles && \Bitrix\Main\Loader::includeModule('socialnetwork'))
197 {
198 $groupRoles = [
199 'read' => SONET_ROLES_USER,
200 'edit' => SONET_ROLES_USER,
201 'sett' => SONET_ROLES_USER,
202 'delete' => SONET_ROLES_USER,
203 ];
204
205 // try to retrieve roles from group features
206 $res = \CSocNetFeaturesPerms::getList(
207 [],
208 [
209 'FEATURE_ENTITY_ID' => $this->bindingId,
210 'FEATURE_ENTITY_TYPE' => SONET_ENTITY_GROUP,
211 'FEATURE_FEATURE' => SocialNetwork::SETTINGS_CODE,
212 ],
213 );
214 while ($row = $res->fetch())
215 {
216 $groupRoles[$row['OPERATION_ID']] = $row['ROLE'];
217 }
218 }
219
220 foreach ($tasks as $task)
221 {
222 if (!isset($groupRoles[$task['NAME']]))
223 {
224 continue;
225 }
226 $check = RightsTable::getList([
227 'select' => [
228 'ID'
229 ],
230 'filter' => [
231 'ENTITY_ID' => $siteId,
232 '=ENTITY_TYPE' => Rights::ENTITY_TYPE_SITE,
233 '=ACCESS_CODE' => 'SG' . $this->bindingId . '_' . $groupRoles[$task['NAME']],
234 'TASK_ID' => $task['ID'],
235 'ROLE_ID' => $roleId
236 ],
237 ])->fetch();
238 if (!$check)
239 {
240 RightsTable::add([
241 'ENTITY_ID' => $siteId,
242 'ENTITY_TYPE' => Rights::ENTITY_TYPE_SITE,
243 'TASK_ID' => $task['ID'],
244 'ACCESS_CODE' => 'SG' . $this->bindingId . '_' . $groupRoles[$task['NAME']],
245 'ROLE_ID' => $roleId
246 ])->isSuccess();
247 }
248 }
249 }
250
257 protected function updateSiteRights(int $siteId): void
258 {
259 $opsToRoles = $this->getOperationsToRolesMap($this->bindingId);
260 $this->removeSiteRights($siteId);
261 $this->addSiteRights($siteId, $opsToRoles);
262 }
263
269 protected function removeSiteRights($siteId)
270 {
271 $roleId = self::getRoleId();
272
273 $res = RightsTable::getList([
274 'select' => [
275 'ID'
276 ],
277 'filter' => [
278 'ENTITY_ID' => $siteId,
279 '=ENTITY_TYPE' => Rights::ENTITY_TYPE_SITE,
280 'ACCESS_CODE' => 'SG' . $this->bindingId . '_%',
281 'ROLE_ID' => $roleId
282 ]
283 ]);
284 while ($row = $res->fetch())
285 {
286 RightsTable::delete($row['ID'])->isSuccess();
287 }
288 }
289
296 private function getOperationsToRolesMap(int $groupId): array
297 {
298 $opsToRoles = [];
299
300 if (!\Bitrix\Main\Loader::includeModule('socialnetwork'))
301 {
302 return $opsToRoles;
303 }
304
305 $res = \CSocNetFeaturesPerms::getList(
306 [],
307 [
308 'FEATURE_ENTITY_ID' => $groupId,
309 'FEATURE_ENTITY_TYPE' => SONET_ENTITY_GROUP,
310 'FEATURE_FEATURE' => SocialNetwork::SETTINGS_CODE,
311 ],
312 );
313 while ($row = $res->fetch())
314 {
315 $opsToRoles[$row['OPERATION_ID']] = $row['ROLE'];
316 }
317
318 return $opsToRoles;
319 }
320}
addSiteRights(int $siteId, array $groupRoles=[])
Definition group.php:190
static getAccessTasks(bool $fullData=false)
Definition group.php:115
static getSiteIdByGroupId(int $groupId)
Definition group.php:29
static recognizeSiteTitle(array $input)
Definition group.php:50
updateSiteRights(int $siteId)
Definition group.php:257