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|object 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), new User() |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Function to get user. |
41
|
|
|
* |
42
|
|
|
* @param array $paramArray Possible values for $paramArray 'username', 'key'. |
43
|
|
|
* "Either the 'username' or the 'key' query parameters need to be provided". |
44
|
|
|
* |
45
|
|
|
* @throws \JiraRestApi\JiraException |
46
|
|
|
* @throws \JsonMapper_Exception |
47
|
|
|
* |
48
|
|
|
* @return User|object User class |
49
|
|
|
*/ |
50
|
|
|
public function get($paramArray) |
51
|
|
|
{ |
52
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
53
|
|
|
|
54
|
|
|
$ret = $this->exec($this->uri.$queryParam, null); |
55
|
|
|
|
56
|
|
|
$this->log->info("Result=\n".$ret); |
57
|
|
|
|
58
|
|
|
return $this->json_mapper->map( |
59
|
|
|
json_decode($ret), new User() |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns a list of users that match the search string and/or property. |
65
|
|
|
* |
66
|
|
|
* @param array $paramArray |
67
|
|
|
* |
68
|
|
|
* @throws \JiraRestApi\JiraException |
69
|
|
|
* @throws \JsonMapper_Exception |
70
|
|
|
* |
71
|
|
|
* @return User[] |
72
|
|
|
*/ |
73
|
|
|
public function findUsers($paramArray) |
74
|
|
|
{ |
75
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
76
|
|
|
|
77
|
|
|
$ret = $this->exec($this->uri.'/search'.$queryParam, null); |
78
|
|
|
|
79
|
|
|
$this->log->info("Result=\n".$ret); |
80
|
|
|
|
81
|
|
|
$userData = json_decode($ret); |
82
|
|
|
$users = []; |
83
|
|
|
|
84
|
|
|
foreach ($userData as $user) { |
85
|
|
|
$users[] = $this->json_mapper->map( |
86
|
|
|
$user, new User() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $users; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns a list of users that match the search string. |
95
|
|
|
* Please note that this resource should be called with an issue key when a list of assignable users is retrieved for editing. |
96
|
|
|
* |
97
|
|
|
* @param array $paramArray |
98
|
|
|
* |
99
|
|
|
* @throws \JiraRestApi\JiraException |
100
|
|
|
* @throws \JsonMapper_Exception |
101
|
|
|
* |
102
|
|
|
* @return User[] |
103
|
|
|
* |
104
|
|
|
* @see https://docs.atlassian.com/jira/REST/cloud/#api/2/user-findAssignableUsers |
105
|
|
|
*/ |
106
|
|
|
public function findAssignableUsers($paramArray) |
107
|
|
|
{ |
108
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
109
|
|
|
|
110
|
|
|
$ret = $this->exec($this->uri.'/assignable/search'.$queryParam, null); |
111
|
|
|
|
112
|
|
|
$this->log->info("Result=\n".$ret); |
113
|
|
|
|
114
|
|
|
$userData = json_decode($ret); |
115
|
|
|
$users = []; |
116
|
|
|
|
117
|
|
|
foreach ($userData as $user) { |
118
|
|
|
$users[] = $this->json_mapper->map( |
119
|
|
|
$user, |
120
|
|
|
new User() |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $users; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns a list of users that match with a specific query. |
129
|
|
|
* |
130
|
|
|
* @param array $paramArray |
131
|
|
|
* |
132
|
|
|
* @throws \JiraRestApi\JiraException |
133
|
|
|
* @throws \JsonMapper_Exception |
134
|
|
|
* |
135
|
|
|
* @return User[] |
136
|
|
|
* |
137
|
|
|
* @see https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-user-search-query-get |
138
|
|
|
*/ |
139
|
|
|
public function findUsersByQuery($paramArray) |
140
|
|
|
{ |
141
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
142
|
|
|
|
143
|
|
|
$ret = $this->exec($this->uri.'/search/query'.$queryParam, null); |
144
|
|
|
|
145
|
|
|
$this->log->info("Result=\n".$ret); |
146
|
|
|
|
147
|
|
|
$userData = json_decode($ret); |
148
|
|
|
$users = []; |
149
|
|
|
|
150
|
|
|
foreach ($userData->values as $user) { |
151
|
|
|
$users[] = $this->json_mapper->map( |
152
|
|
|
$user, new User() |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $users; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Delete a User. |
161
|
|
|
* |
162
|
|
|
* @param $paramArray |
163
|
|
|
* |
164
|
|
|
* @throws \JiraRestApi\JiraException |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function deleteUser($paramArray) |
169
|
|
|
{ |
170
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
171
|
|
|
|
172
|
|
|
$ret = $this->exec($this->uri.$queryParam, null, 'DELETE'); |
173
|
|
|
|
174
|
|
|
return $ret; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* get a user info details. |
179
|
|
|
* |
180
|
|
|
* @throws \JiraRestApi\JiraException |
181
|
|
|
* |
182
|
|
|
* @return Reporter user Object |
183
|
|
|
*/ |
184
|
|
|
public function getMyself() |
185
|
|
|
{ |
186
|
|
|
$ret = $this->exec('myself', null); |
187
|
|
|
|
188
|
|
|
$user = $this->json_mapper->map( |
189
|
|
|
json_decode($ret), new Reporter() |
190
|
|
|
); |
191
|
|
|
|
192
|
|
|
return $user; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param array $paramArray |
197
|
|
|
* |
198
|
|
|
* @throws \JiraRestApi\JiraException |
199
|
|
|
* @throws \JsonMapper_Exception |
200
|
|
|
* |
201
|
|
|
* @return User[] |
202
|
|
|
*/ |
203
|
|
|
public function getUsers($paramArray) |
204
|
|
|
{ |
205
|
|
|
$queryParam = '?'.http_build_query($paramArray); |
206
|
|
|
|
207
|
|
|
$ret = $this->exec('/users'.$queryParam, null); |
208
|
|
|
|
209
|
|
|
$this->log->info("Result=\n".$ret); |
210
|
|
|
|
211
|
|
|
$userData = json_decode($ret); |
212
|
|
|
$users = []; |
213
|
|
|
|
214
|
|
|
foreach ($userData as $user) { |
215
|
|
|
$users[] = $this->json_mapper->map($user, new User()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $users; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|