-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRightsChecker.php
113 lines (98 loc) · 3.47 KB
/
RightsChecker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace YaleREDCap\ApiUserRights;
class RightsChecker
{
private ApiUserRights $module;
private string $username;
private array $methodCodes;
private array $rightsToCheck;
public $badMethods = [];
public $goodUsers = [];
public $goodMethods = [];
public $badUsers = [];
public $errorMessages = [];
private $valid = true;
private bool $default;
public function __construct(ApiUserRights $module, string $username, array $rightsToCheck, $default = false)
{
$this->module = $module;
$this->username = $username;
$this->rightsToCheck = $rightsToCheck;
$this->methodCodes = $this->module->getTableHeader()['methodCodes'];
$this->default = $default;
}
private function verifyMethods()
{
foreach ( $this->rightsToCheck as $method => $value ) {
if ( !in_array($method, $this->methodCodes, true) ) {
$this->badMethods[] = $this->module->framework->escape($method);
$this->valid = false;
} elseif ( in_array($method, $this->goodMethods, true) ) {
$this->errorMessages[] = 'Duplicate method: ' . $this->module->framework->escape($method);
$this->valid = false;
} else {
$this->goodMethods[] = $method;
}
}
}
private function verifyValues()
{
foreach ( $this->rightsToCheck as $method => $value ) {
$intValue = (int) $value;
if ( !in_array($intValue, [ 0, 1 ], true) ) {
$this->errorMessages[] = 'Invalid value for ' . $method . ': ' . $this->module->framework->escape($value);
$this->valid = false;
}
}
}
private function checkUsername()
{
$username = trim($this->username);
if ( empty($username) ) {
$this->valid = false;
$this->badUsers[] = $this->module->framework->escape($username);
} elseif ( in_array($username, $this->goodUsers, true) ) {
$this->errorMessages[] = 'Duplicate username: ' . $this->module->framework->escape($username);
$this->valid = false;
} else {
$this->goodUsers[] = $username;
}
return $username;
}
private function checkUser($username)
{
$user = $this->module->framework->getUser($username);
$userRights = $user->getRights();
if ( is_null($userRights) ) {
$this->badUsers[] = $this->module->framework->escape($username);
$this->valid = false;
}
}
public function isValid()
{
$this->valid = true;
// Check user
if ( !$this->default ) {
$thisUsername = $this->checkUsername();
$this->checkUser($thisUsername);
}
// Check methods
$this->verifyMethods();
// Check values
$this->verifyValues();
if ( !empty($this->badUsers) ) {
$this->errorMessages[] = 'Invalid username';
$this->valid = false;
}
if ( !empty($this->badMethods) ) {
$this->errorMessages[] = 'Invalid API methods';
$this->valid = false;
}
$this->errorMessages = array_values(array_unique($this->errorMessages));
return $this->valid;
}
public function getErrorMessages()
{
return $this->errorMessages;
}
}