symfony1.4.9で自作認証機能

2011年3月19日 オフ 投稿者: KYO

Symfony1.4.9で、sfGuardPluginを利用していたら、propel:build-modelが一切できなくなった。propel:build-sqlも同様。duplicateエラーを吐く。
そこで、簡単に自作してみた。

構成は、単純。

バックエンドへのアクセスを全て認証経由にするようにしただけ。

あくまでメモなので、全てを行って動作するかどうかは保証出来ません。。

 

$ symfony generate:module backend admin2

$ vim apps/backend/modules/admin2/templates/indexSuccess.php

login
ログインID:
<input type="text" name="uid" /> パスワード:
<input type="password" name="password" />

<input type="submit" value="ログイン" />

$ vim apps/backend/modules/admin2/templates/secureSuccess.php

Login: getAttribute('username') ?&gt;

Last Login: getAttribute('last_logined_at') ?&gt;

$ vim apps/backend/modules/admin2/actions/actions.class.php

getParameter('uid');
$password = $request-&gt;getParameter('password');
$c = new Criteria();
$c-&gt;add(AuthPeer::UID, $uid);
$auth = AuthPeer::doSelectOne($c);
if ($auth &amp;&amp; $auth-&gt;getValid()) {
if ($password == $auth-&gt;getPassword()) {
$this-&gt;getUser()-&gt;setAuthenticated(true);
$this-&gt;getUser()-&gt;addCredential('admin');
$this-&gt;getUser()-&gt;setAttribute('uid', $auth-&gt;getUid());
$this-&gt;getUser()-&gt;setAttribute('username', $auth-&gt;getUserName());
$this-&gt;getUser()-&gt;setAttribute('last_logined_at', $auth-&gt;getLastLoginedAt());
$auth-&gt;setLastLoginedAt(date('Y/m/d H:i:s', time()));
$auth-&gt;save();
return $this-&gt;redirect($this-&gt;getRequestParameter('referer', '@secure'));
}
}
return $this-&gt;redirect('enter_password');
}
public function executeLogout(sfWebRequest $request)
{
$this-&gt;getUser()-&gt;setAuthenticated(false);
$this-&gt;getUser()-&gt;clearCredentials();
return $this-&gt;redirect('enter_password');
}
public function executeIndex(sfWebRequest $request)
{
}
public function executeSecure(sfWebRequest $request)
{
}
}

あとは、各設定ファイルをごにょごにょやっていく

 

$ vim apps/backend/config/security.yml
default:

is_secure: on

.actions:

login_module:   admin2
login_action:   index

 

あとは、バックエンドモジュール全てにsecuriry.ymlを作って、templates/layout.phpを変更、ルーティングを設定する程度。

 

案外あっさり完了。