Monday, August 24, 2015

Authentication with multiple tables (using myAuthComponent) doesn't work in Cakephp 3.0

Hello Everyone,

Since i've started my project from the analysis and not from coding, i came to diagram models with 3 tables that need an authentication.
For these reason i don't want to use one 'users' table, but 3 of them: 'clients', 'administrators' and 'employers'.

I tried to follow the solution with myAuthComponent (which extends basic AuthComponent) of this post on stack overflow http://stackoverflow.com/questions/2973620/cakephp-auth-with-multiple-users-tables

So..
-i implemented the AppController.php by loading myAuthComponent omitting the 'usermodel'
-i created the login.ctp under the 'Pages' folder, it has a dropdown to make the selection of the model
-this is the login function on the PagesController.php:

public function login()
   
{
       
if ($this->request->is('post')) {
           
if($this->request->data['type'] == NULL){
                $this
->Flash->error(__('Login Failed. Please, choose one type of user and try again.'));
               
return;
           
}
           
            $user
= $this->AppAuth->identify($this->request);
            debug
("i don't reach this");
           
/* if ($client) {
                $this->Auth->setUser($client);
                $this->Flash->success(__('Welcome on ArchiGest'));
                return $this->redirect(['controller' => 'Exams', 'action' => 'index']);
            }
            else if ($client) {
                $this->Auth->setUser($client);
                $this->Flash->success(__('Welcome on ArchiGest'));
                return $this->redirect(['controller' => 'Exams', 'action' => 'index']);
            }
            else if ($client) {
                $this->Auth->setUser($client);
                $this->Flash->success(__('Welcome on ArchiGest'));
                return $this->redirect(['controller' => 'Exams', 'action' => 'index']);
            }
            $this->Flash->error(__('Invalid username or password, try again'));*/

       
}
   
}      


and this is MyAuthComponent.php:

<?php
namespace App\Controller\Component;

use Cake\Core\App;
use Cake\Controller\Component;
use Cake\Controller\Component\AuthComponent;

//App::import('Component', 'AuthComponent');
class AppAuthComponent extends AuthComponent {

   
function identify($user = null, $conditions = null) {
       
       
if($user->data["type"] == 0){
           
            $this
->userModel = "Client";
            $this
->_config["authenticate"]["Form"]["userModel"] = "Client";
            debug
($this);
            debug
($this->_config);
            $result
= parent::identify($user, $conditions); // let cake do it's thing
           
if ($result) {
                debug
("don't reach this");
               
return $result; // login success
           
}
       
}
       
else if($user->data["type"] == 1){
            $this
->userModel = "Employer";
            $result
= parent::identify($user, $conditions); // let cake do it's thing
           
if ($result) {
               
return $result; // login success
           
}
       
}
       
else if($user->data["type"] == 2){
            $this
->userModel = "Administrator";
            $result
= parent::identify($user, $conditions); // let cake do it's thing
           
if ($result) {
               
return $result; // login success
           
}
       
}
       
return null; // login failure
   
}
}


I got stuck on changing the usermodel from 'users' to 'clients', 'administrators' or 'employers' on-the-fly by having this error: "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'archigest.users' doesn't exist".

Can someone please free this poor student from the swamp?

thanks in advance

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: