ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
BasicAccessCheckClosures.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use Closure;
25 use Throwable;
28 
35 {
39  private Container $dic;
40  private array $access_cache = [];
41 
45  public function __construct(?Container $dic = null)
46  {
47  global $DIC;
48  $this->dic = $dic ?? $DIC;
49  }
50 
51  public function isRepositoryReadable(?Closure $additional = null): Closure
52  {
53  if (!isset($this->access_cache['repo_read'])) {
54  $is_user_logged_in = $this->isUserLoggedIn()();
55  if ($is_user_logged_in) {
56  $this->access_cache['repo_read'] = $this->dic->access()->checkAccess(
57  'read',
58  '',
60  );
61  } else {
62  $this->access_cache['repo_read'] = $this->dic->settings()->get('pub_section') && $this->dic->access(
63  )->checkAccessOfUser(
64  $this->dic->user()->getId() ?: ANONYMOUS_USER_ID,
65  'read',
66  '',
68  );
69  }
70  }
71 
72  return $this->getClosureWithOptinalClosure(fn(): bool => $this->access_cache['repo_read'], $additional);
73  }
74 
75  public function isRepositoryVisible(?Closure $additional = null): Closure
76  {
77  if (!isset($this->access_cache['repo_visible'])) {
78  $is_user_logged_in = $this->isUserLoggedIn()();
79  if ($is_user_logged_in) {
80  $this->access_cache['repo_visible'] = $this->dic->access()->checkAccess(
81  'visible',
82  '',
84  );
85  } else {
86  $this->access_cache['repo_visible'] = $this->dic->settings()->get('pub_section') && $this->dic->access(
87  )->checkAccessOfUser(
88  $this->dic->user()->getId() ?: ANONYMOUS_USER_ID,
89  'visible',
90  '',
92  );
93  }
94  }
95 
96  return $this->getClosureWithOptinalClosure(fn(): bool => $this->access_cache['repo_visible'], $additional);
97  }
98 
99  public function isUserLoggedIn(?Closure $additional = null): Closure
100  {
101  if (!isset($this->access_cache['is_anonymous'])) {
102  $this->access_cache['is_anonymous'] = ($this->dic->user()->isAnonymous() || $this->dic->user()->getId(
103  ) === 0);
104  }
105 
106  return $this->getClosureWithOptinalClosure(fn(): bool => !$this->access_cache['is_anonymous'], $additional);
107  }
108 
109  public function hasAdministrationAccess(?Closure $additional = null): Closure
110  {
111  if (!isset($this->access_cache['has_admin_access'])) {
112  $this->access_cache['has_admin_access'] = ($this->dic->rbac()->system()->checkAccess(
113  'visible',
115  ));
116  }
117  return $this->getClosureWithOptinalClosure(fn(): bool => $this->access_cache['has_admin_access'], $additional);
118  }
119 
120 
121  //
122  // Internal
123  //
124 
125  private function checkClosureForBoolReturnValue(Closure $c): bool
126  {
127  try {
128  $r = new ReflectionFunction($c);
129  } catch (Throwable) {
130  return false;
131  }
132 
133  if (!$r->hasReturnType() || !$r->getReturnType()->isBuiltin()) {
134  throw new InvalidArgumentException('the additional Closure MUST return a bool dy declaration');
135  }
136  return true;
137  }
138 
139  private function getClosureWithOptinalClosure(Closure $closure, ?Closure $additional = null): Closure
140  {
141  if ($additional instanceof Closure && $this->checkClosureForBoolReturnValue($additional)) {
142  return static fn(): bool => $additional() && $closure();
143  }
144 
145  return $closure;
146  }
147 }
const ANONYMOUS_USER_ID
Definition: constants.php:27
const ROOT_FOLDER_ID
Definition: constants.php:32
$c
Definition: deliver.php:25
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
const SYSTEM_FOLDER_ID
Definition: constants.php:35
__construct(?Container $dic=null)
BasicAccessCheckClosuresSingleton constructor.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getClosureWithOptinalClosure(Closure $closure, ?Closure $additional=null)
global $DIC
Definition: shib_login.php:22
$r