diff --git a/README.MD b/README.MD index 069ded8..da57222 100644 --- a/README.MD +++ b/README.MD @@ -13,12 +13,11 @@ php bin/console doctrine:database:create php bin/console doctrine:schema:update --force ``` -#### SQL +#### Roles ```bash - insert into public.roles (id, name, created_at) - values (3, 'USER', '2025-05-21 13:22:52'), - (2, 'ADMIN', '2025-05-21 13:22:52'), - (1, 'SUPER ADMIN', '2025-05-21 13:22:52'); + php bin/console app:create-role USER + php bin/console app:create-role ADMIN + php bin/console app:create-role "SUPER ADMIN" ``` #### Choices.js ```bash @@ -31,4 +30,7 @@ - `uoId` pour `User Organization Id` - `oa` pour `Organization Application` - `at` pour `Access Token` - +- A delete command is available to delete roles +```bash + php bin/console app:delete-role ROLE_NAME +``` diff --git a/src/Command/DeleteRoleCommand.php b/src/Command/DeleteRoleCommand.php new file mode 100644 index 0000000..de29992 --- /dev/null +++ b/src/Command/DeleteRoleCommand.php @@ -0,0 +1,83 @@ +entityManager = $entityManager; + } + + protected function configure(): void + { + $this + ->addArgument('name', InputArgument::REQUIRED, 'The name of the role to delete'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $roleName = trim($input->getArgument('name')); + $roleName = strtoupper($roleName); // Normalize to uppercase + + if ($roleName === '') { + $output->writeln('The role name cannot be empty'); + return Command::FAILURE; + } + + // Find the role + $role = $this->entityManager->getRepository(Roles::class) + ->findOneBy(['name' => $roleName]); + + if (!$role) { + $output->writeln("Role '{$roleName}' not found."); + return Command::FAILURE; + } + + // Check if role is being used (optional safety check) + $usageCount = $this->entityManager->getRepository(\App\Entity\UserOrganizatonApp::class) + ->count(['role' => $role]); + + if ($usageCount > 0) { + $output->writeln("Cannot delete role '{$roleName}' - it is assigned to {$usageCount} user(s)."); + $output->writeln('Remove all assignments first, then try again.'); + return Command::FAILURE; + } + + // Confirmation prompt + $helper = $this->getHelper('question'); + $question = new ConfirmationQuestion( + "Are you sure you want to delete role '{$roleName}'? [y/N] ", + false + ); + + if (!$helper->ask($input, $output, $question)) { + $output->writeln('Operation cancelled.'); + return Command::SUCCESS; + } + + // Delete the role + $this->entityManager->remove($role); + $this->entityManager->flush(); + + $output->writeln("Role '{$roleName}' deleted successfully!"); + + return Command::SUCCESS; + } +} \ No newline at end of file