remove roles from users when role is deleted (#1640)

This commit is contained in:
Kevin Papst
2020-04-15 15:44:28 +02:00
committed by GitHub
parent 57254bf175
commit 4254c2590a
3 changed files with 54 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ use App\Form\RoleType;
use App\Model\PermissionSection;
use App\Repository\RolePermissionRepository;
use App\Repository\RoleRepository;
use App\Repository\UserRepository;
use App\Security\RolePermissionManager;
use App\Security\RoleService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
@@ -195,9 +196,16 @@ final class PermissionController extends AbstractController
* @Route(path="/roles/{id}/delete", name="admin_user_role_delete", methods={"GET", "POST"})
* @Security("is_granted('role_permissions')")
*/
public function deleteRole(Role $role): Response
public function deleteRole(Role $role, UserRepository $userRepository): Response
{
try {
// workaround, as roles is still a string array on users table
// until this is fixed, the users must be manually updated
$users = $userRepository->findUsersWithRole($role->getName());
foreach ($users as $user) {
$user->removeRole($role->getName());
$userRepository->saveUser($user);
}
$this->roleRepository->deleteRole($role);
$this->flashSuccess('action.delete.success');
} catch (\Exception $ex) {