Skip to content
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

Do not remove user's role and abilities when softDeleting #491

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Database/Concerns/HasAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ trait HasAbilities
public static function bootHasAbilities()
{
static::deleted(function ($model) {
if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) {
return;
}

$model->abilities()->detach();
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Concerns/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ trait HasRoles
public static function bootHasRoles()
{
static::deleted(function ($model) {
if (method_exists($model, 'isForceDeleting') && ! $model->isForceDeleting()) {
return;
}

$model->roles()->detach();
});
}
Expand Down
7 changes: 7 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;

abstract class BaseTestCase extends TestCase
{
Expand Down Expand Up @@ -99,6 +100,7 @@ protected function migrateTestTables()
$table->string('name')->nullable();
$table->integer('age')->nullable();
$table->timestamps();
$table->softDeletes();
});

Schema::create('accounts', function ($table) {
Expand Down Expand Up @@ -211,6 +213,11 @@ class User extends Eloquent
protected $guarded = [];
}

class UserWithSoftDeletes extends User
{
use SoftDeletes;
}

class Account extends Eloquent
{
use HasRolesAndAbilities;
Expand Down
44 changes: 44 additions & 0 deletions tests/HasRolesAndAbilitiesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Silber\Bouncer\Tests;

use Silber\Bouncer\Database\Models;

class HasRolesAndAbilitiesTraitTest extends BaseTestCase
{
use Concerns\TestsClipboards;
Expand Down Expand Up @@ -239,6 +241,27 @@ function deleting_a_model_deletes_the_permissions_pivot_table_records()
$this->assertEquals(1, $this->db()->table('permissions')->count());
}

/**
* @test
*/
function soft_deleting_a_model_persists_the_permissions_pivot_table_records()
{
Models::setUsersModel(UserWithSoftDeletes::class);
$bouncer = $this->bouncer();

$user1 = UserWithSoftDeletes::create();
$user2 = UserWithSoftDeletes::create();

$bouncer->allow($user1)->everything();
$bouncer->allow($user2)->everything();

$this->assertEquals(2, $this->db()->table('permissions')->count());

$user1->delete();

$this->assertEquals(2, $this->db()->table('permissions')->count());
}

/**
* @test
*/
Expand All @@ -258,4 +281,25 @@ function deleting_a_model_deletes_the_assigned_roles_pivot_table_records()

$this->assertEquals(1, $this->db()->table('assigned_roles')->count());
}

/**
* @test
*/
function soft_deleting_a_model_persists_the_assigned_roles_pivot_table_records()
{
Models::setUsersModel(UserWithSoftDeletes::class);
$bouncer = $this->bouncer();

$user1 = UserWithSoftDeletes::create();
$user2 = UserWithSoftDeletes::create();

$bouncer->assign('admin')->to($user1);
$bouncer->assign('admin')->to($user2);

$this->assertEquals(2, $this->db()->table('assigned_roles')->count());

$user1->delete();

$this->assertEquals(2, $this->db()->table('assigned_roles')->count());
}
}