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 Psr\Http\Message\ResponseInterface; |
13
|
|
|
|
14
|
|
|
final class UsersApi extends HttpApi |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string $loginId The login Id |
18
|
|
|
* @param string $password The password |
19
|
|
|
* @param null $token The login token, as output variable |
20
|
|
|
* |
21
|
|
|
* @return User|ResponseInterface |
22
|
|
|
*/ |
23
|
10 |
|
public function login($loginId, $password, &$token = null) |
24
|
|
|
{ |
25
|
10 |
|
if (empty($loginId) || empty($password)) { |
26
|
2 |
|
throw new InvalidArgumentException('LoginId and Password cannot be empty'); |
27
|
|
|
} |
28
|
|
|
|
29
|
8 |
|
$response = $this->httpPost('/users/login', [ |
30
|
8 |
|
'login_id' => $loginId, |
31
|
8 |
|
'password' => $password, |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
// Use any valid status code here |
35
|
8 |
|
if ($response->getStatusCode() !== 200) { |
36
|
7 |
|
$this->handleErrors($response); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$tokens = $response->getHeader('Token'); |
40
|
1 |
|
if (count($tokens)) { |
41
|
1 |
|
$token = reset($tokens); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return $this->hydrator->hydrate($response, User::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns an user by its ID, use "me" to get the current logged in user. |
49
|
|
|
* |
50
|
|
|
* @param string $userId User GUID |
51
|
|
|
* |
52
|
|
|
* @return User|ResponseInterface |
53
|
|
|
*/ |
54
|
9 |
|
public function getUserById(string $userId) |
55
|
|
|
{ |
56
|
9 |
|
if (empty($userId)) { |
57
|
1 |
|
throw new InvalidArgumentException('UserId can not be empty'); |
58
|
|
|
} |
59
|
|
|
|
60
|
8 |
|
$response = $this->httpGet(sprintf('/users/%s', $userId)); |
61
|
|
|
|
62
|
8 |
|
return $this->handleResponse($response, User::class); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get a list of teams that a user is on. |
67
|
|
|
* |
68
|
|
|
* @param string $userId User GUID |
69
|
|
|
* |
70
|
|
|
* @return User|ResponseInterface |
71
|
|
|
*/ |
72
|
9 |
View Code Duplication |
public function getUserTeams(string $userId) |
|
|
|
|
73
|
|
|
{ |
74
|
9 |
|
if (empty($userId)) { |
75
|
1 |
|
throw new InvalidArgumentException('UserId can not be empty'); |
76
|
|
|
} |
77
|
|
|
|
78
|
8 |
|
$response = $this->httpGet(sprintf('/users/%s/teams', $userId)); |
79
|
|
|
|
80
|
8 |
|
return $this->handleResponse($response, Teams::class); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns a collection of users matching the given IDs. |
85
|
|
|
* |
86
|
|
|
* @param array $userIds |
87
|
|
|
* |
88
|
|
|
* @return Users|ResponseInterface |
89
|
|
|
*/ |
90
|
9 |
View Code Duplication |
public function getUsersByIds(array $userIds) |
|
|
|
|
91
|
|
|
{ |
92
|
9 |
|
if (empty($userIds)) { |
93
|
1 |
|
throw new InvalidArgumentException('UserIDs can not be empty'); |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
$response = $this->httpPost('/users/ids', $userIds); |
97
|
|
|
|
98
|
8 |
|
return $this->handleResponse($response, Users::class); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns a collection of users. |
103
|
|
|
* |
104
|
|
|
* @param array $params The listing params, 'page', 'per_page', 'in_channel', 'in_team', 'not_in_channel' |
105
|
|
|
* |
106
|
|
|
* @return Users|ResponseInterface |
107
|
|
|
*/ |
108
|
9 |
|
public function getUsers(array $params = []) |
109
|
|
|
{ |
110
|
9 |
|
$response = $this->httpGet('/users', $params); |
111
|
|
|
|
112
|
9 |
|
return $this->handleResponse($response, Users::class); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns a user given its email. |
117
|
|
|
* |
118
|
|
|
* @param string $email |
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 array $userNames |
137
|
|
|
* |
138
|
|
|
* @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1usernames%2Fpost |
139
|
|
|
* |
140
|
|
|
* @return Users|ResponseInterface |
141
|
|
|
*/ |
142
|
9 |
View Code Duplication |
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 |
View Code Duplication |
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 |
View Code Duplication |
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
|
|
|
* @param array $params |
223
|
|
|
* |
224
|
|
|
* @return User|ResponseInterface |
225
|
|
|
*/ |
226
|
8 |
|
public function createUser(array $params) |
227
|
|
|
{ |
228
|
8 |
|
$response = $this->httpPost('/users', $params); |
229
|
|
|
|
230
|
8 |
|
return $this->handleResponse($response, User::class); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Patch a user. |
235
|
|
|
* |
236
|
|
|
* @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D~1patch%2Fput |
237
|
|
|
* |
238
|
|
|
* @param string $userId |
239
|
|
|
* @param array $params |
240
|
|
|
* |
241
|
|
|
* @return User|ResponseInterface |
242
|
|
|
*/ |
243
|
9 |
|
public function patchUser(string $userId, array $params) |
244
|
|
|
{ |
245
|
9 |
|
if (empty($userId)) { |
246
|
1 |
|
throw new InvalidArgumentException('UserId can not be empty'); |
247
|
|
|
} |
248
|
|
|
|
249
|
8 |
|
$response = $this->httpPut(sprintf('/users/%s/patch', $userId), $params); |
250
|
|
|
|
251
|
8 |
|
return $this->handleResponse($response, User::class); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Update a user, required parameter: 'id'. |
256
|
|
|
* |
257
|
|
|
* @see https://api.mattermost.com/v4/#tag/users%2Fpaths%2F~1users~1%7Buser_id%7D%2Fput |
258
|
|
|
* |
259
|
|
|
* @param string $userId |
260
|
|
|
* @param array $params |
261
|
|
|
* |
262
|
|
|
* @return User|ResponseInterface |
263
|
|
|
*/ |
264
|
9 |
|
public function updateUser(string $userId, array $params) |
265
|
|
|
{ |
266
|
9 |
|
if (empty($userId)) { |
267
|
1 |
|
throw new InvalidArgumentException('UserId can not be empty'); |
268
|
|
|
} |
269
|
|
|
|
270
|
8 |
|
$response = $this->httpPut(sprintf('/users/%s', $userId), $params); |
271
|
|
|
|
272
|
8 |
|
return $this->handleResponse($response, User::class); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Update a user's password. New password must meet password policy set by server configuration. |
277
|
|
|
* |
278
|
|
|
* @param string $userId User GUID |
279
|
|
|
* @param string $currentPassword The current password for the user |
280
|
|
|
* @param string $newPassword The new password for the user |
281
|
|
|
* |
282
|
|
|
* @return Status|ResponseInterface |
283
|
|
|
*/ |
284
|
11 |
|
public function updateUserPassword(string $userId, string $currentPassword, string $newPassword) |
285
|
|
|
{ |
286
|
11 |
|
if (empty($userId)) { |
287
|
1 |
|
throw new InvalidArgumentException('UserId can not be empty'); |
288
|
|
|
} |
289
|
10 |
|
if (empty($currentPassword) || empty($newPassword)) { |
290
|
2 |
|
throw new InvalidArgumentException('The current password and the new password can not be empty'); |
291
|
|
|
} |
292
|
|
|
|
293
|
8 |
|
$response = $this->httpPut(sprintf('/users/%s/password', $userId), [ |
294
|
8 |
|
'current_password' => $currentPassword, |
295
|
8 |
|
'new_password' => $newPassword, |
296
|
|
|
]); |
297
|
|
|
|
298
|
8 |
|
return $this->handleResponse($response, Status::class); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Returns an user by its username, use "me" to get the current logged in user. |
303
|
|
|
* |
304
|
|
|
* @param string $username |
305
|
|
|
* |
306
|
|
|
* @return User|ResponseInterface |
307
|
|
|
*/ |
308
|
9 |
|
public function getUserByUsername(string $username) |
309
|
|
|
{ |
310
|
9 |
|
if (empty($username)) { |
311
|
1 |
|
throw new InvalidArgumentException('Username can not be empty'); |
312
|
|
|
} |
313
|
|
|
|
314
|
8 |
|
$response = $this->httpGet(sprintf('/users/username/%s', $username)); |
315
|
|
|
|
316
|
8 |
|
return $this->handleResponse($response, User::class); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.