CakePHP 2 separate login tables

↘锁芯ラ 提交于 2020-01-10 15:27:12

问题


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?


回答1:


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.




回答2:


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



回答3:


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.




回答4:


I have done this previously by writing custom Authentication components that extend from BaseAuthenticate. As long as they implement the authenticate() method then you'll be able to do whatever you want to each of the different types of user.

In your AppController you need to register the different components by doing something like

    public $components = array(
    "Session",
    "Auth" => array(
        'authenticate'      => array("UserType1", "UserType2"),
    )
);

Check out the cookbook for the rest of it.




回答5:


You can have a look on this.
Define Model for both login member and then define table which you want to use for the user.
set variable in model.


class SearchedCategory extends AppModel {  
    var $name = 'SearchedCategory';  
    Var useTable = 'give your table name here.';
    var $primaryKey = 'id';


}


来源:https://stackoverflow.com/questions/11135066/cakephp-2-separate-login-tables

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