CakePHP 2 separate login tables

后端 未结 5 1752
醉梦人生
醉梦人生 2021-01-01 03:24

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 table

相关标签:
5条回答
  • 2021-01-01 03:45

    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.

    0 讨论(0)
  • 2021-01-01 03:49

    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';  
    }
    
    0 讨论(0)
  • 2021-01-01 03:49

    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.

    0 讨论(0)
  • 2021-01-01 03:50

    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.

    0 讨论(0)
  • 2021-01-01 03:53
    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';
    
    
    }
    
    0 讨论(0)
提交回复
热议问题