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