diff --git a/README.md b/README.md index cf759d9..16bbd46 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ Completely framework-agnostic and database-agnostic. * [Additional user information](#additional-user-information) * [Administration (managing users)](administration-managing-users) * [Creating new users](creating-new-users) + * [Deleting users](#deleting-users) * [Utilities](#utilities) * [Creating a random string](#creating-a-random-string) * [Creating a UUID v4 as per RFC 4122](#creating-a-uuid-v4-as-per-rfc-4122) @@ -391,6 +392,42 @@ The username in the third parameter is optional. You can pass `null` there if yo If you want to enforce unique usernames, on the other hand, simply call `createUserWithUniqueUsername` instead of `createUser`, and be prepared to catch the `DuplicateUsernameException`. +#### Deleting users + +```php +try { + $auth->admin()->deleteUserById($_POST['id']); +} +catch (\Delight\Auth\UnknownIdException $e) { + // unknown ID +} +``` + +or + +```php +try { + $auth->admin()->deleteUserByEmail($_POST['email']); +} +catch (\Delight\Auth\InvalidEmailException $e) { + // unknown email address +} +``` + +or + +```php +try { + $auth->admin()->deleteUserByUsername($_POST['username']); +} +catch (\Delight\Auth\UnknownUsernameException $e) { + // unknown username +} +catch (\Delight\Auth\AmbiguousUsernameException $e) { + // ambiguous username +} +``` + ### Utilities #### Creating a random string diff --git a/src/Administration.php b/src/Administration.php index e13f64d..12e68c2 100644 --- a/src/Administration.php +++ b/src/Administration.php @@ -56,6 +56,61 @@ final class Administration extends UserManager { return $this->createUserInternal(true, $email, $password, $username, null); } + /** + * Deletes the user with the specified ID + * + * This action cannot be undone + * + * @param int $id the ID of the user to delete + * @throws UnknownIdException if no user with the specified ID has been found + * @throws AuthError if an internal problem occurred (do *not* catch) + */ + public function deleteUserById($id) { + $numberOfDeletedUsers = $this->deleteUsersByColumnValue('id', (int) $id); + + if ($numberOfDeletedUsers === 0) { + throw new UnknownIdException(); + } + } + + /** + * Deletes the user with the specified email address + * + * This action cannot be undone + * + * @param string $email the email address of the user to delete + * @throws InvalidEmailException if no user with the specified email address has been found + * @throws AuthError if an internal problem occurred (do *not* catch) + */ + public function deleteUserByEmail($email) { + $email = self::validateEmailAddress($email); + + $numberOfDeletedUsers = $this->deleteUsersByColumnValue('email', $email); + + if ($numberOfDeletedUsers === 0) { + throw new InvalidEmailException(); + } + } + + /** + * Deletes the user with the specified username + * + * This action cannot be undone + * + * @param string $username the username of the user to delete + * @throws UnknownUsernameException if no user with the specified username has been found + * @throws AmbiguousUsernameException if multiple users with the specified username have been found + * @throws AuthError if an internal problem occurred (do *not* catch) + */ + public function deleteUserByUsername($username) { + $userData = $this->getUserDataByUsername( + trim($username), + [ 'id' ] + ); + + $this->deleteUsersByColumnValue('id', (int) $userData['id']); + } + protected function throttle($actionType, $customSelector = null) {} /** diff --git a/src/Exceptions.php b/src/Exceptions.php index 6cba5f3..888bc42 100644 --- a/src/Exceptions.php +++ b/src/Exceptions.php @@ -10,6 +10,8 @@ namespace Delight\Auth; class AuthException extends \Exception {} +class UnknownIdException extends AuthException {} + class InvalidEmailException extends AuthException {} class UnknownUsernameException extends AuthException {}