これはできなかった記事です。ブラウザバック!
要件
- ゲストユーザー用のログインフォーム
- 管理ユーザー用のログインフォーム
- この2つはそれぞれ保存されているパスワードが違う
テーブルはユーザーのタイプフィールドを用意してもよかったんだけど、「もしバグを発生させて、管理者ユーザーがバレてしまったら・・・((((;゚Д゚))))ガクガクブルブル」ってな感じで、セキュリティ的にどうなのよ? を、考慮したかったので、2つのテーブルを利用することに。
だけど、結局出来なかったのね(´・ω・`)
他のサイトを探し回した結果のコピペしただけだから、出来なかったかもしれないけど。
ぶっちゃけ全然理解できていない。初めて試したころからそうだけどAuthコンポーネントってなんか難しいのよ。
とりあえず結果内容として試した内容をメモ。※絶対参考にならない自信があるので、申し訳ないですが、ブラウザバックしてください(´・ω・`)
試したメモ
AppController.php
<?php App::uses('Controller', 'Controller'); class AppController extends Controller { public $components = array( 'DebugKit.Toolbar', 'Session', 'Auth' => array( 'loginAction' => array('controller' => 'user_sites', 'action' => 'login'), //ログインを行なうaction 'loginRedirect' => array('controller' => 'user_sites', 'action' => 'index'), //ログイン後のページ 'logoutRedirect' => array('controller' => 'user_sites', 'action' => 'index'), //ログアウト後のページ 'authError' =>'ログインして下さい。', 'authenticate' => array( 'Form' => array( 'userModel' => 'User', //ユーザー情報のモデル 'fields' => array('username' => 'name','password' => 'password') ) ) ) ); // ここに管理者用のAuthを記述 public function beforeFilter(){ // reqeustの中に「admin」が存在したら管理者ユーザーとして処理 if(isset($this->request->params['admin'])){ $this->Auth->authenticate = array( 'Form' => array( 'userModel' => 'Admin', // 管理者ユーザーのモデル 'fields' => array('username' => 'name','password'=>'password') ) ); $this->Auth->loginAction = array('controller' => 'admin_sites', 'action' => 'index', 'admin' => true); $this->Auth->loginRedirect = array('controller' => 'admin_sites', 'action' => 'index', 'admin' => true); $this->Auth->logoutRedirect = array('controller' => 'admin_sites', 'action' => 'index', 'admin' => true); AuthComponent::$sessionKey = 'Auth.Admin'; if($this->Auth->user()){ $this->loadModel('Admin'); $loginUser = $this->Admin->find('first',array( 'conditions' => array( 'id' => $this->Auth->user('id'), 'is_login' => 1, 'is_delete'=> 0 ), 'fields' => array('id','name','auth'), 'contain' => false )); if(empty($loginUser)){ $this->Auth->logout(); $this->redirect('/admin/'); } $this->set(compact('loginUser')); } $this->layout = 'admin'; } } }
AdminsController.php
<?php 〜 省略 〜 class AdminsController extends AppController { 〜 省略 〜 function beforeFilter(){ parent::beforeFilter(); } public function admin_index(){ if(! ($this->request->is('post') && empty($this->request->data['Admin']))) { // エラーどす $this->redirect($this->Auth->redirect()); } if(! $this->Auth->login()){ // エラーどす $this->Session->setFlash("ログインエラーです。",'default',array('class'=>'msg-error')); $this->redirect($this->Auth->redirect()); } // 他ユーザーがログインしていないか確認する if($this->Auth->user('is_login')){ /* 何かしらの処理 */ } else { /* 指定のユーザーでなければログアウト*/ $this->Auth->logout(); } $this->redirect($this->Auth->redirect()); } public function admin_logout(){ $this->redirect($this->Auth->logout()); } 〜 省略 〜