diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 663fbf5..ace4899 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -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 diff --git a/config/services.yaml b/config/services.yaml index ce1d138..1d754c9 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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' diff --git a/src/Command/InitCommand.php b/src/Command/InitCommand.php index 7663636..265d2cd 100644 --- a/src/Command/InitCommand.php +++ b/src/Command/InitCommand.php @@ -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; } } diff --git a/src/Controller/CompanyController.php b/src/Controller/CompanyController.php new file mode 100644 index 0000000..1e70ee8 --- /dev/null +++ b/src/Controller/CompanyController.php @@ -0,0 +1,18 @@ +render('company/index.html.twig', [ + 'controller_name' => 'CompanyController', + ]); + } +} diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php new file mode 100644 index 0000000..76bf5c4 --- /dev/null +++ b/src/Controller/SecurityController.php @@ -0,0 +1,32 @@ +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.'); + } +} diff --git a/src/Entity/Accounting.php b/src/Entity/Accounting.php index 098812d..09a810a 100644 --- a/src/Entity/Accounting.php +++ b/src/Entity/Accounting.php @@ -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; diff --git a/src/EventSubscriber/CompanySubscriber.php b/src/EventSubscriber/CompanySubscriber.php new file mode 100644 index 0000000..298ef08 --- /dev/null +++ b/src/EventSubscriber/CompanySubscriber.php @@ -0,0 +1,54 @@ +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(); + } + + } + +} \ No newline at end of file diff --git a/templates/company/index.html.twig b/templates/company/index.html.twig new file mode 100644 index 0000000..167b2c4 --- /dev/null +++ b/templates/company/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello CompanyController!{% endblock %} + +{% block body %} + + +
/home/afornerot/cadoles/nine-project/ninecompta/src/Controller/CompanyController.php
/home/afornerot/cadoles/nine-project/ninecompta/templates/company/index.html.twig