gist/src/Gist/Controller/MyController.php

138 lines
4 KiB
PHP
Raw Normal View History

2015-11-21 18:28:48 +01:00
<?php
namespace Gist\Controller;
use Symfony\Component\HttpFoundation\Request;
use Gist\Form\DeleteGistForm;
use Gist\Form\FilterGistForm;
2016-12-23 10:28:09 +01:00
use Gist\Form\UserPasswordForm;
use Symfony\Component\HttpFoundation\RedirectResponse;
2018-08-20 17:05:36 +02:00
use Symfony\Component\HttpFoundation\Response;
2015-11-21 18:28:48 +01:00
/**
2016-11-13 00:44:23 +01:00
* Class MyController.
*
2015-11-21 18:28:48 +01:00
* @author Simon Vieille <simon@deblan.fr>
*/
class MyController extends Controller
{
2016-11-13 00:44:23 +01:00
/**
* "My" page.
*
* @param Request $request
* @param int $page
*
2018-08-20 17:05:36 +02:00
* @return Response
2016-11-13 00:44:23 +01:00
*/
public function myAction(Request $request, $page)
2015-11-21 18:28:48 +01:00
{
2016-11-13 00:44:23 +01:00
$page = (int) $page;
$app = $this->getApp();
$deleteForm = new DeleteGistForm($app['form.factory'], $app['translator']);
$deleteForm = $deleteForm->build()->getForm();
2016-11-13 00:44:23 +01:00
$options = array(
2016-11-13 00:44:23 +01:00
'type' => 'all',
'cipher' => 'anyway',
);
$filterForm = new FilterGistForm(
$app['form.factory'],
$app['translator'],
$options,
['csrf_protection' => false]
);
$filterForm = $filterForm->build()->getForm();
2016-12-23 10:28:09 +01:00
$passwordForm = new UserPasswordForm($app['form.factory'], $app['translator']);
$passwordForm = $passwordForm->build()->getForm();
if ($request->query->has('filter')) {
$filterForm->submit($request);
if ($filterForm->isValid()) {
$options = $filterForm->getData();
}
}
2016-11-13 00:44:23 +01:00
$gists = $this->getUser()->getGistsPager($page, $options);
2017-06-25 19:13:27 +02:00
$apiKey = $this->getUser()->getApiKey();
if (empty($apiKey)) {
$regenerateApiKey = true;
2018-09-11 13:15:12 +02:00
}
// FIXME: CSRF issue!
2017-06-25 23:06:24 +02:00
elseif ($request->request->get('apiKey') === $apiKey && $request->request->has('generateApiKey')) {
2017-06-25 19:13:27 +02:00
$regenerateApiKey = true;
} else {
$regenerateApiKey = false;
}
if ($regenerateApiKey) {
$apiKey = $app['salt_generator']->generate(32, true);
$this->getUser()
->setApiKey($apiKey)
->save();
}
if ($request->isMethod('post')) {
2016-12-23 10:28:09 +01:00
$deleteForm->handleRequest($request);
$passwordForm->handleRequest($request);
2016-12-23 10:28:09 +01:00
if ($deleteForm->isSubmitted() && $deleteForm->isValid()) {
$id = (int) $deleteForm->getData()['id'];
foreach ($gists as $gist) {
if ($gist->getId() === $id) {
$gist->delete();
$deleted = true;
$gists = $this->getUser()->getGistsPager($page, $options);
}
}
}
2016-12-23 10:28:09 +01:00
if ($passwordForm->isSubmitted() && $passwordForm->isValid()) {
$currentPassword = $passwordForm->getData()['currentPassword'];
$newPassword = $passwordForm->getData()['newPassword'];
$passwordUpdated = 0;
if ($app['user.provider']->isCurrentUserPassword($this->getUser(), $currentPassword)) {
$app['user.provider']->updateUserPassword(
$this->getUser(),
$newPassword
);
$passwordUpdated = 1;
}
return new RedirectResponse(
$app['url_generator']->generate(
'my',
[
'passwordUpdated' => $passwordUpdated,
]
)
);
}
}
2018-08-21 09:56:51 +02:00
return $this->createResponse(
'My/my.html.twig',
array(
2016-11-13 00:44:23 +01:00
'gists' => $gists,
'page' => $page,
2017-06-25 19:13:27 +02:00
'apiKey' => $apiKey,
2016-11-13 00:44:23 +01:00
'deleteForm' => $deleteForm->createView(),
'filterForm' => $filterForm->createView(),
2016-12-23 10:28:09 +01:00
'passwordForm' => $passwordForm->createView(),
2016-11-13 00:44:23 +01:00
'deleted' => !empty($deleted),
'no_cache' => true,
)
);
2015-11-21 18:28:48 +01:00
}
}