Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
57.14% |
12 / 21 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
Permission | |
57.14% |
12 / 21 |
|
25.00% |
1 / 4 |
15.38 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isAuthorized | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
allowDisplay | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getAlternateContent | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 |
1 | <?php |
2 | |
3 | /** |
4 | * Permission helper |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2017. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package View_Helpers |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @author Oliver Goldschmidt <o.goldschmidt@tuhh.de> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link http://vufind.org/wiki/ Wiki |
29 | */ |
30 | |
31 | namespace VuFind\View\Helper\Root; |
32 | |
33 | use Laminas\View\Helper\AbstractHelper; |
34 | use VuFind\Role\PermissionDeniedManager; |
35 | use VuFind\Role\PermissionManager; |
36 | |
37 | /** |
38 | * Permission helper |
39 | * |
40 | * @category VuFind |
41 | * @package View_Helpers |
42 | * @author Demian Katz <demian.katz@villanova.edu> |
43 | * @author Oliver Goldschmidt <o.goldschmidt@tuhh.de> |
44 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
45 | * @link http://vufind.org/wiki/ Wiki |
46 | */ |
47 | class Permission extends AbstractHelper |
48 | { |
49 | /** |
50 | * PermissionDenied manager for behavior on denied permissions |
51 | * |
52 | * @var PermissionDeniedManager |
53 | */ |
54 | protected $permissionDeniedManager; |
55 | |
56 | /** |
57 | * Permission manager to decide if a permission has been granted or not |
58 | * |
59 | * @var PermissionManager |
60 | */ |
61 | protected $permissionManager; |
62 | |
63 | /** |
64 | * Constructor |
65 | * |
66 | * @param PermissionManager $permissionManager Manager to decide if a permission has been granted or |
67 | * not |
68 | * @param PermissionDeniedManager $permissionDeniedManager Manager for behavior on denied permissions |
69 | */ |
70 | public function __construct( |
71 | PermissionManager $permissionManager, |
72 | PermissionDeniedManager $permissionDeniedManager |
73 | ) { |
74 | $this->permissionManager = $permissionManager; |
75 | $this->permissionDeniedManager = $permissionDeniedManager; |
76 | } |
77 | |
78 | /** |
79 | * Determine if the current user is authorized for a permission. |
80 | * |
81 | * @param string $context Name of the permission rule |
82 | * |
83 | * @return bool |
84 | */ |
85 | public function isAuthorized($context) |
86 | { |
87 | return $this->permissionManager->isAuthorized($context) === true; |
88 | } |
89 | |
90 | /** |
91 | * Determine if a local block inside the template should be displayed |
92 | * |
93 | * @param string $context Name of the permission rule |
94 | * |
95 | * @return bool |
96 | */ |
97 | public function allowDisplay($context) |
98 | { |
99 | // If there is no context, assume permission is granted. |
100 | if (empty($context)) { |
101 | return true; |
102 | } |
103 | |
104 | // If permission has been granted, we do not need to continue |
105 | // Just return true to indicate that the default can get applied |
106 | if ($this->permissionManager->isAuthorized($context) === true) { |
107 | return true; |
108 | } |
109 | // If we are getting to this point, we know that the permission has been |
110 | // denied. Nevertheless show the local block if there is no |
111 | // deniedTemplateBehavior set for this context. |
112 | $displayLogic = $this->permissionDeniedManager |
113 | ->getDeniedTemplateBehavior($context); |
114 | return !isset($displayLogic['action']); |
115 | } |
116 | |
117 | /** |
118 | * Get content to display in place of blocked content |
119 | * |
120 | * @param string $context Name of the permission rule |
121 | * |
122 | * @return string |
123 | */ |
124 | public function getAlternateContent($context) |
125 | { |
126 | $displayLogic = $this->permissionDeniedManager |
127 | ->getDeniedTemplateBehavior($context); |
128 | |
129 | switch ($displayLogic['action'] ?? '') { |
130 | case 'showMessage': |
131 | return $this->view->transEsc($displayLogic['value']); |
132 | case 'showTemplate': |
133 | return $this->view->context($this->view)->renderInContext( |
134 | $displayLogic['value'], |
135 | $displayLogic['params'] |
136 | ); |
137 | default: |
138 | return null; |
139 | } |
140 | } |
141 | } |