-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathRole.php
More file actions
193 lines (165 loc) · 5.58 KB
/
Role.php
File metadata and controls
193 lines (165 loc) · 5.58 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace Redmine\Api;
use Redmine\Client\Client;
use Redmine\Exception;
use Redmine\Exception\SerializerException;
use Redmine\Exception\UnexpectedResponseException;
use Redmine\Http\HttpClient;
use Redmine\Http\HttpFactory;
use Redmine\Serializer\JsonSerializer;
/**
* Listing roles.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_Roles
*
* @author Kevin Saliou <kevin at saliou dot name>
*/
class Role extends AbstractApi
{
final public static function fromHttpClient(HttpClient $httpClient): self
{
return new self($httpClient, true);
}
/**
* @var null|array<mixed>
*/
private ?array $roles = null;
/**
* @var null|array<int,string>
*/
private ?array $roleNames = null;
/**
* @deprecated v2.9.0 Use fromHttpClient() instead.
* @see Role::fromHttpClient()
*
* @param Client|HttpClient $client
*/
public function __construct($client/*, bool $privatelyCalled = false*/)
{
$privatelyCalled = (func_num_args() > 1) ? func_get_arg(1) : false;
if ($privatelyCalled === true) {
parent::__construct($client);
return;
}
if (static::class !== self::class) {
$className = (new \ReflectionClass($this))->isAnonymous() ? '' : ' in `' . static::class . '`';
@trigger_error('Class `' . self::class . '` will declared as final in v3.0.0, stop extending it' . $className . '.', E_USER_DEPRECATED);
} else {
@trigger_error('Method `' . __METHOD__ . '()` is deprecated since v2.9.0 and will declared as private in v3.0.0, use `' . self::class . '::fromHttpClient()` instead.', E_USER_DEPRECATED);
}
parent::__construct($client);
}
/**
* List roles.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_Roles#GET
*
* @param array<mixed> $params optional parameters to be passed to the api (offset, limit, ...)
*
* @throws UnexpectedResponseException if response body could not be converted into array
*
* @return array<mixed> list of roles found
*/
final public function list(array $params = []): array
{
try {
return $this->retrieveData('/roles.json', $params);
} catch (SerializerException $th) {
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}
/**
* Returns an array of all roles with id/name pairs.
*
* @return array<int,string> list of roles (id => name)
*/
final public function listNames(): array
{
if ($this->roleNames !== null) {
return $this->roleNames;
}
$this->roleNames = [];
$list = $this->list();
if (array_key_exists('roles', $list)) {
foreach ($list['roles'] as $role) {
$this->roleNames[(int) $role['id']] = $role['name'];
}
}
return $this->roleNames;
}
/**
* List roles.
*
* @deprecated v2.4.0 Use list() instead.
* @see Role::list()
* @see http://www.redmine.org/projects/redmine/wiki/Rest_Roles#GET
*
* @param array<mixed> $params optional parameters to be passed to the api (offset, limit, ...)
*
* @return array<mixed>|string|false list of roles found or error message or false
*/
public function all(array $params = [])
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.4.0, use `' . self::class . '::list()` instead.', E_USER_DEPRECATED);
try {
$this->roles = $this->list($params);
} catch (Exception $e) {
if ($this->getLastResponse()->getContent() === '') {
return false;
}
if ($e instanceof UnexpectedResponseException && $e->getPrevious() instanceof \Throwable) {
$e = $e->getPrevious();
}
return $e->getMessage();
}
return $this->roles;
}
/**
* Returns an array of roles with name/id pairs.
*
* @deprecated v2.7.0 Use listNames() instead.
* @see Role::listNames()
*
* @param bool $forceUpdate to force the update of the roles var
*
* @return array<string,int> list of roles (name => id)
*/
public function listing($forceUpdate = false)
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.7.0, use `' . self::class . '::listNames()` instead.', E_USER_DEPRECATED);
if ($forceUpdate || $this->roles === null) {
$this->roles = $this->list();
}
$ret = [];
foreach ($this->roles['roles'] as $e) {
$ret[$e['name']] = (int) $e['id'];
}
return $ret;
}
/**
* Returns the list of permissions for a given role (Redmine v2.2.0).
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_Roles#GET-2
*
* @param int $id the role id
*
* @return array<mixed>|false|string information about the role as array or false|string on error
*/
public function show($id)
{
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeJsonRequest(
'GET',
'/roles/' . urlencode(strval($id)) . '.json',
));
$body = $this->lastResponse->getContent();
if ('' === $body) {
return false;
}
try {
return JsonSerializer::createFromString($body)->getNormalized();
} catch (SerializerException $e) {
return 'Error decoding body as JSON: ' . $e->getPrevious()->getMessage();
}
}
}