ILIAS  release_8 Revision v8.24
BasicAccessCheckClosures.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Closure;
24use ReflectionFunction;
25use Throwable;
26use InvalidArgumentException;
28
35{
36 private Container $dic;
37 private array $access_cache = [];
38
42 public function __construct(?Container $dic = null)
43 {
44 global $DIC;
45 $this->dic = $dic ?? $DIC;
46 }
47
48 public function isRepositoryReadable(?Closure $additional = null): Closure
49 {
50 if (!isset($this->access_cache['repo_read'])) {
51 $is_user_logged_in = $this->isUserLoggedIn()();
52 if ($is_user_logged_in) {
53 $this->access_cache['repo_read'] = $this->dic->access()->checkAccess(
54 'read',
55 '',
57 );
58 } else {
59 $this->access_cache['repo_read'] = $this->dic->settings()->get('pub_section') && $this->dic->access(
60 )->checkAccessOfUser(
61 $this->dic->user()->getId() ?: ANONYMOUS_USER_ID,
62 'read',
63 '',
65 );
66 }
67 }
68
69 return $this->getClosureWithOptinalClosure(function (): bool {
70 return $this->access_cache['repo_read'];
71 }, $additional);
72 }
73
74 public function isRepositoryVisible(?Closure $additional = null): Closure
75 {
76 if (!isset($this->access_cache['repo_visible'])) {
77 $is_user_logged_in = $this->isUserLoggedIn()();
78 if ($is_user_logged_in) {
79 $this->access_cache['repo_visible'] = $this->dic->access()->checkAccess(
80 'visible',
81 '',
83 );
84 } else {
85 $this->access_cache['repo_visible'] = $this->dic->settings()->get('pub_section') && $this->dic->access(
86 )->checkAccessOfUser(
87 $this->dic->user()->getId() ?: ANONYMOUS_USER_ID,
88 'visible',
89 '',
91 );
92 }
93 }
94
95 return $this->getClosureWithOptinalClosure(function (): bool {
96 return $this->access_cache['repo_visible'];
97 }, $additional);
98 }
99
100 public function isUserLoggedIn(?Closure $additional = null): Closure
101 {
102 if (!isset($this->access_cache['is_anonymous'])) {
103 $this->access_cache['is_anonymous'] = ($this->dic->user()->isAnonymous() || $this->dic->user()->getId(
104 ) === 0);
105 }
106
107 return $this->getClosureWithOptinalClosure(function (): bool {
108 return !$this->access_cache['is_anonymous'];
109 }, $additional);
110 }
111
112 public function hasAdministrationAccess(?Closure $additional = null): Closure
113 {
114 if (!isset($this->access_cache['has_admin_access'])) {
115 $this->access_cache['has_admin_access'] = ($this->dic->rbac()->system()->checkAccess(
116 'visible',
118 ));
119 }
120 return $this->getClosureWithOptinalClosure(function (): bool {
121 return $this->access_cache['has_admin_access'];
122 }, $additional);
123 }
124
125
126 //
127 // Internal
128 //
129
130 private function checkClosureForBoolReturnValue(Closure $c): bool
131 {
132 try {
133 $r = new ReflectionFunction($c);
134 } catch (Throwable $e) {
135 return false;
136 }
137
138 if (!$r->hasReturnType() || !$r->getReturnType()->isBuiltin()) {
139 throw new InvalidArgumentException('the additional Closure MUST return a bool dy declaration');
140 }
141 return true;
142 }
143
144 private function getClosureWithOptinalClosure(Closure $closure, ?Closure $additional = null): Closure
145 {
146 if ($additional instanceof Closure && $this->checkClosureForBoolReturnValue($additional)) {
147 return static function () use ($closure, $additional): bool {
148 return $additional() && $closure();
149 };
150 }
151
152 return $closure;
153 }
154}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
__construct(?Container $dic=null)
BasicAccessCheckClosuresSingleton constructor.
getClosureWithOptinalClosure(Closure $closure, ?Closure $additional=null)
$c
Definition: cli.php:38
const ANONYMOUS_USER_ID
Definition: constants.php:27
const SYSTEM_FOLDER_ID
Definition: constants.php:35
const ROOT_FOLDER_ID
Definition: constants.php:32
global $DIC
Definition: feed.php:28
$additional
Definition: goto.php:53