-
Notifications
You must be signed in to change notification settings - Fork 138
Feature: Banning Users #650
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
a2db120
Added migration file
davidnsai 0293227
Added new feilds to the UserModel
davidnsai 9886eb4
Added methods to modify the ban status of a user
davidnsai cae9014
Added exception for banned users
davidnsai 1af7fd3
Added banned user checks on access tokens
davidnsai 2bb6a4f
Added banned user checks on attempting to log in
davidnsai d40e090
Added language translations for banned user
davidnsai ddfed19
Fixed coding standard fail
davidnsai 00654ed
cs fix
davidnsai b8625de
Added user model to list of imported classes
davidnsai c9963dd
Removed extra whitespace
davidnsai d6c2425
ran composer cs-fix
davidnsai 5cddb94
Added a bannable trait
davidnsai 28206d1
Completed banning logic
davidnsai 1bc78de
Added docs for banning feature
davidnsai 0e27d3b
Fixed failing unit test
davidnsai 7088052
Update docs/banning_users.md
davidnsai ad2984f
Update src/Filters/SessionAuth.php
davidnsai 70ab02f
Update src/Language/fa/Auth.php
davidnsai f256b69
Update src/Language/ja/Auth.php
davidnsai a186793
Merge branch 'codeigniter4:develop' into develop
davidnsai 8591f67
Merge branch 'codeigniter4:develop' into develop
davidnsai 5fdc155
Removed migration adding banned and banned_message fields
davidnsai 040f17a
Removed banned fields from user model
davidnsai 9d54ac5
Implemented ban and unban logic
davidnsai 6371431
FIxed docs on banning users
davidnsai df48197
removed unneccessary (bool)
davidnsai 206fe9a
Reverted redirect route for when a user is not activated
davidnsai c00b84e
Added test for getBanMessage()
davidnsai ed74635
Removed unnecesary whitespaces from the docs
davidnsai 88bb918
Update docs/banning_users.md
davidnsai 524a6c9
Update docs/banning_users.md
davidnsai fb1e851
Update docs/banning_users.md
davidnsai 7134871
Update docs/banning_users.md
davidnsai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Banning Users | ||
|
||
Shield provides a way to ban users from your application. This is useful if you need to prevent a user from logging in, or logging them out in the event that they breach your terms of service. | ||
|
||
- [Checking if the User is Banned](#check-if-a-user-is-banned) | ||
- [Banning a User](#banning-a-user) | ||
- [Unbanning a User](#unbanning-a-user) | ||
- [Getting the Reason for Ban ](#getting-the-reason-for-ban) | ||
|
||
### Check if a User is Banned | ||
|
||
You can check if a user is banned using `isBanned()` method on the `User` entity. The method returns a boolean `true`/`false`. | ||
|
||
```php | ||
if ($user->isBanned()) { | ||
//... | ||
} | ||
``` | ||
|
||
### Banning a User | ||
|
||
To ban a user from the application, the `ban(?string $message = null)` method can be called on the `User` entity. The method takes an optional string as a parameter. The string acts as the reason for the ban. | ||
|
||
```php | ||
// banning a user without passing a message | ||
$user->ban(); | ||
// banning a user with a message and reason for the ban passed. | ||
$user->ban('Your reason for banning the user here'); | ||
``` | ||
|
||
### Unbanning a User | ||
|
||
Unbanning a user can be done using the `unBan()` method on the `User` entity. This method will also reset the `status_message` property. | ||
|
||
```php | ||
$user->unBan(); | ||
``` | ||
|
||
### Getting the Reason for Ban | ||
|
||
The reason for the ban can be obtained user the `getBanMessage()` method on the `User` entity. | ||
|
||
```php | ||
$user->getBanMessage(); | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeIgniter\Shield\Traits; | ||
|
||
trait Bannable | ||
{ | ||
/** | ||
* Is the user banned? | ||
*/ | ||
public function isBanned(): bool | ||
{ | ||
return $this->status && $this->status === 'banned'; | ||
} | ||
|
||
/** | ||
* Ban the user from logging in. | ||
* | ||
* @return $this | ||
*/ | ||
public function ban(?string $message = null): self | ||
{ | ||
$this->status = 'banned'; | ||
$this->status_message = $message; | ||
|
||
$users = auth()->getProvider(); | ||
|
||
$users->save($this); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Unban the user and allow them to login | ||
* | ||
* @return $this | ||
*/ | ||
public function unBan(): self | ||
{ | ||
$this->status = null; | ||
$this->status_message = null; | ||
|
||
$users = auth()->getProvider(); | ||
|
||
$users->save($this); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the ban message. | ||
*/ | ||
public function getBanMessage(): ?string | ||
davidnsai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return $this->status_message; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -326,4 +326,33 @@ public function testCreatedAtIfDefaultLocaleSetFaWithAddGroup(): void | |
Locale::setDefault($currentLocale); | ||
Time::setTestNow(); | ||
} | ||
|
||
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 test doesn't cover fully or at least 80%, however I don't mind. |
||
public function testBanningUser(): void | ||
{ | ||
$this->assertFalse($this->user->isBanned()); | ||
|
||
$this->user->ban(); | ||
|
||
$this->assertTrue($this->user->isBanned()); | ||
} | ||
|
||
public function testUnbanningUser(): void | ||
{ | ||
$this->user->ban(); | ||
|
||
$this->assertTrue($this->user->isBanned()); | ||
|
||
$this->user->unBan(); | ||
|
||
$this->assertFalse($this->user->isBanned()); | ||
} | ||
|
||
public function testGetBanMessage(): void | ||
{ | ||
$this->assertNull($this->user->getBanMessage()); | ||
|
||
$this->user->ban('You are banned'); | ||
|
||
$this->assertSame('You are banned', $this->user->getBanMessage()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.