1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
UpdatePermissionsHandler.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Control\Handler\Update;
6
7use Bitrix\Main\DI\ServiceLocator;
8use Bitrix\SocialNetwork\Collab\Analytics\CollabAnalytics;
9use Bitrix\Socialnetwork\Collab\Collab;
10use Bitrix\Socialnetwork\Collab\Control\Command\CollabUpdateCommand;
11use Bitrix\Socialnetwork\Collab\Log\CollabLogEntryCollection;
12use Bitrix\Socialnetwork\Collab\Log\Entry\UpdateCollabLogEntry;
13use Bitrix\Socialnetwork\Collab\Property\Permission;
14use Bitrix\Socialnetwork\Control\Command\UpdateCommand;
15use Bitrix\Socialnetwork\Control\Handler\HandlerResult;
16use Bitrix\Socialnetwork\Item\Workgroup;
17use Bitrix\Socialnetwork\Provider\FeatureProvider;
18use CSocNetFeaturesPerms;
19
21{
22 public function update(UpdateCommand $command, Workgroup $entityBefore, Workgroup $entityAfter): HandlerResult
23 {
24 $handlerResult = new HandlerResult();
25
26 $permissions = $command->getPermissions()?->getValue() ?? [];
27 if (empty($permissions))
28 {
29 return $handlerResult;
30 }
31
32 $handlerResult->setGroupChanged();
33
34 $currentFeatures = FeatureProvider::getInstance()->getFeatures($command->getId());
35 foreach ($permissions as $featureName => $operations)
36 {
37 $featureId = $currentFeatures[$featureName]?->id ?? 0;
38 if ($featureId <= 0)
39 {
40 continue;
41 }
42
43 foreach ($operations as $operationName => $operationValue)
44 {
45 $operationId = CSocNetFeaturesPerms::SetPerm(
46 $featureId,
47 $operationName,
48 $operationValue
49 );
50
51 if (!$operationId)
52 {
53 $handlerResult->addApplicationError();
54 }
55 }
56 }
57
58 return $handlerResult->merge($this->writePermissionsChangesToLog($command, $entityBefore));
59 }
60
61 private function writePermissionsChangesToLog(UpdateCommand $command, Workgroup $entityBefore): HandlerResult
62 {
63 $handlerResult = new HandlerResult();
64
65 if (!($command instanceof CollabUpdateCommand) || !($entityBefore instanceof Collab))
66 {
67 return $handlerResult;
68 }
69
70 $permissions = $command->getPermissions()?->getValue() ?? null;
71
72 if (empty($permissions))
73 {
74 return $handlerResult;
75 }
76
77 $analytics = CollabAnalytics::getInstance();
78
79 $logEntryCollection = new CollabLogEntryCollection();
80 $previousPermissions = $entityBefore->getPermissions();
81
82 foreach ($permissions as $featureName => $operations)
83 {
84 $previousPermission = $this->getPermissionByFeature($previousPermissions, $featureName);
85
86 if (!$previousPermission)
87 {
88 continue;
89 }
90
91 $previousPermission = $previousPermission->toArray();
92
93 foreach ($operations as $operationName => $operationValue)
94 {
95 $previousValue = $previousPermission[$featureName][$operationName] ?? null;
96
97 if ($previousValue === $operationValue)
98 {
99 continue;
100 }
101
102 $logEntry = new UpdateCollabLogEntry(
103 userId: $command->getInitiatorId(),
104 collabId: $command->getId(),
105 );
106
107 $fieldName = UpdateCollabLogEntry::PERMISSION_FIELD_PREFIX . '_' . $featureName . '_' . $operationName;
108
109 $logEntry
110 ->setFieldName($fieldName)
111 ->setPreviousValue($previousValue)
112 ->setCurrentValue($operationValue)
113 ;
114
115 $logEntryCollection->add($logEntry);
116
117 $analytics->onSettingsChanged($command->getInitiatorId(), $command->getId(), $operationName);
118 }
119 }
120
121 if ($logEntryCollection->isEmpty())
122 {
123 return $handlerResult;
124 }
125
126 $service = ServiceLocator::getInstance()->get('socialnetwork.collab.log.service');
127 $service->saveCollection($logEntryCollection);
128
129 return $handlerResult;
130 }
131
132 private function getPermissionByFeature(array $permissions, string $name): ?Permission
133 {
134 foreach ($permissions as $permission)
135 {
136 if (isset($permission?->toArray()[$name]))
137 {
138 return $permission;
139 }
140 }
141
142 return null;
143 }
144}
Определения CollabLogEntryCollection.php:9
update(UpdateCommand $command, Workgroup $entityBefore, Workgroup $entityAfter)
Определения UpdatePermissionsHandler.php:22
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$name
Определения menu_edit.php:35
toArray(bool $recursive=false)
Определения ActiveRecordImplementation.php:562
$service
Определения payment.php:18