[EXCEPTION][UI][UX] Add RedirectException, which can be thrown anywhere to redirect somewhere, and an exception handler

This commit is contained in:
Hugo Sales 2020-09-06 21:38:37 +00:00 committed by Hugo Sales
parent 420b4767b2
commit b906dde059
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 74 additions and 21 deletions

View File

@ -110,27 +110,25 @@ END;
public function replies(Request $request) public function replies(Request $request)
{ {
$actor_id = Common::ensureLoggedIn()->getActor()->getId(); $actor_id = Common::ensureLoggedIn()->getId();
return [ return [
'_template' => 'network/public.html.twig', '_template' => 'network/public.html.twig',
'notes' => DB::dql('select n from App\Entity\Note n ' . 'notes' => DB::dql('select n from App\Entity\Note n ' .
'where n.reply_to is not null and n.gsactor_id = ' . $actor_id . 'where n.reply_to is not null and n.gsactor_id = :id ' .
'order by n.created DESC' 'order by n.created DESC', ['id' => $gsactor_id]),
),
]; ];
} }
public function favourites(Request $request) public function favourites(Request $request)
{ {
$actor_id = Common::ensureLoggedIn()->getActor()->getId(); $actor_id = Common::ensureLoggedIn()->getId();
return [ return [
'_template' => 'network/public.html.twig', '_template' => 'network/public.html.twig',
'notes' => DB::dql('select f from App\Entity\Favourite f ' . 'notes' => DB::dql('select f from App\Entity\Favourite f ' .
'where f.gsactor_id = ' . $actor_id . 'where f.gsactor_id = :id ' .
'order by f.created DESC' 'order by f.created DESC', ['id' => $gsactor_id]),
),
]; ];
} }
} }

View File

@ -82,14 +82,16 @@ class Security extends Controller
} }
$actor = GSActor::create(['nickname' => $data['nickname']]); $actor = GSActor::create(['nickname' => $data['nickname']]);
$user = LocalUser::create([
'nickname' => $data['nickname'],
'email' => $data['email'],
'password' => LocalUser::hashPassword($data['password']),
]);
DB::persist($user);
DB::persist($actor); DB::persist($actor);
DB::flush();
$user = LocalUser::create([
'id' => $actor->getId(),
'nickname' => $data['nickname'],
'outgoing_email' => $data['email'],
'incoming_email' => $data['email'],
'password' => LocalUser::hashPassword($data['password']),
]);
DB::persist($user);
// generate a signed url and email it to the user // generate a signed url and email it to the user
if (Common::config('site', 'use_email')) { if (Common::config('site', 'use_email')) {

View File

@ -68,8 +68,8 @@ class UserPanel extends AbstractController
public function personal_info(Request $request) public function personal_info(Request $request)
{ {
$user = Common::user(); $user = Common::user();
$actor = $user->getActor(); $user = $user->getActor();
$extra = ['self_tags' => $actor->getSelfTags()]; $extra = ['self_tags' => $user->getSelfTags()];
$form_definition = [ $form_definition = [
['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]], ['nickname', TextType::class, ['label' => _m('Nickname'), 'required' => true, 'help' => _m('1-64 lowercase letters or numbers, no punctuation or spaces.')]],
['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'help' => _m('A full name is required, if empty it will be set to your nickname.')]], ['full_name', TextType::class, ['label' => _m('Full Name'), 'required' => false, 'help' => _m('A full name is required, if empty it will be set to your nickname.')]],
@ -80,7 +80,7 @@ class UserPanel extends AbstractController
['save', SubmitType::class, ['label' => _m('Save')]], ['save', SubmitType::class, ['label' => _m('Save')]],
]; ];
$extra_step = function ($data, $extra_args) use ($user) { $user->setNickname($data['nickname']); }; $extra_step = function ($data, $extra_args) use ($user) { $user->setNickname($data['nickname']); };
$form = Form::handle($form_definition, $request, $actor, $extra, $extra_step, [['self_tags' => $extra['self_tags']]]); $form = Form::handle($form_definition, $request, $user, $extra, $extra_step, [['self_tags' => $extra['self_tags']]]);
return ['_template' => 'settings/profile.html.twig', 'prof' => $form->createView()]; return ['_template' => 'settings/profile.html.twig', 'prof' => $form->createView()];
} }
@ -136,8 +136,8 @@ class UserPanel extends AbstractController
} else { } else {
throw new ClientException('Invalid form'); throw new ClientException('Invalid form');
} }
$actor = Common::actor(); $user = Common::user();
$actor_id = $actor->getId(); $actor_id = $user->getId();
$file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $title = null, $is_local = true, $use_unique = $actor_id); $file = Media::validateAndStoreFile($sfile, Common::config('avatar', 'dir'), $title = null, $is_local = true, $use_unique = $actor_id);
$old_file = null; $old_file = null;
$avatar = DB::find('avatar', ['gsactor_id' => $actor_id]); $avatar = DB::find('avatar', ['gsactor_id' => $actor_id]);
@ -154,7 +154,7 @@ class UserPanel extends AbstractController
if ($old_file != null) { if ($old_file != null) {
@unlink($old_file); @unlink($old_file);
} }
Event::handle('delete_cached_avatar', [$actor->getNickname()]); Event::handle('delete_cached_avatar', [$user->getNickname()]);
} }
return ['_template' => 'settings/avatar.html.twig', 'avatar' => $form->createView()]; return ['_template' => 'settings/avatar.html.twig', 'avatar' => $form->createView()];

View File

@ -30,11 +30,14 @@
namespace App\Core; namespace App\Core;
use App\Util\Exception\RedirectException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent; use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent; use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
@ -95,10 +98,23 @@ class Controller extends AbstractController implements EventSubscriberInterface
return $event; return $event;
} }
public function onKernelException(ExceptionEvent $event)
{
if (($except = $event->getThrowable()) instanceof RedirectException) {
if (($redir = $except->redirect_response) != null) {
$event->setResponse($redir);
} else {
$event->setResponse(new RedirectResponse($event->getRequest()->getPathInfo()));
}
}
return $event;
}
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return [ return [
KernelEvents::CONTROLLER => 'onKernelController', KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::EXCEPTION => 'onKernelException',
KernelEvents::VIEW => 'onKernelView', KernelEvents::VIEW => 'onKernelView',
]; ];
} }

View File

@ -0,0 +1,37 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Util\Exception;
use App\Core\Router\Router;
use Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
class RedirectException extends Exception
{
public ?RedirectResponse $redirect_response = null;
public function __construct(string $url_id = '', $message = '', $code = 302, Exception $previous_exception = null)
{
if (!empty($url_id)) {
$this->redirect_response = new RedirectResponse(Router::url($urlid));
}
parent::__construct($message, $code, $previous_exception);
}
}