Skip to content

Commit

Permalink
Add security, fixtures and admin backend
Browse files Browse the repository at this point in the history
  • Loading branch information
tgalopin committed Mar 14, 2020
1 parent 027496b commit 5194e50
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.php_cs.cache
docker-compose.override.yaml

###> symfony/framework-bundle ###
/.env.local
Expand Down
10 changes: 4 additions & 6 deletions config/packages/easy_admin.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#easy_admin:
# entities:
# # List the entity class name you want to manage
# - App\Entity\Product
# - App\Entity\Category
# - App\Entity\User
easy_admin:
entities:
- App\Entity\Helper
- App\Entity\HelpRequest
30 changes: 17 additions & 13 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
security:
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
encoders:
App\Entity\Admin:
algorithm: auto

providers:
users_in_memory: { memory: null }
app_user_provider:
entity:
class: App\Entity\Admin
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false

main:
anonymous: lazy
provider: users_in_memory

# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#firewalls-authentication

# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
provider: app_user_provider
guard:
authenticators:
- App\Security\AdminAuthenticator
logout:
path: app_logout
target: homepage

# 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 }
- { path: ^/admin, roles: ROLE_ADMIN }
6 changes: 6 additions & 0 deletions docker-compose.override.yaml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'

services:
database:
ports:
- "5432:5432"
9 changes: 7 additions & 2 deletions src/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ class HomeController extends AbstractController
*/
public function index()
{
return $this->render('home/index.html.twig', [
'controller_name' => 'HomeController',
$response = $this->render('home/index.html.twig');
$response->setCache([
'public' => true,
'max_age' => 86400, // 1 day
's_maxage' => 86400, // 1 day
]);

return $response;
}
}
34 changes: 34 additions & 0 deletions src/Controller/SecurityController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends AbstractController
{
/**
* @Route("/login", name="security_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('easyadmin');
}

$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();

return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}

/**
* @Route("/logout", name="security_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}
110 changes: 107 additions & 3 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,120 @@

namespace App\DataFixtures;

use App\Entity\Admin;
use App\Entity\Helper;
use App\Entity\HelpRequest;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectManager;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class AppFixtures extends Fixture
{
private UserPasswordEncoderInterface $encoder;

public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}

public function load(ObjectManager $manager)
{
// $product = new Product();
// $manager->persist($product);
$this->loadAdmin($manager);
$this->loadHelpers($manager);
$this->loadHelpRequests($manager);

$manager->flush();
}

private function loadAdmin(ObjectManager $manager)
{
$admin = new Admin();
$admin->username = 'tgalopin';
$admin->setPassword($this->encoder->encodePassword($admin, 'password'));

$manager->persist($admin);
}

private function loadHelpers(ObjectManager $manager)
{
$helpers = [
[
'firstName' => 'Elizabeth',
'lastName' => 'Gregory',
'email' => 'elizabeth.gregory@example.com',
'zipCode' => '75008',
'haveChildren' => false,
'canBabysit' => true,
'babysitMaxChildren' => 1,
'babysitAgeRanges' => null,
'canBuyGroceries' => true,
],
];

foreach ($helpers as $data) {
$helper = new Helper();
$helper->firstName = $data['firstName'];
$helper->lastName = $data['lastName'];
$helper->email = $data['email'];
$helper->zipCode = $data['zipCode'];
$helper->haveChildren = $data['haveChildren'];
$helper->canBabysit = $data['canBabysit'];
$helper->babysitMaxChildren = $data['babysitMaxChildren'];
$helper->babysitAgeRanges = $data['babysitAgeRanges'];
$helper->canBuyGroceries = $data['canBuyGroceries'];

$manager->persist($helper);
}
}

private function loadHelpRequests(ObjectManager $manager)
{
$requests = [
[
'helpType' => HelpRequest::TYPE_BABYSIT,
'ownerUuid' => 'cd34e489-ec5a-4fb7-8fa5-e36554f1cd6c',
'firstName' => 'Jeanne',
'lastName' => 'Martin',
'email' => 'jeanne.martin@example.com',
'zipCode' => '75008',
'jobType' => 'health',
'childAgeRange' => HelpRequest::AGE_RANGE_35,
],
[
'helpType' => HelpRequest::TYPE_GROCERIES,
'ownerUuid' => 'cd34e489-ec5a-4fb7-8fa5-e36554f1cd6c',
'firstName' => 'Jeanne',
'lastName' => 'Martin',
'email' => 'jeanne.martin@example.com',
'zipCode' => '75008',
'jobType' => 'health',
'childAgeRange' => null,
],
[
'helpType' => HelpRequest::TYPE_GROCERIES,
'ownerUuid' => '4c4813df-ac99-4484-9cde-fdda1a7a910d',
'firstName' => 'Catherine',
'lastName' => 'Lambert',
'email' => 'catherine.lambert@example.com',
'zipCode' => '75010',
'jobType' => 'food',
'childAgeRange' => null,
],
];

foreach ($requests as $data) {
$request = new HelpRequest();
$request->helpType = $data['helpType'];
$request->ownerUuid = Uuid::fromString($data['ownerUuid']);
$request->firstName = $data['firstName'];
$request->lastName = $data['lastName'];
$request->email = $data['email'];
$request->zipCode = $data['zipCode'];
$request->jobType = $data['jobType'];
$request->childAgeRange = $data['childAgeRange'];

$manager->persist($request);
}
}
}
87 changes: 87 additions & 0 deletions src/Entity/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @ORM\Entity(repositoryClass="App\Repository\AdminRepository")
* @ORM\Table(name="admins")
*/
class Admin implements UserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="bigint", options={"unsigned": true})
*/
private ?int $id;

/**
* @ORM\Column(length=180, unique=true)
*/
public ?string $username;

/**
* @var string The hashed password
*
* @ORM\Column()
*/
private ?string $password;

public function __toString()
{
return $this->username;
}

public function getId(): ?int
{
return $this->id;
}

/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->username;
}

/**
* @see UserInterface
*/
public function getRoles(): array
{
return ['ROLE_USER', 'ROLE_ADMIN'];
}

/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}

public function setPassword(string $password)
{
$this->password = $password;
}

/**
* @see UserInterface
*/
public function getSalt()
{
}

/**
* @see UserInterface
*/
public function eraseCredentials()
{
}
}
Loading

0 comments on commit 5194e50

Please sign in to comment.