前回の続き

抽象メソッド全て実装。simpleauthパクってる所も多い。
本来はDB操作はモデルでメソッド作成するべきだろう。
あとUtil_Const::DB_DLT_OFFをバインドパラメータに直でbind(‘dlt_flg’ , Util_Const::DB_DLT_OFF)とパラメータ参照エラーになる。fuelphpとかではなくDB上の話。
dlt_flgでデフォルトの物理削除ではなく論理削除に回避しているので、取得時も条件式にいれている。

<?php
class Auth_Login_BaseAuth extends Auth\Auth_Login_Driver {

protected $config = array('drivers' =&gt; array('group' =&gt; array('BaseAuth')));

protected $user;

protected function perform_check() {
$current_user = Session::get('current_user');

if (!is_null($current_user) &amp;&amp; is_array($current_user)) {
if (isset($current_user['id']) &amp;&amp; isset($current_user['salt'])) {

$dlt_flg = Util_Const::DB_DLT_OFF;

$users = DB::query(
'SELECT * FROM tb_users WHERE id = :id AND salt = :salt AND dlt_flg = :dlt_flg'
)
-&gt;bind('id', $current_user['id'])
-&gt;bind('salt' , $current_user['salt'])
-&gt;bind('dlt_flg' , $dlt_flg)
-&gt;as_object('Model_User')
-&gt;execute()
-&gt;as_array();

if (!is_null($users) &amp;&amp; count($users) === 1) {
$this-&gt;user = reset($users);
return true;
}

}

}
return false;
}

public function validate_user($username_or_email = '', $password = '') {

if(empty($username_or_email) || empty($password)) return false;

$username_or_email = trim($username_or_email);
$password = trim($password);

$password = $this-&gt;hash_password($password);
$dlt_flg = Util_Const::DB_DLT_OFF;

$users = DB::query(
'SELECT * FROM tb_users WHERE (username = :username_or_email or email = :username_or_email) AND password = :password AND dlt_flg = :dlt_flg'
)
-&gt;bind('username_or_email', $username_or_email)
-&gt;bind('password' , $password)
-&gt;bind('dlt_flg' , $dlt_flg)
-&gt;as_object('Model_User')
-&gt;execute()
-&gt;as_array();

if (!is_null($users) &amp;&amp; count($users) === 1){

$this-&gt;user = reset($users);
$this-&gt;user-&gt;last_login = Date::forge()-&gt;get_timestamp();
$this-&gt;user-&gt;salt = $this-&gt;create_salt();
$this-&gt;user-&gt;save();

Session::set('current_user', array('id' =&gt; $this-&gt;user-&gt;id,'salt' =&gt; $this-&gt;user-&gt;salt));

return true;
}

return false;
}

public function login($username_or_email = '', $password = '') {
return $this-&gt;validate_user($username_or_email, $password);
}

public function logout() {
Session::delete('current_user');
return true;
}

public function get_user_id() {
if (!empty($this-&gt;user) &amp;&amp; isset($this-&gt;user['id'])) {
return array($this-&gt;id, (int)$this-&gt;user['id']);
}

return null;
}

public function get_groups() {
if (!empty($this-&gt;user) &amp;&amp; isset($this-&gt;user['group'])) {
return array(array('BaseAuth', $this-&gt;user['group']));
}
return null;
}

public function get_email() {
if (!empty($this-&gt;user) &amp;&amp; isset($this-&gt;user['email'])) {
return $this-&gt;user['email'];
}
return null;
}

public function get_screen_name() {
if (!empty($this-&gt;user) &amp;&amp; isset($this-&gt;user['username'])) {
return $this-&gt;user['username'];
}
return null;
}

public function has_access($condition, $driver = null, $entity = null) {
if (is_null($entity) &amp;&amp; !empty($this-&gt;user)) {
$groups = $this-&gt;get_groups();
$entity = reset($groups);
}
return parent::has_access($condition, $driver, $entity);
}

public function create_salt()
{
if (empty($this-&gt;user)) throw new Exception();
return sha1(Config::get('baseauth.login_hash_salt').$this-&gt;user-&gt;username.$this-&gt;user-&gt;last_login);
}
}
<?php 
class Auth_Group_BaseAuth extends Auth\Auth_Group_Driver {

protected $config = array('drivers' =&gt; array('acl' =&gt; array('BaseAuth')));

public function get_name($group = null) {
// noop
}

public function member($group, $user = null) {
$auth = empty($user) ? Auth::instance() : Auth::instance($user[0]);
$groups = $auth-&gt;get_groups();
return in_array(array($this-&gt;id, $group), $groups);
}
}
<?php 
class Auth_Acl_BaseAuth extends Auth\Auth_Acl_Driver {
public function has_access($condition, Array $entity) {
if (count($entity) &gt; 0) {
$group = Auth::group($entity[0]);
if (!is_null($group) || !empty($group)) return $group-&gt;member($condition);
}
return false;
}
}

ここまでで実装完了。コード中にutilで定義している定数を使っているのでこちらのソースも公開

<?php
class Util_Const
{
/* ======= db ======= */

const DB_DLT_OFF = 0;
const DB_DLT_ON = 1;
}

パスワードの暗号化はsimpleauthと同様driversのメソッドを呼び出し。

今回はここまで
ここまででドライバは一応実装済み。
simpleauthドライバよりもシンプルで特に勝るところもなく仕上がったかな。
あとは呼び出しのみ。

その他おすすめの備忘録

Tagged with:
 

One Response to [FuelPHP]独自オリジナル認証ドライバをsimpleauthを参考に実装する2

  1. mukaken より:

    [FuelPHP][20121231] / “[FuelPHP]独自オリジナル認証ドライバをsimpleauthを参考に実装する2 | Memorandum blog” http://t.co/8xZq42S5

コメントを残す