I don't have a User table. I got a Customer table. It seems like Cakephp Auth doesn't recognise any other table beside User. Is there any way to walk around this error?
If I tried using $this->Auth->User('role') - it works perfectly fine. But all my auth credentials is under the Customer table. please help.
I got this error below
Fatal error: Call to undefined method AuthComponent::Customer() in
/Applications/MAMP/htdocs/development03/app/app_controller.php on line 56
The code:
function beforeFilter() {
$this->set('admin', $this->_isAdmin());
}
function _isAdmin() {
$admin = FALSE;
if ($this->Auth->Customer('role') == 'admin') {
$admin = TRUE;
}
return $admin;
}
Let's say that you want administrators table where username is field email and password is field password:
In your AppController.php:
public $components = array('Session',
'Auth' => array('authenticate' => array('Form' => array( 'userModel' => 'Administrator',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
),
'authorize' => array('Controller'),
'loginAction' => array('controller' => 'administrators', 'action' => 'login'),
'loginRedirect' => array('controller' => 'Home', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'Home', 'action' => 'index'),
),
);
In AppController.php add this too:
public function isAuthorized($user){
return true;
}
You will have to use userModel => ModelName property. Your AppController should looks like:
class AppController extends Controller
{
public $components = array(
'Auth' => array('authenticate' => array('Form' => array('userModel' =>'Customer',
'fields' => array('username' => 'USER_LOGINNAME', 'password' => 'USER_PASSWORD'
)
)
)
)
);
/*...... Your code ............*/
}
This link might help you to resolve bugs.
Related
I have been trying to get the CakePHP ACL's working with my new application, it is causing me a world of pain. For some reason the ACL doesnt seem to be working however the tutorials are rubbish and don't explain each component very well. e.g. how a ACO links to a controller / function / views
I have got the ACL working correctly up until getting the pages to know if the user is allowed to view it, also same issue with menu items that they can / cant see.
I have noticed that if i add this code to my page the array shows the group as blank:
$user = $this->Auth->user();
pr($user);
The array returned:
Array
(
[id] => 80
[first_name] => Bob
[last_name] => Test
[email] => email#emial.com
[username] => TestAdmin
[tokenhash] => cleared
[is_active] => 1
[created] => 2014-10-03 16:32:45
[modified] => 2014-10-03 16:32:45
[token_expires_at] =>
[group_id] => 3
[Group] => Array
(
[id] =>
[name] =>
[enabled] =>
[created] =>
[modified] =>
)
)
The site is basically a portal, visitors should only have access to login / register. and the users groups all have access to the dashboard however it ends up in a continuous loop unless i allow everyone access to the dashboard (due to the group not being recognised i guess)
any help would be appreciated, I realise that you may need me to post the code im using so please let me know what you would require.
Thanks in advance
EDIT:
I have updated my AppController as below and it has started showing the group in the array as it should!!! weird thanks for the push in the right direction.
AppController.php
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public function beforeRender() {
if((($this->params['controller']==='Users') || ($this->params['controller']==='users'))&&(($this->params['action']=='login') || ($this->params['action']=='register') || ($this->params['action']=='success') || ($this->params['action']=='forgot_password') || ($this->params['action']=='reset_password')) ){
$this->theme = 'DataHouseLogin';
}else{
$this->theme = 'DataHouse';
}
parent::beforeRender();
}
public $components = array(
'Acl',
'RequestHandler',
'DebugKit.Toolbar' => array('panels' => array('history' => false)),
'Session',
'Auth' => array(
'authorize' => array(
'Actions' => array(
'actionPath' => 'controllers'
)
),
'loginAction' => array(
'controller' => 'Users',
'action' => 'login'
),
'loginRedirect' => array(
'controller' => 'Dashboard',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'Users',
'action' => 'login'
),
'authError' => 'Did you really think you are allowed to see that?',
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);
public function beforeFilter() {
//$this->Auth->allowedActions = array('display','index','register');
$this->set('user', $this->Auth->user());
$this->set('acl', $this->Acl);
$this->Auth->authorize = array(
'Controller',
'Actions' => array('actionPath' => 'controllers')
);
parent::beforeFilter();
}
public function isAuthorized($user) {
// Default deny
return false;
}
}
I think you should try to configure Auth component correctly. Try to put this code in your AppController:
class AppController extends Controller {
public $components = array('RequestHandler', 'Session',
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
);
public function beforeFilter() {
$this->Auth->authorize = array(
'Controller',
'Actions' => array('actionPath' => 'controllers')
);
$this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'name', 'password' => 'password')));
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'plugin' => false);
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'plugin' => false);
}
public function isAuthorized($user) {
// Default deny
return false;
}
}
EDIT:
in UserModel and GroupModel add act as property:
public $actsAs = array('Acl' => array('type' => 'requester'));
in UserModel setup parentNode function:
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
Been trying to figure this out for plenty of hours, but without success.
$this->request->data['Login'] array contains the right username and hash which matches the entry in the database. $this->Auth->login(); is always returning false for some reason.
The Login table has a column called UserID and Password, Password is hashed using MD5 and a salt which is stored in the table Login as well.
I tried adding a beforeSave function in the Login model which hashed the password, but this didn't seem to work either?
/app/Controller/AppController.php
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array('App', 'Html', 'Form', 'Session');
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array('controller' => 'login', 'action' => 'authenticate'),
'loginRedirect' => array('controller' => 'account', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'home', 'action' => 'index'),
'authenticate' => array('Form' => array('userModel' => 'Login', 'fields' => array('username' => 'UserID', 'password' => 'Password'))))
);
// Execute on every page load.
public function beforeFilter(){
$this->Auth->allow('index', 'view');
}
}
/app/Controller/LoginController.php
<?php
class loginController extends AppController{
public $uses = array('Login');
public function index(){
$this->render('/login');
}
public function authenticate(){
if($this->request->is('post')){
$this->request->data['Login']['password'] = $this->hashPassword($this->data['Login']);
if($this->Auth->login()){
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash('Invalid username or password.');
return $this->redirect('/login');
}
}
private function hashPassword($login){
// Get the salt of the user.
$salt = $this->Login->find('first', array(
'fields' => array(
'Salt'),
'conditions' => array(
'Login.UserID' => $login['username'])));
return md5(md5($login['password']) . $salt['Login']['Salt']);
}
}
/app/Model/Login.php
<?php
class Login extends AppModel{
public $useTable = 'Login';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please fill in all of the fields.'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please fill in all of the fields.'
)
));
}
/app/View/login.ctp
<?php
echo $this->Session->flash('flash', array('element' => 'fail'));
echo $this->Form->create('Login', array('url' => '/login/authenticate'));
echo $this->Form->input('username', array('label' => false, 'div' => false, 'autocomplete' => 'off'));
echo $this->Form->input('password', array('label' => false, 'div' => false));
echo $this->Form->end('Login');
?>
Per the incredibly detailed example in the CakePHP book:
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
You shouldn't be manually hashing your password prior to submitting.
Alright I solved it:
Changed the field names in the login view file to match the column names in the database.
Added a custom PasswordHasher
I've tried all the possible combination for modifying the default model for authentication in cakephp 2.5
actually my current appController is
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'utente',
'action' => 'login'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
)
)
);
function beforeFilter() {
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
$this->Auth->userModel = 'Utente';
}
}
I have also tried with this answers but going into /cakephp-master/ redirect me with no regret to users/login. why?
I need a login/logout system in my project I've done an authorization class but It won't work for me because the login function always returns false
Here is my UserController.php
if ($this->request->is('post')) {
$this->Auth->fields = array("username"=>"username","password"=>"password");
debug($this->Auth->login(),false,true);
if ($this->Auth->login()) {
$this->Session->setFlash(__('You are now logged in.'));
}else{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
Configure::write('debug', 2);
$log = $this->User->getDataSource()->getLog(false, false);
debug($log);
}
Also, my AppController.php have:
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'User',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'User', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'User', 'action' => 'login'),
'authError' => "You can't acces that page",
'authorize' => array('Controller'),
));
public function isAuthorized(){
return TRUE;
}
public function beforeFilter() {
$this->Auth->authenticate = array('Form');
$this->Auth->allow('index', 'view');
}
And finally my model file is:
public $validate = array(
'email' => array(
'required' => array(
'rule' => array("notEmpty"),
'message' => 'A E-mail field is required'
)
),'password' => array(
'required' => array(
'rule' => array("notEmpty"),
'message' => "A Password field is required"
)
)
);
I'm using Cake's Auth component and can't seem to figure out how to set specific flash data/error message when using scope.
While testing by changing active from 0 to 1, I can confirm the scope parameter works, however if scope returns false, I get flash data associated with my login method, Your username or password was incorrect..
UsersController
public function login(){
if($this->request->is('post')){
if($this->Auth->login()){
$this->Session->setFlash('You are logged in!');
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Your username or password was incorrect.'));
}
}
AppController
public $components = array(
'DebugKit.Toolbar',
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email'),
'scope' => array('active' => '1')
)
)
),
'Session'
);
public function beforeFilter() {
$this->Auth->loginAction = array(
'controller' => 'Users',
'action' => 'login'
);
$this->Auth->logoutRedirect = array(
'controller' => 'Users',
'action' => 'login'
);
$this->Auth->loginRedirect = array(
'controller' => 'Users',
'action' => 'index'
);
}
Is it possible to bind a specific error message for each scope parameter and login method?
Very simple in CakePHP :)
You can custom error message in every method of Controller
Simple, you do it as I say ^_^
Exmple:
<?php
public function accessSite(){
//disable default message authError of Auth
$this->Auth->authError = false;
$message = 'You not have permission access here';
//set new custom message
$this->Auth->flash($message);
}