mirror of
https://github.com/delight-im/PHP-Auth.git
synced 2025-08-08 00:56:28 +02:00
Compare commits
34 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
729c76668f | ||
|
cc6430a83e | ||
|
6f933ac560 | ||
|
157a7095b0 | ||
|
0f976a260b | ||
|
dcd893a12c | ||
|
0086419175 | ||
|
d49b35690c | ||
|
171519fdf3 | ||
|
14ce7b1e8f | ||
|
49c70eff41 | ||
|
2f772b00c8 | ||
|
5214da1f59 | ||
|
d8847fb197 | ||
|
1757ad3fd1 | ||
|
54f6c5320a | ||
|
4b3f2ab91c | ||
|
df990b5b75 | ||
|
7b2ac9b107 | ||
|
ad90c7d04a | ||
|
c0baa517fa | ||
|
3120e3a6a5 | ||
|
4cd6360fc7 | ||
|
382832457d | ||
|
f70923679f | ||
|
521e73662d | ||
|
2b3bf611e2 | ||
|
352260c759 | ||
|
cbf2b52f29 | ||
|
c685f22937 | ||
|
9d08c939a0 | ||
|
7a8508d56e | ||
|
f6607f664d | ||
|
49a4ef8280 |
298
README.md
298
README.md
@@ -55,6 +55,9 @@ Migrating from an earlier version of this project? See our [upgrade guide](Migra
|
||||
* [Email verification](#email-verification)
|
||||
* [Keeping the user logged in](#keeping-the-user-logged-in)
|
||||
* [Password reset (“forgot password”)](#password-reset-forgot-password)
|
||||
* [Initiating the request](#step-1-of-3-initiating-the-request)
|
||||
* [Verifying an attempt](#step-2-of-3-verifying-an-attempt)
|
||||
* [Updating the password](#step-3-of-3-updating-the-password)
|
||||
* [Changing the current user’s password](#changing-the-current-users-password)
|
||||
* [Changing the current user’s email address](#changing-the-current-users-email-address)
|
||||
* [Re-sending confirmation requests](#re-sending-confirmation-requests)
|
||||
@@ -64,6 +67,7 @@ Migrating from an earlier version of this project? See our [upgrade guide](Migra
|
||||
* [User ID](#user-id)
|
||||
* [Email address](#email-address)
|
||||
* [Display name](#display-name)
|
||||
* [Status information](#status-information)
|
||||
* [Checking whether the user was “remembered”](#checking-whether-the-user-was-remembered)
|
||||
* [IP address](#ip-address)
|
||||
* [Additional user information](#additional-user-information)
|
||||
@@ -114,7 +118,7 @@ Migrating from an earlier version of this project? See our [upgrade guide](Migra
|
||||
$auth = new \Delight\Auth\Auth($db);
|
||||
```
|
||||
|
||||
If you have an open `PDO` connection already, just re-use it.
|
||||
If you have an open `PDO` connection already, just re-use it. The database user (e.g. `my-username`) needs at least the privileges `SELECT`, `INSERT`, `UPDATE` and `DELETE` for the tables used by this library (or their parent database).
|
||||
|
||||
If your web server is behind a proxy server and `$_SERVER['REMOTE_ADDR']` only contains the proxy’s IP address, you must pass the user’s real IP address to the constructor in the second argument, which is named `$ipAddress`. The default is the usual remote IP address received by PHP.
|
||||
|
||||
@@ -124,34 +128,46 @@ During development, you may want to disable the request limiting or throttling p
|
||||
|
||||
During the lifetime of a session, some user data may be changed remotely, either by a client in another session or by an administrator. That means this information must be regularly resynchronized with its authoritative source in the database, which this library does automatically. By default, this happens every five minutes. If you want to change this interval, pass a custom interval in seconds to the constructor as the fifth argument, which is named `$sessionResyncInterval`.
|
||||
|
||||
If all your database tables need a common database name, schema name, or other qualifier that must be specified explicitly, you can optionally pass that qualifier to the constructor as the sixth parameter, which is named `$dbSchema`.
|
||||
|
||||
### Registration (sign up)
|
||||
|
||||
```php
|
||||
try {
|
||||
$userId = $auth->register($_POST['email'], $_POST['password'], $_POST['username'], function ($selector, $token) {
|
||||
// send `$selector` and `$token` to the user (e.g. via email)
|
||||
echo 'Send ' . $selector . ' and ' . $token . ' to the user (e.g. via email)';
|
||||
});
|
||||
|
||||
// we have signed up a new user with the ID `$userId`
|
||||
echo 'We have signed up a new user with the ID ' . $userId;
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// invalid email address
|
||||
die('Invalid email address');
|
||||
}
|
||||
catch (\Delight\Auth\InvalidPasswordException $e) {
|
||||
// invalid password
|
||||
die('Invalid password');
|
||||
}
|
||||
catch (\Delight\Auth\UserAlreadyExistsException $e) {
|
||||
// user already exists
|
||||
die('User already exists');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
**Note:** The anonymous callback function is a [closure](http://php.net/manual/en/functions.anonymous.php). Thus, besides its own parameters, only [superglobals](http://php.net/manual/en/language.variables.superglobals.php) like `$_GET`, `$_POST`, `$_COOKIE` and `$_SERVER` are available inside. For any other variable from the parent scope, you need to explicitly make a copy available inside by adding a `use` clause after the parameter list.
|
||||
|
||||
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 `registerWithUniqueUsername` instead of `register`, and be prepared to catch the `DuplicateUsernameException`.
|
||||
|
||||
**Note:** When accepting and managing usernames, you may want to exclude non-printing control characters and certain printable special characters, as in the character class `[\x00-\x1f\x7f\/:\\]`. In order to do so, you could wrap the call to `Auth#register` or `Auth#registerWithUniqueUsername` inside a conditional branch, for example by only accepting usernames when the following condition is satisfied:
|
||||
|
||||
```php
|
||||
if (\preg_match('/[\x00-\x1f\x7f\/:\\\\]/', $username) === 0) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
For email verification, you should build an URL with the selector and token and send it to the user, e.g.:
|
||||
|
||||
```php
|
||||
@@ -160,25 +176,29 @@ $url = 'https://www.example.com/verify_email?selector=' . \urlencode($selector)
|
||||
|
||||
If you don’t want to perform email verification, just omit the last parameter to `Auth#register`. The new user will be active immediately, then.
|
||||
|
||||
Need to store additional user information? Read on [here](#additional-user-information).
|
||||
|
||||
**Note:** When sending an email to the user, please note that the (optional) username, at this point, has not yet been confirmed as acceptable to the owner of the (new) email address. It could contain offensive or misleading language chosen by someone who is not actually the owner of the address.
|
||||
|
||||
### Login (sign in)
|
||||
|
||||
```php
|
||||
try {
|
||||
$auth->login($_POST['email'], $_POST['password']);
|
||||
|
||||
// user is logged in
|
||||
echo 'User is logged in';
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// wrong email address
|
||||
die('Wrong email address');
|
||||
}
|
||||
catch (\Delight\Auth\InvalidPasswordException $e) {
|
||||
// wrong password
|
||||
die('Wrong password');
|
||||
}
|
||||
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
||||
// email not verified
|
||||
die('Email not verified');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -192,24 +212,26 @@ Extract the selector and token from the URL that the user clicked on in the veri
|
||||
try {
|
||||
$auth->confirmEmail($_GET['selector'], $_GET['token']);
|
||||
|
||||
// email address has been verified
|
||||
echo 'Email address has been verified';
|
||||
}
|
||||
catch (\Delight\Auth\InvalidSelectorTokenPairException $e) {
|
||||
// invalid token
|
||||
die('Invalid token');
|
||||
}
|
||||
catch (\Delight\Auth\TokenExpiredException $e) {
|
||||
// token expired
|
||||
die('Token expired');
|
||||
}
|
||||
catch (\Delight\Auth\UserAlreadyExistsException $e) {
|
||||
// email address already exists
|
||||
die('Email address already exists');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
If you want the user to be automatically signed in after successful confirmation, just call `confirmEmailAndSignIn` instead of `confirmEmail`. That alternative method also supports [persistent logins](#keeping-the-user-logged-in) via its optional third parameter.
|
||||
|
||||
On success, the two methods `confirmEmail` and `confirmEmailAndSignIn` both return an array with the user’s new email address, which has just been verified, at index one. If the confirmation was for an address change instead of a simple address verification, the user’s old email address will be included in the array at index zero.
|
||||
|
||||
### Keeping the user logged in
|
||||
|
||||
The third parameter to the `Auth#login` and `Auth#confirmEmailAndSignIn` methods controls whether the login is persistent with a long-lived cookie. With such a persistent login, users may stay authenticated for a long time, even when the browser session has already been closed and the session cookies have expired. Typically, you’ll want to keep the user logged in for weeks or months with this feature, which is known as “remember me” or “keep me logged in”. Many users will find this more convenient, but it may be less secure if they leave their devices unattended.
|
||||
@@ -242,31 +264,35 @@ Omit the third parameter or set it to `null` to disable the feature. Otherwise,
|
||||
```php
|
||||
try {
|
||||
$auth->forgotPassword($_POST['email'], function ($selector, $token) {
|
||||
// send `$selector` and `$token` to the user (e.g. via email)
|
||||
echo 'Send ' . $selector . ' and ' . $token . ' to the user (e.g. via email)';
|
||||
});
|
||||
|
||||
// request has been generated
|
||||
echo 'Request has been generated';
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// invalid email address
|
||||
die('Invalid email address');
|
||||
}
|
||||
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
||||
// email not verified
|
||||
die('Email not verified');
|
||||
}
|
||||
catch (\Delight\Auth\ResetDisabledException $e) {
|
||||
// password reset is disabled
|
||||
die('Password reset is disabled');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
**Note:** The anonymous callback function is a [closure](http://php.net/manual/en/functions.anonymous.php). Thus, besides its own parameters, only [superglobals](http://php.net/manual/en/language.variables.superglobals.php) like `$_GET`, `$_POST`, `$_COOKIE` and `$_SERVER` are available inside. For any other variable from the parent scope, you need to explicitly make a copy available inside by adding a `use` clause after the parameter list.
|
||||
|
||||
You should build an URL with the selector and token and send it to the user, e.g.:
|
||||
|
||||
```php
|
||||
$url = 'https://www.example.com/reset_password?selector=' . \urlencode($selector) . '&token=' . \urlencode($token);
|
||||
```
|
||||
|
||||
If the default lifetime of the password reset requests does not work for you, you can use the third parameter of `Auth#forgotPassword` to specify a custom interval in seconds after which the requests should expire.
|
||||
|
||||
#### Step 2 of 3: Verifying an attempt
|
||||
|
||||
As the next step, users will click on the link that they received. Extract the selector and token from the URL.
|
||||
@@ -277,22 +303,22 @@ If the selector/token pair is valid, let the user choose a new password:
|
||||
try {
|
||||
$auth->canResetPasswordOrThrow($_GET['selector'], $_GET['token']);
|
||||
|
||||
// put the selector into a `hidden` field (or keep it in the URL)
|
||||
// put the token into a `hidden` field (or keep it in the URL)
|
||||
echo 'Put the selector into a "hidden" field (or keep it in the URL)';
|
||||
echo 'Put the token into a "hidden" field (or keep it in the URL)';
|
||||
|
||||
// ask the user for their new password
|
||||
echo 'Ask the user for their new password';
|
||||
}
|
||||
catch (\Delight\Auth\InvalidSelectorTokenPairException $e) {
|
||||
// invalid token
|
||||
die('Invalid token');
|
||||
}
|
||||
catch (\Delight\Auth\TokenExpiredException $e) {
|
||||
// token expired
|
||||
die('Token expired');
|
||||
}
|
||||
catch (\Delight\Auth\ResetDisabledException $e) {
|
||||
// password reset is disabled
|
||||
die('Password reset is disabled');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -300,10 +326,10 @@ Alternatively, if you don’t need any error messages but only want to check the
|
||||
|
||||
```php
|
||||
if ($auth->canResetPassword($_GET['selector'], $_GET['token'])) {
|
||||
// put the selector into a `hidden` field (or keep it in the URL)
|
||||
// put the token into a `hidden` field (or keep it in the URL)
|
||||
echo 'Put the selector into a "hidden" field (or keep it in the URL)';
|
||||
echo 'Put the token into a "hidden" field (or keep it in the URL)';
|
||||
|
||||
// ask the user for their new password
|
||||
echo 'Ask the user for their new password';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -315,25 +341,29 @@ Now when you have the new password for the user (and still have the other two pi
|
||||
try {
|
||||
$auth->resetPassword($_POST['selector'], $_POST['token'], $_POST['password']);
|
||||
|
||||
// password has been reset
|
||||
echo 'Password has been reset';
|
||||
}
|
||||
catch (\Delight\Auth\InvalidSelectorTokenPairException $e) {
|
||||
// invalid token
|
||||
die('Invalid token');
|
||||
}
|
||||
catch (\Delight\Auth\TokenExpiredException $e) {
|
||||
// token expired
|
||||
die('Token expired');
|
||||
}
|
||||
catch (\Delight\Auth\ResetDisabledException $e) {
|
||||
// password reset is disabled
|
||||
die('Password reset is disabled');
|
||||
}
|
||||
catch (\Delight\Auth\InvalidPasswordException $e) {
|
||||
// invalid password
|
||||
die('Invalid password');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
Do you want to have the respective user signed in automatically when their password reset succeeds? Simply use `Auth#resetPasswordAndSignIn` instead of `Auth#resetPassword` to log in the user immediately.
|
||||
|
||||
If you need the user’s ID or email address, e.g. for sending them a notification that their password has successfully been reset, just use the return value of `Auth#resetPassword`, which is an array containing two entries named `id` and `email`.
|
||||
|
||||
### Changing the current user’s password
|
||||
|
||||
If a user is currently logged in, they may change their password.
|
||||
@@ -342,16 +372,16 @@ If a user is currently logged in, they may change their password.
|
||||
try {
|
||||
$auth->changePassword($_POST['oldPassword'], $_POST['newPassword']);
|
||||
|
||||
// password has been changed
|
||||
echo 'Password has been changed';
|
||||
}
|
||||
catch (\Delight\Auth\NotLoggedInException $e) {
|
||||
// not logged in
|
||||
die('Not logged in');
|
||||
}
|
||||
catch (\Delight\Auth\InvalidPasswordException $e) {
|
||||
// invalid password(s)
|
||||
die('Invalid password(s)');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -369,40 +399,46 @@ If a user is currently logged in, they may change their email address.
|
||||
try {
|
||||
if ($auth->reconfirmPassword($_POST['password'])) {
|
||||
$auth->changeEmail($_POST['newEmail'], function ($selector, $token) {
|
||||
// send `$selector` and `$token` to the user (e.g. via email to the *new* address)
|
||||
echo 'Send ' . $selector . ' and ' . $token . ' to the user (e.g. via email to the *new* address)';
|
||||
});
|
||||
|
||||
// the change will take effect as soon as the new email address has been confirmed
|
||||
echo 'The change will take effect as soon as the new email address has been confirmed';
|
||||
}
|
||||
else {
|
||||
// we can't say if the user is who they claim to be
|
||||
echo 'We can\'t say if the user is who they claim to be';
|
||||
}
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// invalid email address
|
||||
die('Invalid email address');
|
||||
}
|
||||
catch (\Delight\Auth\UserAlreadyExistsException $e) {
|
||||
// email address already exists
|
||||
die('Email address already exists');
|
||||
}
|
||||
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
||||
// account not verified
|
||||
die('Account not verified');
|
||||
}
|
||||
catch (\Delight\Auth\NotLoggedInException $e) {
|
||||
// not logged in
|
||||
die('Not logged in');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
**Note:** The anonymous callback function is a [closure](http://php.net/manual/en/functions.anonymous.php). Thus, besides its own parameters, only [superglobals](http://php.net/manual/en/language.variables.superglobals.php) like `$_GET`, `$_POST`, `$_COOKIE` and `$_SERVER` are available inside. For any other variable from the parent scope, you need to explicitly make a copy available inside by adding a `use` clause after the parameter list.
|
||||
|
||||
For email verification, you should build an URL with the selector and token and send it to the user, e.g.:
|
||||
|
||||
```php
|
||||
$url = 'https://www.example.com/verify_email?selector=' . \urlencode($selector) . '&token=' . \urlencode($token);
|
||||
```
|
||||
|
||||
**Note:** When sending an email to the user, please note that the (optional) username, at this point, has not yet been confirmed as acceptable to the owner of the (new) email address. It could contain offensive or misleading language chosen by someone who is not actually the owner of the address.
|
||||
|
||||
After the request to change the email address has been made, or even better, after the change has been confirmed by the user, you should send an email to their account’s *previous* email address as an out-of-band notification informing the account owner about this critical change.
|
||||
|
||||
**Note:** Changes to a user’s email address take effect in the local session immediately, as expected. In other sessions (e.g. on other devices), the changes may need up to five minutes to take effect, though. This increases performance and usually poses no problem. If you want to change this behavior, nevertheless, simply decrease (or perhaps increase) the value that you pass to the [`Auth` constructor](#creating-a-new-instance) as the argument named `$sessionResyncInterval`.
|
||||
|
||||
### Re-sending confirmation requests
|
||||
|
||||
If an earlier confirmation request could not be delivered to the user, or if the user missed that request, or if they just don’t want to wait any longer, you may re-send an earlier request like this:
|
||||
@@ -410,16 +446,16 @@ If an earlier confirmation request could not be delivered to the user, or if the
|
||||
```php
|
||||
try {
|
||||
$auth->resendConfirmationForEmail($_POST['email'], function ($selector, $token) {
|
||||
// send `$selector` and `$token` to the user (e.g. via email)
|
||||
echo 'Send ' . $selector . ' and ' . $token . ' to the user (e.g. via email)';
|
||||
});
|
||||
|
||||
// the user may now respond to the confirmation request (usually by clicking a link)
|
||||
echo 'The user may now respond to the confirmation request (usually by clicking a link)';
|
||||
}
|
||||
catch (\Delight\Auth\ConfirmationRequestNotFound $e) {
|
||||
// no earlier request found that could be re-sent
|
||||
die('No earlier request found that could be re-sent');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// there have been too many requests -- try again later
|
||||
die('There have been too many requests -- try again later');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -428,25 +464,29 @@ If you want to specify the user by their ID instead of by their email address, t
|
||||
```php
|
||||
try {
|
||||
$auth->resendConfirmationForUserId($_POST['userId'], function ($selector, $token) {
|
||||
// send `$selector` and `$token` to the user (e.g. via email)
|
||||
echo 'Send ' . $selector . ' and ' . $token . ' to the user (e.g. via email)';
|
||||
});
|
||||
|
||||
// the user may now respond to the confirmation request (usually by clicking a link)
|
||||
echo 'The user may now respond to the confirmation request (usually by clicking a link)';
|
||||
}
|
||||
catch (\Delight\Auth\ConfirmationRequestNotFound $e) {
|
||||
// no earlier request found that could be re-sent
|
||||
die('No earlier request found that could be re-sent');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// there have been too many requests -- try again later
|
||||
die('There have been too many requests -- try again later');
|
||||
}
|
||||
```
|
||||
|
||||
**Note:** The anonymous callback function is a [closure](http://php.net/manual/en/functions.anonymous.php). Thus, besides its own parameters, only [superglobals](http://php.net/manual/en/language.variables.superglobals.php) like `$_GET`, `$_POST`, `$_COOKIE` and `$_SERVER` are available inside. For any other variable from the parent scope, you need to explicitly make a copy available inside by adding a `use` clause after the parameter list.
|
||||
|
||||
Usually, you should build an URL with the selector and token and send it to the user, e.g. as follows:
|
||||
|
||||
```php
|
||||
$url = 'https://www.example.com/verify_email?selector=' . \urlencode($selector) . '&token=' . \urlencode($token);
|
||||
```
|
||||
|
||||
**Note:** When sending an email to the user, please note that the (optional) username, at this point, has not yet been confirmed as acceptable to the owner of the (new) email address. It could contain offensive or misleading language chosen by someone who is not actually the owner of the address.
|
||||
|
||||
### Logout
|
||||
|
||||
```php
|
||||
@@ -458,7 +498,7 @@ try {
|
||||
$auth->logOutEverywhereElse();
|
||||
}
|
||||
catch (\Delight\Auth\NotLoggedInException $e) {
|
||||
// not logged in
|
||||
die('Not logged in');
|
||||
}
|
||||
|
||||
// or
|
||||
@@ -467,7 +507,7 @@ try {
|
||||
$auth->logOutEverywhere();
|
||||
}
|
||||
catch (\Delight\Auth\NotLoggedInException $e) {
|
||||
// not logged in
|
||||
die('Not logged in');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -477,16 +517,18 @@ Additionally, if you store custom information in the session as well, and if you
|
||||
$auth->destroySession();
|
||||
```
|
||||
|
||||
**Note:** Global logouts take effect in the local session immediately, as expected. In other sessions (e.g. on other devices), the changes may need up to five minutes to take effect, though. This increases performance and usually poses no problem. If you want to change this behavior, nevertheless, simply decrease (or perhaps increase) the value that you pass to the [`Auth` constructor](#creating-a-new-instance) as the argument named `$sessionResyncInterval`.
|
||||
|
||||
### Accessing user information
|
||||
|
||||
#### Login state
|
||||
|
||||
```php
|
||||
if ($auth->isLoggedIn()) {
|
||||
// user is signed in
|
||||
echo 'User is signed in';
|
||||
}
|
||||
else {
|
||||
// user is *not* signed in yet
|
||||
echo 'User is not signed in yet';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -513,7 +555,7 @@ If the user is not currently signed in, this returns `null`.
|
||||
#### Display name
|
||||
|
||||
```php
|
||||
$email = $auth->getUsername();
|
||||
$username = $auth->getUsername();
|
||||
```
|
||||
|
||||
Remember that usernames are optional and there is only a username if you supplied it during registration.
|
||||
@@ -524,27 +566,27 @@ If the user is not currently signed in, this returns `null`.
|
||||
|
||||
```php
|
||||
if ($auth->isNormal()) {
|
||||
// user is in default state
|
||||
echo 'User is in default state';
|
||||
}
|
||||
|
||||
if ($auth->isArchived()) {
|
||||
// user has been archived
|
||||
echo 'User has been archived';
|
||||
}
|
||||
|
||||
if ($auth->isBanned()) {
|
||||
// user has been banned
|
||||
echo 'User has been banned';
|
||||
}
|
||||
|
||||
if ($auth->isLocked()) {
|
||||
// user has been locked
|
||||
echo 'User has been locked';
|
||||
}
|
||||
|
||||
if ($auth->isPendingReview()) {
|
||||
// user is pending review
|
||||
echo 'User is pending review';
|
||||
}
|
||||
|
||||
if ($auth->isSuspended()) {
|
||||
// user has been suspended
|
||||
echo 'User has been suspended';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -552,10 +594,10 @@ if ($auth->isSuspended()) {
|
||||
|
||||
```php
|
||||
if ($auth->isRemembered()) {
|
||||
// user did not sign in but was logged in through their long-lived cookie
|
||||
echo 'User did not sign in but was logged in through their long-lived cookie';
|
||||
}
|
||||
else {
|
||||
// user signed in manually
|
||||
echo 'User signed in manually';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -601,17 +643,17 @@ For example, when a user has been remembered by a long-lived cookie and thus `Au
|
||||
```php
|
||||
try {
|
||||
if ($auth->reconfirmPassword($_POST['password'])) {
|
||||
// the user really seems to be who they claim to be
|
||||
echo 'The user really seems to be who they claim to be';
|
||||
}
|
||||
else {
|
||||
// we can't say if the user is who they claim to be
|
||||
echo 'We can\'t say if the user is who they claim to be';
|
||||
}
|
||||
}
|
||||
catch (\Delight\Auth\NotLoggedInException $e) {
|
||||
// the user is not signed in
|
||||
die('The user is not signed in');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -625,19 +667,19 @@ Users may have no role at all (which they do by default), exactly one role, or a
|
||||
|
||||
```php
|
||||
if ($auth->hasRole(\Delight\Auth\Role::SUPER_MODERATOR)) {
|
||||
// the user is a super moderator
|
||||
echo 'The user is a super moderator';
|
||||
}
|
||||
|
||||
// or
|
||||
|
||||
if ($auth->hasAnyRole(\Delight\Auth\Role::DEVELOPER, \Delight\Auth\Role::MANAGER)) {
|
||||
// the user is either a developer, or a manager, or both
|
||||
echo 'The user is either a developer, or a manager, or both';
|
||||
}
|
||||
|
||||
// or
|
||||
|
||||
if ($auth->hasAllRoles(\Delight\Auth\Role::DEVELOPER, \Delight\Auth\Role::MANAGER)) {
|
||||
// the user is both a developer and a manager
|
||||
echo 'The user is both a developer and a manager';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -704,20 +746,20 @@ function canEditArticle(\Delight\Auth\Auth $auth) {
|
||||
|
||||
// ...
|
||||
|
||||
if (canEditArticle($app->auth())) {
|
||||
// the user can edit articles here
|
||||
if (canEditArticle($auth)) {
|
||||
echo 'The user can edit articles here';
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
if (canEditArticle($app->auth())) {
|
||||
// ... and here
|
||||
if (canEditArticle($auth)) {
|
||||
echo '... and here';
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
if (canEditArticle($app->auth())) {
|
||||
// ... and here
|
||||
if (canEditArticle($auth)) {
|
||||
echo '... and here';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -773,17 +815,17 @@ try {
|
||||
if ($auth->reconfirmPassword($_POST['password'])) {
|
||||
$auth->setPasswordResetEnabled($_POST['enabled'] == 1);
|
||||
|
||||
// the setting has been changed
|
||||
echo 'The setting has been changed';
|
||||
}
|
||||
else {
|
||||
// we can't say if the user is who they claim to be
|
||||
echo 'We can\'t say if the user is who they claim to be';
|
||||
}
|
||||
}
|
||||
catch (\Delight\Auth\NotLoggedInException $e) {
|
||||
// the user is not signed in
|
||||
die('The user is not signed in');
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// too many requests
|
||||
die('Too many requests');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -806,7 +848,7 @@ try {
|
||||
// throttle the specified resource or feature to *3* requests per *60* seconds
|
||||
$auth->throttle([ 'my-resource-name' ], 3, 60);
|
||||
|
||||
// do something with the resource or feature
|
||||
echo 'Do something with the resource or feature';
|
||||
}
|
||||
catch (\Delight\Auth\TooManyRequestsException $e) {
|
||||
// operation cancelled
|
||||
@@ -840,16 +882,16 @@ Do not forget to implement secure access control before exposing access to this
|
||||
try {
|
||||
$userId = $auth->admin()->createUser($_POST['email'], $_POST['password'], $_POST['username']);
|
||||
|
||||
// we have signed up a new user with the ID `$userId`
|
||||
echo 'We have signed up a new user with the ID ' . $userId;
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// invalid email address
|
||||
die('Invalid email address');
|
||||
}
|
||||
catch (\Delight\Auth\InvalidPasswordException $e) {
|
||||
// invalid password
|
||||
die('Invalid password');
|
||||
}
|
||||
catch (\Delight\Auth\UserAlreadyExistsException $e) {
|
||||
// user already exists
|
||||
die('User already exists');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -866,7 +908,7 @@ try {
|
||||
$auth->admin()->deleteUserById($_POST['id']);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownIdException $e) {
|
||||
// unknown ID
|
||||
die('Unknown ID');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -877,7 +919,7 @@ try {
|
||||
$auth->admin()->deleteUserByEmail($_POST['email']);
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// unknown email address
|
||||
die('Unknown email address');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -888,13 +930,23 @@ try {
|
||||
$auth->admin()->deleteUserByUsername($_POST['username']);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownUsernameException $e) {
|
||||
// unknown username
|
||||
die('Unknown username');
|
||||
}
|
||||
catch (\Delight\Auth\AmbiguousUsernameException $e) {
|
||||
// ambiguous username
|
||||
die('Ambiguous username');
|
||||
}
|
||||
```
|
||||
|
||||
#### Retrieving a list of registered users
|
||||
|
||||
When fetching a list of all users, the requirements vary greatly between projects and use cases, and customization is common. For example, you might want to fetch different columns, join related tables, filter by certain criteria, change how results are sorted (in varying direction), and limit the number of results (while providing an offset).
|
||||
|
||||
That’s why it’s easier to use a single custom SQL query. Start with the following:
|
||||
|
||||
```sql
|
||||
SELECT id, email, username, status, verified, roles_mask, registered, last_login FROM users;
|
||||
```
|
||||
|
||||
#### Assigning roles to users
|
||||
|
||||
```php
|
||||
@@ -902,7 +954,7 @@ try {
|
||||
$auth->admin()->addRoleForUserById($userId, \Delight\Auth\Role::ADMIN);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownIdException $e) {
|
||||
// unknown user ID
|
||||
die('Unknown user ID');
|
||||
}
|
||||
|
||||
// or
|
||||
@@ -911,7 +963,7 @@ try {
|
||||
$auth->admin()->addRoleForUserByEmail($userEmail, \Delight\Auth\Role::ADMIN);
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// unknown email address
|
||||
die('Unknown email address');
|
||||
}
|
||||
|
||||
// or
|
||||
@@ -920,13 +972,15 @@ try {
|
||||
$auth->admin()->addRoleForUserByUsername($username, \Delight\Auth\Role::ADMIN);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownUsernameException $e) {
|
||||
// unknown username
|
||||
die('Unknown username');
|
||||
}
|
||||
catch (\Delight\Auth\AmbiguousUsernameException $e) {
|
||||
// ambiguous username
|
||||
die('Ambiguous username');
|
||||
}
|
||||
```
|
||||
|
||||
**Note:** Changes to a user’s set of roles may need up to five minutes to take effect. This increases performance and usually poses no problem. If you want to change this behavior, nevertheless, simply decrease (or perhaps increase) the value that you pass to the [`Auth` constructor](#creating-a-new-instance) as the argument named `$sessionResyncInterval`.
|
||||
|
||||
#### Taking roles away from users
|
||||
|
||||
```php
|
||||
@@ -934,7 +988,7 @@ try {
|
||||
$auth->admin()->removeRoleForUserById($userId, \Delight\Auth\Role::ADMIN);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownIdException $e) {
|
||||
// unknown user ID
|
||||
die('Unknown user ID');
|
||||
}
|
||||
|
||||
// or
|
||||
@@ -943,7 +997,7 @@ try {
|
||||
$auth->admin()->removeRoleForUserByEmail($userEmail, \Delight\Auth\Role::ADMIN);
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// unknown email address
|
||||
die('Unknown email address');
|
||||
}
|
||||
|
||||
// or
|
||||
@@ -952,26 +1006,28 @@ try {
|
||||
$auth->admin()->removeRoleForUserByUsername($username, \Delight\Auth\Role::ADMIN);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownUsernameException $e) {
|
||||
// unknown username
|
||||
die('Unknown username');
|
||||
}
|
||||
catch (\Delight\Auth\AmbiguousUsernameException $e) {
|
||||
// ambiguous username
|
||||
die('Ambiguous username');
|
||||
}
|
||||
```
|
||||
|
||||
**Note:** Changes to a user’s set of roles may need up to five minutes to take effect. This increases performance and usually poses no problem. If you want to change this behavior, nevertheless, simply decrease (or perhaps increase) the value that you pass to the [`Auth` constructor](#creating-a-new-instance) as the argument named `$sessionResyncInterval`.
|
||||
|
||||
#### Checking roles
|
||||
|
||||
```php
|
||||
try {
|
||||
if ($auth->admin()->doesUserHaveRole($userId, \Delight\Auth\Role::ADMIN)) {
|
||||
// the specified user is an administrator
|
||||
echo 'The specified user is an administrator';
|
||||
}
|
||||
else {
|
||||
// the specified user is *not* an administrator
|
||||
echo 'The specified user is not an administrator';
|
||||
}
|
||||
}
|
||||
catch (\Delight\Auth\UnknownIdException $e) {
|
||||
// unknown user ID
|
||||
die('Unknown user ID');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -988,10 +1044,10 @@ try {
|
||||
$auth->admin()->logInAsUserById($_POST['id']);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownIdException $e) {
|
||||
// unknown ID
|
||||
die('Unknown ID');
|
||||
}
|
||||
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
||||
// email address not verified
|
||||
die('Email address not verified');
|
||||
}
|
||||
|
||||
// or
|
||||
@@ -1000,10 +1056,10 @@ try {
|
||||
$auth->admin()->logInAsUserByEmail($_POST['email']);
|
||||
}
|
||||
catch (\Delight\Auth\InvalidEmailException $e) {
|
||||
// unknown email address
|
||||
die('Unknown email address');
|
||||
}
|
||||
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
||||
// email address not verified
|
||||
die('Email address not verified');
|
||||
}
|
||||
|
||||
// or
|
||||
@@ -1012,13 +1068,13 @@ try {
|
||||
$auth->admin()->logInAsUserByUsername($_POST['username']);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownUsernameException $e) {
|
||||
// unknown username
|
||||
die('Unknown username');
|
||||
}
|
||||
catch (\Delight\Auth\AmbiguousUsernameException $e) {
|
||||
// ambiguous username
|
||||
die('Ambiguous username');
|
||||
}
|
||||
catch (\Delight\Auth\EmailNotVerifiedException $e) {
|
||||
// email address not verified
|
||||
die('Email address not verified');
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1029,10 +1085,10 @@ try {
|
||||
$auth->admin()->changePasswordForUserById($_POST['id'], $_POST['newPassword']);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownIdException $e) {
|
||||
// unknown ID
|
||||
die('Unknown ID');
|
||||
}
|
||||
catch (\Delight\Auth\InvalidPasswordException $e) {
|
||||
// invalid password
|
||||
die('Invalid password');
|
||||
}
|
||||
|
||||
// or
|
||||
@@ -1041,13 +1097,13 @@ try {
|
||||
$auth->admin()->changePasswordForUserByUsername($_POST['username'], $_POST['newPassword']);
|
||||
}
|
||||
catch (\Delight\Auth\UnknownUsernameException $e) {
|
||||
// unknown username
|
||||
die('Unknown username');
|
||||
}
|
||||
catch (\Delight\Auth\AmbiguousUsernameException $e) {
|
||||
// ambiguous username
|
||||
die('Ambiguous username');
|
||||
}
|
||||
catch (\Delight\Auth\InvalidPasswordException $e) {
|
||||
// invalid password
|
||||
die('Invalid password');
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -6,7 +6,7 @@
|
||||
"ext-openssl": "*",
|
||||
"delight-im/base64": "^1.0",
|
||||
"delight-im/cookie": "^3.1",
|
||||
"delight-im/db": "^1.2"
|
||||
"delight-im/db": "^1.3"
|
||||
},
|
||||
"type": "library",
|
||||
"keywords": [ "auth", "authentication", "login", "security" ],
|
||||
|
12
composer.lock
generated
12
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "54d541ae3c5ba25b0cc06688d2b65467",
|
||||
"content-hash": "e4acd9e4ba13c4d0692f07a03a454859",
|
||||
"packages": [
|
||||
{
|
||||
"name": "delight-im/base64",
|
||||
@@ -90,16 +90,16 @@
|
||||
},
|
||||
{
|
||||
"name": "delight-im/db",
|
||||
"version": "v1.2.0",
|
||||
"version": "v1.3.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/delight-im/PHP-DB.git",
|
||||
"reference": "df99ef7c2e86c7ce206647ffe8ba74447c075b57"
|
||||
"reference": "7a03da20b5592fa445c10cd6c7245d51037292c4"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/delight-im/PHP-DB/zipball/df99ef7c2e86c7ce206647ffe8ba74447c075b57",
|
||||
"reference": "df99ef7c2e86c7ce206647ffe8ba74447c075b57",
|
||||
"url": "https://api.github.com/repos/delight-im/PHP-DB/zipball/7a03da20b5592fa445c10cd6c7245d51037292c4",
|
||||
"reference": "7a03da20b5592fa445c10cd6c7245d51037292c4",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -127,7 +127,7 @@
|
||||
"sql",
|
||||
"sqlite"
|
||||
],
|
||||
"time": "2017-03-18T20:51:59+00:00"
|
||||
"time": "2018-08-28T18:23:01+00:00"
|
||||
},
|
||||
{
|
||||
"name": "delight-im/http",
|
||||
|
@@ -12,17 +12,16 @@ use Delight\Db\PdoDatabase;
|
||||
use Delight\Db\PdoDsn;
|
||||
use Delight\Db\Throwable\Error;
|
||||
|
||||
require_once __DIR__ . '/Exceptions.php';
|
||||
|
||||
/** Component that can be used for administrative tasks by privileged and authorized users */
|
||||
final class Administration extends UserManager {
|
||||
|
||||
/**
|
||||
* @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection to operate on
|
||||
* @param string|null $dbTablePrefix (optional) the prefix for the names of all database tables used by this component
|
||||
* @param string|null $dbSchema (optional) the schema name for all database tables used by this component
|
||||
*/
|
||||
public function __construct($databaseConnection, $dbTablePrefix = null) {
|
||||
parent::__construct($databaseConnection, $dbTablePrefix);
|
||||
public function __construct($databaseConnection, $dbTablePrefix = null, $dbSchema = null) {
|
||||
parent::__construct($databaseConnection, $dbTablePrefix, $dbSchema);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -270,11 +269,14 @@ final class Administration extends UserManager {
|
||||
* @see Role
|
||||
*/
|
||||
public function doesUserHaveRole($userId, $role) {
|
||||
if (empty($role) || !\is_numeric($role)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$userId = (int) $userId;
|
||||
$role = (int) $role;
|
||||
|
||||
$rolesBitmask = $this->db->selectValue(
|
||||
'SELECT roles_mask FROM ' . $this->dbTablePrefix . 'users WHERE id = ?',
|
||||
'SELECT roles_mask FROM ' . $this->makeTableName('users') . ' WHERE id = ?',
|
||||
[ $userId ]
|
||||
);
|
||||
|
||||
@@ -282,6 +284,8 @@ final class Administration extends UserManager {
|
||||
throw new UnknownIdException();
|
||||
}
|
||||
|
||||
$role = (int) $role;
|
||||
|
||||
return ($rolesBitmask & $role) === $role;
|
||||
}
|
||||
|
||||
@@ -298,7 +302,7 @@ final class Administration extends UserManager {
|
||||
$userId = (int) $userId;
|
||||
|
||||
$rolesBitmask = $this->db->selectValue(
|
||||
'SELECT roles_mask FROM ' . $this->dbTablePrefix . 'users WHERE id = ?',
|
||||
'SELECT roles_mask FROM ' . $this->makeTableName('users') . ' WHERE id = ?',
|
||||
[ $userId ]
|
||||
);
|
||||
|
||||
@@ -425,14 +429,14 @@ final class Administration extends UserManager {
|
||||
private function deleteUsersByColumnValue($columnName, $columnValue) {
|
||||
try {
|
||||
return $this->db->delete(
|
||||
$this->dbTablePrefix . 'users',
|
||||
$this->makeTableNameComponents('users'),
|
||||
[
|
||||
$columnName => $columnValue
|
||||
]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,12 +456,12 @@ final class Administration extends UserManager {
|
||||
private function modifyRolesForUserByColumnValue($columnName, $columnValue, callable $modification) {
|
||||
try {
|
||||
$userData = $this->db->selectRow(
|
||||
'SELECT id, roles_mask FROM ' . $this->dbTablePrefix . 'users WHERE ' . $columnName . ' = ?',
|
||||
'SELECT id, roles_mask FROM ' . $this->makeTableName('users') . ' WHERE ' . $columnName . ' = ?',
|
||||
[ $columnValue ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if ($userData === null) {
|
||||
@@ -468,7 +472,7 @@ final class Administration extends UserManager {
|
||||
|
||||
try {
|
||||
$this->db->exec(
|
||||
'UPDATE ' . $this->dbTablePrefix . 'users SET roles_mask = ? WHERE id = ?',
|
||||
'UPDATE ' . $this->makeTableName('users') . ' SET roles_mask = ? WHERE id = ?',
|
||||
[
|
||||
$newRolesBitmask,
|
||||
(int) $userData['id']
|
||||
@@ -478,7 +482,7 @@ final class Administration extends UserManager {
|
||||
return true;
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,12 +548,12 @@ final class Administration extends UserManager {
|
||||
private function logInAsUserByColumnValue($columnName, $columnValue) {
|
||||
try {
|
||||
$users = $this->db->select(
|
||||
'SELECT verified, id, email, username, status, roles_mask FROM ' . $this->dbTablePrefix . 'users WHERE ' . $columnName . ' = ? LIMIT 2 OFFSET 0',
|
||||
'SELECT verified, id, email, username, status, roles_mask FROM ' . $this->makeTableName('users') . ' WHERE ' . $columnName . ' = ? LIMIT 2 OFFSET 0',
|
||||
[ $columnValue ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
$numberOfMatchingUsers = ($users !== null) ? \count($users) : 0;
|
||||
|
11
src/AmbiguousUsernameException.php
Normal file
11
src/AmbiguousUsernameException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class AmbiguousUsernameException extends AuthException {}
|
11
src/AttemptCancelledException.php
Normal file
11
src/AttemptCancelledException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class AttemptCancelledException extends AuthException {}
|
188
src/Auth.php
188
src/Auth.php
@@ -16,8 +16,6 @@ use Delight\Db\PdoDsn;
|
||||
use Delight\Db\Throwable\Error;
|
||||
use Delight\Db\Throwable\IntegrityConstraintViolationException;
|
||||
|
||||
require_once __DIR__ . '/Exceptions.php';
|
||||
|
||||
/** Component that provides all features and utilities for secure authentication of individual users */
|
||||
final class Auth extends UserManager {
|
||||
|
||||
@@ -35,13 +33,14 @@ final class Auth extends UserManager {
|
||||
|
||||
/**
|
||||
* @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection to operate on
|
||||
* @param string $ipAddress the IP address that should be used instead of the default setting (if any), e.g. when behind a proxy
|
||||
* @param string|null $ipAddress (optional) the IP address that should be used instead of the default setting (if any), e.g. when behind a proxy
|
||||
* @param string|null $dbTablePrefix (optional) the prefix for the names of all database tables used by this component
|
||||
* @param bool|null $throttling (optional) whether throttling should be enabled (e.g. in production) or disabled (e.g. during development)
|
||||
* @param int|null $sessionResyncInterval (optional) the interval in seconds after which to resynchronize the session data with its authoritative source in the database
|
||||
* @param string|null $dbSchema (optional) the schema name for all database tables used by this component
|
||||
*/
|
||||
public function __construct($databaseConnection, $ipAddress = null, $dbTablePrefix = null, $throttling = null, $sessionResyncInterval = null) {
|
||||
parent::__construct($databaseConnection, $dbTablePrefix);
|
||||
public function __construct($databaseConnection, $ipAddress = null, $dbTablePrefix = null, $throttling = null, $sessionResyncInterval = null, $dbSchema = null) {
|
||||
parent::__construct($databaseConnection, $dbTablePrefix, $dbSchema);
|
||||
|
||||
$this->ipAddress = !empty($ipAddress) ? $ipAddress : (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null);
|
||||
$this->throttling = isset($throttling) ? (bool) $throttling : true;
|
||||
@@ -114,12 +113,12 @@ final class Auth extends UserManager {
|
||||
if (!empty($parts[0]) && !empty($parts[1])) {
|
||||
try {
|
||||
$rememberData = $this->db->selectRow(
|
||||
'SELECT a.user, a.token, a.expires, b.email, b.username, b.status, b.roles_mask, b.force_logout FROM ' . $this->dbTablePrefix . 'users_remembered AS a JOIN ' . $this->dbTablePrefix . 'users AS b ON a.user = b.id WHERE a.selector = ?',
|
||||
'SELECT a.user, a.token, a.expires, b.email, b.username, b.status, b.roles_mask, b.force_logout FROM ' . $this->makeTableName('users_remembered') . ' AS a JOIN ' . $this->makeTableName('users') . ' AS b ON a.user = b.id WHERE a.selector = ?',
|
||||
[ $parts[0] ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if (!empty($rememberData)) {
|
||||
@@ -156,12 +155,12 @@ final class Auth extends UserManager {
|
||||
// fetch the authoritative data from the database again
|
||||
try {
|
||||
$authoritativeData = $this->db->selectRow(
|
||||
'SELECT email, username, status, roles_mask, force_logout FROM ' . $this->dbTablePrefix . 'users WHERE id = ?',
|
||||
'SELECT email, username, status, roles_mask, force_logout FROM ' . $this->makeTableName('users') . ' WHERE id = ?',
|
||||
[ $this->getUserId() ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
// if the user's data has been found
|
||||
@@ -353,12 +352,12 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$expectedHash = $this->db->selectValue(
|
||||
'SELECT password FROM ' . $this->dbTablePrefix . 'users WHERE id = ?',
|
||||
'SELECT password FROM ' . $this->makeTableName('users') . ' WHERE id = ?',
|
||||
[ $this->getUserId() ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if (!empty($expectedHash)) {
|
||||
@@ -496,7 +495,7 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$this->db->insert(
|
||||
$this->dbTablePrefix . 'users_remembered',
|
||||
$this->makeTableNameComponents('users_remembered'),
|
||||
[
|
||||
'user' => $userId,
|
||||
'selector' => $selector,
|
||||
@@ -506,7 +505,7 @@ final class Auth extends UserManager {
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
$this->setRememberCookie($selector, $token, $expires);
|
||||
@@ -566,13 +565,13 @@ final class Auth extends UserManager {
|
||||
// update the timestamp of the user's last login
|
||||
try {
|
||||
$this->db->update(
|
||||
$this->dbTablePrefix . 'users',
|
||||
$this->makeTableNameComponents('users'),
|
||||
[ 'last_login' => \time() ],
|
||||
[ 'id' => $userId ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
parent::onLoginSuccessful($userId, $email, $username, $status, $roles, $forceLogout, $remembered);
|
||||
@@ -620,12 +619,12 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$confirmationData = $this->db->selectRow(
|
||||
'SELECT a.id, a.user_id, a.email AS new_email, a.token, a.expires, b.email AS old_email FROM ' . $this->dbTablePrefix . 'users_confirmations AS a JOIN ' . $this->dbTablePrefix . 'users AS b ON b.id = a.user_id WHERE a.selector = ?',
|
||||
'SELECT a.id, a.user_id, a.email AS new_email, a.token, a.expires, b.email AS old_email FROM ' . $this->makeTableName('users_confirmations') . ' AS a JOIN ' . $this->makeTableName('users') . ' AS b ON b.id = a.user_id WHERE a.selector = ?',
|
||||
[ $selector ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if (!empty($confirmationData)) {
|
||||
@@ -634,18 +633,18 @@ final class Auth extends UserManager {
|
||||
// invalidate any potential outstanding password reset requests
|
||||
try {
|
||||
$this->db->delete(
|
||||
$this->dbTablePrefix . 'users_resets',
|
||||
$this->makeTableNameComponents('users_resets'),
|
||||
[ 'user' => $confirmationData['user_id'] ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
// mark the email address as verified (and possibly update it to the new address given)
|
||||
try {
|
||||
$this->db->update(
|
||||
$this->dbTablePrefix . 'users',
|
||||
$this->makeTableNameComponents('users'),
|
||||
[
|
||||
'email' => $confirmationData['new_email'],
|
||||
'verified' => 1
|
||||
@@ -657,7 +656,7 @@ final class Auth extends UserManager {
|
||||
throw new UserAlreadyExistsException();
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
// if the user is currently signed in
|
||||
@@ -672,12 +671,12 @@ final class Auth extends UserManager {
|
||||
// consume the token just being used for confirmation
|
||||
try {
|
||||
$this->db->delete(
|
||||
$this->dbTablePrefix . 'users_confirmations',
|
||||
$this->makeTableNameComponents('users_confirmations'),
|
||||
[ 'id' => $confirmationData['id'] ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
// if the email address has not been changed but simply been verified
|
||||
@@ -817,12 +816,12 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$existingUsersWithNewEmail = $this->db->selectValue(
|
||||
'SELECT COUNT(*) FROM ' . $this->dbTablePrefix . 'users WHERE email = ?',
|
||||
'SELECT COUNT(*) FROM ' . $this->makeTableName('users') . ' WHERE email = ?',
|
||||
[ $newEmail ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if ((int) $existingUsersWithNewEmail !== 0) {
|
||||
@@ -831,12 +830,12 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$verified = $this->db->selectValue(
|
||||
'SELECT verified FROM ' . $this->dbTablePrefix . 'users WHERE id = ?',
|
||||
'SELECT verified FROM ' . $this->makeTableName('users') . ' WHERE id = ?',
|
||||
[ $this->getUserId() ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
// ensure that at least the current (old) email address has been verified before proceeding
|
||||
@@ -919,12 +918,12 @@ final class Auth extends UserManager {
|
||||
private function resendConfirmationForColumnValue($columnName, $columnValue, callable $callback) {
|
||||
try {
|
||||
$latestAttempt = $this->db->selectRow(
|
||||
'SELECT user_id, email FROM ' . $this->dbTablePrefix . 'users_confirmations WHERE ' . $columnName . ' = ? ORDER BY id DESC LIMIT 1 OFFSET 0',
|
||||
'SELECT user_id, email FROM ' . $this->makeTableName('users_confirmations') . ' WHERE ' . $columnName . ' = ? ORDER BY id DESC LIMIT 1 OFFSET 0',
|
||||
[ $columnValue ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if ($latestAttempt === null) {
|
||||
@@ -961,6 +960,11 @@ final class Auth extends UserManager {
|
||||
* @throws ResetDisabledException if the user has explicitly disabled password resets for their account
|
||||
* @throws TooManyRequestsException if the number of allowed attempts/requests has been exceeded
|
||||
* @throws AuthError if an internal problem occurred (do *not* catch)
|
||||
*
|
||||
* @see canResetPasswordOrThrow
|
||||
* @see canResetPassword
|
||||
* @see resetPassword
|
||||
* @see resetPasswordAndSignIn
|
||||
*/
|
||||
public function forgotPassword($email, callable $callback, $requestExpiresAfter = null, $maxOpenRequests = null) {
|
||||
$email = self::validateEmailAddress($email);
|
||||
@@ -998,7 +1002,7 @@ final class Auth extends UserManager {
|
||||
throw new ResetDisabledException();
|
||||
}
|
||||
|
||||
$openRequests = (int) $this->getOpenPasswordResetRequests($userData['id']);
|
||||
$openRequests = $this->throttling ? (int) $this->getOpenPasswordResetRequests($userData['id']) : 0;
|
||||
|
||||
if ($openRequests < $maxOpenRequests) {
|
||||
$this->throttle([ 'requestPasswordReset', $this->getIpAddress() ], 4, (60 * 60 * 24 * 7), 2);
|
||||
@@ -1132,12 +1136,12 @@ final class Auth extends UserManager {
|
||||
try {
|
||||
$projection = \implode(', ', $requestedColumns);
|
||||
$userData = $this->db->selectRow(
|
||||
'SELECT ' . $projection . ' FROM ' . $this->dbTablePrefix . 'users WHERE email = ?',
|
||||
'SELECT ' . $projection . ' FROM ' . $this->makeTableName('users') . ' WHERE email = ?',
|
||||
[ $email ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if (!empty($userData)) {
|
||||
@@ -1158,7 +1162,7 @@ final class Auth extends UserManager {
|
||||
private function getOpenPasswordResetRequests($userId) {
|
||||
try {
|
||||
$requests = $this->db->selectValue(
|
||||
'SELECT COUNT(*) FROM ' . $this->dbTablePrefix . 'users_resets WHERE user = ? AND expires > ?',
|
||||
'SELECT COUNT(*) FROM ' . $this->makeTableName('users_resets') . ' WHERE user = ? AND expires > ?',
|
||||
[
|
||||
$userId,
|
||||
\time()
|
||||
@@ -1173,7 +1177,7 @@ final class Auth extends UserManager {
|
||||
}
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1201,7 +1205,7 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$this->db->insert(
|
||||
$this->dbTablePrefix . 'users_resets',
|
||||
$this->makeTableNameComponents('users_resets'),
|
||||
[
|
||||
'user' => $userId,
|
||||
'selector' => $selector,
|
||||
@@ -1211,7 +1215,7 @@ final class Auth extends UserManager {
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if (\is_callable($callback)) {
|
||||
@@ -1225,17 +1229,23 @@ final class Auth extends UserManager {
|
||||
/**
|
||||
* Resets the password for a particular account by supplying the correct selector/token pair
|
||||
*
|
||||
* The selector/token pair must have been generated previously by calling `Auth#forgotPassword(...)`
|
||||
* The selector/token pair must have been generated previously by calling {@see forgotPassword}
|
||||
*
|
||||
* @param string $selector the selector from the selector/token pair
|
||||
* @param string $token the token from the selector/token pair
|
||||
* @param string $newPassword the new password to set for the account
|
||||
* @return string[] an array with the user's ID at index `id` and the user's email address at index `email`
|
||||
* @throws InvalidSelectorTokenPairException if either the selector or the token was not correct
|
||||
* @throws TokenExpiredException if the token has already expired
|
||||
* @throws ResetDisabledException if the user has explicitly disabled password resets for their account
|
||||
* @throws InvalidPasswordException if the new password was invalid
|
||||
* @throws TooManyRequestsException if the number of allowed attempts/requests has been exceeded
|
||||
* @throws AuthError if an internal problem occurred (do *not* catch)
|
||||
*
|
||||
* @see forgotPassword
|
||||
* @see canResetPasswordOrThrow
|
||||
* @see canResetPassword
|
||||
* @see resetPasswordAndSignIn
|
||||
*/
|
||||
public function resetPassword($selector, $token, $newPassword) {
|
||||
$this->throttle([ 'resetPassword', $this->getIpAddress() ], 5, (60 * 60), 10);
|
||||
@@ -1244,12 +1254,12 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$resetData = $this->db->selectRow(
|
||||
'SELECT a.id, a.user, a.token, a.expires, b.resettable FROM ' . $this->dbTablePrefix . 'users_resets AS a JOIN ' . $this->dbTablePrefix . 'users AS b ON b.id = a.user WHERE a.selector = ?',
|
||||
'SELECT a.id, a.user, a.token, a.expires, b.email, b.resettable FROM ' . $this->makeTableName('users_resets') . ' AS a JOIN ' . $this->makeTableName('users') . ' AS b ON b.id = a.user WHERE a.selector = ?',
|
||||
[ $selector ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if (!empty($resetData)) {
|
||||
@@ -1262,13 +1272,18 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$this->db->delete(
|
||||
$this->dbTablePrefix . 'users_resets',
|
||||
$this->makeTableNameComponents('users_resets'),
|
||||
[ 'id' => $resetData['id'] ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $resetData['user'],
|
||||
'email' => $resetData['email']
|
||||
];
|
||||
}
|
||||
else {
|
||||
throw new TokenExpiredException();
|
||||
@@ -1287,12 +1302,57 @@ final class Auth extends UserManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the password for a particular account by supplying the correct selector/token pair
|
||||
*
|
||||
* The selector/token pair must have been generated previously by calling {@see forgotPassword}
|
||||
*
|
||||
* The user will be automatically signed in if this operation is successful
|
||||
*
|
||||
* @param string $selector the selector from the selector/token pair
|
||||
* @param string $token the token from the selector/token pair
|
||||
* @param string $newPassword the new password to set for the account
|
||||
* @param int|null $rememberDuration (optional) the duration in seconds to keep the user logged in ("remember me"), e.g. `60 * 60 * 24 * 365.25` for one year
|
||||
* @return string[] an array with the user's ID at index `id` and the user's email address at index `email`
|
||||
* @throws InvalidSelectorTokenPairException if either the selector or the token was not correct
|
||||
* @throws TokenExpiredException if the token has already expired
|
||||
* @throws ResetDisabledException if the user has explicitly disabled password resets for their account
|
||||
* @throws InvalidPasswordException if the new password was invalid
|
||||
* @throws TooManyRequestsException if the number of allowed attempts/requests has been exceeded
|
||||
* @throws AuthError if an internal problem occurred (do *not* catch)
|
||||
*
|
||||
* @see forgotPassword
|
||||
* @see canResetPasswordOrThrow
|
||||
* @see canResetPassword
|
||||
* @see resetPassword
|
||||
*/
|
||||
public function resetPasswordAndSignIn($selector, $token, $newPassword, $rememberDuration = null) {
|
||||
$idAndEmail = $this->resetPassword($selector, $token, $newPassword);
|
||||
|
||||
if (!$this->isLoggedIn()) {
|
||||
$idAndEmail['email'] = self::validateEmailAddress($idAndEmail['email']);
|
||||
|
||||
$userData = $this->getUserDataByEmailAddress(
|
||||
$idAndEmail['email'],
|
||||
[ 'username', 'status', 'roles_mask', 'force_logout' ]
|
||||
);
|
||||
|
||||
$this->onLoginSuccessful($idAndEmail['id'], $idAndEmail['email'], $userData['username'], $userData['status'], $userData['roles_mask'], $userData['force_logout'], true);
|
||||
|
||||
if ($rememberDuration !== null) {
|
||||
$this->createRememberDirective($idAndEmail['id'], $rememberDuration);
|
||||
}
|
||||
}
|
||||
|
||||
return $idAndEmail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the supplied selector/token pair can be used to reset a password
|
||||
*
|
||||
* The password can be reset using the supplied information if this method does *not* throw any exception
|
||||
*
|
||||
* The selector/token pair must have been generated previously by calling `Auth#forgotPassword(...)`
|
||||
* The selector/token pair must have been generated previously by calling {@see forgotPassword}
|
||||
*
|
||||
* @param string $selector the selector from the selector/token pair
|
||||
* @param string $token the token from the selector/token pair
|
||||
@@ -1301,6 +1361,11 @@ final class Auth extends UserManager {
|
||||
* @throws ResetDisabledException if the user has explicitly disabled password resets for their account
|
||||
* @throws TooManyRequestsException if the number of allowed attempts/requests has been exceeded
|
||||
* @throws AuthError if an internal problem occurred (do *not* catch)
|
||||
*
|
||||
* @see forgotPassword
|
||||
* @see canResetPassword
|
||||
* @see resetPassword
|
||||
* @see resetPasswordAndSignIn
|
||||
*/
|
||||
public function canResetPasswordOrThrow($selector, $token) {
|
||||
try {
|
||||
@@ -1324,12 +1389,17 @@ final class Auth extends UserManager {
|
||||
/**
|
||||
* Check if the supplied selector/token pair can be used to reset a password
|
||||
*
|
||||
* The selector/token pair must have been generated previously by calling `Auth#forgotPassword(...)`
|
||||
* The selector/token pair must have been generated previously by calling {@see forgotPassword}
|
||||
*
|
||||
* @param string $selector the selector from the selector/token pair
|
||||
* @param string $token the token from the selector/token pair
|
||||
* @return bool whether the password can be reset using the supplied information
|
||||
* @throws AuthError if an internal problem occurred (do *not* catch)
|
||||
*
|
||||
* @see forgotPassword
|
||||
* @see canResetPasswordOrThrow
|
||||
* @see resetPassword
|
||||
* @see resetPasswordAndSignIn
|
||||
*/
|
||||
public function canResetPassword($selector, $token) {
|
||||
try {
|
||||
@@ -1355,7 +1425,7 @@ final class Auth extends UserManager {
|
||||
if ($this->isLoggedIn()) {
|
||||
try {
|
||||
$this->db->update(
|
||||
$this->dbTablePrefix . 'users',
|
||||
$this->makeTableNameComponents('users'),
|
||||
[
|
||||
'resettable' => $enabled ? 1 : 0
|
||||
],
|
||||
@@ -1365,7 +1435,7 @@ final class Auth extends UserManager {
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -1384,14 +1454,14 @@ final class Auth extends UserManager {
|
||||
if ($this->isLoggedIn()) {
|
||||
try {
|
||||
$enabled = $this->db->selectValue(
|
||||
'SELECT resettable FROM ' . $this->dbTablePrefix . 'users WHERE id = ?',
|
||||
'SELECT resettable FROM ' . $this->makeTableName('users') . ' WHERE id = ?',
|
||||
[ $this->getUserId() ]
|
||||
);
|
||||
|
||||
return (int) $enabled === 1;
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -1432,7 +1502,7 @@ final class Auth extends UserManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand/alias for `getUserId()`
|
||||
* Shorthand/alias for {@see getUserId}
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
@@ -1563,9 +1633,13 @@ final class Auth extends UserManager {
|
||||
* @see Role
|
||||
*/
|
||||
public function hasRole($role) {
|
||||
$role = (int) $role;
|
||||
if (empty($role) || !\is_numeric($role)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($_SESSION) && isset($_SESSION[self::SESSION_FIELD_ROLES])) {
|
||||
$role = (int) $role;
|
||||
|
||||
return (((int) $_SESSION[self::SESSION_FIELD_ROLES]) & $role) === $role;
|
||||
}
|
||||
else {
|
||||
@@ -1687,12 +1761,12 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$bucket = $this->db->selectRow(
|
||||
'SELECT tokens, replenished_at FROM ' . $this->dbTablePrefix . 'users_throttling WHERE bucket = ?',
|
||||
'SELECT tokens, replenished_at FROM ' . $this->makeTableName('users_throttling') . ' WHERE bucket = ?',
|
||||
[ $key ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if ($bucket === null) {
|
||||
@@ -1724,13 +1798,13 @@ final class Auth extends UserManager {
|
||||
// merge the updated bucket into the database
|
||||
try {
|
||||
$affected = $this->db->update(
|
||||
$this->dbTablePrefix . 'users_throttling',
|
||||
$this->makeTableNameComponents('users_throttling'),
|
||||
$bucket,
|
||||
[ 'bucket' => $key ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if ($affected === 0) {
|
||||
@@ -1738,13 +1812,13 @@ final class Auth extends UserManager {
|
||||
|
||||
try {
|
||||
$this->db->insert(
|
||||
$this->dbTablePrefix . 'users_throttling',
|
||||
$this->makeTableNameComponents('users_throttling'),
|
||||
$bucket
|
||||
);
|
||||
}
|
||||
catch (IntegrityConstraintViolationException $ignored) {}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1768,7 +1842,7 @@ final class Auth extends UserManager {
|
||||
* @return Administration
|
||||
*/
|
||||
public function admin() {
|
||||
return new Administration($this->db, $this->dbTablePrefix);
|
||||
return new Administration($this->db, $this->dbTablePrefix, $this->dbSchema);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1864,7 +1938,7 @@ final class Auth extends UserManager {
|
||||
if (isset($existingSelector)) {
|
||||
// fetch the expiry date for the given selector
|
||||
$existingExpiry = $this->db->selectValue(
|
||||
'SELECT expires FROM ' . $this->dbTablePrefix . 'users_remembered WHERE selector = ? AND user = ?',
|
||||
'SELECT expires FROM ' . $this->makeTableName('users_remembered') . ' WHERE selector = ? AND user = ?',
|
||||
[
|
||||
$existingSelector,
|
||||
$this->getUserId()
|
||||
|
12
src/AuthError.php
Normal file
12
src/AuthError.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
/** Base class for all (unchecked) errors */
|
||||
class AuthError extends \Exception {}
|
12
src/AuthException.php
Normal file
12
src/AuthException.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
/** Base class for all (checked) exceptions */
|
||||
class AuthException extends \Exception {}
|
11
src/ConfirmationRequestNotFound.php
Normal file
11
src/ConfirmationRequestNotFound.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class ConfirmationRequestNotFound extends AuthException {}
|
11
src/DatabaseError.php
Normal file
11
src/DatabaseError.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class DatabaseError extends AuthError {}
|
11
src/DuplicateUsernameException.php
Normal file
11
src/DuplicateUsernameException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class DuplicateUsernameException extends AuthException {}
|
11
src/EmailNotVerifiedException.php
Normal file
11
src/EmailNotVerifiedException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class EmailNotVerifiedException extends AuthException {}
|
11
src/EmailOrUsernameRequiredError.php
Normal file
11
src/EmailOrUsernameRequiredError.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class EmailOrUsernameRequiredError extends AuthError {}
|
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class AuthException extends \Exception {}
|
||||
|
||||
class UnknownIdException extends AuthException {}
|
||||
|
||||
class InvalidEmailException extends AuthException {}
|
||||
|
||||
class UnknownUsernameException extends AuthException {}
|
||||
|
||||
class InvalidPasswordException extends AuthException {}
|
||||
|
||||
class EmailNotVerifiedException extends AuthException {}
|
||||
|
||||
class UserAlreadyExistsException extends AuthException {}
|
||||
|
||||
class NotLoggedInException extends AuthException {}
|
||||
|
||||
class InvalidSelectorTokenPairException extends AuthException {}
|
||||
|
||||
class TokenExpiredException extends AuthException {}
|
||||
|
||||
class TooManyRequestsException extends AuthException {}
|
||||
|
||||
class DuplicateUsernameException extends AuthException {}
|
||||
|
||||
class AmbiguousUsernameException extends AuthException {}
|
||||
|
||||
class AttemptCancelledException extends AuthException {}
|
||||
|
||||
class ResetDisabledException extends AuthException {}
|
||||
|
||||
class ConfirmationRequestNotFound extends AuthException {}
|
||||
|
||||
class AuthError extends \Exception {}
|
||||
|
||||
class DatabaseError extends AuthError {}
|
||||
|
||||
class DatabaseDriverError extends DatabaseError {}
|
||||
|
||||
class MissingCallbackError extends AuthError {}
|
||||
|
||||
class HeadersAlreadySentError extends AuthError {}
|
||||
|
||||
class EmailOrUsernameRequiredError extends AuthError {}
|
11
src/HeadersAlreadySentError.php
Normal file
11
src/HeadersAlreadySentError.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class HeadersAlreadySentError extends AuthError {}
|
11
src/InvalidEmailException.php
Normal file
11
src/InvalidEmailException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class InvalidEmailException extends AuthException {}
|
11
src/InvalidPasswordException.php
Normal file
11
src/InvalidPasswordException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class InvalidPasswordException extends AuthException {}
|
11
src/InvalidSelectorTokenPairException.php
Normal file
11
src/InvalidSelectorTokenPairException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class InvalidSelectorTokenPairException extends AuthException {}
|
11
src/MissingCallbackError.php
Normal file
11
src/MissingCallbackError.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class MissingCallbackError extends AuthError {}
|
11
src/NotLoggedInException.php
Normal file
11
src/NotLoggedInException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class NotLoggedInException extends AuthException {}
|
11
src/ResetDisabledException.php
Normal file
11
src/ResetDisabledException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class ResetDisabledException extends AuthException {}
|
11
src/TokenExpiredException.php
Normal file
11
src/TokenExpiredException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class TokenExpiredException extends AuthException {}
|
11
src/TooManyRequestsException.php
Normal file
11
src/TooManyRequestsException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class TooManyRequestsException extends AuthException {}
|
11
src/UnknownIdException.php
Normal file
11
src/UnknownIdException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class UnknownIdException extends AuthException {}
|
11
src/UnknownUsernameException.php
Normal file
11
src/UnknownUsernameException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class UnknownUsernameException extends AuthException {}
|
11
src/UserAlreadyExistsException.php
Normal file
11
src/UserAlreadyExistsException.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* PHP-Auth (https://github.com/delight-im/PHP-Auth)
|
||||
* Copyright (c) delight.im (https://www.delight.im/)
|
||||
* Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
namespace Delight\Auth;
|
||||
|
||||
class UserAlreadyExistsException extends AuthException {}
|
@@ -15,8 +15,6 @@ use Delight\Db\PdoDsn;
|
||||
use Delight\Db\Throwable\Error;
|
||||
use Delight\Db\Throwable\IntegrityConstraintViolationException;
|
||||
|
||||
require_once __DIR__ . '/Exceptions.php';
|
||||
|
||||
/**
|
||||
* Abstract base class for components implementing user management
|
||||
*
|
||||
@@ -45,6 +43,8 @@ abstract class UserManager {
|
||||
|
||||
/** @var PdoDatabase the database connection to operate on */
|
||||
protected $db;
|
||||
/** @var string|null the schema name for all database tables used by this component */
|
||||
protected $dbSchema;
|
||||
/** @var string the prefix for the names of all database tables used by this component */
|
||||
protected $dbTablePrefix;
|
||||
|
||||
@@ -70,8 +70,9 @@ abstract class UserManager {
|
||||
/**
|
||||
* @param PdoDatabase|PdoDsn|\PDO $databaseConnection the database connection to operate on
|
||||
* @param string|null $dbTablePrefix (optional) the prefix for the names of all database tables used by this component
|
||||
* @param string|null $dbSchema (optional) the schema name for all database tables used by this component
|
||||
*/
|
||||
protected function __construct($databaseConnection, $dbTablePrefix = null) {
|
||||
protected function __construct($databaseConnection, $dbTablePrefix = null, $dbSchema = null) {
|
||||
if ($databaseConnection instanceof PdoDatabase) {
|
||||
$this->db = $databaseConnection;
|
||||
}
|
||||
@@ -87,6 +88,7 @@ abstract class UserManager {
|
||||
throw new \InvalidArgumentException('The database connection must be an instance of either `PdoDatabase`, `PdoDsn` or `PDO`');
|
||||
}
|
||||
|
||||
$this->dbSchema = $dbSchema !== null ? (string) $dbSchema : null;
|
||||
$this->dbTablePrefix = (string) $dbTablePrefix;
|
||||
}
|
||||
|
||||
@@ -140,7 +142,7 @@ abstract class UserManager {
|
||||
if ($username !== null) {
|
||||
// count the number of users who do already have that specified username
|
||||
$occurrencesOfUsername = $this->db->selectValue(
|
||||
'SELECT COUNT(*) FROM ' . $this->dbTablePrefix . 'users WHERE username = ?',
|
||||
'SELECT COUNT(*) FROM ' . $this->makeTableName('users') . ' WHERE username = ?',
|
||||
[ $username ]
|
||||
);
|
||||
|
||||
@@ -157,7 +159,7 @@ abstract class UserManager {
|
||||
|
||||
try {
|
||||
$this->db->insert(
|
||||
$this->dbTablePrefix . 'users',
|
||||
$this->makeTableNameComponents('users'),
|
||||
[
|
||||
'email' => $email,
|
||||
'password' => $password,
|
||||
@@ -172,7 +174,7 @@ abstract class UserManager {
|
||||
throw new UserAlreadyExistsException();
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
$newUserId = (int) $this->db->getLastInsertId();
|
||||
@@ -197,7 +199,7 @@ abstract class UserManager {
|
||||
|
||||
try {
|
||||
$affected = $this->db->update(
|
||||
$this->dbTablePrefix . 'users',
|
||||
$this->makeTableNameComponents('users'),
|
||||
[ 'password' => $newPassword ],
|
||||
[ 'id' => $userId ]
|
||||
);
|
||||
@@ -207,7 +209,7 @@ abstract class UserManager {
|
||||
}
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,12 +260,12 @@ abstract class UserManager {
|
||||
$projection = \implode(', ', $requestedColumns);
|
||||
|
||||
$users = $this->db->select(
|
||||
'SELECT ' . $projection . ' FROM ' . $this->dbTablePrefix . 'users WHERE username = ? LIMIT 2 OFFSET 0',
|
||||
'SELECT ' . $projection . ' FROM ' . $this->makeTableName('users') . ' WHERE username = ? LIMIT 2 OFFSET 0',
|
||||
[ $username ]
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if (empty($users)) {
|
||||
@@ -345,7 +347,7 @@ abstract class UserManager {
|
||||
|
||||
try {
|
||||
$this->db->insert(
|
||||
$this->dbTablePrefix . 'users_confirmations',
|
||||
$this->makeTableNameComponents('users_confirmations'),
|
||||
[
|
||||
'user_id' => (int) $userId,
|
||||
'email' => $email,
|
||||
@@ -356,7 +358,7 @@ abstract class UserManager {
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
|
||||
if (\is_callable($callback)) {
|
||||
@@ -385,12 +387,12 @@ abstract class UserManager {
|
||||
|
||||
try {
|
||||
$this->db->delete(
|
||||
$this->dbTablePrefix . 'users_remembered',
|
||||
$this->makeTableNameComponents('users_remembered'),
|
||||
$whereMappings
|
||||
);
|
||||
}
|
||||
catch (Error $e) {
|
||||
throw new DatabaseError();
|
||||
throw new DatabaseError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,9 +405,50 @@ abstract class UserManager {
|
||||
protected function forceLogoutForUserById($userId) {
|
||||
$this->deleteRememberDirectiveForUserById($userId);
|
||||
$this->db->exec(
|
||||
'UPDATE ' . $this->dbTablePrefix . 'users SET force_logout = force_logout + 1 WHERE id = ?',
|
||||
'UPDATE ' . $this->makeTableName('users') . ' SET force_logout = force_logout + 1 WHERE id = ?',
|
||||
[ $userId ]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a (qualified) full table name from an optional qualifier, an optional prefix, and the table name itself
|
||||
*
|
||||
* The optional qualifier may be a database name or a schema name, for example
|
||||
*
|
||||
* @param string $name the name of the table
|
||||
* @return string[] the components of the (qualified) full name of the table
|
||||
*/
|
||||
protected function makeTableNameComponents($name) {
|
||||
$components = [];
|
||||
|
||||
if (!empty($this->dbSchema)) {
|
||||
$components[] = $this->dbSchema;
|
||||
}
|
||||
|
||||
if (!empty($name)) {
|
||||
if (!empty($this->dbTablePrefix)) {
|
||||
$components[] = $this->dbTablePrefix . $name;
|
||||
}
|
||||
else {
|
||||
$components[] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
return $components;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a (qualified) full table name from an optional qualifier, an optional prefix, and the table name itself
|
||||
*
|
||||
* The optional qualifier may be a database name or a schema name, for example
|
||||
*
|
||||
* @param string $name the name of the table
|
||||
* @return string the (qualified) full name of the table
|
||||
*/
|
||||
protected function makeTableName($name) {
|
||||
$components = $this->makeTableNameComponents($name);
|
||||
|
||||
return \implode('.', $components);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -254,9 +254,21 @@ function processRequestData(\Delight\Auth\Auth $auth) {
|
||||
}
|
||||
else if ($_POST['action'] === 'resetPassword') {
|
||||
try {
|
||||
$auth->resetPassword($_POST['selector'], $_POST['token'], $_POST['password']);
|
||||
if (isset($_POST['login']) && $_POST['login'] > 0) {
|
||||
if ($_POST['login'] == 2) {
|
||||
// keep logged in for one year
|
||||
$rememberDuration = (int) (60 * 60 * 24 * 365.25);
|
||||
}
|
||||
else {
|
||||
// do not keep logged in after session ends
|
||||
$rememberDuration = null;
|
||||
}
|
||||
|
||||
return 'ok';
|
||||
return $auth->resetPasswordAndSignIn($_POST['selector'], $_POST['token'], $_POST['password'], $rememberDuration);
|
||||
}
|
||||
else {
|
||||
return $auth->resetPassword($_POST['selector'], $_POST['token'], $_POST['password']);
|
||||
}
|
||||
}
|
||||
catch (\Delight\Auth\InvalidSelectorTokenPairException $e) {
|
||||
return 'invalid token';
|
||||
@@ -893,6 +905,11 @@ function showGuestUserForm() {
|
||||
echo '<input type="text" name="selector" placeholder="Selector" /> ';
|
||||
echo '<input type="text" name="token" placeholder="Token" /> ';
|
||||
echo '<input type="text" name="password" placeholder="New password" /> ';
|
||||
echo '<select name="login" size="1">';
|
||||
echo '<option value="0">Sign in automatically? — No</option>';
|
||||
echo '<option value="1">Sign in automatically? — Yes</option>';
|
||||
echo '<option value="2">Sign in automatically? — Yes (and remember)</option>';
|
||||
echo '</select> ';
|
||||
echo '<button type="submit">Reset password</button>';
|
||||
echo '</form>';
|
||||
|
||||
|
Reference in New Issue
Block a user