Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2 separate login tables

I have a Cake website and it needs to have two separate logins, each one will have their own login form and see different pages, it would be nice to have two different tables because there are no similarities between the two types of people.

Each login form will only be used by certain people and they will never login to the other form, and vice versa.

Also, the two login tables have a relationship between them, which requires 2 tables?

Is this possible?

like image 936
472084 Avatar asked Jun 21 '12 09:06

472084


3 Answers

First, add a couple of empty custom authenticate objects. We'll reuse the same logic that FormAuthenticate uses (that is, uses POST data to check the database for a user), but simply change the model within the object settings (later).

app/Controller/Component/Auth/ModelOneAuthenticate.php

<?php
App::uses('FormAuthenticate', 'Controller/Component/Auth');

class ModelOneAuthenticate extends FormAuthenticate {
}

app/Controller/Component/Auth/ModelTwoAuthenticate.php

<?php
App::uses('FormAuthenticate', 'Controller/Component/Auth');

class ModelTwoAuthenticate extends FormAuthenticate {
}

Then tell your app to use these objects to authenticate, and tell it what model to use. You can also customize the fields here. In your AppController:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'ModelOne' => array(
                'userModel' => 'ModelOne',
                'fields' => array(
                    'username' => 'my_custom_username_field',
                    'password' => 'some_password_field'
                )
            ),
            'ModelTwo' => array(
                'userModel' => 'ModelTwo'
            )
        )
    )
);

The first authentication object would check the model_ones table for a username in my_custom_username_field and password in some_password_field, while the second one would check model_twos using the standard username and password fields.

like image 73
jeremyharris Avatar answered Sep 22 '22 10:09

jeremyharris


The simplest way to do this is to just set a different session key for each login type:

if ($loginTypeOne) {
  $this->Auth->authenticate = array(
    'Form'=> array(
      'userModel'=> 'TypeOne',
      )
    );
  AuthComponent::$sessionKey = 'Auth.TypeOne';
} else {
  $this->Auth->authenticate = array(
    'Form'=> array(
      'userModel'=> 'TypeTwo',
      )
    );
  AuthComponent::$sessionKey = 'Auth.TypeTwo';  
}
like image 2
Singletonio Avatar answered Sep 21 '22 10:09

Singletonio


When they have to login there is a similarity: Both will require it to enter credentials, usually an username/email and password. So a users table and a foo_profiles table and a bar_profiles table depending on the user type should work also.

If you really want to go with two total different tables and the MVC stack for them, then simply use two different controllers FooUsers and BarUsers and inside of each create a customized login method.

like image 1
floriank Avatar answered Sep 22 '22 10:09

floriank