Remove private projects when a user is deleted
This commit is contained in:
@@ -292,15 +292,29 @@ class User extends Base
|
|||||||
*/
|
*/
|
||||||
public function remove($user_id)
|
public function remove($user_id)
|
||||||
{
|
{
|
||||||
$this->db->startTransaction();
|
return $this->db->transaction(function ($db) use ($user_id) {
|
||||||
|
|
||||||
// All tasks assigned to this user will be unassigned
|
// All assigned tasks are now unassigned
|
||||||
$this->db->table(Task::TABLE)->eq('owner_id', $user_id)->update(array('owner_id' => 0));
|
if (! $db->table(Task::TABLE)->eq('owner_id', $user_id)->update(array('owner_id' => 0))) {
|
||||||
$result = $this->db->table(self::TABLE)->eq('id', $user_id)->remove();
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$this->db->closeTransaction();
|
// All private projects are removed
|
||||||
|
$project_ids = $db->table(Project::TABLE)
|
||||||
|
->eq('is_private', 1)
|
||||||
|
->eq(ProjectPermission::TABLE.'.user_id', $user_id)
|
||||||
|
->join(ProjectPermission::TABLE, 'project_id', 'id')
|
||||||
|
->findAllByColumn(Project::TABLE.'.id');
|
||||||
|
|
||||||
return $result;
|
if (! empty($project_ids)) {
|
||||||
|
$db->table(Project::TABLE)->in('id', $project_ids)->remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally remove the user
|
||||||
|
if (! $db->table(self::TABLE)->eq('id', $user_id)->remove()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -141,5 +141,20 @@ class UserTest extends Base
|
|||||||
$task = $tf->getById(1);
|
$task = $tf->getById(1);
|
||||||
$this->assertEquals(1, $task['id']);
|
$this->assertEquals(1, $task['id']);
|
||||||
$this->assertEquals(0, $task['owner_id']);
|
$this->assertEquals(0, $task['owner_id']);
|
||||||
|
|
||||||
|
// Make sure that private projects are also removed
|
||||||
|
$user_id1 = $u->create(array('username' => 'toto1', 'password' => '123456', 'name' => 'Toto'));
|
||||||
|
$user_id2 = $u->create(array('username' => 'toto2', 'password' => '123456', 'name' => 'Toto'));
|
||||||
|
$this->assertNotFalse($user_id1);
|
||||||
|
$this->assertNotFalse($user_id2);
|
||||||
|
$this->assertEquals(2, $p->create(array('name' => 'Private project #1', 'is_private' => 1), $user_id1, true));
|
||||||
|
$this->assertEquals(3, $p->create(array('name' => 'Private project #2', 'is_private' => 1), $user_id2, true));
|
||||||
|
|
||||||
|
$this->assertTrue($u->remove($user_id1));
|
||||||
|
|
||||||
|
$this->assertNotEmpty($p->getById(1));
|
||||||
|
$this->assertNotEmpty($p->getById(3));
|
||||||
|
|
||||||
|
$this->assertEmpty($p->getById(2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user