CakePHPのACL(Access Control List)の解説を砕いてみる~Auth を追加する準備~
CakePHPのACL(Access Control List)の解説を砕いてみる~アプリケーションの準備~の続き。
ここでやるべきこと。
- UsersControllerにログインとログアウトのアクションを記述する
- AppControllerを追加する
- GroupsControllerを編集
- UsersControllerを編集
UsersControllerにログインとログアウトのアクションを記述しました。
<?php class UsersController extends AppController { var $name = 'Users'; var $helpers = array('Html', 'Form'); //-----add--start--mitsugi-bb------- function login() { } function logout() { } //-----add--end--mitsugi-bb------- function index() { $this->User->recursive = 0; $this->set('users', $this->paginate()); } function view($id = null) { if (!$id) { $this->flash(__('Invalid User', true), array('action'=>'index')); } $this->set('user', $this->User->read(null, $id)); } function add() { if (!empty($this->data)) { $this->User->create(); if ($this->User->save($this->data)) { $this->flash(__('User saved.', true), array('action'=>'index')); } else { } } $groups = $this->User->Group->find('list'); $this->set(compact('groups')); } function edit($id = null) { if (!$id && empty($this->data)) { $this->flash(__('Invalid User', true), array('action'=>'index')); } if (!empty($this->data)) { if ($this->User->save($this->data)) { $this->flash(__('The User has been saved.', true), array('action'=>'index')); } else { } } if (empty($this->data)) { $this->data = $this->User->read(null, $id); } $groups = $this->User->Group->find('list'); $this->set(compact('groups')); } function delete($id = null) { if (!$id) { $this->flash(__('Invalid User', true), array('action'=>'index')); } if ($this->User->del($id)) { $this->flash(__('User deleted', true), array('action'=>'index')); } } } ?>
/cake/libs/controllerからapp_controller.phpをとって来きてソースをに追加する
<?php /* SVN FILE: $Id: app_controller.php 7296 2008-06-27 09:09:03Z gwoo $ */ /** * Short description for file. * * This file is application-wide controller file. You can put all * application-wide controller-related methods here. * * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs.controller * @since CakePHP(tm) v 0.2.9 * @version $Revision: 7296 $ * @modifiedby $LastChangedBy: gwoo $ * @lastmodified $Date: 2008-06-27 02:09:03 -0700 (Fri, 27 Jun 2008) $ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ /** * This is a placeholder class. * Create the same file in app/app_controller.php * * Add your application-wide methods in the class below, your controllers * will inherit them. * * @package cake * @subpackage cake.cake.libs.controller */ class AppController extends Controller { //-----add--start--mitsugi-bb------- var $components = array('Auth', 'Acl'); function beforeFilter() { $this->Auth->authorize = 'actions'; $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login'); $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add'); } //-----add--end--mitsugi-bb------- } ?>
編集したAppController をapp/配下に置く。
GroupsControllerを編集
<?php class GroupsController extends AppController { var $name = 'Groups'; var $helpers = array('Html', 'Form'); //-----add--start--mitsugi-bb------- function beforeFilter() { parent::beforeFilter(); $this->Auth->allowedActions = array('*'); } //-----add--end--mitsugi-bb------- function index() { $this->Group->recursive = 0; $this->set('groups', $this->paginate()); } function view($id = null) { if (!$id) { $this->flash(__('Invalid Group', true), array('action'=>'index')); } $this->set('group', $this->Group->read(null, $id)); } function add() { if (!empty($this->data)) { $this->Group->create(); if ($this->Group->save($this->data)) { $this->flash(__('Group saved.', true), array('action'=>'index')); } else { } } } function edit($id = null) { if (!$id && empty($this->data)) { $this->flash(__('Invalid Group', true), array('action'=>'index')); } if (!empty($this->data)) { if ($this->Group->save($this->data)) { $this->flash(__('The Group has been saved.', true), array('action'=>'index')); } else { } } if (empty($this->data)) { $this->data = $this->Group->read(null, $id); } } function delete($id = null) { if (!$id) { $this->flash(__('Invalid Group', true), array('action'=>'index')); } if ($this->Group->del($id)) { $this->flash(__('Group deleted', true), array('action'=>'index')); } } } ?>
UsersControllerを編集
<?php class UsersController extends AppController { var $name = 'Users'; var $helpers = array('Html', 'Form'); //-----add--start--mitsugi-bb------- function beforeFilter() { parent::beforeFilter(); $this->Auth->allowedActions = array('*'); } //-----add--end--mitsugi-bb------- function login() { } function logout() { } function index() { $this->User->recursive = 0; $this->set('users', $this->paginate()); } function view($id = null) { if (!$id) { $this->flash(__('Invalid User', true), array('action'=>'index')); } $this->set('user', $this->User->read(null, $id)); } function add() { if (!empty($this->data)) { $this->User->create(); if ($this->User->save($this->data)) { $this->flash(__('User saved.', true), array('action'=>'index')); } else { } } $groups = $this->User->Group->find('list'); $this->set(compact('groups')); } function edit($id = null) { if (!$id && empty($this->data)) { $this->flash(__('Invalid User', true), array('action'=>'index')); } if (!empty($this->data)) { if ($this->User->save($this->data)) { $this->flash(__('The User has been saved.', true), array('action'=>'index')); } else { } } if (empty($this->data)) { $this->data = $this->User->read(null, $id); } $groups = $this->User->Group->find('list'); $this->set(compact('groups')); } function delete($id = null) { if (!$id) { $this->flash(__('Invalid User', true), array('action'=>'index')); } if ($this->User->del($id)) { $this->flash(__('User deleted', true), array('action'=>'index')); } } } ?>
ちなみに、GroupsControllerとUsersControllerに追加したコードは、データベースにログインできるユーザとグループを作成したら削除するみたいです。
この状態でhttp://localhost:8080/にアクセスしたら、http://localhost:8080/users/loginにリダイレクトしました。
次のページへ進む。