Skip to content

Commit 8db6cfc

Browse files
committed
New parameter checking end exceptions
1 parent 727733f commit 8db6cfc

File tree

2 files changed

+67
-109
lines changed

2 files changed

+67
-109
lines changed

src/API/Management/Users.php

Lines changed: 33 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Auth0\SDK\API\Management;
44

5-
use Auth0\SDK\Exception\CoreException;
5+
use Auth0\SDK\Exception\EmptyOrInvalidParameterException;
6+
use Auth0\SDK\Exception\InvalidPermissionsArrayException;
67

78
/**
89
* Class Users.
@@ -246,7 +247,7 @@ public function deleteMultifactorProvider($user_id, $mfa_provider)
246247
* @param string $user_id User ID to get roles for.
247248
* @param array $params Additional listing params like page, per_page, and include_totals.
248249
*
249-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
250+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
250251
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
251252
*
252253
* @return mixed
@@ -255,15 +256,10 @@ public function deleteMultifactorProvider($user_id, $mfa_provider)
255256
*/
256257
public function getRoles($user_id, array $params = [])
257258
{
258-
if (empty($user_id) || ! is_string($user_id)) {
259-
throw new CoreException('Invalid or missing user_id parameter.');
260-
}
259+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
261260

262261
$params = $this->normalizePagination( $params );
263-
264-
if (isset( $params['include_totals'] )) {
265-
$params['include_totals'] = boolval( $params['include_totals'] );
266-
}
262+
$params = $this->normalizeIncludeTotals( $params );
267263

268264
return $this->apiClient->method('get')
269265
->addPath('users', $user_id)
@@ -279,8 +275,8 @@ public function getRoles($user_id, array $params = [])
279275
* @param string $user_id User ID to remove roles from.
280276
* @param array $roles Array of permissions to remove.
281277
*
282-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
283-
* @throws CoreException Thrown if the roles parameter is empty.
278+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
279+
* @throws EmptyOrInvalidParameterException Thrown if the roles parameter is empty.
284280
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
285281
*
286282
* @return mixed
@@ -289,12 +285,10 @@ public function getRoles($user_id, array $params = [])
289285
*/
290286
public function removeRoles($user_id, array $roles)
291287
{
292-
if (empty($user_id) || ! is_string($user_id)) {
293-
throw new CoreException('Invalid or missing user_id parameter.');
294-
}
288+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
295289

296290
if (empty($roles)) {
297-
throw new CoreException('Empty roles parameter.');
291+
throw new EmptyOrInvalidParameterException('roles');
298292
}
299293

300294
$data = [ 'roles' => $roles ];
@@ -315,8 +309,8 @@ public function removeRoles($user_id, array $roles)
315309
* @param string $user_id User ID to add roles to.
316310
* @param array $roles Array of roles to add.
317311
*
318-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
319-
* @throws CoreException Thrown if the roles parameter is empty.
312+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
313+
* @throws EmptyOrInvalidParameterException Thrown if the roles parameter is empty.
320314
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
321315
*
322316
* @return mixed
@@ -325,12 +319,10 @@ public function removeRoles($user_id, array $roles)
325319
*/
326320
public function addRoles($user_id, array $roles)
327321
{
328-
if (empty($user_id) || ! is_string($user_id)) {
329-
throw new CoreException('Invalid or missing user_id parameter.');
330-
}
322+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
331323

332324
if (empty($roles)) {
333-
throw new CoreException('Empty roles parameter.');
325+
throw new EmptyOrInvalidParameterException('roles');
334326
}
335327

336328
$data = [ 'roles' => $roles ];
@@ -348,7 +340,7 @@ public function addRoles($user_id, array $roles)
348340
*
349341
* @param string $user_id User ID to get enrollments for.
350342
*
351-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
343+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
352344
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
353345
*
354346
* @return mixed
@@ -357,9 +349,7 @@ public function addRoles($user_id, array $roles)
357349
*/
358350
public function getEnrollments($user_id)
359351
{
360-
if (empty($user_id) || ! is_string($user_id)) {
361-
throw new CoreException('Invalid or missing user_id parameter.');
362-
}
352+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
363353

364354
return $this->apiClient->method('get')
365355
->addPath('users', $user_id)
@@ -374,7 +364,7 @@ public function getEnrollments($user_id)
374364
* @param string $user_id User ID to get permissions for.
375365
* @param array $params Additional listing params like page, per_page, and include_totals.
376366
*
377-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
367+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
378368
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
379369
*
380370
* @return mixed
@@ -383,15 +373,10 @@ public function getEnrollments($user_id)
383373
*/
384374
public function getPermissions($user_id, array $params = [])
385375
{
386-
if (empty($user_id) || ! is_string($user_id)) {
387-
throw new CoreException('Invalid or missing user_id parameter.');
388-
}
376+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
389377

390378
$params = $this->normalizePagination( $params );
391-
392-
if (isset( $params['include_totals'] )) {
393-
$params['include_totals'] = boolval( $params['include_totals'] );
394-
}
379+
$params = $this->normalizeIncludeTotals( $params );
395380

396381
return $this->apiClient->method('get')
397382
->addPath('users', $user_id)
@@ -407,8 +392,8 @@ public function getPermissions($user_id, array $params = [])
407392
* @param string $user_id User ID to remove permissions from.
408393
* @param array $permissions Array of permissions to remove.
409394
*
410-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
411-
* @throws CoreException Thrown if the permissions parameter is malformed.
395+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
396+
* @throws InvalidPermissionsArrayException Thrown if the permissions parameter is malformed.
412397
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
413398
*
414399
* @return mixed
@@ -417,15 +402,8 @@ public function getPermissions($user_id, array $params = [])
417402
*/
418403
public function removePermissions($user_id, array $permissions)
419404
{
420-
if (empty($user_id) || ! is_string($user_id)) {
421-
throw new CoreException('Invalid or missing user_id parameter.');
422-
}
423-
424-
if ($this->containsInvalidPermissions( $permissions )) {
425-
throw new CoreException(
426-
'Permissions must include both permission_name and resource_server_identifier keys.'
427-
);
428-
}
405+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
406+
$this->checkInvalidPermissions( $permissions );
429407

430408
$data = [ 'permissions' => $permissions ];
431409

@@ -443,8 +421,8 @@ public function removePermissions($user_id, array $permissions)
443421
* @param string $user_id User ID to add permissions to.
444422
* @param array $permissions Array of permissions to add.
445423
*
446-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
447-
* @throws CoreException Thrown if the permissions parameter is malformed.
424+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
425+
* @throws InvalidPermissionsArrayException Thrown if the permissions parameter is malformed.
448426
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
449427
*
450428
* @return mixed
@@ -453,15 +431,8 @@ public function removePermissions($user_id, array $permissions)
453431
*/
454432
public function addPermissions($user_id, array $permissions)
455433
{
456-
if (empty($user_id) || ! is_string($user_id)) {
457-
throw new CoreException('Invalid or missing user_id parameter.');
458-
}
459-
460-
if ($this->containsInvalidPermissions( $permissions )) {
461-
throw new CoreException(
462-
'Permissions must include both permission_name and resource_server_identifier keys.'
463-
);
464-
}
434+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
435+
$this->checkInvalidPermissions( $permissions );
465436

466437
$data = [ 'permissions' => $permissions ];
467438

@@ -479,7 +450,7 @@ public function addPermissions($user_id, array $permissions)
479450
* @param string $user_id User ID to get logs entries for.
480451
* @param array $params Additional listing params like page, per_page, sort, and include_totals.
481452
*
482-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
453+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
483454
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
484455
*
485456
* @return mixed
@@ -488,15 +459,10 @@ public function addPermissions($user_id, array $permissions)
488459
*/
489460
public function getLogs($user_id, array $params = [])
490461
{
491-
if (empty($user_id) || ! is_string($user_id)) {
492-
throw new CoreException('Invalid or missing user_id parameter.');
493-
}
462+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
494463

495464
$params = $this->normalizePagination( $params );
496-
497-
if (isset( $params['include_totals'] )) {
498-
$params['include_totals'] = boolval( $params['include_totals'] );
499-
}
465+
$params = $this->normalizeIncludeTotals( $params );
500466

501467
return $this->apiClient->method('get')
502468
->addPath('users', $user_id)
@@ -511,7 +477,7 @@ public function getLogs($user_id, array $params = [])
511477
*
512478
* @param string $user_id User ID to remove and generate recovery codes for.
513479
*
514-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
480+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
515481
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
516482
*
517483
* @return mixed
@@ -520,9 +486,7 @@ public function getLogs($user_id, array $params = [])
520486
*/
521487
public function generateRecoveryCode($user_id)
522488
{
523-
if (empty($user_id) || ! is_string($user_id)) {
524-
throw new CoreException('Invalid or missing user_id parameter.');
525-
}
489+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
526490

527491
return $this->apiClient->method('post')
528492
->addPath('users', $user_id)
@@ -536,7 +500,7 @@ public function generateRecoveryCode($user_id)
536500
*
537501
* @param string $user_id User ID to invalidate browsers for.
538502
*
539-
* @throws CoreException Thrown if the user_id parameter is empty or is not a string.
503+
* @throws EmptyOrInvalidParameterException Thrown if the user_id parameter is empty or is not a string.
540504
* @throws \Exception Thrown by the HTTP client when there is a problem with the API call.
541505
*
542506
* @return mixed
@@ -545,9 +509,7 @@ public function generateRecoveryCode($user_id)
545509
*/
546510
public function invalidateBrowsers($user_id)
547511
{
548-
if (empty($user_id) || ! is_string($user_id)) {
549-
throw new CoreException('Invalid or missing user_id parameter.');
550-
}
512+
$this->checkEmptyOrInvalidString($user_id, 'user_id');
551513

552514
return $this->apiClient->method('post')
553515
->addPath('users', $user_id)

0 commit comments

Comments
 (0)