This commit is contained in:
afornerot 2024-11-20 17:09:09 +01:00
parent d8882c93b9
commit ce7f5a1b4a
9 changed files with 275 additions and 57 deletions

View File

@ -1,43 +1,51 @@
security:
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: "auto"
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
form_login:
login_path: app_login
check_path: app_login
enable_csrf: true
logout:
path: app_logout
# where to redirect after logout
# target: app_any_route
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
when@test:
security:
password_hashers:
# By default, password hashers are resource intensive and take time. This is
# important to generate secure password hashes. In tests however, secure hashes
# are not important, waste resources and increase test times. The following
# reduces the work factor to the lowest possible values.
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
algorithm: auto
cost: 4 # Lowest possible value for bcrypt
time_cost: 3 # Lowest possible value for argon
memory_cost: 10 # Lowest possible value for argon
security:
password_hashers:
# By default, password hashers are resource intensive and take time. This is
# important to generate secure password hashes. In tests however, secure hashes
# are not important, waste resources and increase test times. The following
# reduces the work factor to the lowest possible values.
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
algorithm: auto
cost: 4 # Lowest possible value for bcrypt
time_cost: 3 # Lowest possible value for argon
memory_cost: 10 # Lowest possible value for argon

View File

@ -23,5 +23,10 @@ services:
- "../src/Entity/"
- "../src/Kernel.php"
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\EventSubscriber\CompanySubscriber:
arguments:
$em: "@doctrine.orm.entity_manager"
tags:
- name: "doctrine.event_listener"
event: "postPersist"
entity: 'App\Entity\Company'

View File

@ -2,6 +2,9 @@
namespace App\Command;
use App\Entity\Company;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
@ -9,41 +12,63 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
#[AsCommand(
name: 'app:init',
description: 'Add a short description for your command',
description: 'Initialisation of the app',
)]
class InitCommand extends Command
{
public function __construct()
{
parent::__construct();
}
private EntityManagerInterface $em;
private ParameterBagInterface $params;
private UserPasswordHasherInterface $passwordHasher;
protected function configure(): void
public function __construct(EntityManagerInterface $em,ParameterBagInterface $params,UserPasswordHasherInterface $passwordHasher)
{
$this
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
;
$this->em = $em;
$this->params = $params;
$this->passwordHasher = $passwordHasher;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$arg1 = $input->getArgument('arg1');
$io->title("APP:INIT");
$io->text("Initialisation of the app");
$io->text("");
// Création du compte admin
$io->text("> Création du compte admin");
$user = $this->em->getRepository("App\Entity\User")->findOneBy(["username"=>"admin"]);
if(!$user) {
$user=new User;
if ($arg1) {
$io->note(sprintf('You passed an argument: %s', $arg1));
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$this->params->get("appSecret")
);
$user->setUsername("admin");
$user->setPassword($hashedPassword);
$this->em->persist($user);
$this->em->flush();
}
if ($input->getOption('option1')) {
// ...
// Création d'un company par defaut
$io->text("> Création d'un company par defaut");
$nbcompanys = $this->em->getRepository("App\Entity\Company")->count([]);
if($nbcompanys==0) {
$company=new Company;
$company->setTitle($this->params->get("appName"));
$company->setLogo("logo.png");
$this->em->persist($company);
$this->em->flush();
}
$io->success('You have a new command! Now make it your own! Pass --help to see your options.');
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class CompanyController extends AbstractController
{
#[Route('/admin/company', name: 'app_company')]
public function index(): Response
{
return $this->render('company/index.html.twig', [
'controller_name' => 'CompanyController',
]);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}

View File

@ -24,6 +24,9 @@ class Accounting
#[ORM\Column(length: 255)]
private ?string $title = null;
#[ORM\Column(length: 255)]
private ?string $icon = null;
#[ORM\Column]
private ?bool $actif = null;
@ -90,6 +93,18 @@ class Accounting
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(string $icon): static
{
$this->icon = $icon;
return $this;
}
public function isActif(): ?bool
{
return $this->actif;

View File

@ -0,0 +1,54 @@
<?php
namespace App\EventSubscriber;
use App\Entity\Accounting;
use App\Entity\Company;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Events;
class CompanySubscriber
{
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function postPersist(PostPersistEventArgs $args): void
{
$company = $args->getObject();
if (!$company instanceof Company) {
return;
}
$entityManager = $args->getObjectManager();
// Génération des accounting par défaut
$this->insertAccounting($company,"512","000","Banque",true);
$this->insertAccounting($company,"530","000","Caisse",true);
$this->insertAccounting($company,"600","000","Charge",false);
$this->insertAccounting($company,"700","000","Produit",false);
// Génération du year par
}
private function insertAccounting(Company $company,string $num01, string $num02, string $title, bool $isactif): void
{
$accounting=$this->em->getRepository("App\Entity\Accounting")->findOneBy(["company"=>$company,"num01"=>$num01,"num02"=>$num02]);
if(!$accounting) {
$accounting = new Accounting;
$accounting->setCompany($company);
$accounting->setNum01($num01);
$accounting->setNum02($num02);
$accounting->setTitle($title);
$accounting->SetActif($isactif);
$this->em->persist($accounting);
$this->em->flush();
}
}
}

View File

@ -0,0 +1,20 @@
{% extends 'base.html.twig' %}
{% block title %}Hello CompanyController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code>/home/afornerot/cadoles/nine-project/ninecompta/src/Controller/CompanyController.php</code></li>
<li>Your template at <code>/home/afornerot/cadoles/nine-project/ninecompta/templates/company/index.html.twig</code></li>
</ul>
</div>
{% endblock %}

View File

@ -0,0 +1,41 @@
{% extends 'base.html.twig' %}
{% block title %}Log in!{% endblock %}
{% block body %}
<form method="post">
{% if error %}
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
{% if app.user %}
<div class="mb-3">
You are logged in as {{ app.user.userIdentifier }}, <a href="{{ path('app_logout') }}">Logout</a>
</div>
{% endif %}
<h1 class="h3 mb-3 font-weight-normal">Please sign in</h1>
<label for="username">Username</label>
<input type="text" value="{{ last_username }}" name="_username" id="username" class="form-control" autocomplete="username" required autofocus>
<label for="password">Password</label>
<input type="password" name="_password" id="password" class="form-control" autocomplete="current-password" required>
<input type="hidden" name="_csrf_token"
value="{{ csrf_token('authenticate') }}"
>
{#
Uncomment this section and add a remember_me option below your firewall to activate remember me functionality.
See https://symfony.com/doc/current/security/remember_me.html
<div class="checkbox mb-3">
<input type="checkbox" name="_remember_me" id="_remember_me">
<label for="_remember_me">Remember me</label>
</div>
#}
<button class="btn btn-lg btn-primary" type="submit">
Sign in
</button>
</form>
{% endblock %}