【问题标题】:Authentication not works when users are stored in alternative table当用户存储在备用表中时,身份验证不起作用
【发布时间】:2014-04-10 13:51:30
【问题描述】:

我在开发 CakePHP2 的身份验证系统时遇到问题,其中用户存储在数据库表 participants 中(不是像往常一样 users)。

它根本不对参与者进行身份验证。

participants 具有下一个结构:

CREATE TABLE IF NOT EXISTS `participants` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `confirmed` tinyint(1) NOT NULL DEFAULT '0',
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `password` char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
  `token` char(32) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
)

文件 AppController.php 的内容如下:

App::uses('Controller', 'Controller');

class AppController extends Controller {
    public $components = array('Cookie', 'Session', 'Auth');
    function beforeFilter() {
        $this->Auth->userModel = 'Participant';
        $this->Auth->fields = array('username' => 'name', 'password' => 'password');
        $this->Auth->loginAction = array('controller' => 'participants', 'action' => 'login');
        //$this->Auth->logoutRedirect = array('controller' => 'participants', 'action' => 'logout'); // i will use this later
        //$this->Auth->loginRedirect = array('controller' => 'participants', 'action' => 'index');
    }
}

文件 ParticipantsController.php 的内容如下:

App::uses('AppController', 'Controller');

    class ParticipantsController extends AppController {

        function beforeFilter() {
            parent::beforeFilter(); 
      $this->Auth->allowedActions = array('registration', 'login', 'forgotten', 'recreate', 'confirm');
        }

        function login() {

            if ($this->Auth->login()) {

            $this->redirect(array('controller' => 'participants', 'action'=>'view'));

        } else {
            // it always end-up here
            //pr($this->data);
            //pr(AuthComponent::password($this->data['Participant']['password']));
            //exit;

            $this->Session->setFlash( __('Error, please try again.', true) );

        }

    }

我不知道这里出了什么问题,你能帮我看看我在这里缺少什么吗?

【问题讨论】:

    标签: cakephp authentication login cakephp-2.0


    【解决方案1】:

    我认为这可能是您对字段和 userModel 的配置

    $this->Auth->fields

    我的工作代码更接近:

    $this->Auth->authenticate = array(
        'Form' => array('userModel' => 'Participant'
                       , 'fields' => array('username' => 'name', 'password' => 'password')
                       )
    );
    

    【讨论】:

      【解决方案2】:

      试试这个:

         public function beforeFilter() {
               parent::beforeFilter(); 
               $this->Auth->allow(array('registration', 'login', 
                      'forgotten', 'recreate', 'confirm'));
          }
      

      为什么你的函数在控制器中不公开?

      【讨论】:

      • 不,还是一样 - $this->Auth->login() 在这种情况下不进行身份验证。由于应用程序处于调试模式,看起来根本没有执行任何 sql 查询,因为布局的底部页面中没有 sql 查询列表...
      猜你喜欢
      • 2015-07-13
      • 2019-10-15
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多