Creation organizations

This commit is contained in:
Charles 2025-09-03 10:03:55 +02:00
parent febd2ad6b2
commit 6964bc0214
5 changed files with 109 additions and 38 deletions

View File

@ -32,6 +32,9 @@ services:
App\Service\UserService: App\Service\UserService:
arguments: arguments:
$profileDirectory: '%profile_directory%' $profileDirectory: '%profile_directory%'
App\Service\OrganizationsService:
arguments:
$logoDirectory: '%logos_directory%'
App\EventSubscriber\ScopeResolveListener: App\EventSubscriber\ScopeResolveListener:
tags: tags:
- { name: kernel.event_listener, event: league.oauth2_server.event.scope_resolve, method: onScopeResolve } - { name: kernel.event_listener, event: league.oauth2_server.event.scope_resolve, method: onScopeResolve }

View File

@ -13,12 +13,12 @@ use App\Service\OrganizationsService;
use App\Service\UserOrganizationService; use App\Service\UserOrganizationService;
use App\Service\UserService; use App\Service\UserService;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use App\Entity\Organizations; use App\Entity\Organizations;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Service\Attribute\Required;
#[Route(path: '/organization', name: 'organization_')] #[Route(path: '/organization', name: 'organization_')]
class OrganizationController extends AbstractController class OrganizationController extends AbstractController
@ -26,8 +26,10 @@ class OrganizationController extends AbstractController
private const NOT_FOUND = 'Entity not found'; private const NOT_FOUND = 'Entity not found';
private const ACCESS_DENIED = 'Access denied'; private const ACCESS_DENIED = 'Access denied';
public function __construct(private readonly EntityManagerInterface $entityManager, public function __construct(private readonly EntityManagerInterface $entityManager,
private readonly UserService $userService) private readonly UserService $userService,
private readonly OrganizationsService $organizationsService, private readonly ActionService $actionService)
{ {
} }
@ -36,14 +38,14 @@ class OrganizationController extends AbstractController
{ {
$this->denyAccessUnlessGranted('ROLE_ADMIN'); $this->denyAccessUnlessGranted('ROLE_ADMIN');
$user = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier()); $user = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
if ($this->isGranted("ROLE_SUPER_ADMIN")){ if ($this->isGranted("ROLE_SUPER_ADMIN")) {
$organizations = $this->entityManager->getRepository(Organizations::class)->findAll(); $organizations = $this->entityManager->getRepository(Organizations::class)->findAll();
} else { } else {
//get all the UO //get all the UO of the user
$uos = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user]); $uos = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user]);
$organizations = []; $organizations = [];
foreach ($uos as $uo) {
$roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'ADMIN']); $roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'ADMIN']);
foreach ($uos as $uo) {
$uoaAdmin = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo, 'role' => $roleAdmin]); $uoaAdmin = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo, 'role' => $roleAdmin]);
if ($uoaAdmin) { if ($uoaAdmin) {
$organizations[] = $uo->getOrganization(); $organizations[] = $uo->getOrganization();
@ -57,5 +59,39 @@ class OrganizationController extends AbstractController
]); ]);
} }
#[Route(path: '/new', name: 'new', methods: ['GET', 'POST'])]
public function new(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
if ($request->isMethod('POST')) {
$organization = new Organizations();
$form = $this->createForm(OrganizationForm::class, $organization);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$logoFile = $form->get('logoUrl')->getData();
if ($logoFile) {
$this->organizationService->handleLogo($organization, $logoFile);
}
try{
$this->entityManager->persist($organization);
$this->entityManager->flush();
$this->actionService->createAction("Create Organization", $actingUser, $organization, $organization->getName());
return $this->redirectToRoute('organization_index');
}catch (Exception $e){
$this->addFlash('error', 'Error creating organization: ' . $e->getMessage());
}
}
return $this->render('organization/new.html.twig', [
'form' => $form->createView(),
]);
}
$form = $this->createForm(OrganizationForm::class);
return $this->render('organization/new.html.twig', [
'form' => $form->createView(),
]);
}
} }

View File

