ILIAS  release_7 Revision v7.30-3-g800a261c036
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 isRepositoryVisible(?Closure $additional = null) : Closure
61 {
62 static $repo_visible;
63 if (!isset($repo_visible)) {
64 $is_user_logged_in = $this->isUserLoggedIn()();
65 if (!$is_user_logged_in) {
66 $repo_visible = (bool) $this->dic->settings()->get('pub_section') && $this->dic->access()->checkAccess('visible', '', ROOT_FOLDER_ID);
67 } else {
68 $repo_visible = (bool) $this->dic->access()->checkAccess('visible', '', ROOT_FOLDER_ID);
69 }
70 }
71
72 return $this->getClosureWithOptinalClosure(static function () use ($repo_visible) : bool {
73 return $repo_visible;
74 }, $additional);
75 }
76
77 public function isUserLoggedIn(?Closure $additional = null) : Closure
78 {
79 static $is_anonymous;
80 if (!isset($is_anonymous)) {
81 $is_anonymous = (bool) $this->dic->user()->isAnonymous() || ($this->dic->user()->getId() == 0);
82 }
83
84 return $this->getClosureWithOptinalClosure(static function () use ($is_anonymous) : bool {
85 return !$is_anonymous;
86 }, $additional);
87 }
88
89 public function hasAdministrationAccess(?Closure $additional = null) : Closure
90 {
91 static $has_admin_access;
92 if (!isset($has_admin_access)) {
93 $has_admin_access = (bool) ($this->dic->rbac()->system()->checkAccess('visible', SYSTEM_FOLDER_ID));
94 }
95 return $this->getClosureWithOptinalClosure(static function () use ($has_admin_access) : bool {
96 return $has_admin_access;
97 }, $additional);
98 }
99
100
101 //
102 // Internal
103 //
104
105 private function checkClosureForBoolReturnValue(Closure $c) : bool
106 {
107 try {
108 $r = new ReflectionFunction($c);
109 } catch (\Throwable $e) {
110 return false;
111 }
112
113 if(!$r->hasReturnType() || !$r->getReturnType()->isBuiltin()){
114 throw new \InvalidArgumentException('the additional Closure MUST return a bool dy declaration');
115 }
116 return true;
117 }
118
119 private function getClosureWithOptinalClosure(Closure $closure, ?Closure $additional = null) : Closure
120 {
121 if ($additional instanceof Closure && $this->checkClosureForBoolReturnValue($additional)) {
122 return static function () use ($closure, $additional) : bool {
123 return $additional() && $closure();
124 };
125 }
126
127 return $closure;
128 }
129}
An exception for terminatinating execution or to throw for unit testing.
__construct()
BasicAccessCheckClosures constructor.
getClosureWithOptinalClosure(Closure $closure, ?Closure $additional=null)
$c
Definition: cli.php:37
const SYSTEM_FOLDER_ID
Definition: constants.php:33
const ROOT_FOLDER_ID
Definition: constants.php:30
global $DIC
Definition: goto.php:24
$additional
Definition: goto.php:52