Skip to content

Commit 112df4a

Browse files
authored
Merge pull request #580 from lonnieezell/active-check
feat: User activation checks and utility functions.
2 parents 2b11f95 + 8ab2d6f commit 112df4a

24 files changed

Lines changed: 279 additions & 15 deletions

File tree

docs/authentication.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ user_id();
7070
## Authenticator Responses
7171

7272
Many of the authenticator methods will return a `CodeIgniter\Shield\Result` class. This provides a consistent
73-
way of checking the results and can have additional information return along with it. The class
73+
way of checking the results and can have additional information returned along with it. The class
7474
has the following methods:
7575

7676
### isOK()

docs/authorization.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
- [removeGroup()](#removegroup)
2121
- [syncGroups()](#syncgroups)
2222
- [getGroups()](#getgroups)
23+
- [User Activation](#user-activation)
24+
- [Checking Activation Status](#checking-activation-status)
25+
- [Activating a User](#activating-a-user)
26+
- [Deactivating a User](#deactivating-a-user)
2327

2428
Authorization happens once a user has been identified through authentication. It is the process of
2529
determining what actions a user is allowed to do within your site.
@@ -233,3 +237,43 @@ Returns all groups this user is a part of.
233237
```php
234238
$user->getGroups();
235239
```
240+
241+
## User Activation
242+
243+
All users have an `active` flag. This is only used when the [`EmailActivation` action](./auth_actions.md), or a custom action used to activate a user, is enabled.
244+
245+
### Checking Activation Status
246+
247+
You can determine if a user has been activated with the `isActivated()` method.
248+
249+
```php
250+
if ($user->isActivated()) {
251+
//
252+
}
253+
```
254+
255+
> **Note** If no activator is specified in the `Auth` config file, `actions['register']` property, then this will always return `true`.
256+
257+
You can check if a user has not been activated yet via the `isNotActivated()` method.
258+
259+
```php
260+
if ($user->isNotActivated()) {
261+
//
262+
}
263+
```
264+
265+
## Activating a User
266+
267+
Users are automatically activated withih the `EmailActivator` action. They can be manually activated via the `activate()` method on the User entity.
268+
269+
```php
270+
$user->activate();
271+
```
272+
273+
## Deactivating a User
274+
275+
Users can be manually deactivated via the `deactivate()` method on the User entity.
276+
277+
```php
278+
$user->deactivate();
279+
```

src/Auth.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use CodeIgniter\Shield\Models\UserModel;
1313

1414
/**
15-
* @method void activateUser(User $user) [Session]
1615
* @method Result attempt(array $credentials)
1716
* @method Result check(array $credentials)
1817
* @method bool checkAction(string $token, string $type) [Session]

src/Authentication/Actions/EmailActivator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function verify(IncomingRequest $request)
111111
$user = $authenticator->getUser();
112112

113113
// Set the user active now
114-
$authenticator->activateUser($user);
114+
$user->activate();
115115

116116
// Success!
117117
return redirect()->to(config('Auth')->registerRedirect())

src/Authentication/Authenticators/Session.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,6 @@ public function completeLogin(User $user): void
254254
Events::trigger('login', $user);
255255
}
256256

257-
/**
258-
* Activate a User
259-
*/
260-
public function activateUser(User $user): void
261-
{
262-
$this->provider->activate($user);
263-
}
264-
265257
/**
266258
* @param int|string|null $userId
267259
*/

src/Controllers/RegisterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function registerAction(): RedirectResponse
114114
}
115115

116116
// Set the user active
117-
$authenticator->activateUser($user);
117+
$user->activate();
118118

119119
$authenticator->completeLogin($user);
120120

src/Entities/User.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use CodeIgniter\Shield\Authorization\Traits\Authorizable;
1212
use CodeIgniter\Shield\Models\LoginModel;
1313
use CodeIgniter\Shield\Models\UserIdentityModel;
14+
use CodeIgniter\Shield\Traits\Activatable;
1415
use CodeIgniter\Shield\Traits\Resettable;
1516

1617
/**
@@ -27,6 +28,7 @@ class User extends Entity
2728
use Authorizable;
2829
use HasAccessTokens;
2930
use Resettable;
31+
use Activatable;
3032

3133
/**
3234
* @var UserIdentity[]|null

src/Filters/SessionAuth.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public function before(RequestInterface $request, $arguments = null)
4949
$authenticator->recordActiveDate();
5050
}
5151

52+
// Block inactive users when Email Activation is enabled
53+
$user = $authenticator->getUser();
54+
if ($user !== null && ! $user->isActivated()) {
55+
$authenticator->logout();
56+
57+
return redirect()->route('login')
58+
->with('error', lang('Auth.activationBlocked'));
59+
}
60+
5261
return;
5362
}
5463

src/Filters/TokenAuth.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,24 @@ public function before(RequestInterface $request, $arguments = null)
4949
]);
5050

5151
if (! $result->isOK() || (! empty($arguments) && $result->extraInfo()->tokenCant($arguments[0]))) {
52-
return redirect()->to('/login');
52+
return service('response')
53+
->setStatusCode(Response::HTTP_UNAUTHORIZED)
54+
->setJson(['message' => lang('Auth.badToken')]);
5355
}
5456

5557
if (setting('Auth.recordActiveDate')) {
5658
$authenticator->recordActiveDate();
5759
}
60+
61+
// Block inactive users when Email Activation is enabled
62+
$user = $authenticator->getUser();
63+
if ($user !== null && ! $user->isActivated()) {
64+
$authenticator->logout();
65+
66+
return service('response')
67+
->setStatusCode(Response::HTTP_FORBIDDEN)
68+
->setJson(['message' => lang('Auth.activationBlocked')]);
69+
}
5870
}
5971

6072
/**

src/Language/de/Auth.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
'emailActivateMailBody' => 'Bitte verwenden Sie den unten stehenden Code, um Ihr Konto zu aktivieren und die Website zu nutzen.',
8888
'invalidActivateToken' => 'Der Code war falsch.',
8989
'needActivate' => '(To be translated) You must complete your registration by confirming the code sent to your email address.',
90+
'activationBlocked' => '(to be translated) You must activate your account before logging in.',
9091

9192
// Groups
9293
'unknownGroup' => '{0} ist eine ungültige Gruppe.',

0 commit comments

Comments
 (0)