-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add user trash restore #4
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
10 commits
Select commit
Hold shift + click to select a range
c0a81a0
feat: added login tracking and login count features to User model
sparkybug abe1425
feat: added user trash & restore feature
sparkybug 1bb7d26
fix: made code fixes to the corrections provided for login tracking
sparkybug 2014141
refactor: modified testcases to explicitly check for permission & add…
sparkybug 3241d64
fix: rewrote the user trash restore tests to use filament policies
sparkybug 0572818
Merge branch 'main' into add-user-trash-restore
sparkybug b4adfe9
fix: refactored and fixed all issues regarding
sparkybug 2d5d575
fix: rewrote the user trash restore tests to use filament policies
sparkybug 23fb178
Merge branch 'add-user-trash-restore' of https://github.com/sparkybug…
sparkybug 66c6de6
fix: made all fixes to user trash restore
sparkybug 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
21 changes: 21 additions & 0 deletions
21
database/migrations/2025_03_15_000001_add_soft_deletes_to_users.php
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,21 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration { | ||
| public function up() | ||
| { | ||
| Schema::table('users', function (Blueprint $table) { | ||
| $table->softDeletes(); // ✅ Adds `deleted_at` column | ||
| }); | ||
| } | ||
|
|
||
| public function down() | ||
| { | ||
| Schema::table('users', function (Blueprint $table) { | ||
| $table->dropSoftDeletes(); | ||
| }); | ||
| } | ||
| }; |
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,134 @@ | ||
| <?php | ||
|
|
||
| use Eclipse\Core\Models\User; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Facades\Auth; | ||
| use Illuminate\Support\Facades\Gate; | ||
| use Illuminate\Auth\Access\AuthorizationException; | ||
|
|
||
| uses(RefreshDatabase::class); | ||
|
|
||
| beforeEach(function () { | ||
| $this->set_up_super_admin_and_tenant(); | ||
| }); | ||
|
|
||
| test('authorized user with permission can trash another user', function () { | ||
| $user = User::factory()->create(); | ||
| Auth::login($this->superAdmin); | ||
| $this->assertTrue($this->superAdmin->hasPermissionTo('delete_user')); | ||
| $this->assertTrue($this->superAdmin->can('delete', $user)); | ||
| $user->delete(); | ||
| $this->assertTrue($user->fresh()->trashed()); | ||
| }); | ||
|
|
||
| test('non-authorized user cannot trash another user', function () { | ||
| $user = User::factory()->create(); | ||
| $targetUser = User::factory()->create(); | ||
| Auth::login($user); | ||
| $this->assertFalse($user->hasPermissionTo('delete_user')); | ||
| $this->assertFalse($user->can('delete', $targetUser)); | ||
| $this->expectException(AuthorizationException::class); | ||
| Gate::authorize('delete', $targetUser); | ||
| }); | ||
|
|
||
| test('user cannot trash himself', function () { | ||
| Auth::login($this->superAdmin); | ||
| $this->assertFalse($this->superAdmin->can('delete', $this->superAdmin)); | ||
| try { | ||
| Gate::authorize('delete', $this->superAdmin); | ||
| $this->fail('User was able to authorize self-deletion, which should not be allowed'); | ||
| } catch (AuthorizationException $e) { | ||
| $this->assertTrue(true); | ||
| } | ||
| $this->assertFalse($this->superAdmin->fresh()->trashed()); | ||
| }); | ||
|
|
||
| test('authorized user with restore permission can restore a trashed user', function () { | ||
| $user = User::factory()->create(); | ||
| $user->delete(); | ||
| Auth::login($this->superAdmin); | ||
| $this->assertTrue($this->superAdmin->hasPermissionTo('restore_user')); | ||
| $this->assertTrue($this->superAdmin->can('restore', $user)); | ||
| $user->restore(); | ||
| $this->assertFalse($user->fresh()->trashed()); | ||
| }); | ||
|
|
||
| test('authorized user with restore_any permission can restore any trashed user', function () { | ||
| $userToTrash = User::factory()->create(); | ||
| $userToTrash->delete(); | ||
| $limitedAdmin = User::factory()->create(); | ||
| $limitedAdmin->givePermissionTo('restore_any_user'); | ||
| Auth::login($limitedAdmin); | ||
| $this->assertTrue($limitedAdmin->hasPermissionTo('restore_any_user')); | ||
| $this->assertTrue($limitedAdmin->can('restoreAny', User::class)); | ||
| $userToTrash->restore(); | ||
| $this->assertFalse($userToTrash->fresh()->trashed()); | ||
| }); | ||
|
|
||
| test('non-authorized user cannot restore another user', function () { | ||
| $userToTrash = User::factory()->create(); | ||
| $userToTrash->delete(); | ||
| $nonAuthorizedUser = User::factory()->create(); | ||
| Auth::login($nonAuthorizedUser); | ||
| $this->assertFalse($nonAuthorizedUser->hasPermissionTo('restore_user')); | ||
| $this->assertFalse($nonAuthorizedUser->can('restore', $userToTrash)); | ||
| $this->expectException(AuthorizationException::class); | ||
| Gate::authorize('restore', $userToTrash); | ||
| }); | ||
|
|
||
| test('trashed user cannot login', function () { | ||
| $userToTrash = User::factory()->create([ | ||
| 'email' => 'trashed@example.com', | ||
| 'password' => bcrypt('password') | ||
| ]); | ||
| $userToTrash->delete(); | ||
| Auth::logout(); | ||
| $attempt = Auth::attempt([ | ||
| 'email' => 'trashed@example.com', | ||
| 'password' => 'password' | ||
| ]); | ||
| $this->assertFalse($attempt); | ||
| }); | ||
|
|
||
| test('authorized user with permission can force delete a trashed user', function () { | ||
| $user = User::factory()->create(); | ||
| $user->delete(); | ||
| Auth::login($this->superAdmin); | ||
| $this->assertTrue($this->superAdmin->hasPermissionTo('force_delete_user')); | ||
| $this->assertTrue($this->superAdmin->can('forceDelete', $user)); | ||
| $user->forceDelete(); | ||
| $this->assertNull(User::withTrashed()->find($user->id)); | ||
| }); | ||
|
|
||
| test('non-authorized user cannot force delete a trashed user', function () { | ||
| $userToTrash = User::factory()->create(); | ||
| $userToTrash->delete(); | ||
| $nonAuthorizedUser = User::factory()->create(); | ||
| Auth::login($nonAuthorizedUser); | ||
| $this->assertFalse($nonAuthorizedUser->hasPermissionTo('force_delete_user')); | ||
| $this->assertFalse($nonAuthorizedUser->can('forceDelete', $userToTrash)); | ||
| $this->expectException(AuthorizationException::class); | ||
| Gate::authorize('forceDelete', $userToTrash); | ||
| }); | ||
|
|
||
| test('can view trashed users when user has permissions', function () { | ||
| $trashedUser = User::factory()->create(); | ||
| $trashedUser->delete(); | ||
| Auth::login($this->superAdmin); | ||
| $this->assertTrue($this->superAdmin->hasPermissionTo('view_any_user')); | ||
| $this->assertTrue($this->superAdmin->hasPermissionTo('view_user')); | ||
| $this->assertTrue($this->superAdmin->can('viewAny', User::class)); | ||
| $this->assertTrue($this->superAdmin->can('view', $trashedUser)); | ||
| }); | ||
|
|
||
| test('filament resource can handle trashed users', function () { | ||
| $userToTrash = User::factory()->create([ | ||
| 'name' => 'Trashed User', | ||
| 'email' => 'trashed@example.com' | ||
| ]); | ||
| $userToTrash->delete(); | ||
| Auth::login($this->superAdmin); | ||
| $this->assertTrue($this->superAdmin->can('viewAny', User::class)); | ||
| $this->assertNotNull(User::withTrashed()->where('email', 'trashed@example.com')->first()); | ||
| $this->assertTrue(User::withTrashed()->where('email', 'trashed@example.com')->first()->trashed()); | ||
| }); |
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.