Skip to content

Commit

Permalink
Merge commit 'kawahara/remember_login'
Browse files Browse the repository at this point in the history
  • Loading branch information
kawahara committed Feb 8, 2010
2 parents 2350906 + f6a18da commit 74bbf15
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
3 changes: 3 additions & 0 deletions apps/pc_frontend/config/filters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ enable_app:
xrds_header:
class: opAppendXRDSHeaderFilter

remember_login:
class: opRememberLoginFilter

security: ~

emoji:
Expand Down
4 changes: 4 additions & 0 deletions apps/pc_frontend/i18n/messages.ja.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,10 @@
<source>Failed to post activity.</source>
<target>アクティビティの投稿に失敗しました。</target>
</trans-unit>
<trans-unit id="">
<source>Remember me</source>
<target>次回から自動的にログイン</target>
</trans-unit>
</body>
</file>
</xliff>
36 changes: 36 additions & 0 deletions lib/filter/opRememberLoginFilter.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of the OpenPNE package.
* (c) OpenPNE Project (http://www.openpne.jp/)
*
* For the full copyright and license information, please view the LICENSE
* file and the NOTICE file that were distributed with this source code.
*/

/**
* opRememberLoginFilter
*
* @package OpenPNE
* @subpackage filter
* @author Shogo Kawahara <[email protected]>
*/
class opRememberLoginFilter extends sfFilter
{
/**
* Executes this filter.
*
* @param sfFilterChain $filterChain A sfFilterChain instance
*/
public function execute($filterChain)
{
if ($this->isFirstCall() && !$this->context->getUser()->isAuthenticated())
{
if ($memberId = $this->context->getUser()->getRememberedMemberId())
{
$this->context->getUser()->login($memberId);
}
}
$filterChain->execute();
}
}
7 changes: 7 additions & 0 deletions lib/form/opAuthLoginForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function __construct(opAuthAdapter $adapter, $defaults = array(), $option
$this->setWidget('next_uri', new opWidgetFormInputHiddenNextUri());
$this->setValidator('next_uri', new opValidatorNextUri());

if ($this->getOption('is_use_remember_me', true) && !sfConfig::get('app_is_mobile'))
{
$this->setWidget('is_remember_me', new sfWidgetFormInputCheckbox());
$this->setValidator('is_remember_me', new sfValidatorBoolean());
$this->widgetSchema->setLabel('is_remember_me', 'Remember me');
}

$this->widgetSchema->setNameFormat('auth'.$this->adapter->getAuthModeName().'[%s]');
}

Expand Down
96 changes: 94 additions & 2 deletions lib/user/sfOpenPNESecurityUser.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,99 @@ public function getRegisterEndAction()
return $this->getAuthAdapter()->getRegisterEndAction();
}


/**
* get remember login cookie
*
* @return array
*/
protected function getRememberLoginCookie()
{
$key = md5(sfContext::getInstance()->getRequest()->getHost());
if ($value = sfContext::getInstance()->getRequest()->getCookie($key))
{
$value = unserialize(base64_decode($value));
return $value;
}
return null;
}

/**
* set remember login cookie
*/
protected function setRememberLoginCookie($isDeleteCookie = false)
{
$key = md5(sfContext::getInstance()->getRequest()->getHost());
$path = sfContext::getInstance()->getRequest()->getRelativeUrlRoot();
if (!$path)
{
$path = '/';
}

if ($isDeleteCookie)
{
if (!sfContext::getInstance()->getRequest()->getCookie($key))
{
return;
}

$value = null;
$expire = time() - 3600;
}
else
{
$rememberKey = opToolkit::generatePasswordString();
if (!$this->getMemberId())
{
throw new LogicException('No login');
}
$this->getMember()->setConfig('remember_key', $rememberKey);

$value = base64_encode(serialize(array($this->getMemberId(), $rememberKey)));
$expire = time() + sfConfig::get('op_remember_login_limit', 60*60*24*30);
}
sfContext::getInstance()->getResponse()->setCookie($key, $value, $expire, $path, '', false, true);
}

/**
* get memberd member id
*
* @return integer the member id
*/
public function getRememberedMemberId()
{
$key = md5(sfContext::getInstance()->getRequest()->getHost());
if (($value = $this->getRememberLoginCookie()) && 2 == count($value))
{
try
{
$memberConfig = Doctrine::getTable('MemberConfig')->findOneByMemberIdAndValue($value[0], $value[1]);
}
catch (Doctrine_Table_Exception $e)
{
return null;
}

if ($memberConfig)
{
return $value[0];
}
}
return null;
}

/**
* Login
*
* @param integer $memberId the member id
* @return bool returns true if the current user is authenticated, false otherwise
*/
public function login()
public function login($memberId = null)
{
$memberId = $this->getAuthAdapter()->authenticate();
if (null === $memberId)
{
$memberId = $this->getAuthAdapter()->authenticate();
}

if ($memberId)
{
Expand All @@ -202,6 +287,11 @@ public function login()

$this->initializeCredentials();

if ($this->getAuthAdapter()->getAuthForm()->getValue('is_remember_me'))
{
$this->setRememberLoginCookie();
}

if ($this->isAuthenticated())
{
$this->setCurrentAuthMode($this->getAuthAdapter()->getAuthModeName());
Expand All @@ -219,6 +309,8 @@ public function logout()
{
$authMode = $this->getCurrentAuthMode();

$this->setRememberLoginCookie(true);

$this->setAuthenticated(false);
$this->getAttributeHolder()->removeNamespace('sfOpenPNESecurityUser');
$this->clearCredentials();
Expand Down

0 comments on commit 74bbf15

Please sign in to comment.