diff --git a/newscoop/application/configs/symfony/config.yml b/newscoop/application/configs/symfony/config.yml index b04e7276bb..0e784b2c5b 100644 --- a/newscoop/application/configs/symfony/config.yml +++ b/newscoop/application/configs/symfony/config.yml @@ -77,6 +77,7 @@ fos_rest: 'Newscoop\Exception\ResourceNotModifiedException': 304 'Newscoop\NewscoopException': 500 'OAuth2\OAuth2AuthenticateException': 401 + 'Newscoop\Exception\AuthenticationException': 401 messages: 'Newscoop\Exception\ResourcesConflictException': true 'Newscoop\Exception\InvalidParametersException': true @@ -89,6 +90,7 @@ fos_rest: 'Newscoop\Exception\ResourceNotModifiedException': false 'OAuth2\OAuth2AuthenticateException': true 'Newscoop\NewscoopException': true + 'Newscoop\Exception\AuthenticationException': true fos_oauth_server: service: diff --git a/newscoop/library/Newscoop/Services/CommentNotificationService.php b/newscoop/library/Newscoop/Services/CommentNotificationService.php index fb1fbf3e68..8c80b4fe56 100644 --- a/newscoop/library/Newscoop/Services/CommentNotificationService.php +++ b/newscoop/library/Newscoop/Services/CommentNotificationService.php @@ -4,12 +4,11 @@ * @copyright 2011 Sourcefabric o.p.s. * @license http://www.gnu.org/licenses/gpl-3.0.txt */ - namespace Newscoop\Services; use Newscoop\EventDispatcher\Events\GenericEvent; use Doctrine\ORM\EntityManager; -use Newscoop\NewscoopException; +use Newscoop\Exception\AuthenticationException; /** */ @@ -57,7 +56,7 @@ public function update(GenericEvent $event) try { $user = $this->userService->getCurrentUser(); - } catch (NewscoopException $e) { + } catch (AuthenticationException $e) { $user = null; } diff --git a/newscoop/library/Newscoop/Services/UserService.php b/newscoop/library/Newscoop/Services/UserService.php index fc28f0426d..97973d9046 100644 --- a/newscoop/library/Newscoop/Services/UserService.php +++ b/newscoop/library/Newscoop/Services/UserService.php @@ -4,19 +4,18 @@ * @copyright 2011 Sourcefabric o.p.s. * @license http://www.gnu.org/licenses/gpl-3.0.txt */ - namespace Newscoop\Services; use Doctrine\Common\Persistence\ObjectManager; use Newscoop\Entity\User; use Newscoop\PaginatedCollection; use InvalidArgumentException; -use Newscoop\NewscoopException; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\SecurityContext; use Symfony\Component\Security\Core\Encoder\EncoderFactory; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\EventDispatcher\GenericEvent; +use Newscoop\Exception\AuthenticationException; /** * User service @@ -73,13 +72,13 @@ public function getCurrentUser() } elseif ($this->security->getToken()) { if ($this->security->getToken()->getUser()) { $currentUser = $this->security->getToken()->getUser(); - if ($this->security->isGranted('IS_AUTHENTICATED_FULLY') ) { + if ($this->security->isGranted('IS_AUTHENTICATED_FULLY')) { $this->currentUser = $currentUser; } else { - throw new NewscoopException("User is not Authenticated", 1); + throw new AuthenticationException(); } } else { - throw new NewscoopException("User was not found", 1); + throw new AuthenticationException(); } } } @@ -141,7 +140,7 @@ public function getCollection(array $criteria, array $orderBy, $limit = null, $o $qb->setMaxResults($limit); if (!empty($criteria['q'])) { - $q = $qb->expr()->literal('%' . $criteria['q'] . '%'); + $q = $qb->expr()->literal('%'.$criteria['q'].'%'); $qb->andWhere($qb->expr()->orX( $qb->expr()->like('u.username', $q), $qb->expr()->like('u.email', $q) @@ -268,7 +267,7 @@ public function generateUsername($firstName, $lastName) } $user = new User(); - $user->setUsername(trim($firstName) . ' ' . trim($lastName)); + $user->setUsername(trim($firstName).' '.trim($lastName)); $username = $user->getUsername(); for ($i = '';; $i++) { diff --git a/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php b/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php index 547ad70a62..432f041477 100644 --- a/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php +++ b/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesController.php @@ -23,6 +23,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Newscoop\Criteria\ArticleSearchCriteria; +use Newscoop\Exception\AuthenticationException; /** * Articles controller @@ -269,7 +270,7 @@ public function searchArticlesAction(Request $request) if ($user && $user->isAdmin()) { $onlyPublished = false; } - } catch (\Newscoop\NewscoopException $e) { + } catch (AuthenticationException $e) { } $articleSearchCriteria = new ArticleSearchCriteria(); @@ -327,7 +328,7 @@ public function relatedArticlesAction(Request $request, $number, $language = nul if ($user && $user->isAdmin()) { $onlyPublished = false; } - } catch (\Newscoop\NewscoopException $e) { + } catch (AuthenticationException $e) { } $relatedArticles = $relatedArticlesService diff --git a/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesListController.php b/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesListController.php index 74dcba7cc2..8726049f36 100644 --- a/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesListController.php +++ b/newscoop/src/Newscoop/GimmeBundle/Controller/ArticlesListController.php @@ -25,6 +25,7 @@ use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\FilterControllerEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Newscoop\Exception\AuthenticationException; class ArticlesListController extends FOSRestController { @@ -135,7 +136,7 @@ public function getPlaylistsArticlesAction(Request $request, $id) if ($user && $user->isAdmin()) { $onlyPublished = false; } - } catch (\Newscoop\NewscoopException $e) { + } catch (AuthenticationException $e) { } $playlistArticles = $em->getRepository('Newscoop\Entity\Playlist') diff --git a/newscoop/src/Newscoop/GimmeBundle/Controller/UsersController.php b/newscoop/src/Newscoop/GimmeBundle/Controller/UsersController.php index 4f95743e61..d835d5bb10 100644 --- a/newscoop/src/Newscoop/GimmeBundle/Controller/UsersController.php +++ b/newscoop/src/Newscoop/GimmeBundle/Controller/UsersController.php @@ -6,7 +6,6 @@ * @copyright 2014 Sourcefabric o.p.s. * @license http://www.gnu.org/licenses/gpl-3.0.txt */ - namespace Newscoop\GimmeBundle\Controller; use FOS\RestBundle\Controller\FOSRestController; @@ -16,9 +15,8 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Security\Core\Exception\AccessDeniedException; -use Symfony\Component\Security\Core\Exception\AuthenticationException; +use Newscoop\Exception\AuthenticationException; use Symfony\Component\HttpFoundation\Response; -use Doctrine\ORM\EntityNotFoundException; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Newscoop\GimmeBundle\Entity\Client; @@ -54,7 +52,7 @@ public function getUsersAction(Request $request) $paginator = $this->get('newscoop.paginator.paginator_service'); $users = $paginator->paginate($users, array( - 'distinct' => false + 'distinct' => false, )); return $users; @@ -88,7 +86,8 @@ public function searchUsersAction(Request $request) if ($user && $user->isAdmin()) { $onlyPublic = null; } - } catch (\Newscoop\NewscoopException $e) {} + } catch (AuthenticationException $e) { + } $criteria = new \Newscoop\User\UserCriteria(); $criteria->is_public = $onlyPublic; @@ -192,13 +191,13 @@ public function loginAction(Request $request) $passwordEncoder = $this->container->get('newscoop_newscoop.password_encoder'); $user = $em->getRepository('Newscoop\Entity\User') ->findOneBy(array( - 'username' => $username + 'username' => $username, )); if (!$user) { $user = $user = $em->getRepository('Newscoop\Entity\User') ->findOneBy(array( - 'email' => $username + 'email' => $username, )); } @@ -223,13 +222,13 @@ public function loginAction(Request $request) $authAdapter = $this->get('auth.adapter'); $authAdapter->setEmail($user->getEmail())->setPassword($request->request->get('password')); $zendAuth->authenticate($authAdapter); - setcookie('NO_CACHE', '1', NULL, '/', '.'.$this->extractDomain($_SERVER['HTTP_HOST'])); + setcookie('NO_CACHE', '1', null, '/', '.'.$this->extractDomain($_SERVER['HTTP_HOST'])); $response->setStatusCode($targetPath ? 302 : 200); $response->headers->set( 'X-Location', $targetPath ? $request->getUriForPath($targetPath) : $this->generateUrl('newscoop_gimme_users_getuser', array( - 'id' => $user->getId() + 'id' => $user->getId(), ), true) ); @@ -406,7 +405,7 @@ public function getUserAccessTokenAction(Request $request, $clientId) $authRequest = Request::create($authUrl, 'GET', array( 'client_id' => $clientId, 'redirect_uri' => $redirectUris[0], - 'response_type' => 'code' + 'response_type' => 'code', ), $request->cookies->all()); $kernel = $this->get('http_kernel'); diff --git a/newscoop/src/Newscoop/NewscoopBundle/EventListener/AuthenticationListener.php b/newscoop/src/Newscoop/NewscoopBundle/EventListener/AuthenticationListener.php deleted file mode 100644 index 01166aa688..0000000000 --- a/newscoop/src/Newscoop/NewscoopBundle/EventListener/AuthenticationListener.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @copyright 2014 Sourcefabric o.p.s. - * @license http://www.gnu.org/licenses/gpl-3.0.txt - */ - -namespace Newscoop\NewscoopBundle\EventListener; - -use Newscoop\Services\UserService; -use Symfony\Component\Routing\Router; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\HttpKernel\HttpKernel; -use Symfony\Component\HttpFoundation\RedirectResponse; - -/** - * Run authentication resolver on request - */ -class AuthenticationListener -{ - /** - * User service - * - * @var UserService - */ - protected $userService; - - /** - * Contruct AuthenticationListener object - * - * @param UserService $userService User service - * @param UserService $routerService Symfony router - */ - public function __construct(UserService $userService, Router $router) - { - $this->userService = $userService; - $this->router = $router; - } - - public function onRequest(GetResponseEvent $event) - { - if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) { - // don't do anything if it's not the master request - return; - } - - try { - $this->userService->getCurrentUser(); - } catch (\Newscoop\NewscoopException $e) { - return new RedirectResponse($this->router->generate('admin_logout')); - } - } -} diff --git a/newscoop/src/Newscoop/NewscoopBundle/Resources/config/services.yml b/newscoop/src/Newscoop/NewscoopBundle/Resources/config/services.yml index 9ab32d0af4..d98af996a8 100644 --- a/newscoop/src/Newscoop/NewscoopBundle/Resources/config/services.yml +++ b/newscoop/src/Newscoop/NewscoopBundle/Resources/config/services.yml @@ -1,10 +1,4 @@ services: - newscoop_newscoop.listener.authentication.backend.session: - class: Newscoop\NewscoopBundle\EventListener\AuthenticationListener - arguments: ["@user", "@router"] - tags: - - { name: kernel.event_listener, event: kernel.request, method: onRequest } - newscoop_newscoop.routing.loader.plugins: class: Newscoop\NewscoopBundle\Routing\PluginsLoader arguments: ["@newscoop.plugins.manager", "@service_container"] diff --git a/newscoop/src/Newscoop/NewscoopBundle/Twig/NewscoopExtension.php b/newscoop/src/Newscoop/NewscoopBundle/Twig/NewscoopExtension.php index a8e04c193f..b33e3e791f 100644 --- a/newscoop/src/Newscoop/NewscoopBundle/Twig/NewscoopExtension.php +++ b/newscoop/src/Newscoop/NewscoopBundle/Twig/NewscoopExtension.php @@ -5,9 +5,10 @@ * @copyright 2013 Sourcefabric o.p.s. * @license http://www.gnu.org/licenses/gpl-3.0.txt */ - namespace Newscoop\NewscoopBundle\Twig; +use Newscoop\Exception\AuthenticationException; + class NewscoopExtension extends \Twig_Extension { @@ -46,7 +47,7 @@ public function getGlobals() try { $currentUser = $this->container->getService('user')->getCurrentUser(); - } catch (\Newscoop\NewscoopException $e) { + } catch (AuthenticationException $e) { $currentUser = null; } @@ -55,7 +56,7 @@ public function getGlobals() 'NewscoopVersion' => new \CampVersion(), 'SecurityToken' => \SecurityToken::GetToken(), 'NewscoopUser' => $currentUser, - 'localeFromCookie' => $localeFromCookie + 'localeFromCookie' => $localeFromCookie, ); } @@ -98,7 +99,7 @@ public function getReCaptchaImage() $aFonts = array( $fontsDirectory.'fonts/VeraBd.ttf', $fontsDirectory.'fonts/VeraIt.ttf', - $fontsDirectory.'fonts/Vera.ttf' + $fontsDirectory.'fonts/Vera.ttf', ); $oVisualCaptcha = new \PhpCaptcha($aFonts, 200, 60); $oVisualCaptcha->Create(__DIR__.'/../../../../images/cache/recaptcha.png'); @@ -144,7 +145,7 @@ public function getCsrfToken() public function renderTip($tipMessage) { return $this->container->get('templating')->render('NewscoopNewscoopBundle::tooltip.html.twig', array( - 'tipMessage' => $tipMessage + 'tipMessage' => $tipMessage, )); }