#26

寄り道
続き
・すべてのアクションを実行する前にログインをしているかどうかの判定を行う。
users_controller.php

<?php
class UsersController extends AppController
{
	var $name = 'Users';
	var $uses = array('User','Seminar');
	var $needAuth=true;//認証を必須とする。

	function index (){
//		$this->checkSession();
		$auth=$this->Session->read('auth');
		$this->set('me', $this->User->findById($auth['id']));//これだけで関連するseminar情報も取得できるらしい

	}
	



	function login(){
		$this->pageTitle = 'Welcome to Papazy!';
		$this->set('error', false);
		if (!empty($this->data)){
		
			$cond=array(
									'email'=>$this->data['User']['email'],
									'pwd'=>$this->data['User']['pwd']
									);
									
			$data=$this->User->findAll($cond);
			if(count($data)==0){//NG
				$this->log("LOGINエラー",LOG_ERROR);
				$this->set('error', true);
			}
			else{//OK
//セッションにログイン情報を格納する。
				$this->Session->write('auth', $data[0]['User']);
				$this->log("LOGINユーザー:".$data[0]['User']['email'],LOG_DEBUG);
				$this->flash("{$data[0]['User']['name']}さん、こんにちは。","/users/index/");
			}

/*
			$someone = $this->User->findByEmail($this->data['User']['email']);
			if(!empty($someone['User']['pwd']) && $someone['User']['pwd'] == $this->data['User']['pwd']){
				$this->Session->write('my_id', $someone['User']['id']);
				$this->log("LOGINユーザー:".$this->data['User']['email'],LOG_DEBUG);
				$this->redirect('/users/index/');
			}
			else{
				$this->log("LOGINエラー",LOG_ERROR);
				$this->set('error', true);
			}
*/
		}
	}

	function logout(){
		$this->Session->delete('auth');
		$this->flash("さようなら。","/users/login/");
	}

}
?>


app_controller.php

<?php
class AppController extends Controller
{


	var $needAuth=false;//ログインを必須とするかどうかのフラグ
	
	
/*
	function checkSession(){
		// If the session info hasn't been set...
		if (!$this->Session->check('auth')){
			// Force the user to login
			$this->redirect('/users/login');
			exit();
		}
	}
*/
	
	function beforeFilter(){
		//セッションから取り出したログイン情報をセット
		$auth=$this->Session->read('auth');
		$this->set("auth",$auth);
		
		//ログイン必須の機能でログインされていない場合はログイン画面に転送
		if($this->needAuth){
			if(empty($auth)){
				$this->redirect("/users/login");
				return;
			}
		}
	}
}
?>

以上の修正をしたところ、動かなくなっちゃいました。

疲れたので、また明日。


firefoxで確認したら
「ページのリダイレクト設定が正しくありません
このアドレスへのリクエストに対するサーバのリダイレクト設定がループしています。」
と出ました。

明日考えよう!