How to modify the appearance of auth related flash messages? - php

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

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,

Cakedc.users => always redirect to homepage

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 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

zf2 - url view helper: specify parameters for route

My main router goes like this (simplified):
'router' => [
'routes' => [
'blog' => [
'type' => 'regex',
'options' => [
'regex' => "/(?<language>[a-z]{2})?",
'spec' => "/%language%",
'defaults' => [
'controller' => 'Blog\Controller\Posts',
'action' => 'index'
],
],
'may_terminate' => true,
'child_routes' => [
// [...]
'add_post' => [
'type' => 'literal',
'options' => [
'route' => '/admin/post/add',
'defaults' => [
'controller' => 'Blog\Controller\Posts',
'action' => 'add'
]
]
], // end add post
] // end child routes
] // end blog route (main route)
] // end routes
] // end Router
And in the template displayed on "/en/admin/post/add" I have a call to $this->url(), that ends up printing /%language%/admin/post/add.
I have the language code available on $language on my template, and
I'd like to pass it on to url() so it properly constructs the the url using the spec.
Also, I'd like, if possible, not to specify the name of the route on my call to url(), so it uses the default one for $this.
How would I go around to accomplish this?
Thanks and regards
You could use a segment route instead of a regex one and then use
$this->getHelperPluginManager()->getServiceLocator()->get('request')->getUri()->getPath();
in your view to print the actual route it's been used
While #marcosh answer works, since then I've found a simpler solution:
$this->url($this->route, ['language' => $language]);
Will output what I want. Seems clearer to me.

CakePHP - Routing to admin

Noob question!
I have a complete admin setup: login, authentication an so on, it's working.
So, now i need a new controller to another situation and then i create the controller named PressDownloadsController and the correct views to every action inside this new controller.
I also created the following route:
Router::connect('/pressdownloads', array('controller' => 'pressdownloads', 'action' => 'downloads'));
Inside the pressDownloads controller, there's some redirect between some actions.
When i try to open the url /pressdownloads/downloads or just /pressdownloads it just goes to admin controller and i need to login in before acess the pressdownloads area.
Why?
The Auth Settings inside UsersController (admin):
Ok. I get it.
So the auth settings:
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
'prefix' => 'admin',
),
'loginRedirect' => '/admin/events',
'logoutRedirect' => '/admin',
'authError' => 'Acesso negado',
'flash' => array(
'element' => 'admin/messages/error',
'key' => 'auth',
'params' => array()
),
'authorize' => array('Controller'),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
I recommend to check Auth component settings and how you split admin area from non-admin.
Also, you may specify in your routing not to use admin prefix like this:
Router::connect('/pressdownloads', array('controller' => 'pressdownloads', 'action' => 'downloads', 'admin' => false));
I believe you are going to that link after "Admin" has been enabled. Simply put you must be clicking the link from the pages served by admin.
Try adding additional parameter "admin" => false in your present route.
Router::connect('/pressdownloads',
array(
'controller' => 'pressdownloads',
'action' => 'downloads',
'admin' => false
)
);

Categories