0

看完帖子:无密码登录

我做了一个个人尝试:

应用控制器:

function beforeFilter(){
$this->Auth->loginError = "This message shows up when the wrong credentials are 
 used";       
 //$this->Auth->authError = "This error shows up with the user tries to access a part
 of the website that is protected."; 
    //$this->Auth->authError = "";
    $this->Auth->fields = array(
        'username' => 'username',
        'password' =>   null 
   );

用户控制器,里面add()

// Save new user
   if ($this->User->save(array('username' => $this->request->data['User']['username'],
          'password' => $this->Auth->password(null),
          'name' => $this->request->data['User']['name'],
          'surname' => $this->request->data['User']['surname'],
          'chosenLayout' => $this->request->data['User']['chosenLayout'],
          'dateCreated' => $this->request->data['User']['dateCreated'],
          'dateModified' => $this->request->data['User']['dateModified'],
          'role_id' =>$this->request->data['User']['role_id']        
   ))) {

    $this->Session->setFlash(__('message_success_user_added', 
    array($this->request->data['User']['username'])), 'default', array(), 'success');
    $this->redirect(array('action' => 'index'));
   } 
   else {
    // Validation error
 $this->Session->setFlash(__('message_fail_validation'), 'default', array(), 'fail');
   }

然后以管理员身份输入并创建一些具有空密码或随机密码的虚拟用户。检查数据库加密密码是否相同(散列的空字符串),这意味着add()函数中的修改有效......

内部用户控制器login()

// Login User
public function login() {

    // Check if the user is already logged in
    if ($this->Session->check('Auth.User.id')){ 

        // Redirect to login page
        $this->redirect($this->Auth->loginRedirect); 
    }
    else{
        // If the user is not logged in

        session_set_cookie_params(0); 

        // If the request is a POST request
        if ($this->request->is('post')) { 
            //get credentials
            $this->username = $this->request->data['User']['username'];
            $this->password = $this->request->data['User']['password'];
            $this->domain = $this->request->data['User']['domain'];
            //debug($this->username);
            debug($this->domain) ;
            //Check if username exists in local DB 
            //debug($this->User->findByUsername( $this->username ));
            if ($this->Auth->login(
                          array(
                           'username'=> $this->username,
                            'password'=> null)

                         )){

                //   debug($this->Auth->login(array(
                  //          'username'=> $this->username,
                    //        'password'=> null
                        // )));

    // Successful login
    // Get all the user information and store in Session
    //debug($this->Auth);
$this->User->id = $this->Auth->user('id');
    debug($this->User->id);
    debug($this->User);
$this->User->contain(array('User', 'Role' => array('Ui', 'Action.name')));
    $this->Session->write('User', $this->User->read());

        $actions = array();
foreach ($this->Session->read('User.Role.Action') as $key => $value){
array_push($actions, $value['name']);
}
$this->Session->write('User.Role.Action', $actions);
debug($actions);

// Render different layout depending on user type

   if($this->Session->read('User.Role.Ui.name') == Configure::read('usertype.msp')){

   $this->Session->write('SessionValues.ui', Configure::read('usertype.msp'));
$this->Auth->loginRedirect = array('controller' => 'PortStats', 'action' => 
   'index');
    }
else if($this->Session->read('User.Role.Ui.name') == 
    Configure::read('usertype.tsc')){

    $this->Session->write('SessionValues.ui', Configure::read('usertype.tsc'));
$this->Auth->loginRedirect = array('controller' => 'PortStats', 'action' => 
    'index');
                        }
else if($this->Session->read('User.Role.Ui.name') == 
    Configure::read('usertype.superAdminUserType')){
$this->Auth->loginRedirect = array('controller' => 'Uis', 'action' => 'index');
                        }

                    // Redirect to main login page
                $this->redirect($this->Auth->loginRedirect);

                    }
                    else {
                    // Failed login user
                    session_destroy();
                    $this->Session->setFlash(__('Login failed: 
          access not granted'), 'default', array(), 'fail');

                    }

                }
    }
}

然后我尝试用我的新用户登录。我收到登录失败的消息。这意味着$this->Auth->login返回false

它一定很容易,但是出了点问题。

同时我的调试跟踪:

警告 (2):为 foreach() [APP\Controller\UsersController.php,第 85 行] 提供的参数无效

4

1 回答 1

1

简化它。这不是登录,而是注册过程,所以不要混淆这两个完全不同的东西。

你刚才

  • 创建用户,包括正确的验证
  • 成功使用 Auth->login($user['User']) 手动设置身份验证会话
  • 只有在此处注册后手动重定向到您希望用户去的地方

有关实时示例,请参见https://github.com/dereuromark/cakefest/blob/master/Controller/AccountController.php#L166

于 2013-09-04T12:07:47.380 回答