Cakedc.users => always redirect to homepage - php

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,

Related

CakePHP4 / CakeDC keeps asking to login [duplicate]

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,

Yii2 pretty urls returning 404

I am using nginx and yii2 trying to create a little sample crud rest API, and whenever I enable pretty urls I can't access my routes anmymore.
My config is as follows :
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'only' => ['delete', 'create', 'update', 'get'],
'patterns' => [
'PUT users/<id:\d+>' => 'user/update',
'DELETE users/<id:\d+>' => 'user/delete',
'GET users/<id:\d+>' => 'user/get',
'POST users' => 'user/create',
]
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'campaign',
'only' => ['delete', 'create', 'update', 'get'],
'patterns' => [
'PUT campaigns/<id:\d+>' => 'campaign/update',
'DELETE campaigns/<id:\d+>' => 'campaign/delete',
'GET campaigns/<id:\d+>' => 'campaign/get',
'POST campaigns' => 'campaign/create',
]
],
],
],
Note that when enabling this, I can access gii without any issue at /gii instead of /index.php?r=gii
When disabled, my routes work fine using index parameters. Also it's yii giving me 404 and not directly nginx.
EDIT: This url works /user/get?id=1
However this one does not /users/get/1, nor /user/get/1
A POST on /users will work, but not any of the other http requests

How to use multiple Auth components?

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

Cakephp : Login Admin with AuthComponent

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'
]
]
],
],

How to modify the appearance of auth related flash messages?

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

Categories