Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
featurepermregistry.php
1<?php
2
4
6{
7 private static $instance;
8
9 private array $storage = [];
10
14 public static function getInstance(): self
15 {
16 if (!self::$instance)
17 {
18 self::$instance = new self();
19 }
20 return self::$instance;
21 }
22
26 private function __construct()
27 {
28
29 }
30
39 public function get(int $entityId, string $feature, string $operation, int $userId = 0, string $entityType = SONET_ENTITY_GROUP): bool
40 {
41 if (
42 !$entityId
43 || empty($feature)
44 || empty($operation)
45 || !in_array($entityType, [ SONET_ENTITY_GROUP, SONET_ENTITY_USER ], true)
46 )
47 {
48 return false;
49 }
50
51 $storageKey = $this->getStorageKey($userId, $entityType, $feature, $operation);
52
53 if (!isset($this->storage[$storageKey][$entityId]))
54 {
55 $this->load([$entityId], $feature, $operation, $userId, $entityType);
56 }
57
58 if (!isset($this->storage[$storageKey][$entityId]))
59 {
60 return false;
61 }
62
63 return $this->storage[$storageKey][$entityId];
64 }
65
74 public function load(array $entityIdList, string $feature, string $operation, int $userId = 0, string $entityType = SONET_ENTITY_GROUP): self
75 {
76 if (
77 empty($entityIdList)
78 || empty($feature)
79 || empty($operation)
80 || !in_array($entityType, [ SONET_ENTITY_GROUP, SONET_ENTITY_USER ], true)
81 )
82 {
83 return $this;
84 }
85
86 $storageKey = $this->getStorageKey($userId, $entityType, $feature, $operation);
87
88 if (!isset($this->storage[$storageKey]))
89 {
90 $this->storage[$storageKey] = [];
91 }
92
93 $entityIdList = array_diff(array_unique($entityIdList), array_keys($this->storage[$storageKey]));
94 if (empty($entityIdList))
95 {
96 return $this;
97 }
98
99 $permissionData = \CSocNetFeaturesPerms::canPerformOperation($userId, $entityType, $entityIdList, $feature, $operation);
100 if (!is_array($permissionData))
101 {
102 return $this;
103 }
104
105 foreach ($entityIdList as $id)
106 {
107 $this->storage[$storageKey][$id] = false;
108 }
109
110 foreach ($permissionData as $id => $hasAccess)
111 {
112 $this->storage[$storageKey][$id] = $hasAccess;
113 }
114
115 return $this;
116 }
117
118 private function getStorageKey(int $userId = 0, string $entityType = SONET_ENTITY_GROUP, $feature = '', string $operation = ''): string
119 {
120 return implode(' ', [ (string)$userId, $entityType, $feature, $operation ]);
121 }
122}
load(array $entityIdList, string $feature, string $operation, int $userId=0, string $entityType=SONET_ENTITY_GROUP)