1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JiraRestApi\User; |
4
|
|
|
|
5
|
|
|
use JiraRestApi\Issue\Reporter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class to perform all user related queries. |
9
|
|
|
* |
10
|
|
|
* @author Anik |
11
|
|
|
*/ |
12
|
|
|
class UserService extends \JiraRestApi\JiraClient |
13
|
|
|
{ |
14
|
|
|
private $uri = '/user'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Function to create a new user. |
18
|
|
|
* |
19
|
|
|
* @param User|array $user |
20
|
|
|
* |
21
|
|
|
* @throws \JiraRestApi\JiraException |
22
|
|
|
* @throws \JsonMapper_Exception |
23
|
|
|
* |
24
|
|
|
* @return User User class |
25
|
|
|
*/ |
26
|
|
|
public function create($user) |
27
|
|
|
{ |
28
|
|
|
$data = json_encode($user); |
29
|
|
|
|
30
|
|
|
$this->log->info("Create User=\n".$data); |
31
|
|
|
|
32
|
|
|
$ret = $this->exec($this->uri, $data, 'POST'); |
33
|
|
|
|
34
|
|
|
return $this->json_mapper->map( |
35
|
|
|
json_decode($ret), |
|
|
|
|
36
|
|
|
new User() |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Function to get user. |
42
|
|
|
* |
43
|
|
|
* @param array $paramArray Possible values for $paramArray 'username', 'key'. |
44
|
|
|
* "Either the 'username' or the 'key' query parameters need to be provided". |
45
|
|
|
* |
46
|
|
|
* @throws \JiraRestApi\JiraException |
47
|
|
|
* @throws \JsonMapper_Exception |
48
|
|
|
* |
49
|
|
|
* @return User User class |
50
|
|
|
*/ |
51
|
|
|
public function get($paramArray) |
52
|
|
|
{ |
53
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
54
|
|
|
|
55
|
|
|
$ret = $this->exec($this->uri.$queryParam, null); |
56
|
|
|
|
57
|
|
|
$this->log->info("Result=\n".$ret); |
|
|
|
|
58
|
|
|
|
59
|
|
|
return $this->json_mapper->map( |
60
|
|
|
json_decode($ret), |
|
|
|
|
61
|
|
|
new User() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a list of users that match the search string and/or property. |
67
|
|
|
* |
68
|
|
|
* @param array $paramArray |
69
|
|
|
* |
70
|
|
|
* @throws \JiraRestApi\JiraException |
71
|
|
|
* @throws \JsonMapper_Exception |
72
|
|
|
* |
73
|
|
|
* @return User[] |
74
|
|
|
*/ |
75
|
|
|
public function findUsers($paramArray) |
76
|
|
|
{ |
77
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
78
|
|
|
|
79
|
|
|
$ret = $this->exec($this->uri.'/search'.$queryParam, null); |
80
|
|
|
|
81
|
|
|
$this->log->info("Result=\n".$ret); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$userData = json_decode($ret); |
|
|
|
|
84
|
|
|
$users = []; |
85
|
|
|
|
86
|
|
|
foreach ($userData as $user) { |
87
|
|
|
$users[] = $this->json_mapper->map( |
88
|
|
|
$user, |
89
|
|
|
new User() |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $users; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns a list of users that match the search string. |
98
|
|
|
* Please note that this resource should be called with an issue key when a list of assignable users is retrieved for editing. |
99
|
|
|
* |
100
|
|
|
* @param array $paramArray |
101
|
|
|
* |
102
|
|
|
* @throws \JiraRestApi\JiraException |
103
|
|
|
* @throws \JsonMapper_Exception |
104
|
|
|
* |
105
|
|
|
* @return User[] |
106
|
|
|
* |
107
|
|
|
* @see https://docs.atlassian.com/jira/REST/cloud/#api/2/user-findAssignableUsers |
108
|
|
|
*/ |
109
|
|
|
public function findAssignableUsers($paramArray) |
110
|
|
|
{ |
111
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
112
|
|
|
|
113
|
|
|
$ret = $this->exec($this->uri.'/assignable/search'.$queryParam, null); |
114
|
|
|
|
115
|
|
|
$this->log->info("Result=\n".$ret); |
|
|
|
|
116
|
|
|
|
117
|
|
|
$userData = json_decode($ret); |
|
|
|
|
118
|
|
|
$users = []; |
119
|
|
|
|
120
|
|
|
foreach ($userData as $user) { |
121
|
|
|
$users[] = $this->json_mapper->map( |
122
|
|
|
$user, |
123
|
|
|
new User() |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $users; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns a list of users that match with a specific query. |
132
|
|
|
* |
133
|
|
|
* @param array $paramArray |
134
|
|
|
* |
135
|
|
|
* @throws \JiraRestApi\JiraException |
136
|
|
|
* @throws \JsonMapper_Exception |
137
|
|
|
* |
138
|
|
|
* @return User[] |
139
|
|
|
* |
140
|
|
|
* @see https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-user-search-query-get |
141
|
|
|
*/ |
142
|
|
|
public function findUsersByQuery($paramArray) |
143
|
|
|
{ |
144
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
145
|
|
|
|
146
|
|
|
$ret = $this->exec($this->uri.'/search/query'.$queryParam, null); |
147
|
|
|
|
148
|
|
|
$this->log->info("Result=\n".$ret); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$userData = json_decode($ret); |
|
|
|
|
151
|
|
|
$users = []; |
152
|
|
|
|
153
|
|
|
foreach ($userData->values as $user) { |
154
|
|
|
$users[] = $this->json_mapper->map( |
155
|
|
|
$user, |
156
|
|
|
new User() |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $users; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Delete a User. |
165
|
|
|
* |
166
|
|
|
* @param array $paramArray username or keys |
167
|
|
|
* |
168
|
|
|
* @throws \JiraRestApi\JiraException |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function deleteUser(array $paramArray) |
173
|
|
|
{ |
174
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
175
|
|
|
|
176
|
|
|
$ret = $this->exec($this->uri.$queryParam, null, 'DELETE'); |
177
|
|
|
|
178
|
|
|
return $ret; |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* get a user info details. |
183
|
|
|
* |
184
|
|
|
* @throws \JiraRestApi\JiraException |
185
|
|
|
* |
186
|
|
|
* @return Reporter user Object |
187
|
|
|
*/ |
188
|
|
|
public function getMyself() |
189
|
|
|
{ |
190
|
|
|
$ret = $this->exec('myself', null); |
191
|
|
|
|
192
|
|
|
$user = $this->json_mapper->map( |
193
|
|
|
json_decode($ret), |
|
|
|
|
194
|
|
|
new Reporter() |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
return $user; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param array $paramArray |
202
|
|
|
* |
203
|
|
|
* @throws \JiraRestApi\JiraException |
204
|
|
|
* @throws \JsonMapper_Exception |
205
|
|
|
* |
206
|
|
|
* @return User[] |
207
|
|
|
*/ |
208
|
|
|
public function getUsers($paramArray) |
209
|
|
|
{ |
210
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
211
|
|
|
|
212
|
|
|
$ret = $this->exec('/users'.$queryParam, null); |
213
|
|
|
|
214
|
|
|
$this->log->info("Result=\n".$ret); |
|
|
|
|
215
|
|
|
|
216
|
|
|
$userData = json_decode($ret); |
|
|
|
|
217
|
|
|
$users = []; |
218
|
|
|
|
219
|
|
|
foreach ($userData as $user) { |
220
|
|
|
$users[] = $this->json_mapper->map($user, new User()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $users; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Function to update an existing user. |
228
|
|
|
* |
229
|
|
|
* @param array $paramArray |
230
|
|
|
* @param User|array $user |
231
|
|
|
* |
232
|
|
|
* @throws \JiraRestApi\JiraException |
233
|
|
|
* @throws \JsonMapper_Exception |
234
|
|
|
* |
235
|
|
|
* @return User User class |
236
|
|
|
*/ |
237
|
|
|
public function update($paramArray, $user) |
238
|
|
|
{ |
239
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
240
|
|
|
|
241
|
|
|
$data = json_encode($user); |
242
|
|
|
|
243
|
|
|
$this->log->info("Update User (".$queryParam.") =\n".$data); |
244
|
|
|
|
245
|
|
|
$ret = $this->exec($this->uri.$queryParam, $data, 'PUT'); |
246
|
|
|
|
247
|
|
|
return $this->json_mapper->map( |
248
|
|
|
json_decode($ret), |
|
|
|
|
249
|
|
|
new User() |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|