Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
accesscontroller.php
1<?php
10
17
19{
20 private const BASE_RULE = 'Base';
21
31 public function check(string $action, AccessibleItem $item = null, $params = null): bool
32 {
33 $ruleName = $this->getRuleName($action);
34
35 if (!$ruleName || !class_exists($ruleName))
36 {
37 throw new UnknownActionException($action);
38 }
39
40 $event = $this->sendEvent(EventDictionary::EVENT_ON_BEFORE_CHECK, $action, null, $params);
41 $isAccess = $event->isAccess();
42
43 if (!is_null($isAccess))
44 {
45 return $isAccess;
46 }
47
48 $params['action'] = $action;
49 $isAccess = (new $ruleName($this))->execute(null, $params);
50
51 if($isAccess)
52 {
53 return $isAccess;
54 }
55
56 $event = $this->sendEvent(EventDictionary::EVENT_ON_AFTER_CHECK, $action,null, $params, $isAccess);
57
58 $isAccess = $event->isAccess() ?? $isAccess;
59
60 return $isAccess;
61 }
62
63 public function isAdmin()
64 {
65 return $this->user->isAdmin();
66 }
67
68 protected function getRuleName(string $actionCode): ?string
69 {
70 $actionName = ActionDictionary::getActionName($actionCode);
71 if (!$actionName)
72 {
73 return null;
74 }
75
76 $action = explode('_', $actionName);
77 $action = array_map(
78 function($el)
79 {
80 return ucfirst(mb_strtolower($el));
81 }, $action
82 );
83
84 $ruleClass = $this->getRuleNamespace() .implode($action).self::RULE_SUFFIX;
85
86
87 if(class_exists($ruleClass))
88 {
89 return $ruleClass;
90 }
91
92 return $this->getRuleNamespace().self::BASE_RULE.self::RULE_SUFFIX;
93 }
94
95 protected function loadItem(int $itemId = null): ?AccessibleItem
96 {
97 return null;
98 }
99
100 protected function loadUser(int $userId): AccessibleUser
101 {
102 return UserModel::createFromId($userId);
103 }
104}
sendEvent(string $eventName, string $action, AccessibleItem $item=null, $params=null, bool $isAccess=null)
check(string $action, AccessibleItem $item=null, $params=null)