Skip to content

Commit

Permalink
token regenration button
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelbadawi committed Jul 1, 2021
1 parent 999000a commit 7ff7955
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Controller/QuackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function toggleLike(EntityManagerInterface $entityManager, Request $reque
return $this->redirectToRoute('quacks');
}

$duck = $entityManager->getRepository(Duck::class)->findOneBy(['id' => $this->getUser()->getId()]);
$duck = $this->getUser();
if (in_array($quack, [...$duck->getLikes()])) {
$duck->removeLike($quack);
} else {
Expand Down Expand Up @@ -176,7 +176,7 @@ public function create(EntityManagerInterface $entityManager, ValidatorInterface
}

$quack = new Quack();
$duck = $entityManager->getRepository(Duck::class)->findOneBy(['id' => $this->getUser()->getId()]);
$duck = $this->getUser();
$quack = $this->updateQuackFields($validator, $urlHelper, $quack, $request->get('content'), $duck);
$newFileName = $this->handleFileUpload($request, $slugger);
$tags = $this->handleTags($request->get('tags'));
Expand Down
27 changes: 25 additions & 2 deletions src/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace App\Controller;

use App\Entity\Duck;
use App\Entity\ApiToken;
use Symfony\Component\Mime\Email;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
Expand Down Expand Up @@ -51,8 +54,7 @@ public function profile(EntityManagerInterface $entityManager, ValidatorInterfac
$this->denyAccessUnlessGranted('ROLE_USER');

if ($request->getMethod() === 'POST') {
$user = $this->getUser();
$duck = $entityManager->getRepository(Duck::class)->findOneBy(['id' => $user->getId()]);
$duck = $this->getUser();
$duck->setFirstname(!empty($request->get('first_name')) ? $request->get('first_name') : $duck->getFirstname());
$duck->setLastName(!empty($request->get('last_name')) ? $request->get('last_name') : $duck->getLastName());
$duck->setDuckName(!empty($request->get('duck_name')) ? $request->get('duck_name') : $duck->getDuckName());
Expand Down Expand Up @@ -125,4 +127,25 @@ public function logout()
{
return $this->redirectToRoute('quacks');
}

#[Route('/send_token', name: 'send_token')]
public function sendToken(EntityManagerInterface $entityManager, MailerInterface $mailer): Response
{
$user = $this->getUser();
foreach ($user->getApiTokens() as $token) {
if ($token->isExpired()) {
$entityManager->remove($token);
}
}

$token = new ApiToken($user);
$email = (new Email())
->from('[email protected]')
->to('[email protected]')
->subject('Your Duck Tales API token!')
->html('<p>Your new API token is ' . $token->getToken() . '</p>');

$mailer->send($email);
return $this->redirectToRoute('quacks');
}
}
2 changes: 1 addition & 1 deletion src/Entity/Duck.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Duck implements UserInterface, PasswordAuthenticatedUserInterface
private $likes;

/**
* @ORM\OneToMany(targetEntity=ApiToken::class, mappedBy="duck", orphanRemoval=true)
* @ORM\OneToMany(targetEntity=ApiToken::class, mappedBy="duck", orphanRemoval=true, cascade={"persist"})
*/
private $apiTokens;

Expand Down
21 changes: 21 additions & 0 deletions templates/security/profile.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,26 @@
" type="submit">
Change my Pedigree
</button>
<a class="
mt-2
mx-auto
w-1/4
border-transparent border-4
bg-green-700
text-white
text-center
hover:bg-green-500
text-xs
font-bold
py-1
px-2
rounded
transition
transform
ease-in
"
href="{{ path("send_token") }}">
Generate a new API token
</a>
</form>
{% endblock %}

0 comments on commit 7ff7955

Please sign in to comment.