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:
arguments:
$profileDirectory: '%profile_directory%'
App\Service\OrganizationsService:
arguments:
$logoDirectory: '%logos_directory%'
App\EventSubscriber\ScopeResolveListener:
tags:
- { 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\UserService;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use App\Entity\Organizations;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Service\Attribute\Required;
#[Route(path: '/organization', name: 'organization_')]
class OrganizationController extends AbstractController
@ -26,8 +26,10 @@ class OrganizationController extends AbstractController
private const NOT_FOUND = 'Entity not found';
private const ACCESS_DENIED = 'Access denied';
public function __construct(private readonly EntityManagerInterface $entityManager,
private readonly UserService $userService)
public function __construct(private readonly EntityManagerInterface $entityManager,
private readonly UserService $userService,
private readonly OrganizationsService $organizationsService, private readonly ActionService $actionService)
{
}
@ -36,14 +38,14 @@ class OrganizationController extends AbstractController
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$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();
} else {
//get all the UO
//get all the UO of the user
$uos = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user]);
$organizations = [];
$roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'ADMIN']);
foreach ($uos as $uo) {
$roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'ADMIN']);
$uoaAdmin = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo, 'role' => $roleAdmin]);
if ($uoaAdmin) {
$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\UsersOrganizations;
use Doctrine\ORM\EntityManagerInterface;
use SebastianBergmann\CodeCoverage\Util\DirectoryCouldNotBeCreatedException;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
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];
}
public function handleProfilePicture(User $user, $logoFile): void
public function handleProfilePicture(User $user, $picture): void
{
// Get file extension
$extension = $logoFile->guessExtension();
$extension = $picture->guessExtension();
// Create custom filename: userNameUserSurname_ddmmyyhhmmss
$customFilename = $user->getName() . $user->getSurname() . '_' . date('dmyHis') . '.' . $extension;
@ -255,7 +255,7 @@ class UserService
try {
// 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)
$user->setPictureUrl('uploads/profile/' . $customFilename);

View File

@ -7,45 +7,46 @@
<div class="d-flex justify-content-between align-items-center mb-4">
<h1>Gestion des organisations</h1>
{% 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 %}
</div>
{% if organizations|length == 0 %}
<tr>
<td colspan="4" class="text-center">Aucune organisation trouvée.</td>
<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>
</tr>
{% else %}
<table class="table align-middle shadow">
<thead class="table-light shadow-sm">
<thead class="table-light shadow-sm">
<tr>
<th>Logo</th>
<th>Nom</th>
<th>Email</th>
<th>Visualiser</th>
</tr>
</thead>
<tbody>
{% for organization in organizations %}
<tr>
<th>Logo</th>
<th>Nom</th>
<th>Email</th>
<th>Visualiser</th>
<td>
{% if organization.logoUrl %}
<img src="{{ asset(organization.logoUrl) }}" alt="Organization logo"
class="rounded-circle" style="width:40px; height:40px;">
{% endif %}
</td>
<td>{{ organization.name }}</td>
<td>{{ organization.email }}</td>
<td>
{# <a href="{{ path('organization_show', {'id': organization.id}) }}" class="p-3 align-middle color-primary"> #}
{# {{ ux_icon('fa6-regular:eye', {height: '30px', width: '30px'}) }} #}
{# </a> #}
</td>
</tr>
</thead>
<tbody>
{% for organization in organizations %}
<tr>
<td>
{% if organization.logoUrl %}
<img src="{{ asset('uploads/logos/' ~ organization.logoUrl) }}" alt="Organization logo" class="rounded-circle" style="width:40px; height:40px;">
{% endif %}
</td>
<td>{{ organization.name }}</td>
<td>{{ organization.email }}</td>
<td>
{# <a href="{{ path('organization_show', {'id': organization.id}) }}" class="p-3 align-middle color-primary">#}
{# {{ ux_icon('fa6-regular:eye', {height: '30px', width: '30px'}) }}#}
{# </a>#}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>