From e595782966676c6ef6d02b455fd4255827915701 Mon Sep 17 00:00:00 2001
From: Julian PEREZ-RAMIREZ <julian.perez-ramirez@imt-atlantique.net>
Date: Tue, 25 Feb 2025 23:01:04 +0100
Subject: [PATCH] editing fixtures files to created sample records

---
 README.md                             |  1 +
 web_app/composer.json                 |  2 ++
 web_app/config/bundles.php            |  1 +
 web_app/config/packages/security.yaml | 39 +++++++++++++++++++++++++++
 web_app/config/routes/security.yaml   |  3 +++
 web_app/src/Entity/User.php           | 20 +++++++++++++-
 web_app/symfony.lock                  | 13 +++++++++
 7 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 web_app/config/packages/security.yaml
 create mode 100644 web_app/config/routes/security.yaml

diff --git a/README.md b/README.md
index aee171b..6220432 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,7 @@ php bin/console doctrine:migrations:migrate prev
 
 To add sample data per dafault to the database (after finished editing DataFixtures/ files)
 Note: Add the `--append` flag if you don't want to erase the records already created.
+Also add `--group` to apply and specific file.
 ```
 php bin/console doctrine:fixtures:load
 ```
\ No newline at end of file
diff --git a/web_app/composer.json b/web_app/composer.json
index 8c26bcb..8fc8bf2 100644
--- a/web_app/composer.json
+++ b/web_app/composer.json
@@ -16,6 +16,8 @@
         "symfony/flex": "^2",
         "symfony/framework-bundle": "7.2.*",
         "symfony/runtime": "7.2.*",
+        "symfony/security-bundle": "7.2.*",
+        "symfony/security-core": "7.2.*",
         "symfony/yaml": "7.2.*"
     },
     "config": {
diff --git a/web_app/config/bundles.php b/web_app/config/bundles.php
index fd50f83..d1c745a 100644
--- a/web_app/config/bundles.php
+++ b/web_app/config/bundles.php
@@ -6,4 +6,5 @@ return [
     Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
     Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
     Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
+    Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
 ];
diff --git a/web_app/config/packages/security.yaml b/web_app/config/packages/security.yaml
new file mode 100644
index 0000000..367af25
--- /dev/null
+++ b/web_app/config/packages/security.yaml
@@ -0,0 +1,39 @@
+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:
+        users_in_memory: { memory: null }
+    firewalls:
+        dev:
+            pattern: ^/(_(profiler|wdt)|css|images|js)/
+            security: false
+        main:
+            lazy: true
+            provider: users_in_memory
+
+            # 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
+
+    # 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
diff --git a/web_app/config/routes/security.yaml b/web_app/config/routes/security.yaml
new file mode 100644
index 0000000..f853be1
--- /dev/null
+++ b/web_app/config/routes/security.yaml
@@ -0,0 +1,3 @@
+_security_logout:
+    resource: security.route_loader.logout
+    type: service
diff --git a/web_app/src/Entity/User.php b/web_app/src/Entity/User.php
index dd3739d..b986c66 100644
--- a/web_app/src/Entity/User.php
+++ b/web_app/src/Entity/User.php
@@ -5,9 +5,11 @@ namespace App\Entity;
 use App\Enum\UserRole;
 use App\Repository\UserRepository;
 use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Security\Core\User\UserInterface;
+use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
 
 #[ORM\Entity(repositoryClass: UserRepository::class)]
-class User
+class User implements UserInterface, PasswordAuthenticatedUserInterface
 {
     #[ORM\Id]
     #[ORM\GeneratedValue]
@@ -138,4 +140,20 @@ class User
 
         return $this;
     }
+
+    //Methods neeeded to be implemented here because of the extension of UserInterface, PasswordAuthenticatedUserInterface
+    public function getRoles(): array
+    {
+        return [$this->role->value]; // Assuming UserRole is an enum with string values
+    }
+
+    public function getUserIdentifier(): string
+    {
+        return $this->email; // Or another unique identifier like username
+    }
+
+    public function eraseCredentials(): void
+    {
+        // If storing temporary sensitive data, clear it here
+    } 
 }
diff --git a/web_app/symfony.lock b/web_app/symfony.lock
index 2886e0a..65f77fc 100644
--- a/web_app/symfony.lock
+++ b/web_app/symfony.lock
@@ -103,5 +103,18 @@
             "config/packages/routing.yaml",
             "config/routes.yaml"
         ]
+    },
+    "symfony/security-bundle": {
+        "version": "7.2",
+        "recipe": {
+            "repo": "github.com/symfony/recipes",
+            "branch": "main",
+            "version": "6.4",
+            "ref": "2ae08430db28c8eb4476605894296c82a642028f"
+        },
+        "files": [
+            "config/packages/security.yaml",
+            "config/routes/security.yaml"
+        ]
     }
 }
-- 
GitLab