-
Notifications
You must be signed in to change notification settings - Fork 260
/
Users.php
executable file
·377 lines (335 loc) · 10.3 KB
/
Users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
namespace Zendesk\API\Resources\Core;
use Zendesk\API\Exceptions\CustomException;
use Zendesk\API\Exceptions\MissingParametersException;
use Zendesk\API\Exceptions\ResponseException;
use Zendesk\API\Http;
use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Resource\CreateMany;
use Zendesk\API\Traits\Resource\Defaults;
use Zendesk\API\Traits\Resource\FindMany;
use Zendesk\API\Traits\Resource\MultipartUpload;
use Zendesk\API\Traits\Resource\UpdateMany;
use Zendesk\API\Traits\Utility\InstantiatorTrait;
/**
* The Users class exposes user management methods
* Note: you must authenticate as a user!
*
* @method Groups groups()
* @method UserIdentities identities()
* @method Organizations organizations()
* @method OrganizationMemberships organizationMemberships()
* @method OrganizationSubscriptions organizationSubscriptions()
* @method Requests requests()
*/
class Users extends ResourceAbstract
{
use InstantiatorTrait;
use Defaults {
findAll as traitFindAll;
}
use MultipartUpload;
use CreateMany;
use FindMany {
findMany as traitFindMany;
}
use UpdateMany;
/**
* @var UserIdentities
*/
protected $identities;
/**
* {@inheritdoc}
*/
protected function setUpRoutes()
{
parent::setUpRoutes();
$this->setRoutes([
'related' => 'users/{id}/related.json',
'merge' => 'users/me/merge.json',
'search' => 'users/search.json',
'autocomplete' => 'users/autocomplete.json',
'setPassword' => 'users/{id}/password.json',
'changePassword' => 'users/{id}/password.json',
'updateMany' => 'users/update_many.json',
'createMany' => 'users/create_many.json',
'updateProfileImageFromFile' => 'users/{id}.json',
'updateProfileImageFromUrl' => 'users/{id}.json',
]);
}
/**
* {@inheritdoc}
*/
public static function getValidSubResources()
{
return [
'groupMemberships' => GroupMemberships::class,
'groups' => Groups::class,
'identities' => UserIdentities::class,
'organizations' => Organizations::class,
'organizationMemberships' => OrganizationMemberships::class,
'organizationSubscriptions' => OrganizationSubscriptions::class,
'requests' => Requests::class,
'sessions' => Sessions::class
];
}
/**
* List all users
*
* @param array $params
*
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function findAll(array $params = [])
{
if (isset($params['organization_id'])) {
$this->endpoint = "organizations/{$params['organization_id']}/users.json";
} elseif (isset($params['group_id'])) {
$this->endpoint = 'groups/' . $params['group_id'] . '/users.json';
} else {
$this->endpoint = 'users.json';
}
return $this->traitFindAll();
}
/**
* Find users by ids or external_ids
*
* @param array $params
*
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function findMany(array $params = [])
{
if (isset($params['ids']) xor isset($params['external_ids'])) {
if (isset($params['ids'])) {
$key = 'ids';
$ids = $params['ids'];
} elseif (isset($params['external_ids'])) {
$key = 'external_ids';
$ids = $params['external_ids'];
}
} else {
throw new \Exception('Missing parameters ids or external_ids');
}
return $this->traitFindMany($ids, [], $key);
}
/**
* Get related information about the user
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function related(array $params = [])
{
$params = $this->addChainedParametersToParams($params, ['id' => get_class($this)]);
if (! $this->hasKeys($params, ['id'])) {
throw new MissingParametersException(__METHOD__, ['id']);
}
$queryParams = Http::prepareQueryParams($this->client->getSideload($params), $params);
$response = Http::send(
$this->client,
$this->getRoute(__FUNCTION__, ['id' => $params['id']]),
['queryParams' => $queryParams]
);
return $response;
}
/**
* Merge the specified user (???)
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function merge(array $params = [])
{
$myId = $this->getChainedParameter(get_class($this));
$mergeMe = ! isset($myId) || is_null($myId);
$hasKeys = $mergeMe ? ['email', 'password'] : ['id'];
if (! $this->hasKeys($params, $hasKeys)) {
throw new MissingParametersException(__METHOD__, $hasKeys);
}
$response = Http::send(
$this->client,
$this->getRoute(__FUNCTION__),
['postFields' => [$this->objectName => $params], 'method' => 'PUT']
);
return $response;
}
/**
* Update multiple users
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @return mixed
*/
public function suspend(array $params = [])
{
$params = $this->addChainedParametersToParams($params, ['id' => get_class($this)]);
if (! $this->hasKeys($params, ['id'])) {
throw new MissingParametersException(__METHOD__, ['id']);
}
$params['suspended'] = true;
return $this->update($params['id'], $params);
}
/**
* Search for users
*
* @param array $params
*
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function search(array $params)
{
$queryParams = isset($params['query']) ? ['query' => $params['query']] : [];
$extraParams = Http::prepareQueryParams($this->client->getSideload($params), $params);
return $this->client->get($this->getRoute(__FUNCTION__), array_merge($extraParams, $queryParams));
}
/**
* Requests autocomplete for users
*
* @param array $params
*
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function autocomplete(array $params)
{
$response = Http::send(
$this->client,
$this->getRoute(__FUNCTION__),
['method' => 'POST', 'queryParams' => $params]
);
return $response;
}
/**
* {$@inheritdoc}
*
* @return String
*/
public function getUploadName()
{
return 'user[photo][uploaded_data]';
}
/**
* {$@inheritdoc}
*
* @return String
*/
public function getUploadRequestMethod()
{
return 'PUT';
}
/**
* Update a user's profile image
*
* @param array $params
*
* @throws CustomException
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function updateProfileImageFromFile(array $params)
{
$this->setAdditionalRouteParams(['id' => $this->getChainedParameter(self::class)]);
return $this->upload($params, __FUNCTION__);
}
/**
* Update a user's profile image
*
* @param array $params
*
* @throws CustomException
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function updateProfileImageFromUrl(array $params)
{
if (! isset($params['id']) || empty($params['id'])) {
$params = $this->addChainedParametersToParams($params, ['id' => self::class]);
}
if (! $this->hasKeys($params, ['id', 'url'])) {
throw new MissingParametersException(__METHOD__, ['id', 'url']);
}
$endpoint = $this->getRoute(__FUNCTION__, ['id' => $params['id']]);
$putData = [
$this->objectName => [
'remote_photo_url' => $params['url']
]
];
return $this->client->put($endpoint, $putData);
}
/**
* Show the current user
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @return mixed
*/
public function me(array $params = [])
{
$params['id'] = 'me';
return $this->find($params['id']);
}
/**
* Sets a user's initial password
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function setPassword(array $params)
{
$params = $this->addChainedParametersToParams($params, ['id' => get_class($this)]);
if (! $this->hasKeys($params, ['id', 'password'])) {
throw new MissingParametersException(__METHOD__, ['id', 'password']);
}
$id = $params['id'];
unset($params['id']);
return $this->client->post($this->getRoute(__FUNCTION__, ['id' => $id]), $params);
}
/**
* Change a user's password
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return mixed
*/
public function changePassword(array $params)
{
$params = $this->addChainedParametersToParams($params, ['id' => get_class($this)]);
if (! $this->hasKeys($params, ['id', 'previous_password', 'password'])) {
throw new MissingParametersException(__METHOD__, ['id', 'previous_password', 'password']);
}
$id = $params['id'];
unset($params['id']);
return $this->client->put($this->getRoute(__FUNCTION__, ['id' => $id]), $params);
}
}