Session 通过会话处理登录操作的权限

Session 通过会话处理登录操作的权限,session,cakephp,permissions,login,Session,Cakephp,Permissions,Login,我正在构建一个cakePHP应用程序,学生可以像配置文件一样管理自己的数据。 我不让其他学生参与“查看”操作的想法是: students\u controller.php function view($id = null) { //We check the permissions so a student cannot browse other student's pages $studentid=$this->Student->field('

我正在构建一个cakePHP应用程序,学生可以像配置文件一样管理自己的数据。 我不让其他学生参与“查看”操作的想法是: students\u 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));
    }
    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日志会说,从学生中选择student.id作为学生,其中用户\u id为NULL LIMIT 1,所以它没有获取用户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'));
        }
   }

假设档案是学生模型,为什么你不能这么做呢

class StudentsController extends Model {
 function view( $id = null ) {
  if( $this->Auth->User('id') != $id ) // bad, redirect them to safe page
   $this->redirect( '/badaccess/denied' );
 }
}

由于$id是学生档案的id,只需对照登录用户的id进行检查。

不确定这是否有帮助,但我目前正在开发一个系统,需要在登录时提供额外信息。在我的例子中,它来自同一个users表,只是额外的字段,但可以与其他表/模型一起使用。我假设您必须输入var$uses=array'User',Student';在顶端做这件事

在用户控制器中,我有以下内容:

function login(){
    ...
        $this->Session->write('Auth.User.type', $this->User->field('type'));
        $this->Session->write('Auth.User.isapproved', 
          $this->User->field('isapproved'));
    ...
}
显然,你必须把这些改成

$this->Session->write('Auth.User.student_id',
    $this->User->Student->field('id',array('user_id'=>$this->User->field('id')));
或者类似的东西

如果$this->Auth->User'student\u id',那么您应该能够访问它$id,与上面的答案类似


我不确定这是否有效,因为当我无意中发现这一点时,我正在寻找解决我自己问题的建议,所以我没有时间试着看看我是否做对了:

你好!对不起,我迟到了。我试过了,但它似乎在登录页面上不起作用,因为它没有得到会话信息,我猜,sql日志说,从学生中选择Student.id作为学生,其中user\u id为NULL LIMIT 1,所以它没有得到user\u id。就像我发布的代码一样。登录后工作正常,问题是在登录时,因为我正在重定向到查看页面。谢谢你的回答看起来你的名字不匹配。Cake知道您正在使用Student表作为登录表吗?您是否有10万%的把握始终为所有内容命名?当您将数据发布到login函数时,请检查名称和值以再次检查它们是否匹配。实际上,我使用users表进行登录,还使用students表检查学生是否已经填写了一些数据。所以,我对登录使用了这两个表。我必须告诉cake我在做这个吗?不,我想让用户的桌子单独呆着。让这成为您的身份验证机制。然后,在成功登录或AppController::beforeFilter添加页面的控制器操作中,添加您需要对students表执行的任何检查。也就是说,让他们登录,然后你的学生才能进行检查。顺便说一句,您的SQL日志输出表明您已经告诉cake检查学生表的登录,而不是用户-尝试切换回。我认为此输出只是获取学生的信息,因为我正在重定向到学生页面,但它与登录无关。我想我终于明白你的意思了,所以我把你建议的代码放在了AppController的PreforeFilter中,但它仍然不起作用。我尝试了很多方法,但它说页面不是每次都能很好地重定向。我用beforeFilter编辑了这个问题。真的,非常感谢你的帮助。
$this->Session->write('Auth.User.student_id',
    $this->User->Student->field('id',array('user_id'=>$this->User->field('id')));