Skip to content

Commit

Permalink
override TokenBasedRememberMeServices and authenticate user also in z…
Browse files Browse the repository at this point in the history
…end_auth
  • Loading branch information
ahilles107 committed Apr 23, 2015
1 parent 8fecf4d commit e4b97fa
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
3 changes: 1 addition & 2 deletions newscoop/library/Newscoop/Auth/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function authenticate()
'password' => sha1($this->password),
));

$code = $user ?
Zend_Auth_Result::SUCCESS : Zend_Auth_Result::FAILURE;
$code = $user ? Zend_Auth_Result::SUCCESS : Zend_Auth_Result::FAILURE;

return new Zend_Auth_Result($code, $user ? $user->getId() : NULL);
}
Expand Down
4 changes: 3 additions & 1 deletion newscoop/library/Newscoop/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ 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->security->isGranted('IS_AUTHENTICATED_REMEMBERED')
) {
$this->currentUser = $currentUser;
} else {
throw new AuthenticationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,4 @@ parameters:
newscoop_newscoop.menu_builder.class: "Newscoop\\NewscoopBundle\\Menu\\Builder"
newscoop_newscoop.session.storage.class: "Newscoop\\NewscoopBundle\\Session\\Storage"
ewz_recaptcha.form.type.class: "Newscoop\\NewscoopBundle\\Form\\Type\\RecaptchaType"
security.authentication.rememberme.services.simplehash.class: "Newscoop\\NewscoopBundle\\Security\\Http\\Authentication\\InteractiveLogin"
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* @package Newscoop\NewscoopBundle
* @author Paweł Mikołajczuk <[email protected]>
* @copyright 2015 Sourcefabric o.p.s.
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop\NewscoopBundle\Security\Http\Authentication;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
use Symfony\Component\HttpFoundation\Request;

/**
* Temporary class for remember_me token based authentication
*/
class InteractiveDoctrineAuthService implements \Zend_Auth_Adapter_Interface
{
public $user = null;

/**
* Perform authentication attempt
*
* @return \Zend_Auth_Result
*/
public function authenticate()
{
if (empty($this->user)) {
return new \Zend_Auth_Result(\Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, NULL);
}

if (!$this->user->isActive()) {
return new \Zend_Auth_Result(\Zend_Auth_Result::FAILURE_UNCATEGORIZED, NULL);
}

return new \Zend_Auth_Result(\Zend_Auth_Result::SUCCESS, $this->user->getId());
}
}

/**
* Custom login listener.
*/
class InteractiveLogin extends TokenBasedRememberMeServices
{
/**
* {@inheritdoc}
*/
protected function processAutoLoginCookie(array $cookieParts, Request $request)
{
$user = parent::processAutoLoginCookie($cookieParts, $request);

$zendAuth = \Zend_Auth::getInstance();

$authAdapter = new InteractiveDoctrineAuthService();
$authAdapter->user = $user;

$zendAuth->authenticate($authAdapter);

return $user;
}
}

0 comments on commit e4b97fa

Please sign in to comment.