Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
AccessController.php
1<?php
10
26
28{
30
34 public function __construct(int $userId)
35 {
36 parent::__construct($userId);
37
38 $this->ruleFactory = new CatalogRuleFactory();
39 $this->iblockRuleFactory = new IblockRuleFactory();
40 $this->filterFactory = new CatalogFilterFactory();
41 }
42
43 public static function getCurrent(): self
44 {
45 global $USER;
46
47 $userId = 0;
48 if (isset($USER) && $USER instanceof \CUser)
49 {
50 $userId = (int)$USER->GetID();
51 }
52
53 return static::getInstance($userId);
54 }
55
65 public function check(string $action, AccessibleItem $item = null, $params = null): bool
66 {
67 if (!ModuleManager::isModuleInstalled('crm') || InstallStatus::inProgress())
68 {
69 return $this->checkLegacy($action);
70 }
71
72 $params ??= [];
73 if (is_array($params))
74 {
75 $params['action'] = $action;
76 }
77
78 return parent::check($action, $item, $params);
79 }
80
81 public function checkByValue(string $action, string $value): bool
82 {
83 return $this->check(
84 $action,
85 null,
86 ['value' => $value]
87 );
88 }
89
94 public function getPermissionValue(string $action)
95 {
96 $ruleObject = $this->ruleFactory->createFromAction($action, $this);
97 if (! $ruleObject instanceof BaseRule)
98 {
99 return null;
100 }
101
102 $params = ['action' => $action];
103
104 return
105 $ruleObject instanceof VariableRule
106 ? $ruleObject->getPermissionMultiValues($params)
107 : $ruleObject->getPermissionValue($params)
108 ;
109 }
110
116 private function checkLegacy(string $action): bool
117 {
118 if (CurrentUser::get()->isAdmin())
119 {
120 return true;
121 }
122
123 $legacyActions = ActionDictionary::getLegacyMap();
124 $legacyAction = null;
125 if (array_key_exists($action, $legacyActions))
126 {
127 $legacyAction = $action;
128 }
129 else
130 {
131 foreach ($legacyActions as $oldAction => $newActions)
132 {
133 if (in_array($action, $newActions, true))
134 {
135 $legacyAction = $oldAction;
136
137 break;
138 }
139 }
140 }
141
142 return $legacyAction && CurrentUser::get()->canDoOperation($legacyAction);
143 }
144
145 public function isAdmin()
146 {
147 return $this->user->isAdmin() || (Loader::includeModule("bitrix24") && \CBitrix24::isPortalAdmin($this->user->getUserId()));
148 }
149
150 protected function loadItem(int $itemId = null): ?AccessibleItem
151 {
152 return null;
153 }
154
155 protected function loadUser(int $userId): UserModel
156 {
157 return UserModel::createFromId($userId);
158 }
159
167 public function getAllowedDefaultStoreId(): ?int
168 {
169 $allowStoresIds = $this->getPermissionValue(ActionDictionary::ACTION_STORE_VIEW) ?? [];
170 if (empty($allowStoresIds))
171 {
172 return null;
173 }
174 $allowStoresIds = array_map('intval', $allowStoresIds);
175
176 $defaultStoreId = StoreTable::getDefaultStoreId();
177 if ($defaultStoreId)
178 {
179 $allStoresId = PermissionDictionary::VALUE_VARIATION_ALL;
180 if (
181 in_array($defaultStoreId, $allowStoresIds, true)
182 || in_array($allStoresId, $allowStoresIds, true)
183 )
184 {
185 return $defaultStoreId;
186 }
187 }
188
189 return reset($allowStoresIds);
190 }
191
196 public function hasIblockAccess(string $action): bool
197 {
199 $rule = $this->iblockRuleFactory->createFromAction($action, $this);
200 if (!$rule)
201 {
202 throw new UnknownActionException($action);
203 }
204
205 return $rule->execute();
206 }
207
214 public function checkCompleteRight(string $action): bool
215 {
216 $permissionValue = $this->getPermissionValue($action);
217
218 if ($permissionValue === null)
219 {
220 return false;
221 }
222
223 if (is_array($permissionValue))
224 {
225 return in_array(PermissionDictionary::VALUE_VARIATION_ALL, $permissionValue, true);
226 }
227
228 return $this->check($action);
229 }
230}
checkByValue(string $action, string $value)
check(string $action, AccessibleItem $item=null, $params=null)