ACL - Where to configure custom userModel for authorize? - cakephp

I'm having Player instead of default User model for my Auth.
I recently configured ACL for my app and while trying to do testing by return false in my isAuthorized($player) function, the following error occured:
AclNode::node() - Couldn't find Aro node identified by
Array ( [Aro0.model] => User [Aro0.foreign_key] => 1 )
Isn't the Aro0.model suppose to be Player? I can't find where to change for Auth->authorize. Auth-authenticate works fine as I manage to login since there is a userModel option allow me to specify a custom Model for user login.
Here's My AppController
class AppController extends Controller
{
public $components = array(
'Session',
'Acl',
'RequestHandler',
'Auth' => array(
'authorize' => array(
'controller',
'Actions' => array('actionPath' => 'controllers'),
),
'authenticate' => array(
'Form' => array(
'userModel' => 'Player',
'fields' => array('username' => 'email', 'password' => 'password'),
)
)
),
);
public $helpers = array('Html', 'Form', 'Session');
function isAuthorized($player)
{
//var_dump($player); die;
return false;
return $this->Auth->loggedIn();
}
}

Solved. it is to append userModel together with actionPath.
$this->Auth->authorize = array(
AuthComponent::ALL => array('actionPath' => 'controllers/', 'userModel' => 'Player'),
'Actions',
'Controller'
);

Related

$controller does not implement an isAuthorized() method in cakephp

I have used Auth component in my OrdersController as follows:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email', //Default is 'username' in the userModel
'password' => 'password' //Default is 'password' in the userModel
),
'userModel' => 'Agent'
)
),
'loginAction' => array(
'controller' => 'admins',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'admins', 'action' => 'deshboard'),
'logoutRedirect' => array('controller' => 'admins', 'action' => 'login'),
'authError' => "You can't acces that page",
'authorize' => 'Controller'
)
);
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->allow('login','index');
}
When I tried to login and username and password matched it redirect to adminc/deshboard with the following error message:
$controller does not implement an isAuthorized() method.
Error: An Internal Error Has Occurred.
I searched google for couple of hours no solution. What am I doing for this error? Thanks for your time.
You need to implement isAuthorized(), like so:
class OrdersController extends Controller {
//...
public function isAuthorized($user) {
//auth check
//return boolean
}
//...
}
See http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html for more info.

Check a field before login

I done a user system on a website.
In my UsersController.php I have this method:
public function login()
{
if($this->request->is('post')) {
if($this->Auth->login()) {
$this->Session->setFlash('Connexion établie', 'flash_success');
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash("Nom d'user ou mot de passe invalide, réessayer", 'flash_error');
$this->redirect(array('controller' => 'indexes', 'action' => 'index'));
}
}
}
It works very well, but I need to change it. In my database I have a field "validate" which is a boolean.
On login I want to log user if the field is true but I don't want to log him if the field is on false.
Thanks for help
You need the scope field. You can either add it on the beforeFilter method For example:
public function beforeFilter() {
$this->Auth->authenticate = array(
'YourAuthComponent' => array(
'fields' => array(
'username' => 'username',
'password' => 'password'
),
'userModel' => 'Users.User',
//This is what you need
'scope' => array(
'User.active' => 1,
'User.verified' => 1)
)
);
}
Or you can add the option to your components array at your AppController or UsersController.
class AppController extends Controller {
/**
* Components used from the application
*
* #var array
*/
public $components = array(
'Auth'=> array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email'),
'scope' => array(
'User.active' => 1,
'User.verified' => 1)
)
)
)
),
);
}

CakePHP 2.x Auth Custom Username (Code in place but not working!)

Sorry - Hate to ask but I've spent hour's working this out and researching but havent had any luck.
CakePHP (running the latest version) seems to refuse to use the fields setting (So that I can use the email column in the database as the username). If I set it to 'email' which is the field I wish to use from the database it simply refuses to login stating incorrect details. Cant get any output from SQL in DebugKit for some reason. Although when it's set to username as per below it works fine just using a 'temp' column in the DB. I've tried putting it in the components var but had no luck with that either. What could I be doing wrong? Debug is on, cant see any errors in the log or browser.
The model does contain an email column.
Controller/AppController.php
class AppController extends Controller {
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'allow' => array('login','logout'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
'authorize' => 'Controller'
)
);
function beforeFilter() {
Security::setHash('md5');
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array(
'username' => 'username',
),
),
);
}
}
Controller/UserController.php
class UsersController extends AppController {
public $uses = array('User');
public function beforeFilter() {
parent::beforeFilter();
}
public function isAuthorized($user){
return true;
}
public function login() {
$this->layout = 'login';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid username or password, try again','flash_error');
}
}
}
public function logout() {
$this->layout = 'login';
$this->Session->setFlash('Successfully logged out!','flash_success');
$this->redirect($this->Auth->logout());
}
}
View/Users/login.ctp
<?php
$this->set('title_for_layout', 'Login');
echo $this->Session->flash();
echo $this->Session->flash('auth','flash_info');
echo $this->Form->create('User', array(
'action' => 'login'
));
echo $this->Form->input('username',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Email:'
));
echo $this->Form->input('password',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Password:'
));
echo $this->Form->submit('Login', array(
'class' => 'submit',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to change the name of the field on your form from username to email. Just setting the label to "email" is not enough.
echo $this->Form->input('email',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Email:'
Try updating the components code in your appController to add the authenticate values to the Auth array like this:
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'allow' => array('login','logout'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
'authorize' => 'Controller',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);

cakephp: authen function auto direct to user/login

i am beginning with cakephp framework, i use auth to create a login form, at my appcontroller i add:
class AppController extends Controller {
public $components = array('Auth', 'Cookie');
public function beforeFilter(){
$this->Auth->authenticate = array(
'Form' => array(
'userModel' => 'User',
'fields' => array('name' => 'name', 'password' => 'password'),
)
);
$this->Auth->loginAction = array('controller' => 'TestOnlineSystem', 'action' => 'P001');
$this->Auth->loginRedirect = array('controller' => 'TestOnlineSystem', 'action' => 'index');
$this->Auth->loginError = 'Failed to login';
$this->Auth->authError = ' ';
}
}
but when i run TestOnlineSystem/P001 it auto redirect to users/login anh show message net controller usercontroller. How can i fix it, P001 is my login page
I agree with thaJeztah, try the standard config from http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html :
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'TestOnlineSystem',
'action' => 'P001',
'plugin' => 'users'
)
)
);
and get rid of your beforeFilter().

Cakephp 2.0 Authentication using email instead of username

In my view I have:
<?php
echo $this->Form->create('User', array("controller" => "Users", "action" => "login", "method" => "post"));
echo $this->Form->input('User.email', array("label" => false));
echo $this->Form->input('User.password', array("label" => false, 'class' => 'password-input'));
echo $this->Form->end(); ?>
In my AppController:
public $components = array(
'Session',
'Auth'
);
function beforeFilter(){
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
}
In my UsersController:
function beforeFilter(){
$this->Auth->allow('sign_up', 'login', 'logout', 'forgot_password');
return parent::beforeFilter();
}
public function login() {
if ($this->Auth->login()) {
$this->Session->setFlash(__('Successfully logged in'), 'default', array('class' => 'success'));
$this->redirect($this->Auth->redirect());
} else {
if (!empty($this->request->data)) {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array('class' => 'notice'));
}
}
}
But the login is not working, what am I missing?
Thanks.
I believe the problem is:
function beforeFilter(){
$this->Auth->fields = array(
'username' => 'email',
'password' => 'password'
);
}
That was how custom login fields were specified in CakePHP 1.3. CakePHP 2.0 instead requires you to specify these fields in the public $components = array(...);. The 1.3 API shows that Auth has a $fields property, but the 2.0 API shows that there is no longer a $fields property. So you must:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
More information can be found at: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers
Please tell me how it works out!
Final solution for my problem. Thank you.
I had a problem with userModel and I write this:
'Auth' => array(
'userModel' => 'Member'
)
instead of this:
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'Member'
)
)
)

Resources