Ajout commande delete role
This commit is contained in:
parent
1a49265658
commit
3485bcc48f
14
README.MD
14
README.MD
|
|
@ -13,12 +13,11 @@
|
||||||
php bin/console doctrine:database:create
|
php bin/console doctrine:database:create
|
||||||
php bin/console doctrine:schema:update --force
|
php bin/console doctrine:schema:update --force
|
||||||
```
|
```
|
||||||
#### SQL
|
#### Roles
|
||||||
```bash
|
```bash
|
||||||
insert into public.roles (id, name, created_at)
|
php bin/console app:create-role USER
|
||||||
values (3, 'USER', '2025-05-21 13:22:52'),
|
php bin/console app:create-role ADMIN
|
||||||
(2, 'ADMIN', '2025-05-21 13:22:52'),
|
php bin/console app:create-role "SUPER ADMIN"
|
||||||
(1, 'SUPER ADMIN', '2025-05-21 13:22:52');
|
|
||||||
```
|
```
|
||||||
#### Choices.js
|
#### Choices.js
|
||||||
```bash
|
```bash
|
||||||
|
|
@ -31,4 +30,7 @@
|
||||||
- `uoId` pour `User Organization Id`
|
- `uoId` pour `User Organization Id`
|
||||||
- `oa` pour `Organization Application`
|
- `oa` pour `Organization Application`
|
||||||
- `at` pour `Access Token`
|
- `at` pour `Access Token`
|
||||||
|
- A delete command is available to delete roles
|
||||||
|
```bash
|
||||||
|
php bin/console app:delete-role ROLE_NAME
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Command;
|
||||||
|
|
||||||
|
use App\Entity\Roles;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||||
|
|
||||||
|
#[AsCommand(
|
||||||
|
name: 'app:delete-role',
|
||||||
|
description: 'Deletes a role from the database'
|
||||||
|
)]
|
||||||
|
class DeleteRoleCommand extends Command
|
||||||
|
{
|
||||||
|
private EntityManagerInterface $entityManager;
|
||||||
|
|
||||||
|
public function __construct(EntityManagerInterface $entityManager)
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->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('<error>The role name cannot be empty</error>');
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the role
|
||||||
|
$role = $this->entityManager->getRepository(Roles::class)
|
||||||
|
->findOneBy(['name' => $roleName]);
|
||||||
|
|
||||||
|
if (!$role) {
|
||||||
|
$output->writeln("<error>Role '{$roleName}' not found.</error>");
|
||||||
|
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("<error>Cannot delete role '{$roleName}' - it is assigned to {$usageCount} user(s).</error>");
|
||||||
|
$output->writeln('<comment>Remove all assignments first, then try again.</comment>');
|
||||||
|
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('<comment>Operation cancelled.</comment>');
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the role
|
||||||
|
$this->entityManager->remove($role);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
$output->writeln("<info>Role '{$roleName}' deleted successfully!</info>");
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue