Created
February 6, 2011 12:15
-
-
Save nissuk/813332 to your computer and use it in GitHub Desktop.
CakePHP(1.3)で認証制限をする単純な例
This file contains hidden or 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}/models/user.php | |
// 1. usernameとpasswordフィールドを持つUserモデルを作成します。 | |
class User extends AppModel | |
{ | |
var $name = 'User'; | |
var $displayField = 'username'; | |
} | |
/* | |
CREATE TABLE users( | |
id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
username varchar(255) NOT NULL, | |
password char(40) NOT NULL | |
) type=InnoDB DEFAULT CHARSET=utf8; | |
*/ |
This file contains hidden or 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}/controllers/users_controller.php | |
class UsersController extends AppController | |
{ | |
var $name = 'Users'; | |
// 2. AuthComponentを使用するように設定します。 | |
var $components = array('Auth'); | |
function beforeFilter() | |
{ | |
// 3. ログインしなくてもアクセスできるアクションを設定します。 | |
$this->Auth->allow('add'); | |
} | |
// (ログインしないとアクセスできないアクション) | |
function index() | |
{ | |
$this->User->recursive = 0; | |
$this->set('users', $this->paginate()); | |
} | |
// (ログインしなくてもアクセスできるアクション) | |
function add() | |
{ | |
if (!empty($this->data)) { | |
$this->User->create(); | |
if ($this->User->save($this->data)) { | |
$this->Session->setFlash(__('The user has been saved', true)); | |
$this->redirect(array('action' => 'index')); | |
} else { | |
$this->Session->setFlash(__('The user could not be saved. Please, try again.', true)); | |
} | |
} | |
} | |
// 4. ログインのためのアクションを設定します。 | |
function login(){} | |
// 5. ログアウトのためのアクションを設定します。 | |
function logout() | |
{ | |
$this->redirect($this->Auth->logout()); | |
} | |
} |
This file contains hidden or 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}/views/users/login.ctp | |
// ログインのためのビューを作成します。 | |
if ($session->check('Message.auth')) $session->flash('auth'); | |
echo $form->create('User', array('action' => 'login')); | |
echo $form->input('username'); | |
echo $form->input('password'); | |
echo $form->end('Login'); |
This file contains hidden or 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}/views/users/add.ctp | |
// ユーザー登録のビューを作成します。 | |
?> | |
<div class="users form"> | |
<?php echo $this->Form->create('User');?> | |
<fieldset> | |
<legend><?php __('Add User'); ?></legend> | |
<?php | |
echo $this->Form->input('username'); | |
echo $this->Form->input('password'); | |
?> | |
</fieldset> | |
<?php echo $this->Form->end(__('Submit', true));?> | |
</div> |
This file contains hidden or 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}/views/users/index.ctp | |
// ユーザー一覧のビューを作成します。 | |
?> | |
<div class="users index"> | |
<h2><?php __('Users');?></h2> | |
<table> | |
<tr> | |
<th><?php echo $this->Paginator->sort('id');?></th> | |
<th><?php echo $this->Paginator->sort('username');?></th> | |
<th><?php echo $this->Paginator->sort('password');?></th> | |
</tr> | |
<?php foreach ($users as $user): ?> | |
<tr> | |
<td><?php echo $user['User']['id']; ?> </td> | |
<td><?php echo $user['User']['username']; ?> </td> | |
<td><?php echo $user['User']['password']; ?> </td> | |
</tr> | |
<?php endforeach; ?> | |
</table> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment