mongodbauthenticationdoctrine-ormzend-framework2zfcuser

How to use two collections to authenticate zfcuser?


I use Doctrine and MongoDB ODM modules at my zf2 application. ZfcUser is used for authorization.

Is there a way to use two collections, say users and clients to authenticate via zfcuser+doctrine? I am curious, if there is a way to combine two mongo collections into one to use combined for authentication?


Solution

  • You do not need to merge the users into one collection as you can have multiple 'authentication adapters' (see ZfcUser\Authentication\Adapter\Db for an example)

    These are defined within global config file: zfcuser.global.php

    Each of my adapters are run in order of priority until one returns a successful authentication result.

    For example; I have the following configuration for Users and Candidates entities.

    /**     
     * Authentication Adapters
     *
     * Specify the adapters that will be used to try and authenticate the user
     *
     * Default value: array containing 'ZfcUser\Authentication\Adapter\Db' with priority 100
     * Accepted values: array containing services that implement 'ZfcUser\Authentication\Adapter\ChainableAdapter'
     */
    'auth_adapters' => array( 
        50  => 'JobboardCandidate\Authentication\Adapter\CandidateDatabaseAdapter',
        75  => 'JobboardUser\Authentication\Adapter\UserDatabaseAdapter',
        //100 => 'ZfcUser\Authentication\Adapter\Db', [this is the default]
    ),