CakePHP 2.0.6 でAuthコンポーネントの使用。
公式のチュートリアルにある、「ブログの作り方」を参考にしたところ、できました。
■Simple Authentication and Authorization Application — Cookbook v2.x documentation
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
なお、↑このページの「Authorization (who’s allowed to access what)」以降はブログを作るため専用の設定なので、ここでは参考にしていません。
参考までに、マニュアルの方も。
■Authentication — Cookbook v2.x documentation
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
バグではなかったようで一安心。
なお、今回はcakeのルートフォルダを「cake2_0」にしました。
テーブル作成
CREATE TABLE users (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(50),
role VARCHAR(20),
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
app/Controller/UsersController.php
<?php
// app/Controller/UsersController.php
class UsersController extends AppController {
public function index() {
$this->redirect("login");
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('不正なユーザ名またはパスワードです。もう一度やり直してください。'));
}
}
}
public function welcome() {
}
public function logout() {
$this->redirect($this->Auth->logout());
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'index', 'login', 'logout');
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
}
app/Controller/AppController.php
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'welcome'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
)
);
public function beforeFilter() {
}
}
app/Model/User.php
<?php
// app/Model/User.php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
)
);
public function beforeSave() {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
app/View/Users/add.ctp
<!-- app/View/Users/add.ctp -->
<div class="users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('role', array(
'options' => array('admin' => 'Admin', 'author' => 'Author')
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
app/View/Users/welcome.ctp
ようこそ <form action="/cake2_0/users/logout" method="post"> <input type="submit" value="ログアウト" /> </form>
app/View/Users/login.ctp
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo __('ユーザ名とパスワードを入力してください。'); ?></legend>
<?php
echo $this->Form->input('username', array('label'=>'ユーザ名'));
echo $this->Form->input('password', array('label'=>'パスワード'));
?>
</fieldset>
<?php echo $this->Form->end(__('Login'));?>
</div>
app/View/Users/logout.ctp
空にしておく。
app/View/Users/index.ctp
空にしておく。