私は学生が自分のデータ(プロファイルのような)を管理できるcakePHPアプリケーションを構築しています。 「ビュー」アクションに他の学生へのアクセスを拒否するために私の考えはこれです: (students_controller.php)セッションによるログインアクションからのアクセス許可を処理します。
function view($id = null) {
//We check the permissions so a student cannot browse other student's pages
$studentid=$this->Student->field('id',array('user_id'=>$this->Session->read('Auth.User.id')));
if (!$id) {
$this->Session->setFlash(__('Invalid student', true));
$this->redirect(array('action' => 'add'));
}
if ($this->Auth->user('id') <> ($this->data['Student']['user_id'])){
$this->Session->setFlash(__('You are not authorized to access this page,not allowed', true));
$this->redirect(array('controller' => 'users','action' => 'logout'));
}
$this->set('student', $this->Student->read(null, $id));
}
私のログインアクションで、私はこのコードを持っているので、問題は(私が推測する)来る: (users_controller.php)
function login() {
$studentid= $this->User->Student->field('id',array('user_id'=>$this->Session->read('Auth.User.id')));
$studentformactivated=$this->User->Student->field('form_activated',array('user_id'=>$this->Session->read('Auth.User.id')));
if ($this->Session->read('Auth.User')) {
if($this->Session->read('Auth.User.group_id') == '1')
$this->redirect(array('controller'=>'specializations', 'action' => 'index'));
else if ($this->Session->read('Auth.User.group_id') == '2')
$this->redirect(array('controller'=>'specializations', 'action' => 'index'));
else if ($this->Session->read('Auth.User.group_id') == '3')
if ($studentformactivated == '1')
$this->redirect(array('controller'=>'students', 'action' => 'view',$studentid));
else
$this->redirect(array('controller'=>'students', 'action' => 'add'));
$this->Session->setFlash('You are logged in!');
$this->redirect('/', null, false);
}
}
ので、(「追加」アクションが行われた場合)「ビュー」アクションにリダイレクトされ、ビューのアクションで、我々はセッションからの情報を取っているでは毎回学生ログイン、それはログインアクションを行っている間にこの情報を取得できないようです(SQLログにはSELECT Student
。 id
FROM students
AS Student
WHERE user_id
IS NULL LIMIT 1だから、user_idを取得していません)。
さて、質問:それは私がそれを行う初めてだと、おそらくがあるので、すべての
まず、私は、あなたが許可制御を管理するこの方法についてどう思いますか知りたいのですがそれを行う他の簡単な方法。
ログイン操作の直後にユーザーセッションから情報を取得できないとすればいいですか?
if (user_is_logged_in){ if ($this->Auth->user('id') <> ($this->data['Student']['user_id'])){ $this->Session->setFlash(__('You are not authorized to access this page,not allowed', true)); $this->redirect(array('controller' => 'users','action' => 'logout')); } }
それは私がそれを解決するために見るだけのことだ:
はビュー機能でのような何かを行うには良い解決策になります。しかし、私は(user_is_logged_in)条件のために($ this-> Session-> check( 'Auth.User'))しようとしましたが、うまくいきませんでした... ありがとうございました! ALf。 ----------------------------------------- EDIT ----- -------------------------- これについて議論したところ、ソリューションはAppController beforeFilterにコードを追加しているようです。それはまだよくリダイレクトされていない、おそらく引数が渡されたためですか?私は考えがなくなった...
こんにちは!遅れて申し訳ありません。私はこれを試しましたが、それはセッション情報を取得しないため、ログインページからは動作していないようです(私は推測すると、SQLログはSELECT Student.id FROM studentsと同じです。 user_idを取得していません)。私が掲示したコードのように。一度ログインすれば正常に動作しますが、問題はログイン中です。ビューページにリダイレクトしているためです。お返事ありがとうございます – alfizqu
あなたの名前に不一致があるようです。 CakeはあなたがログインテーブルとしてStudentテーブルを使用していることを知っていますか?一貫してすべての名前を付けていることを100000%確信していますか?ログイン機能にデータを投稿するときは、名前と値が一致しているかどうかを再度チェックしてください。 –
実際には、私はログインのためにユーザーテーブルを使用していますが、学生が既にいくつかのデータを記入しているかどうかを確認するための学生テーブルもあります。したがって、私は両方のテーブルをログインに対して使用しています。私はこれをやっているケーキに言わなければならないのですか? – alfizqu