Skip to content

Commit

Permalink
fixed failed authentication handling
Browse files Browse the repository at this point in the history
  • Loading branch information
takeit committed Mar 27, 2015
1 parent 55c46f3 commit 6adfdce
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 88 deletions.
2 changes: 2 additions & 0 deletions newscoop/application/configs/symfony/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -89,6 +90,7 @@ fos_rest:
'Newscoop\Exception\ResourceNotModifiedException': false
'OAuth2\OAuth2AuthenticateException': true
'Newscoop\NewscoopException': true
'Newscoop\Exception\AuthenticationException': true

fos_oauth_server:
service:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*/
Expand Down Expand Up @@ -57,7 +56,7 @@ public function update(GenericEvent $event)

try {
$user = $this->userService->getCurrentUser();
} catch (NewscoopException $e) {
} catch (AuthenticationException $e) {
$user = null;
}

Expand Down
13 changes: 6 additions & 7 deletions newscoop/library/Newscoop/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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')
Expand Down
19 changes: 9 additions & 10 deletions newscoop/src/Newscoop/GimmeBundle/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
));
}

Expand All @@ -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)
);

Expand Down Expand Up @@ -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');
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
11 changes: 6 additions & 5 deletions newscoop/src/Newscoop/NewscoopBundle/Twig/NewscoopExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand Down Expand Up @@ -46,7 +47,7 @@ public function getGlobals()

try {
$currentUser = $this->container->getService('user')->getCurrentUser();
} catch (\Newscoop\NewscoopException $e) {
} catch (AuthenticationException $e) {
$currentUser = null;
}

Expand All @@ -55,7 +56,7 @@ public function getGlobals()
'NewscoopVersion' => new \CampVersion(),
'SecurityToken' => \SecurityToken::GetToken(),
'NewscoopUser' => $currentUser,
'localeFromCookie' => $localeFromCookie
'localeFromCookie' => $localeFromCookie,
);
}

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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,
));
}

Expand Down

0 comments on commit 6adfdce

Please sign in to comment.