-
Notifications
You must be signed in to change notification settings - Fork 143
feat: User activation checks and utility functions. #580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d37cf14
7d3bc7a
d1b3afc
62b60a9
8ab2d6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,12 +49,24 @@ public function before(RequestInterface $request, $arguments = null) | |
| ]); | ||
|
|
||
| if (! $result->isOK() || (! empty($arguments) && $result->extraInfo()->tokenCant($arguments[0]))) { | ||
| return redirect()->to('/login'); | ||
| return service('response') | ||
| ->setStatusCode(Response::HTTP_UNAUTHORIZED) | ||
| ->setJson(['message' => lang('Auth.badToken')]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change. It is better to document it. #433 suggests 401, not 403. Which is better?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for 401. For example, I already using it in few deployments with own api-filter. I would like to switch to this one if it's will be suitable for me.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In summary, a 401 Unauthorized response should be used for missing or bad authentication, and a 403 Forbidden response should be used afterwards, when the user is authenticated but isn’t authorized to perform the requested operation on the given resource. @lonnieezell Here we are mixing authentication and authorization. Maybe we need to first check if a client provided a valid token and if not, return 401 and after that, check, scopes and if there is no valid scope, return 403.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These make sense. First check will result in a 401, with the not-activated check being a 403. |
||
| } | ||
|
|
||
| if (setting('Auth.recordActiveDate')) { | ||
| $authenticator->recordActiveDate(); | ||
| } | ||
|
|
||
| // Block inactive users when Email Activation is enabled | ||
| $user = $authenticator->getUser(); | ||
| if ($user !== null && ! $user->isActivated()) { | ||
| $authenticator->logout(); | ||
|
|
||
| return service('response') | ||
| ->setStatusCode(Response::HTTP_FORBIDDEN) | ||
| ->setJson(['message' => lang('Auth.activationBlocked')]); | ||
datamweb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CodeIgniter\Shield\Traits; | ||
|
|
||
| trait Activatable | ||
| { | ||
| /** | ||
| * Returns true if the user has been activated | ||
| * and activation is required after registration. | ||
| */ | ||
| public function isActivated(): bool | ||
| { | ||
| // If activation is not required, then we're always active. | ||
| return ! $this->shouldActivate() || $this->active; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the user has not been activated. | ||
| */ | ||
| public function isNotActivated(): bool | ||
| { | ||
| return ! $this->isActivated(); | ||
| } | ||
|
|
||
| /** | ||
| * Activates the user. | ||
| */ | ||
| public function activate(): bool | ||
datamweb marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change the return type to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless it's changing in a PR,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I sent a PR #626 |
||
| { | ||
| $model = auth()->getProvider(); | ||
|
|
||
| return $model->update($this->id, ['active' => 1]); | ||
| } | ||
|
|
||
| /** | ||
| * Deactivates the user. | ||
| */ | ||
| public function deactivate(): bool | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same above. |
||
| { | ||
| $model = auth()->getProvider(); | ||
|
|
||
| return $model->update($this->id, ['active' => 0]); | ||
| } | ||
|
|
||
| /** | ||
| * Does the Auth actions require activation? | ||
| * Check for the generic 'Activator' class name to allow | ||
| * for custom implementations, provided they follow the naming convention. | ||
| */ | ||
| private function shouldActivate(): bool | ||
| { | ||
| return strpos(setting('Auth.actions')['register'] ?? '', 'Activator') !== false; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.