|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2021 ChannelWeb Srl, Chialab Srl |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace App\View\Helper; |
|
14
|
|
|
|
|
15
|
|
|
use Cake\Utility\Hash; |
|
16
|
|
|
use Cake\View\Helper; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Helper class to handle permissions on modules. |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
class PermsHelper extends Helper |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* API methods allowed in current module |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $current = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* API methods allowed in all modules |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $allowed = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritDoc} |
|
40
|
|
|
* |
|
41
|
|
|
* Init API and WebAPP base URL |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function initialize(array $config): void |
|
46
|
|
|
{ |
|
47
|
|
|
$modules = (array)$this->_View->get('modules'); |
|
48
|
|
|
$this->allowed = Hash::combine($modules, '{s}.name', '{s}.hints.allow'); |
|
49
|
|
|
$currentModule = (array)$this->_View->get('currentModule'); |
|
50
|
|
|
$this->current = (array)Hash::get($currentModule, 'hints.allow'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Check create permission. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $module Module name |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function canCreate(string $module = null): bool |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->isAllowed('POST', $module); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check delete permission. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $module Module name |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function canDelete(string $module = null): bool |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->isAllowed('DELETE', $module); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Check save permission. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $module Module name |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function canSave(string $module = null): bool |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->isAllowed('PATCH', $module); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Check read permission. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $module Module name |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function canRead(string $module = null): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->isAllowed('GET', $module); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check if a method is allowed on a module. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $method Method to check |
|
101
|
|
|
* @param string $module Module name, if missing or null current module is used. |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function isAllowed(string $method, string $module = null): bool |
|
105
|
|
|
{ |
|
106
|
|
|
if (empty($module)) { |
|
107
|
|
|
if (empty($this->current)) { |
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return in_array($method, $this->current); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$allowed = (array)Hash::get($this->allowed, $module); |
|
115
|
|
|
|
|
116
|
|
|
return in_array($method, $allowed); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|