CakePHP3: how to use different authentication mechanisms? - php

I have a standard Form authentication declared in AppController.php:
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'scope' => ['Users.active' => 1]
]
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'account'
],
'logoutRedirect' => [
'controller' => 'Index',
'action' => 'index'
]
]);
Now I want an authentication based on api_key in a webservice. The doc, explains to do it like that:
$this->loadComponent('Auth', [
'authenticate' => [
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
So now I wonder how to load the second authentication mechanism in my webservice. I tried to do that:
class DeviceconnectionsController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Auth', [
'authenticate' => [
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
}
....
}
But Cake complains that I try to reload a different Auth Component.
So maybe the right way is to load both authentication mechanisms in AppController.php like below:
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'scope' => ['Users.active' => 1]
],
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'account'
],
'logoutRedirect' => [
'controller' => 'Index',
'action' => 'index'
]
]);
But it seems incorrect as both authentication uses a different storage and unauthorizedRedirect setting.
How to do?

Related

Receiving: "Route with name "imageAdd" not found" for defined route

I am receiving this error in Zend3 for a route that is defined in my module.config.php file where my routes are defined.
Here is the link on my view file that is causing the error:
<p>
Add Image
</p>
Here is my router set up:
'router' => [
'routes' => [
'property' => [
'type' => Segment::class,
'options' => [
'route' => '/property',
'defaults' => [
'controller' => Controller\ListController::class,
'action' => 'forsale',
],
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
],
'may_terminate' => true,
'child_routes' => [
'detail' => [
'type' => Segment::class,
'options' => [
'route' => '/detail[/:p_id]',
'defaults' => [
'action' => 'detail',
],
'constraints' => [
'p_id' => '[1-9]\d*',
],
],
],
'add' => [
'type' => Literal::class,
'options' => [
'route' => '/add',
'defaults' => [
'controller' => Controller\WriteController::class,
'action' => 'add',
],
],
],
'imageAdd' => [
'type' => Segment::class,
'options' => [
'route' => '/imageAdd[/:p_id]',
'defaults' => [
'controller' => Controller\WriteController::class,
'action' => 'imageAdd',
],
'constraints' => [
'p_id' => '[1-9]\d*',
],
],
],
],
],
],
],
Any help you can provide would be most appreciated, I look forward to hearing from you.
You should simply define your parent "property" route as a Literal type and not a Segment type. That should suffice to enable access to the child routes.

CakePHP 3.x - User auth-component contains table?

I am using CakePHP's Auth-component for user-login data and want to associate the users_table with a user_details table. The association works, and if I manually get a user out it works fine, but is it possible to make the auth-component load in the associated table when logging the user in? so far I have tried this with no luck:
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
],
'contain' => ['user_details']
]
],
'loginAction' => [
'controller' => 'users',
'action' => 'login'
]
]);
Note the "contain" part - that is where I try to load associated table but with no luck?
Thanks.
contain has been deprecated in favor of 'finder'
so define a finder in your User Table
public function findDetails($query, array $options)
{
return $query
->contain(['UserDetails']);
}
and in the AppController
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
],
'finder' => ['details']
]
],
'loginAction' => [
'controller' => 'users',
'action' => 'login'
]
]);
https://book.cakephp.org/3.0/en/controllers/components/authentication.html#customizing-find-query

The requested controller could not be mapped to an existing controller class. zf3 with routes to several controllers in one module

When i'm trying to create routes to several controllers in one module and i'm getting this
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:action]',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'vehicles' => [
'type' => Segment::class,
'options' => [
'route' => '/vehicles[/:action]',
'defaults' => [
'controller' => Controller\VehiclesController::class,
'action' => 'index',
],
],
],
Calling the indexAction() of IndexController works perfectly. However it doesn't work for VehiclesController. I tried to invoke VehiclesController in factories - unsuccessful.
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
Controller\VehiclesController::class => InvokableFactory::class,
],
],
Using ServiceManager function
'controllers' => [
'factories' => [
Controller\VehiclesController::class => function($sm){
$vehiclesService=$sm->get('Application\Model\VehiclesTable');
return new Controller\VehiclesController($vehiclesService);
}
],
],
May be you are doing something like below to invoke your controllers in your module.config.php
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
Controller\IndexController::class => InvokableFactory::class,
],
],
Therefore, you may be using same controller name twice under factories key. It may be as above or different way.
So define them once under factories subkey of controllers key as the following
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
Controller\VehiclesController::class => InvokableFactory::class,
],
],

Child routes are not working

I'm new to Zend-Framework3.
And migrating my ZF2 application to ZF3.
In this child routes are not working.
Here is router from my module.config.php
'router' => [
'routes' => [
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'kk' => [
'type' => Literal::class,
'options' => [
'route' => 'kk',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'kk'
],
],
],
]
]
],
],
When I try to call /application/kk action. It generates 404 error.
Where am I wrong? Or do I have to register all actions manually?
...do I have to register all actions manually?
No, you are just missing / character in route value
'router' => [
'routes' => [
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'kk' => [
'type' => Literal::class,
'options' => [
'route' => '/kk', <-- here
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'kk'
],
],
],
]
]
],
],
As long as action kk exists, you should not get 404 error.
If your routes are same as actions name. You can use Segment type:
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:action]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
]

ZF2 cannot match dynamic wildcard route for breadcrumb

My system has global dynamic routes which allow to develop modules using the same code style.
I want to generate breadcrumb for url like this /checkout/list/cart-type/2 but navigation config cannot match my url.
On the other hand, when I simply route to /checkout/list it works correctly.
Please, help me to configure my config properly.
My router config
'router' => [
'routes' => [
'default' => [
'type' => 'Segment',
'options' => [
'route' => '/[:controller[/[:action]]]', // global route
'constraints' => [
'controller' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
'action' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => 'index',
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard',
'priority' => 10,
'options' => [],
],
],
],
],
],
My navigation config
'navigation' => [
'default' => [
'checkout' => [
'module' => 'checkout',
'label' => 'Home',
'route' => 'default',
'controller' => 'index',
'action' => 'index',
'pages' => [
'checkout-list' => [
'label' => 'Invoices',
'route' => 'default/wildcard',
'controller' => 'checkout',
'action' => 'list',
'params' => [
'cart-type' => 2
],
],
],
],
],
],
You defined controller and action as parameters, so try (not tested):
'navigation' => [
'default' => [
'checkout' => [
'module' => 'checkout',
'label' => 'Home',
'route' => 'default',
'controller' => 'index',
'action' => 'index',
'pages' => [
'checkout-list' => [
'label' => 'Invoices',
'route' => 'default/wildcard',
'params' => [
'controller' => 'checkout',
'action' => 'list',
'cart-type' => 2
],
],
],
],
],
],
Or:
$url('default/wildcard', [
'controller' => 'checkout',
'action' => 'list',
'cart-type' => 2
];
I found out the solution.
Problem was in 'id' child Segment route which re defined wildcard route
'default' => [
'type' => 'Segment',
'options' => [
'route' => '/[:controller[/[:action]]]', // global route
'constraints' => [
'controller' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
'action' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => 'index',
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'id' => [
'type' => 'Segment',
'priority' => 100,
'options' => [
//'route' => '[/:id]', // this was changed without brackets (!)
'route' => '/:id',
'constraints' => [
'id' => '[0-9]+',
],
'defaults' => [
'id' => '0',
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard',
'options' => [],
],
],
],
]
]

Categories