Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
rolepermissionservice.php
1<?php
3
16
17Loc::loadMessages(__FILE__);
18
20{
21 private const DB_ERROR_KEY = "SENDER_CONFIG_PERMISSIONS_DB_ERROR";
22
26 private $roleRelationService;
27
37 public function saveRolePermissions(array &$permissionSettings, int $dealCategoryId = -1): void
38 {
39 $query = [];
40 $roles = [];
41
42 foreach ($permissionSettings as &$setting)
43 {
44
45 $roleId = (int) $setting['id'];
46 $roleTitle = (string) $setting['title'];
47
48 $roleId = $this->saveRole($roleTitle, $dealCategoryId, $roleId);
49 $setting['id'] = $roleId;
50 $roles[] = $roleId;
51
52 if(!isset($setting['accessRights']))
53 {
54 continue;
55 }
56 $adsAccessMap = SectionDictionary::getAdsAccessMap();
57
58 foreach ($setting['accessRights'] as $permission)
59 {
60 if((int)$permission['id'] < 1)
61 {
62 continue;
63 }
64 $messageCodeByPermission = $adsAccessMap[(int)$permission['id']] ?? null;
65
66 if ($messageCodeByPermission !== null && !Service::isAdVisibleInRegion($messageCodeByPermission))
67 {
68 continue;
69 }
70
71 $query[] = [
72 'ROLE_ID' => $roleId,
73 'PERMISSION_ID' => $permission['id'],
74 'VALUE' => $permission['value'],
75 ];
76 }
77 }
78
79 if($query)
80 {
81 if (!PermissionTable::deleteList(["=ROLE_ID" =>$roles]))
82 {
83 throw new SqlQueryException(Loc::getMessage(self::DB_ERROR_KEY));
84 }
85
86 try
87 {
88 RoleUtil::insertPermissions($query);
89 if (\Bitrix\Main\Loader::includeModule("intranet"))
90 {
91 \CIntranetUtils::clearMenuCache();
92 }
93 } catch (\Exception $e)
94 {
95 throw new SqlQueryException(self::DB_ERROR_KEY);
96 }
97 }
98 }
99
109 public function saveRole(string $name, int $dealCategoryId = -1, int $roleId = null): int
110 {
111 $nameField = [
112 "NAME" => Encoding::convertEncodingToCurrent($name),
113 "DEAL_CATEGORY_ID" => $dealCategoryId
114 ];
115
116 try
117 {
118 if(!$roleId)
119 {
120 if(!
121 (
122 $role = RoleTable::getList(
123 ['filter' => [
124 '=NAME' => $nameField['NAME'],
125 '=DEAL_CATEGORY_ID' => $nameField['DEAL_CATEGORY_ID']
126 ]])->fetchObject()
127 )
128 )
129 {
130 $role = RoleTable::add($nameField);
131 }
132 }
133 else
134 {
135 $role = RoleTable::update($roleId, $nameField);
136 }
137 } catch (\Exception $e)
138 {
139 throw new SqlQueryException(self::DB_ERROR_KEY);
140 }
141
142 return $role->getId();
143 }
144
149 public function deleteRole(int $roleId): void
150 {
151 if(!is_int($roleId))
152 {
153 return;
154 }
155
156 if (!PermissionTable::deleteList(["=ROLE_ID" => $roleId]))
157 {
158 throw new SqlQueryException(Loc::getMessage(self::DB_ERROR_KEY));
159 }
160
161 $this->roleRelationService->deleteRoleRelations($roleId);
162
163 if (!RoleTable::delete($roleId))
164 {
165 throw new SqlQueryException(Loc::getMessage(self::DB_ERROR_KEY));
166 }
167 }
168
169 public function __construct()
170 {
171 $this->roleRelationService = new RoleRelationService();
172 }
173
177 public function getRoleList(array $parameters = []): array
178 {
179 return RoleTable::getList($parameters)->fetchAll();
180 }
181
185 public function getSavedPermissions(array $parameters = []): array
186 {
187 return PermissionTable::getList($parameters)->fetchAll();
188 }
189
193 public function getRoleListByUser(int $userId): array
194 {
195 $userAccessCodes = \CAccess::getUserCodesArray($userId);
196
197 return $this->roleRelationService->getRolesByRelations($userAccessCodes);
198 }
199
205 public function getUserGroups(int $dealCategoryId): array
206 {
207 $res = $this->getRoleList(
208 [
209 "filter" => ["=DEAL_CATEGORY_ID" => $dealCategoryId]
210 ]
211 );
212
213 $roles = [];
214 foreach ($res as $row)
215 {
216 $roleId = (int) $row['ID'];
217
218 $roles[] = [
219 'id' => $roleId,
220 'title' => $row['NAME'],
221 'accessRights' => $this->getRoleAccessRights($roleId),
222 'members' => $this->getRoleMembers($roleId)
223 ];
224 }
225
226 return $roles;
227 }
228
233 public function getAccessRights(): array
234 {
235 $sections = SectionDictionary::getMap();
236 $adsAccessMap = SectionDictionary::getAdsAccessMap();
237
238 $res = [];
239
240 foreach ($sections as $sectionId => $permissions)
241 {
242
243 $rights = [];
244 foreach ($permissions as $permissionId)
245 {
246 $messageCodeByPermission = $adsAccessMap[$permissionId] ?? null;
247 if (
248 $messageCodeByPermission !== null
249 && !Service::isAdVisibleInRegion($adsAccessMap[$permissionId])
250 )
251 {
252 continue;
253 }
254 if (
255 $messageCodeByPermission === iBase::CODE_MASTER_YANDEX
256 && !Service::isMasterYandexVisibleInRegion()
257 )
258 {
259 continue;
260 }
261
262 $rights[] = [
263 'id' => $permissionId,
264 'type' => PermissionDictionary::getType($permissionId),
265 'title' => PermissionDictionary::getTitle($permissionId)
266 ];
267 }
268 $res[] = [
269 'sectionTitle' => SectionDictionary::getTitle($sectionId),
270 'rights' => $rights
271 ];
272 }
273
274 return $res;
275 }
276
277 private function getRoleAccessRights(int $roleId): array
278 {
279 $settings = $this->getSettings();
280
281 $accessRights = [];
282 if (array_key_exists($roleId, $settings))
283 {
284 foreach ($settings[$roleId] as $permissionId => $permission)
285 {
286 $accessRights[] = [
287 'id' => $permissionId,
288 'value' => $permission['VALUE']
289 ];
290 }
291 }
292
293 return $accessRights;
294 }
295
296 private function getMemberInfo(string $code)
297 {
298 $accessCode = new AccessCode($code);
299 $member = (new DataProvider())->getEntity($accessCode->getEntityType(), $accessCode->getEntityId());
300 return $member->getMetaData();
301 }
302
303
304 private function getRoleMembers(int $roleId): array
305 {
306 $members = [];
307
308 $relations = $this
309 ->roleRelationService
310 ->getRelationList(["filter" =>["=ROLE_ID" => $roleId]]);
311
312 foreach ($relations as $row)
313 {
314 $accessCode = $row['RELATION'];
315 $members[$accessCode] = $this->getMemberInfo($accessCode);
316 }
317
318 return $members;
319 }
320
321 private function getSettings()
322 {
323 $settings = [];
324 $res = $this->getSavedPermissions();
325
326 foreach ($res as $row)
327 {
328 $settings[$row['ROLE_ID']][$row['PERMISSION_ID']] = $row;
329 }
330 return $settings;
331 }
332}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
saveRole(string $name, int $dealCategoryId=-1, int $roleId=null)
saveRolePermissions(array &$permissionSettings, int $dealCategoryId=-1)