PR

CakePHP – ACL再入門(6)

やっぱりACLは難しい。整理の意味も含めてもう一度入門しておく。(今回はCakePHP1.3.2です)

次にusersコントローラー。

スポンサードリンク

controllers/users_controller.php

<?php
class UsersController extends AppController {

	var $name = 'Users';
	var $helpers = array('Html', 'Form');

	function beforeFilter() {
	    parent::beforeFilter();
	    //$this->Auth->allowedActions = array('*');
	    //$this->Auth->allow = array('*');
	    //$this->initDB();

	    // for password validation
	    if(!empty( $this->data['User']['password1'])){
			$this->data['User']['password'] = $this->data['User']['password1'];
		}

	}

	function initDB() {
	    $group =& $this->User->Group;
	    App::import('Model', 'Group');
	    //$Group = ClassRegistry::init('Group');
	    $group = new Group();

	    // 管理者グループには全てを許可する
	    $group->id = 1;
	    $this->Acl->allow($group, 'controllers');

	    // ユーザグループ
	    $group->id = 2;
	    $this->Acl->deny($group, 'controllers');
	    $this->Acl->allow($group, 'controllers/hoge/indext');
	    $this->Acl->allow($group, 'controllers/Users/logout');
	}

	function login() {
	}

	function logout() {
		$this->Session->setFlash('Good-Bye');
		$this->redirect($this->Auth->logout());
	}

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid User.', true));
			$this->redirect(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->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));
			}
		}
		$groups = $this->User->Group->find('list', array('order' => array('Group.id' => 'desc')));
		$this->set(compact('groups'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			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));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list', array('order' => array('Group.id' => 'desc')));
		$this->set(compact('groups'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for User', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>

コメント

タイトルとURLをコピーしました