AclNode::node() - Couldn't find Aro node identified by "Array ( [Aro0.model] => User [Aro0.foreign_key] => 1 ) "
Error: An Internal Error Has Occurred.
Stack Trace
My model name is Admin not user, I have set it in the Auth component configurations
public $components = array('Acl',
'Auth' => array(
'loginRedirect' => array('controller' => 'admins', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'admins', 'action' => 'login'),
'loginAction' => array('controller' => 'admins','action' => 'login'),
'authError' => '<font color="red">Did you really think you are allowed to see that? If yes then login!</font>',
'authenticate' => array(
'Form' => array(
'userModel' => 'Admin',
'fields' => array('username' => 'username', 'password' => 'password')
)
)
), 'Session', 'Email');
I don't know how it is User not Admin in the error.
Thanks in advance.
Please realise that Authentication is not the same as Access Control, although you properly configured your AuthComponent (Form) authentication to use the Admin model, the ACL may still be trying to access the User model.
For more information, please refer to the manual here;
http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html#assigning-permissions
And here;
http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html#acts-as-a-requester
Especially, the 'model' column when assigning permissions and the section on 'requester'.
I realise this is not a full answer, I don't have the right examples at hand here, but maybe these suggestions will help you to find the solution.
Related
After logging a user in, I want to redirect them back to where they came from but It's not working properly in CakePHP 3.5. Here are the required info's to help me figure out this problem.
URL while login(session time out),
http://dev.scys.com/db/admin?redirect=%2Fadmin%2Fstatuses
This is my Auth config,
$this->loadComponent('Auth', [
'loginAction' => ['controller' => 'Admins', 'action' => 'login'],
'loginRedirect' => ['controller' => 'Admins', 'action' => 'index'],
'logoutRedirect' => ['controller' => 'Admins', 'action' => 'login'],
'unauthorizedRedirect' => $this->referer(),
'authenticate' => [
'Form' => [
'finder' => 'auth',
'userModel' => 'Admins',
'fields' => ['username' => 'username', 'password' => 'password']
]
]
]);
And in the Login method/action
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
More Points
I have also tried $this->redirect($this->request->getQuery('redirect'));
Am I missing anything or something else I have to add to work this out :(
I figured out my mistake,
Actually, I was using for action URL like,
$this->Form->create(NULL, ['url'=> ['controller' => 'Admins', 'action' => 'login'],'style'=>'display: block;');
Because of this, the URL became "admins/login" and the redirect query string get removed that's why the problem occurred, because "$this->redirect($this->Auth->redirectUrl());" didn't find any redirect query string(as per the case 1), so it uses $this->Auth->config('loginRedirect');(as per case 2).
Then I solve it by removing the URL key and value from the form create option parameter.
I am using an application in which customer can also login from frontend. Its working, but problem is in this case is customer can also see backend functionality. Vice-versa, when i login in backend, I automatically signin in fronend also. It cause problem for me.
Anyone please suggest me for implementing separate login for admin and customer in a website.
Or is there other way to do this? please suggest.
You don't need a separate login, you just need to use roles (or some variation of them).
The basic idea is, when someone logs in, you can check their 'role', and then act accordingly.
So, if someone logs in, and they're a "user", send them to the user profile page. If they're an "admin", send them to the admin dashboard.
Then, use some form of a check to verify access rights before a user gets into an action.
One example would be to set up a isAuthorized() method in your AppController. See more details here, in the CakePHP book.
This will allow you to compare the controller/action they're trying to access, their role, and the URL prefix (if you're using /admin/... as example) and determine if they should be granted access.
There are MANY ways to handle this situation, but the basic idea is there. Find out who they are, and what role they are, and build your logic to determine 1) where they should go, and 2) where they're allowed to go.
You can do it by adding below code in cakephp appcontroller beforeFilter function.
if($this->params['prefix'] == 'admin'){
$this->layout = 'admin';
AuthComponent::$sessionKey = 'Auth.Admin';
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array('username' => 'email'),
'scope' => array ('User.role_id' => '1', 'User.status' => 1),
)
);
$this->Auth->loginAction = array('controller' => 'users','action' => 'login','admin' => true);
$this->Auth->loginRedirect = array('controller' => 'users','action' => 'dashboard','admin' => true);
$this->Auth->logoutRedirect = array('controller' => 'users','action' => 'login','admin' => true);
}else{
AuthComponent::$sessionKey = 'Auth.User';
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array('username' => 'email'),
'scope' => array ('User.role_id >' => '2','User.status' => 1),
)
);
$this->Auth->loginAction = array('controller' => 'users','action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'users','action' => 'dashboard');
if($this->Session->read('Auth.User.role_id') == CORPORATE){
$this->Auth->logoutRedirect = array('controller' => 'users','action' => 'corporate_login');
}else{
$this->Auth->logoutRedirect = array('controller' => 'users','action' => 'login');
}
}
if you have don't have roles than you can remove it from scope..
I have a strange issue going on as I try to learn and program website using CakePHP 2.0. I have worked in the past with version 1.3 but never seen this problem before. I am running the Simple Authentication and Authorization Application tutorial from the Cookbook (p.638) and I have checked and doubled checked to make sure it is the same.
The issue I am having is that when I call /users/login and fill out the form with bogus info or simply leave it empty. Auth logs it in and if I do if($this->Auth->user()) I will receive true.
I have given up trying to understand why that is happening. It is strange....
/users/login
public function login(){
if($this->request->is('post')){
if($this->Auth->login($this->request->data)){
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Wrong login credentials!', 'default', array('class' => 'notification error closeable'));
}
}
}
// Appcontroller.php ---- Auth configuration
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'authorize' => array('Controller')
)
);
public function isAuthorized($user){
if(isset($user['role']) && $user['role'] === 'admin'){
return true;
}
return false;
}
Ok.Seems like i know what the problem is. Try doing:
if($this->Auth->login())
instead of passing it CakeRequest object.
The reason is:
In 2.0 $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful. Maybe thats why you are able to log in without any data.
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
),
'loginAction' => array('admin' => false, 'controller' => 'users', 'action' => 'login')
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'authorize' => array('Controller')
)
);
Also try putting a debug($user); in beginning your isAuthorized method in AppController to see if its not getting a false from there.
What I did
The CakePHP version is 2.2.3
I used this part of the Cookbook to create my authentication: Link
After i was finished, i changed the fields to email and password (in AppController.php):
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array('userModel' => 'User', 'fields' => array('username' => 'email', 'password' => 'password'))
),
'loginRedirect' => array('controller' => 'twitter', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authorize' => array('Controller')
)
);
What happened
When i log in with my credentials, it redirects me to the loggedin page, even when the credentials aren't correct!
I hope someone can help me with the problem.
Thanks in advance!
EDIT:
Login now works as far as i can see! But the logout doesnt work. It doesnt remove my session.
You are passing something to $this->Auth->login($something). The code has changed since 1.x and anything passed to this method will cause the user to be logged in.
You should call $this->Auth->login() with no parameters
I'm using a login with a scope to ensure that the user account is active. If the login fails how can i tell if it failed because the email was not matched or if the accounts wasn't activated?
Also, i didn't understand all of the documentation in the cakephp cookbooks, could you please look at my auth section in my components array?
The Email field in the html table and sql table are called 'AccountEmail'
The Password field in the html table and sql table are called 'AccountPassword'
The row in the sql table for active accounts is called 'AccountActive' and is an int type with a value of 0 if the user is not active and 1 if it is active.
'Auth' => array(
'logoutRedirect' => array('controller' => 'Accounts', 'action' => 'login'),
'authError' => 'You can\'t Access That Page',
'authorize' => array('Controller'),
'fields' => array('AccountEmail' => 'AccountEmail', 'AccountPassword' => 'AccountPassword'),
'scope' => array('AccountActive' => '1')
)
You're missing which authentication handler to use and your fields configuration was wrong.
I'll assume you want to use a form to login:
/**
* Auth component configuration
*/
public $components = array(
'Auth'=> array(
'logoutRedirect' => array(
'controller' => 'Accounts',
'action' => 'login'
),
'authError' => 'You can\'t Access That Page',
'authorize' => array('Controller'),
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'AccountEmail',
'password' => 'AccountPassword'
),
'scope' => array('AccountActive' => '1')
)
)
)
);