From cf41c9a105d8405409d5be7cfbe0cde5d395ee6a Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 21 Feb 2017 10:02:03 +0100 Subject: [PATCH] Add methods 'createUser' and 'createUserWithUniqueUsername' --- README.md | 31 ++++++++++++++++++++++ src/Administration.php | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/README.md b/README.md index d42076b..030c92b 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ Completely framework-agnostic and database-agnostic. * [Checking whether the user was "remembered"](#checking-whether-the-user-was-remembered) * [IP address](#ip-address) * [Additional user information](#additional-user-information) + * [Administration (managing users)](administration-managing-users) + * [Creating new users](creating-new-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) @@ -360,6 +362,35 @@ Here's how to use this library with your own tables for custom user information } ``` +### Administration (managing users) + +The administrative interface is available via `$auth->admin()`. You can call various method on this interface, as documented below. + +**Warning**: Do *not* forget to implement secure access control before exposing access to this interface. For example, you may provide access to this interface to logged in users with the administrator role only, or use the interface in private scripts only. + +#### Creating new users + +```php +try { + $userId = $auth->admin()->createUser($_POST['email'], $_POST['password'], $_POST['username']); + + // we have signed up a new user with the ID `$userId` +} +catch (\Delight\Auth\InvalidEmailException $e) { + // invalid email address +} +catch (\Delight\Auth\InvalidPasswordException $e) { + // invalid password +} +catch (\Delight\Auth\UserAlreadyExistsException $e) { + // user already exists +} +``` + +The username in the third parameter is optional. You can pass `null` there if you don't want to manage usernames. + +If you want to enforce unique usernames, on the other hand, simply call `createUserWithUniqueUsername` instead of `createUser`, and be prepared to catch the `DuplicateUsernameException`. + ### Utilities #### Creating a random string diff --git a/src/Administration.php b/src/Administration.php index c455998..5b44abb 100644 --- a/src/Administration.php +++ b/src/Administration.php @@ -22,6 +22,65 @@ final class Administration extends UserManager { parent::__construct($databaseConnection); } + /** + * Creates a new user + * + * If you want the user's account to be activated by default, pass `null` as the callback + * + * If you want to make the user verify their email address first, pass an anonymous function as the callback + * + * The callback function must have the following signature: + * + * `function ($selector, $token)` + * + * Both pieces of information must be sent to the user, usually embedded in a link + * + * When the user wants to verify their email address as a next step, both pieces will be required again + * + * @param string $email the email address to register + * @param string $password the password for the new account + * @param string|null $username (optional) the username that will be displayed + * @param callable|null $callback (optional) the function that sends the confirmation email to the user + * @return int the ID of the user that has been created (if any) + * @throws InvalidEmailException if the email address was invalid + * @throws InvalidPasswordException if the password was invalid + * @throws UserAlreadyExistsException if a user with the specified email address already exists + * @throws AuthError if an internal problem occurred (do *not* catch) + */ + public function createUser($email, $password, $username = null, callable $callback = null) { + return $this->createUserInternal(false, $email, $password, $username, $callback); + } + + /** + * Creates a new user while ensuring that the username is unique + * + * If you want the user's account to be activated by default, pass `null` as the callback + * + * If you want to make the user verify their email address first, pass an anonymous function as the callback + * + * The callback function must have the following signature: + * + * `function ($selector, $token)` + * + * Both pieces of information must be sent to the user, usually embedded in a link + * + * When the user wants to verify their email address as a next step, both pieces will be required again + * + * @param string $email the email address to register + * @param string $password the password for the new account + * @param string|null $username (optional) the username that will be displayed + * @param callable|null $callback (optional) the function that sends the confirmation email to the user + * @return int the ID of the user that has been created (if any) + * @throws InvalidEmailException if the email address was invalid + * @throws InvalidPasswordException if the password was invalid + * @throws UserAlreadyExistsException if a user with the specified email address already exists + * @throws DuplicateUsernameException if the specified username wasn't unique + * @throws AuthError if an internal problem occurred (do *not* catch) + */ + public function createUserWithUniqueUsername($email, $password, $username = null, callable $callback = null) { + return $this->createUserInternal(true, $email, $password, $username, $callback); + } + protected function throttle($actionType, $customSelector = null) {} }