1
0
mirror of https://github.com/delight-im/PHP-Auth.git synced 2025-07-31 13:20:11 +02:00

Add method 'deleteUserById' and similar methods for email and username

This commit is contained in:
Marco
2017-02-25 17:32:35 +01:00
parent 63144d4dc0
commit 81bdd79906
3 changed files with 94 additions and 0 deletions

View File

@@ -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

View File

@@ -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) {}
/**

View File

@@ -10,6 +10,8 @@ namespace Delight\Auth;
class AuthException extends \Exception {}
class UnknownIdException extends AuthException {}
class InvalidEmailException extends AuthException {}
class UnknownUsernameException extends AuthException {}