Created
October 2, 2012 07:05
-
-
Save zaru/3816960 to your computer and use it in GitHub Desktop.
CakePHP2.x + Authコンポーネントで別々の認証を使う方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('AppController', 'Controller'); | |
class AdminController extends AppController { | |
public $name = 'Admin'; | |
public $uses = array('Administrator'); | |
public $components = array( | |
'Session', | |
'Auth' => array( | |
'loginRedirect' => Array('controller' => 'admin', 'action' => 'index'), | |
'logoutRedirect' => Array('controller' => 'admin', 'action' => 'login'), | |
'loginAction' => Array('controller' => 'admin', 'action' => 'login'), | |
'authenticate' => Array('Form' => Array('userModel' => 'Administrator')) | |
) | |
); | |
public function beforeFilter(){ | |
parent::beforeFilter(); | |
$this->Auth->allow('login','register'); | |
//ここでセッションキーを分けることで、認証を別にする | |
AuthComponent::$sessionKey = "Auth.AdminUser"; | |
} | |
public function index(){ | |
} | |
public function login(){ | |
if ($this->request->is('post')) { | |
if ($this->Auth->login()) { | |
return $this->redirect($this->Auth->redirect()); | |
} else { | |
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); | |
} | |
} | |
} | |
public function logout(){ | |
$this->redirect($this->Auth->logout()); | |
} | |
public function register(){ | |
if ($this->request->is('post')) { | |
if ($this->Administrator->save($this->request->data)) { | |
$id = $this->Administrator->id; | |
$this->request->data['Administrator'] = array_merge($this->request->data['Administrator'], array('id' => $id)); | |
$this->Auth->login($this->request->data['Administrator']); | |
$this->redirect('/admin/'); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Administrator extends AppModel { | |
public function beforeSave($options = array()) { | |
$this->data['Administrator']['password'] = AuthComponent::password($this->data['Administrator']['password']); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User extends AppModel { | |
public function beforeSave($options = array()) { | |
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('AppController', 'Controller'); | |
class UserController extends AppController { | |
public $name = 'User'; | |
public $uses = array('User'); | |
public $components = array( | |
'Session', | |
'Auth' => array( | |
'loginRedirect' => Array('controller' => 'user', 'action' => 'index'), | |
'logoutRedirect' => Array('controller' => 'user', 'action' => 'login'), | |
'loginAction' => Array('controller' => 'user', 'action' => 'login'), | |
'authenticate' => Array('Form' => Array('userModel' => 'User')) | |
) | |
); | |
public function beforeFilter(){ | |
parent::beforeFilter(); | |
$this->Auth->allow('login','register'); | |
//ここでセッションキーを分けることで、認証を別にする | |
AuthComponent::$sessionKey = "Auth.NormalUser"; | |
} | |
public function index(){ | |
} | |
public function login(){ | |
if ($this->request->is('post')) { | |
if ($this->Auth->login()) { | |
return $this->redirect($this->Auth->redirect()); | |
} else { | |
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); | |
} | |
} | |
} | |
public function logout(){ | |
$this->redirect($this->Auth->logout()); | |
} | |
public function register(){ | |
if ($this->request->is('post')) { | |
if ($this->User->save($this->request->data)) { | |
$id = $this->User->id; | |
$this->request->data['User'] = array_merge($this->request->data['User'], array('id' => $id)); | |
$this->Auth->login($this->request->data['User']); | |
$this->redirect('/user/'); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment