kohanakohana-3.2kohana-auth

Multiple Auth drivers in kohana3.2


I'm working on a project where I'm trying to implement authentication against external user base for customers, this seems to be working correctly.

Recently there has been added another requirement that some people (not present in the aforementioned base) need to be able to edit parts of pages' content. First thing that comes to mind is to have separate ORM/File Auth driver enabled for those few editors to be able to authenticate them separately.

Is it possible to use two Auth drivers at the same time in Kohana 3.2?


Solution

  • Yes, you can use different drivers at once. Just create another instance instead of standard singleton:

    // default Auth
    $config = Kohana::$config->load('auth');
    $auth = new Auth($config);
    $user = $auth->get_user();
    // special Auth for administration
    $config2 = Kohana::$config->load('admin_auth');
    $auth2 = new Auth($config2);
    $admin = $auth2->get_user();
    

    Restrictions:

    1. You must use differ configs (driver and session_key values must differ). Note that some settings are defined in classes and cant be changed by config (for example, "remember" cookie named authautologin).
    2. You cant share default ORM models (Model_User, Model_Token, Model_Role), because their names are hardcoded. But ORM driver & File driver can be used.