ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
BasicAccessCheckClosures.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
4
5use Closure;
6use ReflectionFunction;
7
13{
14
18 protected static $instance;
19 private $dic;
20
25 protected function __construct()
26 {
27 global $DIC;
28 $this->dic = $DIC;
29 }
30
34 public static function getInstance() : self
35 {
36 if (!isset(self::$instance)) {
37 self::$instance = new self();
38 }
39
40 return self::$instance;
41 }
42
43 public function isRepositoryReadable(?Closure $additional = null) : Closure
44 {
45 static $repo_read;
46 if (!isset($repo_read)) {
47 $is_user_logged_in = $this->isUserLoggedIn()();
48 if (!$is_user_logged_in) {
49 $repo_read = (bool) $this->dic->settings()->get('pub_section') && $this->dic->access()->checkAccess('read', '', ROOT_FOLDER_ID);
50 } else {
51 $repo_read = (bool) $this->dic->access()->checkAccess('read', '', ROOT_FOLDER_ID);
52 }
53 }
54
55 return $this->getClosureWithOptinalClosure(static function () use ($repo_read) : bool {
56 return $repo_read;
57 }, $additional);
58 }
59
60 public function isPublicSectionActive(?Closure $additional = null) : Closure
61 {
62 static $public_section_active;
63 if (!isset($public_section_active)) {
64 $public_section_active = (bool) $this->dic->settings()->get('pub_section') && $this->dic->access()->checkAccess('visible',
65 '', ROOT_FOLDER_ID);
66 }
67
68 return $this->getClosureWithOptinalClosure(function () use ($public_section_active) : bool {
69 return $public_section_active;
70 }, $additional);
71 }
72
73 public function isRepositoryVisible(?Closure $additional = null) : Closure
74 {
75 static $repo_visible;
76 if (!isset($repo_visible)) {
77 $is_user_logged_in = $this->isUserLoggedIn()();
78 if (!$is_user_logged_in) {
79 $repo_visible = (bool) $this->dic->settings()->get('pub_section') && $this->dic->access()->checkAccess('visible', '', ROOT_FOLDER_ID);
80 } else {
81 $repo_visible = (bool) $this->dic->access()->checkAccess('visible', '', ROOT_FOLDER_ID);
82 }
83 }
84
85 return $this->getClosureWithOptinalClosure(static function () use ($repo_visible) : bool {
86 return $repo_visible;
87 }, $additional);
88 }
89
90 public function isUserLoggedIn(?Closure $additional = null) : Closure
91 {
92 static $is_anonymous;
93 if (!isset($is_anonymous)) {
94 $is_anonymous = (bool) $this->dic->user()->isAnonymous() || ($this->dic->user()->getId() == 0);
95 }
96
97 return $this->getClosureWithOptinalClosure(static function () use ($is_anonymous) : bool {
98 return !$is_anonymous;
99 }, $additional);
100 }
101
102 public function hasAdministrationAccess(?Closure $additional = null) : Closure
103 {
104 static $has_admin_access;
105 if (!isset($has_admin_access)) {
106 $has_admin_access = (bool) ($this->dic->rbac()->system()->checkAccess('visible', SYSTEM_FOLDER_ID));
107 }
108 return $this->getClosureWithOptinalClosure(static function () use ($has_admin_access) : bool {
109 return $has_admin_access;
110 }, $additional);
111 }
112
113
114 //
115 // Internal
116 //
117
118 private function checkClosureForBoolReturnValue(Closure $c) : bool
119 {
120 try {
121 $r = new ReflectionFunction($c);
122 } catch (\Throwable $e) {
123 return false;
124 }
125
126 if(!$r->hasReturnType() || !$r->getReturnType()->isBuiltin()){
127 throw new \InvalidArgumentException('the additional Closure MUST return a bool dy declaration');
128 }
129 return true;
130 }
131
132 private function getClosureWithOptinalClosure(Closure $closure, ?Closure $additional = null) : Closure
133 {
134 if ($additional instanceof Closure && $this->checkClosureForBoolReturnValue($additional)) {
135 return static function () use ($closure, $additional) : bool {
136 return $additional() && $closure();
137 };
138 }
139
140 return $closure;
141 }
142}
An exception for terminatinating execution or to throw for unit testing.
__construct()
BasicAccessCheckClosures constructor.
getClosureWithOptinalClosure(Closure $closure, ?Closure $additional=null)
$additional
Definition: goto.php:50
$DIC
Definition: xapitoken.php:46