In my app controller I have:
public $components = array
(
'Session',
'Auth' => array
(
'loginRedirect' => array('controller' => 'devices', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authError' => 'You can\'t access that page',
'authorize'=>array('Controller'),
'authenticate' => array('Z1')
)
);
How can I localize the error message? Using __() throws an error.
Thanks
It throws an error because PHP doesn't like use of functions inside array definitions.
What you can do is define the error after that, in every call to the controller like this
function beforeFilter() {
$this->Auth->authError= __('You can\'t access that page');
}
Or, the other option is to leave your array like that, and every time you output the error (maybe in a flash message or something), translate it there
echo __($this->Auth->authError);
But that won't let PoEdit or similar recognize the string to translate, so you'll have to add it by hand.
Related
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
I have seen many similar problems here in SO but none have helped me solving my problem.
I don't think CakePHP's AuthComponent is behaving properly when it comes to permission errors.
My CakePHP app is in a subdomain of my website (e.g. http://www.example.com/myapp). Everytime a permission error occurs (user is not allowed to see this page), Cake adds another "/myapp" in the URL (so it becomes http://www.example.com/myapp/myapp) and naturally throws an error which says that "myapp" controller doesn't exist.
In my scenario, the login view is linked to the root of the website. So, I have in my AppController:
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'users',
'action' => 'login'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authorize' => 'Controller',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
And in routes.php:
Router::connect('/', array('controller' => 'users', 'action' => 'login'));
Router::connect('/users', array('controller' => 'users', 'action' => 'login'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
All I want is to be able to configure manually which URL Cake should redirect to in case of permission errors. Anybody knows how to do that?
I'm sorry guys, I accidentally found the answer a bit later. As CakePHP has some very intuitive labels, I started guessing some possible options for the Auth Component that would solve my problem. And I ended up discovering the unauthorizedRedirect option!
So I have:
'unauthorizedRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
And it's working just fine now. I just wish Cake's documentation had covered this... If it does, I swear I couldn't find it anywhere.
I am currently using cake's routes config in order to set up admin views, different from that of the non-admin user. I read the routing chapter of the documentation(cake's), and stumbled upon the prefix routing. Which I thought that it is something I need to use, to accomplish what I need. So I started it with setting up the config/core.php as suggested, and uncommented this
Configure::write('Routing.prefixes', array('admin'));
Then, I added a route in the routes.php :
Router::connect('/admin', array('controller' => 'donors', 'action' => 'index', 'admin' => true));
From what I understood, with the above set, I can define a specific action for the admin, names like : admin_index or admin_view, etc. .
So my AppController has a component set like this :
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'donors',
'action' => 'index'
),
'authError' => 'Access Denied',
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authorize' => array('Controller')
)
);
So when a non-admin user logs in he should be redirected to 'donors/index', and when the admin logs in I want to redirect him to 'donors/admin_index'.. How can i do this ?
I tried this :
public function beforeFilter(){
if(isset($this->params['admin'])){
$this->layout = 'stafflayout';
$this->Auth->loginRedirect = array(
'controller'=>'donors',
'action'=>'index',
'prefix'=>'admin',
'admin'=>true
);
}
And in the process of testing it out, at first glance I though it worked. but the URL does not change like 'donor/admin_index .. and am still being redirected to donors/index or equivalent, simply to /donors... Why is this not working ?
(seconndary question)Also during the process of testing this out, I changed my the controller and actions of the Auth component LoginRedirect to
'controller'=>'posts'
and
'action'=>'index'
other then 'donors', 'index', and when I logged in, I still got redirected to donors/index.. were it should have redirected me to 'posts/index'
Anyone can help me on these two issues? Primary questions is more important though!
Well the code is fine!
Router::connect('/admin', array('controller' => 'donors',
'action' => 'index', 'admin' => true));
the above will render /donors/index page whenever /admin is written in the url.
Now if you want to add prefix like /donors/admin_index then you have to create one more rule such as:
Router::connect('/donors/admin_index', array('controller' => 'donors',
'action' => 'index', 'admin' => true));
and in beforeFilter function
if(isset($this->params['admin'])){
$this->layout = 'stafflayout';
$this->Auth->loginRedirect = array(
'controller'=>'donors',
'action'=>'admin_index',
'admin'=>true
);
the above code will redirect to /donors/admin_index and routing will render /donors/index page
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
)
);
I'm following the Auth tutorial brought with CakePHP and I'm encountering a weird issue. Basically, I'm trying to set up auth when calling it from the components variable in the AppController file, instead of assigning the values in the beforeFilter action as the tutorial suggests. Right now my components variable looks like that:
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'display', 'action' => 'home')
//)
),
'Session'
);
When I try to run this code I get the error Fatal error: Class 'AppController' not found in C:\wamp\www\cakephp\lib\Cake\Controller\CakeErrorController.php on line 31. The weird thing is that everything works fine if I just comment out the logoutRedirect line. Do you have any clue on what is happening?