Coffee Breakにプログラミング備忘録
前回の続き
抽象メソッド全て実装。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' => array('group' => array('BaseAuth'))); protected $user; protected function perform_check() { $current_user = Session::get('current_user'); if (!is_null($current_user) && is_array($current_user)) { if (isset($current_user['id']) && 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' ) ->bind('id', $current_user['id']) ->bind('salt' , $current_user['salt']) ->bind('dlt_flg' , $dlt_flg) ->as_object('Model_User') ->execute() ->as_array(); if (!is_null($users) && count($users) === 1) { $this->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->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' ) ->bind('username_or_email', $username_or_email) ->bind('password' , $password) ->bind('dlt_flg' , $dlt_flg) ->as_object('Model_User') ->execute() ->as_array(); if (!is_null($users) && count($users) === 1){ $this->user = reset($users); $this->user->last_login = Date::forge()->get_timestamp(); $this->user->salt = $this->create_salt(); $this->user->save(); Session::set('current_user', array('id' => $this->user->id,'salt' => $this->user->salt)); return true; } return false; } public function login($username_or_email = '', $password = '') { return $this->validate_user($username_or_email, $password); } public function logout() { Session::delete('current_user'); return true; } public function get_user_id() { if (!empty($this->user) && isset($this->user['id'])) { return array($this->id, (int)$this->user['id']); } return null; } public function get_groups() { if (!empty($this->user) && isset($this->user['group'])) { return array(array('BaseAuth', $this->user['group'])); } return null; } public function get_email() { if (!empty($this->user) && isset($this->user['email'])) { return $this->user['email']; } return null; } public function get_screen_name() { if (!empty($this->user) && isset($this->user['username'])) { return $this->user['username']; } return null; } public function has_access($condition, $driver = null, $entity = null) { if (is_null($entity) && !empty($this->user)) { $groups = $this->get_groups(); $entity = reset($groups); } return parent::has_access($condition, $driver, $entity); } public function create_salt() { if (empty($this->user)) throw new Exception(); return sha1(Config::get('baseauth.login_hash_salt').$this->user->username.$this->user->last_login); } }
<?php class Auth_Group_BaseAuth extends Auth\Auth_Group_Driver { protected $config = array('drivers' => array('acl' => 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->get_groups(); return in_array(array($this->id, $group), $groups); } }
<?php class Auth_Acl_BaseAuth extends Auth\Auth_Acl_Driver { public function has_access($condition, Array $entity) { if (count($entity) > 0) { $group = Auth::group($entity[0]); if (!is_null($group) || !empty($group)) return $group->member($condition); } return false; } }
ここまでで実装完了。コード中にutilで定義している定数を使っているのでこちらのソースも公開
<?php class Util_Const { /* ======= db ======= */ const DB_DLT_OFF = 0; const DB_DLT_ON = 1; }
パスワードの暗号化はsimpleauthと同様driversのメソッドを呼び出し。
今回はここまで
ここまででドライバは一応実装済み。
simpleauthドライバよりもシンプルで特に勝るところもなく仕上がったかな。
あとは呼び出しのみ。
その他おすすめの備忘録
- [FuelPHP]完成?:独自オリジナル認証ドライバをsimpleauthを参考に実装する3
- [FuelPHP]独自オリジナル認証ドライバをsimpleauthを参考に実装する1
- [FuelPHP]サンプルデモアプリ「BaseDemoプロジェクト」アップ ※オリジナル認証ドライバ込
- [FuelPHP]opauthライブラリでFacebook認証を速攻実装する
- [FuelPHP]DB::queryの戻り値をModel型で返す方法
- [FuelPHP]opauthライブラリでTwitter認証を速攻実装する
- [FuelPHP]simpleauthのsaltの可変指定について
- [Java]JavaでC#のFuncデリゲートを再現
- [FuelPHP]created_atとupdated_atのUNIXタイムスタンプとdatetime
- 本日の勉強会レポート(WebAPI:校正支援APIについて)
One Response to [FuelPHP]独自オリジナル認証ドライバをsimpleauthを参考に実装する2
コメントを残す コメントをキャンセル
コメントを投稿するにはログインしてください。
coffee-break
Don't write code that useless.
1日5杯はコーヒー、カフェオレ飲みます。狭心症のため安静にします☆松本 雄貴
Kotlinでサービスリリース目指す!
iOSでチャットアプリ作成中。自然言語解析LSIを習得中
Mac / Android・iOS / Rails / Oracle
2017年 Lpic L2取得
2012年 Android技術者資格取得
2010年 OracleMasterGold10g取得
2008年 CCNAQiitaもたまに投稿
https://qiita.com/y-matsumoto東京近郊で常駐開発探してる方はこちらよりご連絡ください
SES企業でパートナー会社を探している企業様はこちらよりご連絡ください
スプリットカメラ iOS / Android
音声認識で聞いた日付から曜日当てアプリ Android
ソーシャルタイマー Android
カテゴリー
- ActiveRecord (2)
- Android (52)
- AndroidStudio (10)
- Ansible (1)
- AWS (1)
- Bash (18)
- Blog (7)
- BootStrap (1)
- CentOS (16)
- Chef (1)
- css (2)
- Eclipse (5)
- error (1)
- Facebook (2)
- Firebase (1)
- FuelPHP (16)
- Git (22)
- GitHub (3)
- Gradle (2)
- GraphAPI (1)
- Grunt (1)
- heroku (2)
- illustrator (1)
- iOS (17)
- Java (4)
- Jenkins (1)
- jQuery (3)
- Kotlin (2)
- Mac (22)
- nginx (1)
- Node.js (3)
- peco (1)
- php (5)
- Python (1)
- Rails (16)
- Ruby (11)
- shell (1)
- SNS (1)
- Swift (2)
- tmux (2)
- Vagrant (6)
- Vim (6)
- windows (2)
- WordPress (3)
- zsh (4)
- フリーランス (1)
- ライブラリ (1)
- 勉強会 (2)
- 宣伝 (1)
- 未分類 (2)
最近の投稿
- [MAC]HighSierraでgitプッシュエラー「Unable to negotiate with xxx.xxx.xxx.xxx port xx: no matching cipher found. Their offer: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se fatal: Could not read from remote repository.」
- [iOS]iOS11からFacebook,Twitter連携(シェアなど)廃止の対応方法
- [iOS]速報2017AppleSpecialEventのiOS11、iPhone8など発表内容について
- [iOS][Firebase]The default Firebase app has not yet been configured. Add `[FIRApp configure];
- [iOS]2017年9月リリースのiOS11で開発者が対応するべきこと
- 今人気の現金化サービスCASH(キャッシュ)を使ったレビュー
- [Pandoc][Mac]pandocでmarkdownからwordファイル作成
- [Android]映画サマーウォーズの聞いた日付(誕生日)から曜日当てをアプリ音声認識で簡単に実現
- [Android]起動しているActivityを取得するadb shell コマンド
- [Android][Kotlin]kotlin学習で参考になるサイト一覧
2023年10月 月 火 水 木 金 土 日 « 5月 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 アーカイブ
- 2018年5月
- 2017年9月
- 2017年8月
- 2017年7月
- 2017年6月
- 2017年5月
- 2017年2月
- 2017年1月
- 2016年12月
- 2016年7月
- 2016年6月
- 2016年1月
- 2015年12月
- 2015年11月
- 2015年10月
- 2015年9月
- 2015年8月
- 2015年7月
- 2015年6月
- 2015年5月
- 2015年4月
- 2015年3月
- 2015年2月
- 2015年1月
- 2014年12月
- 2014年11月
- 2014年6月
- 2014年5月
- 2014年4月
- 2014年3月
- 2014年2月
- 2014年1月
- 2013年12月
- 2013年11月
- 2013年9月
- 2013年8月
- 2013年7月
- 2013年6月
- 2013年5月
- 2013年4月
- 2013年3月
- 2013年2月
- 2013年1月
- 2012年12月
- 2012年10月
- 2012年5月
- 2010年6月
エントリ
[FuelPHP][20121231] / “[FuelPHP]独自オリジナル認証ドライバをsimpleauthを参考に実装する2 | Memorandum blog” http://t.co/8xZq42S5