Can't change 'userModel' using AuthComponent in CakePHP - php

I'va been trying to change the 'userModel' from the default 'user' to 'usuario'. I'va done this before in CakePHP 1.3 but I can't get it to work using the lastest version.
Here's my code (AppController.php):
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Auth' => array(
'loginError' => "Nombre de usuario o contraseña incorrectos.",
'authError' => "Debes ingresar con tu cuenta de usuario.",
'loginRedirect' => array('controller' => 'administrador', 'action' => 'productos'),
'logoutRedirect' => array('controller' => 'usuarios', 'action' => 'login')
),
'Session',
'Email'
);
public function beforeFilter() {
$this->Auth->authenticate = array(
'Basic' => array('userModel' => 'Usuario'),
'Form' => array('userModel' => 'Usuario')
);
}
}
Thanks in advance.
EDIT: The component redirects me to "/users/login" instead of "/usuarios/login" and the login form in "/usuarios/login" doesn't work. It's like I never changed the userModel.

Try doing this while initializing the Auth Component
In you AppController:
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'Usuario',
'fields' => array(
'username' => 'username',
'password' => 'password'
)
)
)
)
);

Set custom UserModel is nothing to do with Auth::loginAction which is point to /users/login by default. You can override it in Controller::beforeFilter() callback or Controller::$components array. Hope help.

Change you $components array to this:
public $components = array(
'Auth' => array(
'loginError' => "Nombre de usuario o contraseña incorrectos.",
'authError' => "Debes ingresar con tu cuenta de usuario.",
'loginRedirect' => array('controller' => 'administrador', 'action' => 'productos'),
'logoutRedirect' => array('controller' => 'usuarios', 'action' => 'login'),
'loginAction' => array('controller' => 'usuario','action' => 'login','plugin' => null),
'authenticate' => array('Form' => array('userModel' => 'Usuario')
),
),
'Session',
'Email'
);

Try this code
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Auth' => array(
'loginError' => "Nombre de usuario o contraseña incorrectos.",
'authError' => "Debes ingresar con tu cuenta de usuario.",
'loginAction' => array('controller' => 'aucusers','action' => 'login'),
'loginRedirect' => array('controller' => 'administrador', 'action' => 'productos'),
'logoutRedirect' => array('controller' => 'usuarios', 'action' => 'login')
),
'Session',
'Email'
);
}
i hv added this the folowing line
'loginAction' => array('controller' => 'aucusers','action' => 'login'),

Related

Modifying userModel is totally ignored in Cakephp

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?

Auth->authorize works with beforeFilter but not $components

In AppController I have
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'profile', 'action' => 'index'),
'logoutRedirect' => array(
'controller' => 'homepage',
'action' => 'index',
'home'
),
'authorize' => 'Controller'
),
);
I've tried it with 'authorize'=> array('Controller') to no avail.
However, the isAuthorized function never gets called.
When I add the line $this->Auth->authorize = 'Controller'; to a specific controller's beforeFilter, it works. What's going on?

Auth repeats controller in URL

I am setting for the first time the Auth component on my site, and everything seems to work fine except when I try to access a restricted page. Instead of being redirected to http://localhost/MySite/users/login, I get redirected to http://localhost/MySite/users/users/login, the controller name is repeated on the url. How can this issue be fixed?
I am using CakePhp 2.4.4
AppController
class AppController extends Controller {
public $components = array('DebugKit.Toolbar',
'Session','Auth' => array(
'loginRedirect'=> array(
'controller' => 'admins',
'action' => 'admin_index'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
'plugin' => 'users'
),
'authError' => 'Não tem permissão para aceder a esta área. Por favor faça login.',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'username', 'password' => 'password'
),
'userModel' => 'User'
)
),
'authorize' =>array('Controller'
)
)
);
public function beforeFilter(){
$this->Auth->allow('index','ShowImages','ShowShowbill','ShowVideos','ShowContactUs','contact','login','DisplayMusic','DisplayEntertainment','DisplayPromotion','DisplayStaff','DisplayEquipments');
}
In Auth component you need to add 'unauthorizedRedirect' otherwise Cake tries to redirect to /{app-directory} (this was giving me a headache yesterday).
public $components = array(
//your other components
'Auth' => array(
//your other options for Auth
'unauthorizedRedirect' => '/home'
)
);
This would direct any user trying to access a page they shouldn't be allowed on to 'yourDomain/home'.

How do I allow non-authenticated users to access content in CakePHP?

I have a site in cakephp 2.x where I want that a guest user (not logged in) can see same pages:
users/login
users/forgot_password
users/reset_password
I have AuthComponent that can't access my page. I can access to users/login but not to forgot_password and reset_password, if i try to ccess always redirect to the login page.
This is my AppController with AuthComponent:
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array('controller'=>'users','action'=>'login', 'admin'=>false),
'logoutRedirect' => array('controller'=>'users','action'=>'login'),
'loginRedirect' => array('controller'=>'projects', 'action'=>'index'),
'authError' => 'Questa risorsa non sembra appartenere al tuo account, oppure non hai eseguito l\'accesso',
'autoRedirect' => false,
'authorize' => array(
'Controller',
'Actions' => array(
'actionPath' => 'controllers'
)
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
In this case I can't access to forgot_password and reset_password.
But if I change it to this:
public $components = array(
'Session',
'Auth' => array(
'loginAction' => null,
'logoutRedirect' => array('controller'=>'users','action'=>'login'),
'loginRedirect' => array('controller'=>'projects', 'action'=>'index'),
'authError' => 'Questa risorsa non sembra appartenere al tuo account, oppure non hai eseguito l\'accesso',
'autoRedirect' => false,
'authorize' => array(
'Controller',
'Actions' => array(
'actionPath' => 'controllers'
)
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
I have set null to loginAction in this case I can access to forgot_password and reset_password but page are blank without errors, body are empty. These page are simply html without query like this:
controller action
public function forgot_password(){
}
view
<div>
<p>RESTORE PASSWORD</p>
</div>
Someone can help me? Thanks
In your beforeFilter() callback, add $this->Auth->allow('forgot_password');
Api: AuthComponent:allow()

active/inactive check not working while login cakephp

I am having a user table with an active field datatype enum(1,0) for my cake php application
I want to dis allow the inactive users from logging into my application. For this i added this code in my Appcontroller.php
public $components = array('Acl', 'Session',
'Auth' => array('authorize' => array('Controller'),
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'Form' => array('userModel' => 'User'),
'all' => array('scope' => array('User.active' => 1)))
);
My user model is User
But this is allowing the in active users also to login to the application.
What am i doing wrong?
Thanks
please check your array it is wrong assing
check below array and it will work for you
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'users',
'action' => 'login'
),
'authError' => 'Je hebt geen toegang tot dit gedeelte',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email'),
'scope' => array('is_admin' => '1')
),
)
),
'Session'
);
Whenever I've dealt with enums in CakePHP, their value is always a string. Therefore I think the problem you're encountering is that 1 is not equal to '1'.
For your active field, try using the BOOLEAN datatype which is a synonym of TINYINT(1). You should then write in your Auth component configuration:
public $components = array('Acl', 'Session',
'Auth' => array('authorize' => array('Controller'),
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'Form' => array('userModel' => 'User'),
'all' => array('scope' => array('User.active' => true)))
);
Semantically, I feel this makes more sense.

Categories