1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pnz\MattermostClient\Api; |
6
|
|
|
|
7
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder; |
8
|
|
|
use Pnz\MattermostClient\Exception\InvalidArgumentException; |
9
|
|
|
use Pnz\MattermostClient\Model\Status; |
10
|
|
|
use Pnz\MattermostClient\Model\Team\Teams; |
11
|
|
|
use Pnz\MattermostClient\Model\User\User; |
12
|
|
|
use Pnz\MattermostClient\Model\User\Users; |
13
|
|
|
use Pnz\MattermostClient\Model\User\UserStatus; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Psr\Http\Message\StreamInterface; |
16
|
|
|
|
17
|
|
|
final class UsersApi extends HttpApi |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param string $loginId The login Id |
21
|
|
|
* @param string $password The password |
22
|
|
|
* @param string $token The login token, as output variable |
23
|
|
|
* |
24
|
10 |
|
* @return User|ResponseInterface |
25
|
|
|
*/ |
26
|
10 |
|
public function login(string $loginId, string $password, string &$token = null) |
27
|
2 |
|
{ |
28
|
|
|
if (empty($loginId) || empty($password)) { |
29
|
|
|
throw new InvalidArgumentException('LoginId and Password cannot be empty'); |
30
|
8 |
|
} |
31
|
8 |
|
|
32
|
8 |
|
$response = $this->httpPost('/users/login', [ |
33
|
|
|
'login_id' => $loginId, |
34
|
|
|
'password' => $password, |
35
|
|
|
]); |
36
|
8 |
|
|
37
|
7 |
|
// Use any valid status code here |
38
|
|
|
if (200 !== $response->getStatusCode()) { |
39
|
|
|
$this->handleErrors($response); |
40
|
1 |
|
} |
41
|
1 |
|
|
42
|
1 |
|
$tokens = $response->getHeader('Token'); |
43
|
|
|
if (\count($tokens)) { |
44
|
|
|
$token = reset($tokens); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
return $this->handleResponse($response, User::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns an user by its ID, use "me" to get the current logged in user. |
52
|
|
|
* |
53
|
|
|
* @param string $userId User GUID |
54
|
|
|
* |
55
|
9 |
|
* @return User|ResponseInterface |
56
|
|
|
*/ |
57
|
9 |
|
public function getUserById(string $userId) |
58
|
1 |
|
{ |
59
|
|
|
if (empty($userId)) { |
60
|
|
|
throw new InvalidArgumentException('UserId can not be empty'); |
61
|
8 |
|
} |
62
|
|
|
|
63
|
8 |
|
$response = $this->httpGet(sprintf('/users/%s', $userId)); |
64
|
|
|
|
65
|
|
|
return $this->handleResponse($response, User::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get a list of teams that a user is on. |
70
|
|
|
* |
71
|
|
|
* @param string $userId User GUID |
72
|
|
|
* |
73
|
9 |
|
* @return User|ResponseInterface |
74
|
|
|
*/ |
75
|
9 |
|
public function getUserTeams(string $userId) |
76
|
1 |
|
{ |
77
|
|
|
if (empty($userId)) { |
78
|
|
|
throw new InvalidArgumentException('UserId can not be empty'); |
79
|
8 |
|
} |
80
|
|
|
|
81
|
8 |
|
$response = $this->httpGet(sprintf('/users/%s/teams', $userId)); |
82
|
|
|
|
83
|
|
|
return $this->handleResponse($response, Teams::class); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns a collection of users matching the given IDs. |
88
|
|
|
* |
89
|
|
|
* @param string[] $userIds |
90
|
|
|
* |
91
|
9 |
|
* @return Users|ResponseInterface |
92
|
|
|
*/ |
93
|
9 |
|
public function getUsersByIds(array $userIds) |
94
|
1 |
|
{ |
95
|
|
|
if (empty($userIds)) { |
96
|
|
|
throw new InvalidArgumentException('UserIDs can not be empty'); |
97
|
8 |
|
} |
98
|
|
|
|
99
|
8 |
|
$response = $this->httpPost('/users/ids', $userIds); |
100
|
|
|
|
101
|
|
|
return $this->handleResponse($response, Users::class); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns a collection of users. |
106
|
|
|
* |
107
|
|
|
* @param array $params The listing params, 'page', 'per_page', 'in_channel', 'in_team', 'not_in_channel' |
108
|
|
|
* |
109
|
9 |
|
* @return Users|ResponseInterface |
110
|
|
|
*/ |
111
|
9 |
|
public function getUsers(array $params = []) |
112
|
|
|
{ |
113
|
9 |
|
$response = $this->httpGet('/users', $params); |
114
|
|
|
|
115
|
|
|
return $this->handleResponse($response, Users::class); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns a user given its email. |
120
|
|
|
* |
121
|
9 |
|
* @return User|ResponseInterface |
122
|
|
|
*/ |
123
|
9 |
|
public function getUserByEmail(string $email) |
124
|
1 |
|
{ |
125
|
|
|
if (empty($email)) { |
126
|
|
|
throw new InvalidArgumentException('Email can not be empty'); |
127
|
8 |
|
} |
128
|
|
|
|
129
|
8 |
|
$response = $this->httpGet(sprintf('/users/email/%s', $email)); |
130
|
|
|
|
131
|
|
|
return $this->handleResponse($response, User::class); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns a collection of users matching the given usernames. |
136
|
|
|
* |
137
|
|
|
* @param string[] $userNames |
138
|
|
|
* |
139
|
|
|
* @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1usernames%2Fpost |
140
|
|
|
* |
141
|
9 |
|
* @return Users|ResponseInterface |
142
|
|
|
*/ |
143
|
9 |
|
public function getUsersByUsernames(array $userNames) |
144
|
1 |
|
{ |
145
|
|
|
if (empty($userNames)) { |
146
|
|
|
throw new InvalidArgumentException('Usernames can not be empty'); |
147
|
8 |
|
} |
148
|
|
|
|
149
|
8 |
|
$response = $this->httpPost('/users/usernames', $userNames); |
150
|
|
|
|
151
|
|
|
return $this->handleResponse($response, Users::class); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Update user active or inactive status. |
156
|
|
|
* |
157
|
|
|
* @param string $userId The user ID |
158
|
|
|
* @param bool $activeStatus Use `true` to set the user active, `false` for inactive |
159
|
|
|
* |
160
|
9 |
|
* @return Status|ResponseInterface |
161
|
|
|
*/ |
162
|
9 |
|
public function setUserActive(string $userId, bool $activeStatus) |
163
|
1 |
|
{ |
164
|
|
|
if (empty($userId)) { |
165
|
|
|
throw new InvalidArgumentException('User ID can not be empty'); |
166
|
8 |
|
} |
167
|
8 |
|
|
168
|
|
|
$response = $this->httpPut(sprintf('/users/%s/active', $userId), [ |
169
|
|
|
'active' => $activeStatus, |
170
|
8 |
|
]); |
171
|
|
|
|
172
|
|
|
return $this->handleResponse($response, Status::class); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Deactivates the user by archiving its user object. |
177
|
|
|
* |
178
|
|
|
* @param string $userId The user GUID |
179
|
|
|
* |
180
|
|
|
* @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D%2Fdelete |
181
|
|
|
* |
182
|
9 |
|
* @return Status|ResponseInterface |
183
|
|
|
*/ |
184
|
9 |
|
public function deactivateUser(string $userId) |
185
|
1 |
|
{ |
186
|
|
|
if (empty($userId)) { |
187
|
|
|
throw new InvalidArgumentException('User ID can not be empty'); |
188
|
8 |
|
} |
189
|
|
|
|
190
|
8 |
|
$response = $this->httpDelete(sprintf('/users/%s', $userId)); |
191
|
|
|
|
192
|
|
|
return $this->handleResponse($response, Status::class); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Update a user's system-level roles. |
197
|
|
|
* Valid user roles are "system_user", "system_admin" or both of them. |
198
|
|
|
* Overwrites any previously assigned system-level roles. |
199
|
|
|
* |
200
|
|
|
* @param string $userId The user GUID |
201
|
|
|
* @param string $roles Space-delimited system roles to assign to the user |
202
|
|
|
* |
203
|
9 |
|
* @return Status|ResponseInterface |
204
|
|
|
*/ |
205
|
9 |
|
public function updateUserRoles(string $userId, string $roles) |
206
|
1 |
|
{ |
207
|
|
|
if (empty($userId)) { |
208
|
|
|
throw new InvalidArgumentException('User ID can not be empty'); |
209
|
8 |
|
} |
210
|
8 |
|
|
211
|
|
|
$response = $this->httpPut(sprintf('/users/%s/roles', $userId), [ |
212
|
|
|
'roles' => $roles, |
213
|
8 |
|
]); |
214
|
|
|
|
215
|
|
|
return $this->handleResponse($response, Status::class); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Create a user. Required parameters: 'username', 'email' and 'password'. |
220
|
|
|
* |
221
|
|
|
* @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users%2Fpost |
222
|
|
|
* |
223
|
8 |
|
* @return User|ResponseInterface |
224
|
|
|
*/ |
225
|
8 |
|
public function createUser(array $params) |
226
|
|
|
{ |
227
|
8 |
|
$response = $this->httpPost('/users', $params); |
228
|
|
|
|
229
|
|
|
return $this->handleResponse($response, User::class); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Patch a user. |
234
|
|
|
* |
235
|
|
|
* @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D~1patch%2Fput |
236
|
|
|
* |
237
|
9 |
|
* @return User|ResponseInterface |
238
|
|
|
*/ |
239
|
9 |
|
public function patchUser(string $userId, array $params) |
240
|
1 |
|
{ |
241
|
|
|
if (empty($userId)) { |
242
|
|
|
throw new InvalidArgumentException('UserId can not be empty'); |
243
|
8 |
|
} |
244
|
|
|
|
245
|
8 |
|
$response = $this->httpPut(sprintf('/users/%s/patch', $userId), $params); |
246
|
|
|
|
247
|
|
|
return $this->handleResponse($response, User::class); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Update a user. |
252
|
|
|
* |
253
|
|
|
* @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D%2Fput |
254
|
|
|
* |
255
|
9 |
|
* @return User|ResponseInterface |
256
|
|
|
*/ |
257
|
9 |
|
public function updateUser(string $userId, array $params) |
258
|
1 |
|
{ |
259
|
|
|
if (empty($userId)) { |
260
|
|
|
throw new InvalidArgumentException('UserId can not be empty'); |
261
|
8 |
|
} |
262
|
|
|
|
263
|
8 |
|
$response = $this->httpPut(sprintf('/users/%s', $userId), $params); |
264
|
|
|
|
265
|
|
|
return $this->handleResponse($response, User::class); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Update a user's password. New password must meet password policy set by server configuration. |
270
|
|
|
* |
271
|
|
|
* @param string $currentPassword The current password for the user |
272
|
|
|
* @param string $newPassword The new password for the user |
273
|
|
|
* |
274
|
11 |
|
* @return Status|ResponseInterface |
275
|
|
|
*/ |
276
|
11 |
|
public function updateUserPassword(string $userId, string $currentPassword, string $newPassword) |
277
|
1 |
|
{ |
278
|
|
|
if (empty($userId)) { |
279
|
10 |
|
throw new InvalidArgumentException('UserId can not be empty'); |
280
|
2 |
|
} |
281
|
|
|
if (empty($currentPassword) || empty($newPassword)) { |
282
|
|
|
throw new InvalidArgumentException('The current password and the new password can not be empty'); |
283
|
8 |
|
} |
284
|
8 |
|
|
285
|
8 |
|
$response = $this->httpPut(sprintf('/users/%s/password', $userId), [ |
286
|
|
|
'current_password' => $currentPassword, |
287
|
|
|
'new_password' => $newPassword, |
288
|
8 |
|
]); |
289
|
|
|
|
290
|
|
|
return $this->handleResponse($response, Status::class); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Returns an user by its username, use "me" to get the current logged in user. |
295
|
|
|
* |
296
|
9 |
|
* @return User|ResponseInterface |
297
|
|
|
*/ |
298
|
9 |
|
public function getUserByUsername(string $username) |
299
|
1 |
|
{ |
300
|
|
|
if (empty($username)) { |
301
|
|
|
throw new InvalidArgumentException('Username can not be empty'); |
302
|
8 |
|
} |
303
|
|
|
|
304
|
8 |
|
$response = $this->httpGet(sprintf('/users/username/%s', $username)); |
305
|
|
|
|
306
|
|
|
return $this->handleResponse($response, User::class); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Get the user status by its ID. |
311
|
|
|
* |
312
|
|
|
* @return UserStatus|ResponseInterface |
313
|
|
|
*/ |
314
|
|
|
public function getUserStatus(string $userId) |
315
|
|
|
{ |
316
|
|
|
if (empty($userId)) { |
317
|
|
|
throw new InvalidArgumentException('UserId can not be empty'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$response = $this->httpGet(sprintf('/users/%s/status', $userId)); |
321
|
|
|
|
322
|
|
|
return $this->handleResponse($response, UserStatus::class); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Delete a user's picture. |
327
|
|
|
* |
328
|
|
|
* @return Status|ResponseInterface |
329
|
|
|
*/ |
330
|
|
|
public function deleteProfileImage(string $userId) |
331
|
|
|
{ |
332
|
|
|
if (empty($userId)) { |
333
|
|
|
throw new InvalidArgumentException('UserId can not be empty'); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$response = $this->httpDelete(sprintf('/users/%s/image', $userId)); |
337
|
|
|
|
338
|
|
|
return $this->handleResponse($response, Status::class); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Update a user's picture. |
343
|
|
|
* |
344
|
|
|
* @param string|resource|StreamInterface|null $image The image contents to use as profile image |
345
|
|
|
* |
346
|
|
|
* @return Status|ResponseInterface |
347
|
|
|
*/ |
348
|
|
|
public function updateProfileImage(string $userId, $image) |
349
|
|
|
{ |
350
|
|
|
if (empty($userId)) { |
351
|
|
|
throw new InvalidArgumentException('UserId can not be empty'); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
if (!($image instanceof StreamInterface || \is_resource($image) || \is_string($image))) { |
355
|
|
|
throw new InvalidArgumentException('Image: must be a string, resource or StreamInterface'); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
$multipartStreamBuilder = new MultipartStreamBuilder(); |
359
|
|
|
$multipartStreamBuilder->addResource('image', $image); |
360
|
|
|
$headers = ['Content-Type' => 'multipart/form-data; boundary='.$multipartStreamBuilder->getBoundary()]; |
361
|
|
|
$multipartStream = $multipartStreamBuilder->build(); |
362
|
|
|
|
363
|
|
|
$response = $this->httpPostRaw(sprintf('/users/%s/image', $userId), $multipartStream, $headers); |
364
|
|
|
|
365
|
|
|
return $this->handleResponse($response, Status::class); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|