Zf2 + Doctrine 2 Authentication Adapter with Multiple or Alternate Columns - zend-framework

I am working on a site in which user can enter two email address(primary and secondary) along with password.
If user enters his primary email and password, he gets logged in successfully.
But, what I am trying to provide is if user enters his secondary email instead of primary, even then he gets logged in. And the problem I am getting is how to create an alternate Doctrine Auth Adapter or something like that.
this is what I have done in my module.config.php:
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
)
)
),
'authentication' => array(
'orm_default' => array(
'object_manager' => 'Doctrine\ORM\EntityManager',
'identity_class' => 'User\Entity\LoginDetails',
'identity_property' => 'primary_email',
'credential_property' => 'password',
),
),
)
Is there any option to add an identity property which will be alternative ?
I am using Zend framework 2 and Doctrine 2

Is there any option to add an identity property which will be alternative ?
No, there is no such option built-in to DoctrineModule.
Consider extending DoctrineModule\Authentication\Adapter\ObjectRepository to override the authenticate() method.
Then, at minimum, you'll want to replace the default adapter with your new more different one. If you look at the various factories in DoctrineModule, you should be off to a good start.
Basically, one of your modules will want to override the doctrine.authenticationadapter.[orm|odm]_default configuration key in the ServiceManager. That will cause DoctrineModule to inject your extended ObjectRepository into the AuthenticationService in place of you the default one.

Related

Add Entity Reference to Drupal 8 Form Field?

How can users add products to a simple plugin config using an autocomplete field?
I tried to use Config Entity but it looks the same as Form API (and I can't use entity fields there).
I was able to do this in Drupal 8 using the form API and the entity_autocomplete type.
$form['stories'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#title' => $this->t('Stories'),
'#description' => $this->t('Select some stories.'),
'#default_value' => $default_entities,
'#tags' => TRUE,
'#selection_settings' => array(
'target_bundles' => array('page', 'article'),
),
'#weight' => '0',
];
Use webform - there is an entity reference field.
This won't be useable for your purpose .. but you can check the source for sake.

how to change default module in Zend 2

I am new to ZendFeamerwork version 2. I could easily change the default controller in Zend1 but it seems very difficult to me to find out how to change default module in Zend2.
I searched over google but there is no easy solution.
I just created a module named "CsnUser" I can access this module via the following url
http://localhost/zcrud/public/csn-user/
I want csn-user to load instead of "application" module i.e url should be
http://localhost/zcrud/public/
or
http://localhost/zcrud/
Please let me know how to get this done.
Based on #Hoolis comment:
You have to set that action on this route
'home' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'That\Namespace\CsnUser',
'action' => 'index'
)
)
)
)
In the skeleton application this route is set in the Application Module, but you can move this somewhere or edit it.

Zf2 : Controller forwarding/calling generate 404 Error

I am implementing separate auth solution for two different modules described in another question by me.
Zend framework 2 : Add different authentication adapter for two different modules
Now in AuthListener file I write code for forward/call to an different controller/action if authentication failed. That is
$result = $this->adapter->authenticate();
if (!$result->isValid()) {
$response = $event->getResponse();
// Set some response content
$response->setStatusCode(401);
$routeMatch = $event->getRouteMatch();
$routeMatch->setParam('controller', 'First\Controller\Error');
$routeMatch->setParam('action', 'Auth');
}
Now I am getting 404 error - "The requested controller was unable to dispatch the request". First I think I do not added route for Error/Auth, but then I verified it got 404 for all other controller/action too. All are directly accessible through their respective route. But forwarding resulting in 404 error. One important thing - I sending authentication request through phpunit to make unit test cases.
UPDATE : route details :
'routes' => array(
'rest' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/rest[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'First\Controller\Index'
),
),
),
'error' => array(
'type' => 'segment',
'options' => array(
'route' => '/rest-error/[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'First\Controller\Error',
'action' => 'auth',
),
),
)
),
'controllers' => array(
'invokables' => array(
'First\Controller\Auth' => 'First\Controller\AuthController',
'First\Controller\Error' => 'First\Controller\ErrorController'
),
),
Module.php
$listener = $serviceManager->get('First\Service\AuthListener');
$listener->setAdapter($serviceManager->get('Rest\Service\BasicAuthAdapter'));
$eventManager->getSharedManager()->attach('First', 'dispatch', $listener, 100);
I also tried to use forward instead of above solutio, But that gives error for circular forward Circular forwarding detected: greater than 10 nested forwards. I think event called when forward called.
try to call your action like that :
$routeMatch->setParam('action', 'auth');
I think the problem might occure because you are listening to MvcEvent::EVENT_DISPATCH. In your listener you set new controller and action variables for RouteMatch but since you are already passed the route event changing those parameters will not have any effect.
You should listen to MvcEvent::EVENT_ROUTE instead and then it will probably work.

ZfcUserDoctrineMongoODM Configuration examples

I am trying to configure ZendFramwork 2 to use MongoDB as the storage for user Authentication.
I already have installed what I believe are the correct modules and have a correctly installed instance of ZF2 running on Nginx. I also have an instance of Mongo running and I am already using this for another project. The modules declaration in application.config.php looks like this
'modules' => array(
'Application',
'ZfcBase',
'ZfcUser',
'ZfcUserDoctrineMongoODM',
'DoctrineModule',
'DoctrineMongoODMModule'
)
Documentation is a bit thin on how to configure the system to get it working. Can anyone provide any code fragments around configuring the database settings to allow Mongo to run with ZfcUser, this is where I am at a loss now.
Any help, clues or cheat sheets would be greatly appreciated.
Darren Breeze
Have a look at Doctrine Mongo ODM module readme https://github.com/doctrine/DoctrineMongoODMModule :
copy
vendor/doctrine/doctrine-mongo-odm-module/config/module.doctrine-mongo-odm.local.php.dist
into your application's config/autoload directory, rename it to
module.doctrine-mongo-odm.local.php and make the appropriate changes.
With this config file you can configure your mongo connection, add
extra annotations to register, add subscribers to the event manager,
add filters to the filter collection, and drivers to the driver chain.
So you have something like this at your autoload config:
<?php
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
// 'connectionString' => null,
// 'user' => null,
// 'password' => null,
// 'dbname' => null,
// 'options' => array()
),
),
....
'configuration' => array(
'odm_default' => array(
... 'default_db' => 'myappdb',
)
)
So tune it and try to register at user/register. Collection name is user by default.

Zend 2 router setting

Im very mew to ZF2, and cant figure out how to set the global router.
I know how to set on module level:
http://packages.zendframework.com/docs/latest/manual/en/user-guide/routing-and-controllers.html says:
The mapping of a URL to a particular action is done using routes that
are defined in the module’s module.config.php file. We will add a route
for our album actions. This is the updated config file with the new
code commented.
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
Is there a way to config a default behaviour for the whole application? Or i have to confing in each module?
config/application.config.php would be a logical place for it. Is it somewhere documented?
What your ZF2 module defines in getConfig() gets merged with all the other module configs and eventually replaced by modules loaded afer it if some config keys collide. Thus your getConfig() already affects the entire application and its scope is not limited to your module only.