I configure a Auth component to "Admin page", using the users model. But now, I also want create/configure a Auth to the clients. I try "rewrite" the inialize()
//This is in my ClientsController.php
public function initialize()
{
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'userModel' => 'clients',
'fields' => ['username' => 'client_email', 'password' => 'client_password']
]
],
'loginRedirect' => [
'controller' => 'Clients',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Clients',
'action' => 'login'
],
]);
}
With this, I receive this log(if uses parent::initalize() receive the same)
[RuntimeException] The "Auth" alias has already been loaded with the following config: array (...
I not want create a "Auth" manualy. How to use more of one Auth?
Thanks....
Reconfigure
You don't necessarily need to use multiple auth component instances, you can simply reconfigure it in the extended controller, using the components config() method, something along the lines of:
public function initialize()
{
parent::initialize();
// ...
$this->Auth->config(
[
'authenticate' => [
'Form' => [
'userModel' => 'clients',
'fields' => [
'username' => 'client_email',
'password' => 'client_password'
]
]
],
'loginRedirect' => [
'controller' => 'Clients',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Clients',
'action' => 'login'
],
'storage' => [
'className' => 'Session',
'key' => 'Auth.Client'
]
],
null,
false
);
}
Note the use of the storage option, you should define a different key here (the default is Auth.User), otherwise an authenticated client might be able to access the admin area and vice versa, as the user data would get stored in the same session key!
Use aliasing
You could use multiple auth components if required, to do so you'd have to use aliasing, so that the components don't try to override each other:
$this->loadComponent('ClientAuth', [
'className' => 'Auth',
// ....
]);
Don't forget to use a different session key in this case too!
You'd access that component instance as $this->ClientAuth accordingly, and you may have to allow access to the login() method via $this->Auth, ie. in ClientsController::initialize() or beforeFilter() do:
$this->Auth->allow('login');
There might be further side-effects, so be careful.
See also
Cookbook > Controllers > Components > Authentication > Configuration options
Cookbook > Controllers > Components > Aliasing Components
Related
I'm using the plugins "CakeDC/Users" on a brain new Cakephp installation.
I've got two controllers : PagesController.php, CardsController.php.
Pages has 1 action (Beta, it's the homepage), and Cards two actions (index, and single).
Here is the setup in the bootstrap :
Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);
And the configuration of the plugin in config/users.php :
return [
'Users' => [
'Email' => [
'validate' => false
]
],
'Auth' => [
'loginAction' => [
'plugin' => null,
'controller' => 'Members',
'action' => 'login',
'prefix' => null
],
'logoutAction' => [
'plugin' => null,
'controller' => 'Members',
'action' => 'logout',
'prefix' => null
],
'authenticate' => [
'all' => [
'finder' => 'auth',
],
'CakeDC/Users.ApiKey',
'CakeDC/Users.RememberMe',
'Form',
],
'authorize' => [
//'CakeDC/Users.Superuser',
//'CakeDC/Users.SimpleRbac',
],
],
];
I've only have one route configured :
$routes->connect('/', ['controller' => 'Pages', 'action' => 'Beta', 'home']);
And here is my AppController.php :
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('CakeDC/Users.UsersAuth');
}
The homepage is Allowed :
$this->Auth->allow('beta');
When not logged in, I can only access /pages/beta, which is ok. I can register, login, and logout with the plugin, no problem on this side.
Once I'm logged, I can't access any other pages than the homepage.
If I got to /cards/index, or /cards/single, I'm always redirect to the homepage. If I disabled the plugin, pages access is ok.
I'm stuck on this since a while now, any help ?
Thanks,
Best Regards
Nevermind, I've replaced :
'authorize' => [
//'CakeDC/Users.Superuser',
//'CakeDC/Users.SimpleRbac',
],
By :
'authorize' => false,
The plugin used authorize with a component by default, so if you're not going to use it, you have the set "false" to be sure you don't have issues. Or you have to setup the authorized controllers and actions by setting up the good setup.
Thanks,
How can I block several pages I've made (E.g. dashboard.ctp , crm.ctp) from users that are not logged in using the auth component and redirect them to the Login page if they try to access the above mentioned pages?
This is my AppController.
public function beforeFilter(Event $event)
{
$this->Auth->allow(['display']);
}
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'name',
'password' => 'password'
],
'scope' => [ 'status' => 1 ]
]
],
'authorize' => ['Controller'], // Added this line
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Pages',
'action' => 'dashboard'
],
]);
}
public function isAuthorized($user)
{
// Admin can access every action
if (isset($user['group_id']) && $user['group_id'] === 1) {
return true;
}
// Default deny
return false;
}
I'm using the cakephp version - 3.4.5.
I assume you created dashboard.ctp, crm.ctp, in src/Templates/Pages and use the default PagesController. When you visit http://example.com/pages/crm CakePHP calls PagesController->display() and you allowed the display action for everyone:
$this->Auth->allow(['display']);
Remove it and decide in isAuthorized() if you want to allow it or not.
I'm using the plugins "CakeDC/Users" on a brain new Cakephp installation.
I've got two controllers : PagesController.php, CardsController.php.
Pages has 1 action (Beta, it's the homepage), and Cards two actions (index, and single).
Here is the setup in the bootstrap :
Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);
And the configuration of the plugin in config/users.php :
return [
'Users' => [
'Email' => [
'validate' => false
]
],
'Auth' => [
'loginAction' => [
'plugin' => null,
'controller' => 'Members',
'action' => 'login',
'prefix' => null
],
'logoutAction' => [
'plugin' => null,
'controller' => 'Members',
'action' => 'logout',
'prefix' => null
],
'authenticate' => [
'all' => [
'finder' => 'auth',
],
'CakeDC/Users.ApiKey',
'CakeDC/Users.RememberMe',
'Form',
],
'authorize' => [
//'CakeDC/Users.Superuser',
//'CakeDC/Users.SimpleRbac',
],
],
];
I've only have one route configured :
$routes->connect('/', ['controller' => 'Pages', 'action' => 'Beta', 'home']);
And here is my AppController.php :
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('CakeDC/Users.UsersAuth');
}
The homepage is Allowed :
$this->Auth->allow('beta');
When not logged in, I can only access /pages/beta, which is ok. I can register, login, and logout with the plugin, no problem on this side.
Once I'm logged, I can't access any other pages than the homepage.
If I got to /cards/index, or /cards/single, I'm always redirect to the homepage. If I disabled the plugin, pages access is ok.
I'm stuck on this since a while now, any help ?
Thanks,
Best Regards
Nevermind, I've replaced :
'authorize' => [
//'CakeDC/Users.Superuser',
//'CakeDC/Users.SimpleRbac',
],
By :
'authorize' => false,
The plugin used authorize with a component by default, so if you're not going to use it, you have the set "false" to be sure you don't have issues. Or you have to setup the authorized controllers and actions by setting up the good setup.
Thanks,
I have one table whose name is admins. Its contains username and password fields. My sales user use admins table for the sales login. I want to login sales using AuthComponent. I have write code for this as below.
AppController
public $components = [
'Auth' => [
'loginAction' => [
'controller' => '',
'action' => 'login'
],
'logoutRedirect' => [
'controller' => '',
'action' => 'login'
],
'loginRedirect' => [
'controller' => '',
'action' => 'deshboard'
],
'className' => 'MyAuth'
]]
public function beforeFilter() {
$this->Auth->authenticate = [
'Form' => ['userModel' => 'admin', "fields" => ["username" => "username",
"password" => "password"]
]];
}
SalesController
function login() {
$post = $this->request->data('Admin');
if ($this->request->is('post') && !empty($post)) {
//var_dump($this->Auth->login());exit;
if ($this->Auth->login()) {
return $this->Auth->redirect($this->Auth->redirectUrl());
}
// perform login throttling (failure and block) if Sales or Admin portal
// set an appropriate failure message
}
}
When I have print the return value of auth->login() function. Its always return false.
I have search a lots for this issue but I am unable to find any proper answer.
Thanks in advance for helping me.
I have find the solution for my above issue.
This issue is because of passwordHaser. I use difference password in the add or update password so we have to define the passwordHaser in the component configuration, if we don’t use simple encription.
'Auth' => [
'className' => 'MyAuth',
'authenticate' => [
'Form' => [
'userModel' => 'Admin',
'fields' => [
'username' => 'username',
'password' => 'password'
],
'passwordHasher' => [
'className' => 'Simple'
]
]
],
],
I am having no luck trying to change the Flash Element on the AuthError from default to error?
I was just trying to see if I can change it, but now its driving me up the wall as I can not seem to change it?
This is how I have loaded my Auth in the AppController,
$this->loadComponent('Auth', [
'authError' => 'Did you really think you are allowed to see that? -2',
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email', 'password' => 'password']
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'Login'
],
'loginRedirect' => [
'controller' => 'Pages',
'action' => 'LoginPage'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'HomePage'
]
]);
So when I go to a not allowed page, it displays the authError message but using whatever class/id's are in the Element/Flash/default.ctp I wanted to change it to just use the same as the error.cpt
I have debugged the Auth Component, there was a 'flash' setting, tried setting that, but it did not work?
So how do I change the authError to use a different Flash Layout?
Thanks,
As you've figured, auth messages are using the default.ctp element by default, and that it's possible to configure the flash element when rendering it directly. To affect this globally, you can configure the component instead.
If all you want to do is to change the classname, then you can use the class parameter in the flash configuration options params setting:
$this->loadComponent('Auth', [
// ...
'flash' => [
'params' => [
'class' => 'some-custom-class'
]
]
]);
If you want to use a different element, for example the error.ctp one, just use the element setting to specify its name
'flash' => [
'element' => 'error'
]
See also Cookbook > Controllers > Components > Authentication > Configuration Options
I think I have got a solution.
It seems that when I print $this->Flash->render('auth');
I need to change it at this point to $this->Flash->render('auth',['element' => 'error']);
But if anyone knows any better ways to do this, please let me know
In Cakephp 3 you can define element name for flash errors -
$this->loadComponent('Auth', array(
'authorize'=> 'Controller',
'flash' => array(
'element' => 'error'
),
'authError' => 'Your session expired, please login again',
'logoutRedirect' => array(
'controller' => 'Users',
'action' => 'login',
'prefix' => false
)
));
and error.ctp at - \src\Template\Element\Flash\error.ctp