Skip to content

Commit

Permalink
Merge pull request #3 from BAD-WOLF/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
BAD-WOLF authored Aug 13, 2023
2 parents 07a1a26 + cfc38dc commit 220f365
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 14 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/symfony.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ jobs:
restore-keys: |
${{ runner.os }}-php-
- name: Install Dependencies
run: |
composer update
composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Create Database
run: |
mkdir -p data
Expand Down
10 changes: 7 additions & 3 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ security:

# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used

role_hierarchy:
ROLE_ADMIN: ["ROLE_NORMAL_USER"]

access_control:
- { path: ^/(new|delete), roles: ROLE_ADMIN }
- { path: ^/(edit|register/admin), roles: ROLE_NORMAL_USER }
- { path: ^/(home|show), roles: PUBLIC_ACCESS }
- { path: ^/(edit|verify/email/admin), roles: ROLE_NORMAL_USER }
- { path: ^/(new|delete), roles: ROLE_ADMIN }
- { path: ^/(home|show), roles: PUBLIC_ACCESS }

when@test:
security:
Expand Down
71 changes: 67 additions & 4 deletions src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,32 @@
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;

class RegistrationController extends AbstractController
{
private EmailVerifier $emailVerifier;

/**
* Summary of __construct
* @param \App\Security\EmailVerifier $emailVerifier
*/
public function __construct(EmailVerifier $emailVerifier)
{
$this->emailVerifier = $emailVerifier;
}

#[Route('/register/{role<user|admin>}', name: 'app_register')]

/**
* Summary of register
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface $userPasswordHasher
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
$user = new User();
Expand All @@ -43,7 +56,7 @@ public function register(Request $request, UserPasswordHasherInterface $userPass
)
);

$user->setRoles(["ROLE_USERR"]);
$user->setRoles(["ROLE_NORMAL_USER"]);

$entityManager->persist($user);
$entityManager->flush();
Expand All @@ -66,9 +79,24 @@ public function register(Request $request, UserPasswordHasherInterface $userPass
]);
}

#[Route('/verify/email', name: 'app_verify_email')]

/**
* Summary of verifyUserEmail
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator
* @param \App\Repository\UserRepository $userRepository
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route('/verify/email/', name: 'app_verify_email')]
#[Route('/verify/email/admin', name: 'app_verify_email_admin')]
public function verifyUserEmail(Request $request, TranslatorInterface $translator, UserRepository $userRepository): Response
{
$rote_name = $request->attributes->get("_route");
if ($rote_name == "app_verify_email_admin") {
$role = "admin";
} else {
$role = null;
}
$id = $request->query->get('id');

if (null === $id) {
Expand All @@ -83,7 +111,7 @@ public function verifyUserEmail(Request $request, TranslatorInterface $translato

// validate email confirmation link, sets User::isVerified=true and persists
try {
$this->emailVerifier->handleEmailConfirmation($request, $user);
$this->emailVerifier->handleEmailConfirmation($request, $user, $role);
} catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));

Expand All @@ -96,6 +124,13 @@ public function verifyUserEmail(Request $request, TranslatorInterface $translato
return $this->redirectToRoute('app_login');
}


/**
* Summary of reverify
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \App\Repository\UserRepository $userRepository
* @return Response|\Symfony\Component\HttpFoundation\RedirectResponse
*/
#[Route("/send-reverify", "app_reverify")]
public function reverify(Request $request, UserRepository $userRepository) {
if ($this->getUser()) {
Expand Down Expand Up @@ -126,4 +161,32 @@ public function reverify(Request $request, UserRepository $userRepository) {
"reverifyForm" => $form
]);
}


/**
* Summary of ChargeToAdmin
* @param \Symfony\Component\Security\Core\User\UserInterface $user
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
* @return void
*/
#[Route(path: "/charge-to-admin", name: "app_charge_to_admin")]
public function ChargeToAdmin(UserInterface $user, EntityManagerInterface $entityManager): Response
{

if (!$user instanceof User) {
dd("barriu boy, + deu error!!");
return new Response("success");
}

$this->emailVerifier->sendEmailConfirmation(
"app_verify_email_admin",
$user,
(new TemplatedEmail())
->from(new Address("matheusviaira160@gmail.com"))
->to("matheusviaira160@gmail.com")
->subject("Matheus Vieira")
->htmlTemplate('registration/confirmation_email.html.twig')
);
return new Response("success");
}
}
37 changes: 35 additions & 2 deletions src/Security/EmailVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Security;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
Expand All @@ -12,15 +14,32 @@

class EmailVerifier
{
/**
* Summary of __construct
* @param \SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface $verifyEmailHelper
* @param \Symfony\Component\Mailer\MailerInterface $mailer
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
*/
public function __construct(
private VerifyEmailHelperInterface $verifyEmailHelper,
private MailerInterface $mailer,
private EntityManagerInterface $entityManager
) {
}

/**
* Summary of sendEmailConfirmation
* @param string $verifyEmailRouteName
* @param \Symfony\Component\Security\Core\User\UserInterface $user
* @param \Symfony\Bridge\Twig\Mime\TemplatedEmail $email
* @return void
*/
public function sendEmailConfirmation(string $verifyEmailRouteName, UserInterface $user, TemplatedEmail $email): void
{
if (!$user instanceof User) {
return;
}

$signatureComponents = $this->verifyEmailHelper->generateSignature(
$verifyEmailRouteName,
$user->getId(),
Expand All @@ -39,13 +58,27 @@ public function sendEmailConfirmation(string $verifyEmailRouteName, UserInterfac
}

/**
* Summary of handleEmailConfirmation
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\Security\Core\User\UserInterface $user
* @throws VerifyEmailExceptionInterface
* @param string|null $role
* @return void
*/
public function handleEmailConfirmation(Request $request, UserInterface $user): void
public function handleEmailConfirmation(Request $request, UserInterface $user, string|null $role = null): void
{
if (!$user instanceof User) {
return;
}

$this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());

$user->setIsVerified(true);

if ($role === null) {
$user->setIsVerified(true);
}else if($role === "admin"){
$user->setRoles(["ROLE_ADMIN"]);
}

$this->entityManager->persist($user);
$this->entityManager->flush();
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8">
{% block metas %}
<meta property="og:image" content="https://c9a5-2804-7f7-a089-afde-3735-7b0c-e6e2-d038.ngrok-free.app/build/images/quadro-poster-paisagem-04-detalhes-abaixo-para-imprimir.jpg"/>
<meta property="og:image" content="https://8026-2804-7f7-a089-afde-9765-f23f-4d2c-1881.ngrok-free.app/build/images/quadro-poster-paisagem-04-detalhes-abaixo-para-imprimir.jpg"/>
<meta property="og:description" content="Stock Only Test: só de testa ja esta me ajudando"/>
<meta property="og:url" content="https://www.stock.com"/>
{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion templates/home/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<a href={{ path('app_home_new') }}><button class="btn btn-outline-success-">Create new</button></a>
{% elseif app.user.roles.0 == "ROLE_NORMAL_USER" %}
you can become admin for more privileges: </br>
<a href={{ path("app_register", {'role':'admin'}) }}><button class="btn btn-outline-success">Go to admin regiser</button></a>
<a href={{ path("app_charge_to_admin") }}><button class="btn btn-outline-success">Go to admin regiser</button></a>
{% endif %}
{% endif %}

Expand Down

0 comments on commit 220f365

Please sign in to comment.