@ -7,11 +7,42 @@ use App\Entity\Organizations;
use App\Entity\Roles; use App\Entity\Roles;
use App\Entity\UsersOrganizations; use App\Entity\UsersOrganizations;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use SebastianBergmann\CodeCoverage\Util\DirectoryCouldNotBeCreatedException;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
class OrganizationsService class OrganizationsService
{ {
public function __construct(private readonly EntityManagerInterface $entityManager) private string $logoDirectory;
public function __construct(private readonly EntityManagerInterface $entityManager,
string $logoDirectory
)
{ {
$this->logoDirectory = $logoDirectory;
}
public function handleLogo(Organizations $organization, $logoFile)
{
$extension = $logoFile->guessExtension();
$customFilename = $organization->getName() . '_'. date('dmyHis') . "." . $extension;
$uploadDirectory = $this->logoDirectory;
if (!is_dir($uploadDirectory) && !mkdir($uploadDirectory, 0755, true) && !is_dir($uploadDirectory)) {
throw new DirectoryCouldNotBeCreatedException(sprintf('Directory "%s" was not created', $uploadDirectory));
}
try {
// Move the file to the upload directory
$logoFile->move($uploadDirectory, $customFilename);
// Update user entity with the file path (relative to public directory)
$organization->setLogoUrl('uploads/logos/' . $customFilename);
} catch (FileException $e) {
// Handle upload error
throw new FileException('File upload failed: ' . $e->getMessage());
}
} }
} }

View File

@ -238,10 +238,10 @@ class UserService
return ['none' => $group]; return ['none' => $group];
} }
public function handleProfilePicture(User $user, $logoFile): void public function handleProfilePicture(User $user, $picture): void
{ {
// Get file extension // Get file extension
$extension = $logoFile->guessExtension(); $extension = $picture->guessExtension();
// Create custom filename: userNameUserSurname_ddmmyyhhmmss // Create custom filename: userNameUserSurname_ddmmyyhhmmss
$customFilename = $user->getName() . $user->getSurname() . '_' . date('dmyHis') . '.' . $extension; $customFilename = $user->getName() . $user->getSurname() . '_' . date('dmyHis') . '.' . $extension;
@ -255,7 +255,7 @@ class UserService
try { try {
// Move the file to the upload directory // Move the file to the upload directory
$logoFile->move($uploadDirectory, $customFilename); $picture->move($uploadDirectory, $customFilename);
// Update user entity with the file path (relative to public directory) // Update user entity with the file path (relative to public directory)
$user->setPictureUrl('uploads/profile/' . $customFilename); $user->setPictureUrl('uploads/profile/' . $customFilename);

View File

@ -7,14 +7,14 @@
<div class="d-flex justify-content-between align-items-center mb-4"> <div class="d-flex justify-content-between align-items-center mb-4">
<h1>Gestion des organisations</h1> <h1>Gestion des organisations</h1>
{% if is_granted("ROLE_SUPER_ADMIN") %} {% if is_granted("ROLE_SUPER_ADMIN") %}
{# <a href="{{ path('organization_new') }}" class="btn btn-primary">Ajouter une organisation</a>#} <a href="{{ path('organization_new') }}" class="btn btn-primary">Ajouter une organisation</a>
{% endif %} {% endif %}
</div> </div>
{% if organizations|length == 0 %} {% if organizations|length == 0 %}
<tr> <tr>
<td colspan="4" class="text-center">Aucune organisation trouvée.</td> <td colspan="4" class="text-center">Aucune organisation trouvée.</td>
<td colspan="4" class="text-center"> <td colspan="4" class="text-center">
{# <a href="{{ path('organization_new') }}" class="btn btn-primary">Créer une organisation</a>#} <a href="{{ path('organization_new') }}" class="btn btn-primary">Créer une organisation</a>
</td> </td>
</tr> </tr>
{% else %} {% else %}
@ -32,15 +32,16 @@
<tr> <tr>
<td> <td>
{% if organization.logoUrl %} {% if organization.logoUrl %}
<img src="{{ asset('uploads/logos/' ~ organization.logoUrl) }}" alt="Organization logo" class="rounded-circle" style="width:40px; height:40px;"> <img src="{{ asset(organization.logoUrl) }}" alt="Organization logo"
class="rounded-circle" style="width:40px; height:40px;">
{% endif %} {% endif %}
</td> </td>
<td>{{ organization.name }}</td> <td>{{ organization.name }}</td>
<td>{{ organization.email }}</td> <td>{{ organization.email }}</td>
<td> <td>
{# <a href="{{ path('organization_show', {'id': organization.id}) }}" class="p-3 align-middle color-primary">#} {# <a href="{{ path('organization_show', {'id': organization.id}) }}" class="p-3 align-middle color-primary"> #}
{# {{ ux_icon('fa6-regular:eye', {height: '30px', width: '30px'}) }}#} {# {{ ux_icon('fa6-regular:eye', {height: '30px', width: '30px'}) }} #}
{# </a>#} {# </a> #}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}