Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseaccesscontroller.php
1<?php
9namespace Bitrix\Main\Access;
10
20
22 implements AccessibleController
23{
27 protected const RULE_SUFFIX = 'Rule';
28
29 protected static $register = [];
30
31 /* @var AccessibleUser $user */
32 protected $user;
33
35
37
38 public static function getInstance($userId)
39 {
40 if (!isset(static::$register[static::class][$userId]))
41 {
42 static::$register[static::class][$userId] = new static($userId);
43 }
44 return static::$register[static::class][$userId];
45 }
46
47 public static function can($userId, string $action, $itemId = null, $params = null): bool
48 {
49 $userId = (int) $userId;
50 $itemId = (int) $itemId;
51
52 $controller = static::getInstance($userId);
53 return $controller->checkByItemId($action, $itemId, $params);
54 }
55
56 public function __construct(int $userId)
57 {
58 $this->user = $this->loadUser($userId);
59 $this->ruleFactory = new RuleControllerFactory();
60 $this->filterFactory = new FilterControllerFactory();
61 }
62
63 public function getUser(): AccessibleUser
64 {
65 return $this->user;
66 }
67
68 public function checkByItemId(string $action, int $itemId = null, $params = null): bool
69 {
70 $item = $this->loadItem($itemId);
71 return $this->check($action, $item, $params);
72 }
73
74 public function check(string $action, AccessibleItem $item = null, $params = null): bool
75 {
76 $rule = $this->ruleFactory->createFromAction($action, $this);
77 if (!$rule)
78 {
79 throw new UnknownActionException($action);
80 }
81
82 $event = $this->sendEvent(EventDictionary::EVENT_ON_BEFORE_CHECK, $action, $item, $params);
83 $isAccess = $event->isAccess();
84
85 if (!is_null($isAccess))
86 {
87 return $isAccess;
88 }
89
90 $isAccess = $rule->execute($item, $params);
91
92 $event = $this->sendEvent(EventDictionary::EVENT_ON_AFTER_CHECK, $action, $item, $params, $isAccess);
93
94 $isAccess = $event->isAccess() ?? $isAccess;
95
96 return $isAccess;
97 }
98
111 public function batchCheck(array $request, AccessibleItem $item): array
112 {
113 $result = [];
114 foreach ($request as $actionId => $params)
115 {
116 $result[$actionId] = $this->check($actionId, $item, $params);
117 }
118 return $result;
119 }
120
121 abstract protected function loadItem(int $itemId = null): ?AccessibleItem;
122
123 abstract protected function loadUser(int $userId): AccessibleUser;
124
128 protected function getRuleName(string $action): ?string
129 {
130 $action = explode('_', $action);
131 $action = array_map(function($el) {
132 return ucfirst(strtolower($el));
133 }, $action);
134 return $this->getRuleNamespace() . implode($action) . static::RULE_SUFFIX;
135 }
136
140 protected function getRuleNamespace(): string
141 {
142 $class = new \ReflectionClass($this);
143 $namespace = $class->getNamespaceName();
144 return $namespace.'\\'.static::RULE_SUFFIX.'\\';
145 }
146
147 protected function sendEvent(string $eventName, string $action, AccessibleItem $item = null, $params = null, bool $isAccess = null)
148 {
149 $event = new Event(
150 static::class,
151 $eventName,
152 [
153 'user' => $this->user,
154 'item' => $item,
155 'action' => $action,
156 'params' => $params,
157 'isAccess' => $isAccess
158 ]
159 );
160 $event->send();
161
162 return $event;
163 }
164
168 public function getEntityFilter(string $action, string $entityName, $params = null): ?array
169 {
170 $filter = $this->filterFactory->createFromAction($action, $this);
171 if (!$filter)
172 {
173 return null;
174 }
175
176 $params = (array)($params ?? []);
177 $params['action'] = $action;
178
179 return $filter->getFilter($entityName, $params);
180 }
181}
getEntityFilter(string $action, string $entityName, $params=null)
batchCheck(array $request, AccessibleItem $item)
static can($userId, string $action, $itemId=null, $params=null)
checkByItemId(string $action, int $itemId=null, $params=null)
sendEvent(string $eventName, string $action, AccessibleItem $item=null, $params=null, bool $isAccess=null)
check(string $action, AccessibleItem $item=null, $params=null)