1

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?

shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
  • I have no experiance with envoirment You work with, but in pure mongodb You would just make two queries. – Jarema Jun 26 '14 at 12:05

2 Answers2

2

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]
),
AlexP
  • 9,906
  • 1
  • 24
  • 43
1

As I understand from your question you want to get one collection with two different document types so that you can use this collection for authentication. If you use doctrine Inheritance mapping you can have two different classes and resolve them in one collection. In this case your Client class would extend your User class. If you would use the findAll method in your UserRepository you would get both the clients and the users in one Collection

This will help you achieve what you want:

<?php
namespace MyProject\Model;

/**
 * @Document
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField(name="discriminator", type="string")
 * @DiscriminatorMap({"user" = "User", "client" = "Client"})
 */
class User
{
    // ...
}

/**
 * @Document
 */
class Client extends User
{
    // ...
}

And then

$userRepository->findAll();

Read more on inheritance mapping here in the Doctrine documentation

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Why do you mention Entity, ORM and so on? I use MongoDB Doctrine ODM. But ODM also has inheritance mapping - http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/inheritance-mapping.html – shukshin.ivan Jun 29 '14 at 13:14
  • Sorry my mistake, I thought you were using Doctrine ORM2. But I think you could indeed do the same with Doctrine ODM Inheritance mapping. The concept is exactly the same. – Wilt Jun 30 '14 at 07:08