Skip to content

Commit

Permalink
Detach users on role/permission physical deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Mar 22, 2023
1 parent c7e0eb2 commit 25cb3a1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
13 changes: 7 additions & 6 deletions src/Traits/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@ public static function bootHasPermissions()
return;
}

if (is_a($model, Permission::class)) {
return;
}

$teams = app(PermissionRegistrar::class)->teams;
app(PermissionRegistrar::class)->teams = false;
$model->permissions()->detach();
if (! is_a($model, Permission::class)) {
$model->permissions()->detach();
}
if (is_a($model, Role::class)) {
$model->users()->detach();
}
app(PermissionRegistrar::class)->teams = $teams;
});
}

public function getPermissionClass()
{
if (! isset($this->permissionClass)) {
if (! $this->permissionClass) {
$this->permissionClass = app(PermissionRegistrar::class)->getPermissionClass();
}

Expand Down
5 changes: 4 additions & 1 deletion src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ public static function bootHasRoles()
$teams = app(PermissionRegistrar::class)->teams;
app(PermissionRegistrar::class)->teams = false;
$model->roles()->detach();
if (is_a($model, Permission::class)) {
$model->users()->detach();
}
app(PermissionRegistrar::class)->teams = $teams;
});
}

public function getRoleClass()
{
if (! isset($this->roleClass)) {
if (! $this->roleClass) {
$this->roleClass = app(PermissionRegistrar::class)->getRoleClass();
}

Expand Down
18 changes: 10 additions & 8 deletions tests/HasPermissionsWithCustomModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function it_doesnt_detach_roles_when_soft_deleting()

$permission = Permission::onlyTrashed()->find($this->testUserPermission->getKey());

$this->assertEquals(1, DB::table(config('permission.table_names.role_has_permissions'))->where('permission_test_id', $this->testUserPermission->getKey())->count());
$this->assertEquals(1, DB::table(config('permission.table_names.role_has_permissions'))->where('permission_test_id', $permission->getKey())->count());
}

/** @test */
Expand All @@ -92,25 +92,27 @@ public function it_doesnt_detach_users_when_soft_deleting()
$this->assertSame(1, count(DB::getQueryLog()));

$permission = Permission::onlyTrashed()->find($this->testUserPermission->getKey());
$permission->restore();

$this->assertEquals(1, DB::table(config('permission.table_names.model_has_permissions'))->where('permission_test_id', $this->testUserPermission->getKey())->count());
$this->assertEquals(1, DB::table(config('permission.table_names.model_has_permissions'))->where('permission_test_id', $permission->getKey())->count());
}

/** @test */
public function it_does_detach_roles_when_force_deleting()
public function it_does_detach_roles_and_users_when_force_deleting()
{
$this->testUserRole->givePermissionTo($this->testUserPermission);
$permission_id = $this->testUserPermission->getKey();
$this->testUserRole->givePermissionTo($permission_id);
$this->testUser->givePermissionTo($permission_id);

DB::enableQueryLog();
$this->testUserPermission->forceDelete();
DB::disableQueryLog();

$this->assertSame(2, count(DB::getQueryLog())); //avoid detach permissions on permissions
$this->assertSame(3, count(DB::getQueryLog())); //avoid detach permissions on permissions

$permission = Permission::withTrashed()->find($this->testUserPermission->getKey());
$permission = Permission::withTrashed()->find($permission_id);

$this->assertNull($permission);
$this->assertEquals(0, DB::table(config('permission.table_names.role_has_permissions'))->where('permission_test_id', $this->testUserPermission->getKey())->count());
$this->assertEquals(0, DB::table(config('permission.table_names.role_has_permissions'))->where('permission_test_id', $permission_id)->count());
$this->assertEquals(0, DB::table(config('permission.table_names.model_has_permissions'))->where('permission_test_id', $permission_id)->count());
}
}
17 changes: 10 additions & 7 deletions tests/HasRolesWithCustomModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function it_doesnt_detach_permissions_when_soft_deleting()

$role = Role::onlyTrashed()->find($this->testUserRole->getKey());

$this->assertEquals(1, DB::table(config('permission.table_names.role_has_permissions'))->where('role_test_id', $this->testUserRole->getKey())->count());
$this->assertEquals(1, DB::table(config('permission.table_names.role_has_permissions'))->where('role_test_id', $role->getKey())->count());
}

/** @test */
Expand All @@ -44,23 +44,26 @@ public function it_doesnt_detach_users_when_soft_deleting()

$role = Role::onlyTrashed()->find($this->testUserRole->getKey());

$this->assertEquals(1, DB::table(config('permission.table_names.model_has_roles'))->where('role_test_id', $this->testUserRole->getKey())->count());
$this->assertEquals(1, DB::table(config('permission.table_names.model_has_roles'))->where('role_test_id', $role->getKey())->count());
}

/** @test */
public function it_does_detach_permissions_when_force_deleting()
public function it_does_detach_permissions_and_users_when_force_deleting()
{
$this->testUserRole->givePermissionTo($this->testUserPermission);
$role_id = $this->testUserRole->getKey();
$this->testUserPermission->assignRole($role_id);
$this->testUser->assignRole($role_id);

DB::enableQueryLog();
$this->testUserRole->forceDelete();
DB::disableQueryLog();

$this->assertSame(2, count(DB::getQueryLog()));
$this->assertSame(3, count(DB::getQueryLog()));

$role = Role::withTrashed()->find($this->testUserRole->getKey());
$role = Role::withTrashed()->find($role_id);

$this->assertNull($role);
$this->assertEquals(0, DB::table(config('permission.table_names.role_has_permissions'))->where('role_test_id', $this->testUserRole->getKey())->count());
$this->assertEquals(0, DB::table(config('permission.table_names.role_has_permissions'))->where('role_test_id', $role_id)->count());
$this->assertEquals(0, DB::table(config('permission.table_names.model_has_roles'))->where('role_test_id', $role_id)->count());
}
}

0 comments on commit 25cb3a1

Please sign in to comment.