Authentication not works when users are stored in alternative table

為{幸葍}努か 提交于 2019-12-13 04:39:41

问题


I have problems with development of CakePHP2's authentication system, where users are stored in database table participants (not users, like usual).

It simply does not authenticate participant.

Table participants have next structure:

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`)
)

File AppController.php have content like this:

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');
    }
}

File ParticipantsController.php have content like:

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) );

        }

    }

I don't know what is wrong here, can you please help me what I'm missing here?


回答1:


I think it might be your configuration of fields and userModel

$this->Auth->fields

My working code is closer to :

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



回答2:


Try this:

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

Why your functions is not public in controllers?



来源:https://stackoverflow.com/questions/22990023/authentication-not-works-when-users-are-stored-in-alternative-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!