I am facing problems trying to use cakephp auth component - cakephp

when i try to login using auth componente i do not get any error but it doesn't login and doesn't redirect-me to any other page. Please help me. Here is the UtilizadoresController code:
<?php
App::uses('AppController', 'Controller');
/**
* Utilizadores Controller
*
* #property Utilizadore $Utilizadore
* #property PaginatorComponent $Paginator
* #property SessionComponent $Session
*/
class UtilizadoresController extends AppController {
public $components = array('Paginator', 'Session');
public $helpers=array('Session');
function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('add');
if($this->action=='add' || $this->action=='edit'){
$this->Auth->authenticate=$this->utilizadore;
}
}
function login(){
}
function logout(){
$this->redirect($this->Auth->logout());
}
}
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'utilizadores',
'action' => 'login'
)
)
);
public $helpers=array('Session');
function beforeFilter(){
$this->Auth->allow('index','view');
$this->Auth->userModel = 'utilizadore';
$this->Auth->authError='Por favor, registe-se para vizualizar a pagina.';
$this->Auth->loginError='Passord ou Username incorrectos.';
$this->Auth->loginRedirect=array('controller'=>'fichadeobras','action'=>'index');
$this->Auth->logoutRedirect=array('controller'=>'fichadeobras','action'=>'index');
$this->set('admin', $this->_isAdmin());
$this->set('logged_in', $this->_loggedIn());
$this->set('utilizadores_username',$this->_utilizadoresusername());
}
function _loggedIn(){
$logged_in = FALSE;
if($this->Auth->user()){
$logged_in =TRUE;
}
return $logged_in;
}
function _utilizadoresusername(){
$utilizadores_username=NULL;
if($this->Auth->user()){
$utilizadores_username=$this->Auth->utilizadore('username');
}
return $utilizadores_username;
}
}

Related

How to apply Role based authorization after login in cakephp 2.7?

I'm new on cakephp . I have implementd a code for login and I m trying to providing Role based action for differecnt users.
Such as I have a table in which i store id and role (Admin,Normal,SubAdmin).
I want that super admin can add delete update and edit and view everones record.
Admin can only edit and delete add users and cant delete/edit his record.
How could i achieve this.
you can do by use this code
In your App Controller :
class AppController extends Controller {
public $components =array(
'Session',
'Flash',
'Auth'=>array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'Email','password'=>'Passward'),
)),
'loginRedirect'=>array('controller'=>'Users','action'=>'index'),
'logoutRedirect'=>array('controller'=>'Users','action'=>'login'),
'authError'=>"You Can't access this page",
'authorize' => array('controller')
)
);
public function beforeFilter()
{
$this->Auth->allow('index');
}
public function isAuthorized($user)
{
return true;
}
}
In your Users Controller:
class UsersController extends AppController {
public $helpers = array('Html', 'Form','Session','Flash');
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('add');
}
public function isAuthorized($user) {
// The owner of a post can edit and delete it
if (in_array($this->action, array('edit', 'delete'))) {
switch ($user['Role']) {
case "Super user":
return true;
break;
case "Admin":
if($user['id']==$this->request->params['pass'][0])
{
return false;
}
else
{
return true;
}
break;
default:
return false;
}
}
return true;
}

when i try $this->Auth->user() it work but when i add id $this->Auth->user('id') it don't work it is null

$this->Auth->user('id') always null
appcontrollers code :
<?php
App::uses('AuthComponent', 'Controller/Component');
App::uses('Controller', 'Controller');
class AppController extends Controller {
//public $components = array('DebugKit.Toolbar','Session');
public $components = array(
'Acl',
'Flash',
'Auth' => array(
'authorize' => array(
'Actions' =>
array('actionPath' => 'controllers','action'=>'index')
)
),
'Session'
);
public $helpers = array('Html', 'Form', 'Session');
public function beforeFilter() {
//Configure AuthComponent
$this->Auth->allow();
$this->Auth->loginAction = array(
'controller' => 'personnes',
'action' => 'login'
);
$this->Auth->logoutRedirect = array(
'controller' => 'personnes',
'action' => 'login'
);
$this->Auth->loginRedirect = array(
'controller' => 'biens',
'action' => 'index'
); }}
Personnescontrollers
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow();
}
public function login() {
if ($this->request->is('post')) {
$user = $this->Personne->findAllByLoginAndMdp
($this->data['Personne']['login'],$this->data['Personne']['mdp']);
if ($this->Auth->login($user)) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__
('Votre nom d\'user ou mot de passe sont incorrects.'));
}
}
}
bienscONTROLLERS
public function index( ) {
$this->Bien->recursive = 0;
$this->paginate = array(
'paramType' => 'querystring',
'limit' => 5
);
$biens = $this->paginate('Bien');
$this->set(compact('biens'));
$user = $this->Auth->user('id');
Debugger::dump($user);
}
Personne model`
<?php
App::uses('AuthComponent', 'Controller/Component');
App::uses('AppModel', 'Model');
class Personne extends AppModel {`
public function beforeSave($options = array()) {
$this->data['Personne']['mdp'] =
AuthComponent::password($this>data['Personne']['mdp']);
return true;
}
when i try $this->Auth->user() it work but when i add id $this->Auth->user()'id' it don't work
Plz what can i do ? can someone help me ?

cakephp auth component is not working

class UsersController extends AppController {
var $uses = array('User', 'Feed', 'Author', 'Comment', 'Tag', 'SingleArticle', 'Category');
var $helpers = array('Html', 'Form');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('signup');
}
public $components = array('Session', 'RequestHandler',
'Auth' => array(
'logoutRedirect' => array('controller' => 'users', 'action' => 'index'),
'authError' => "you can't access that page",
'authenticate' => array(
'Form' => array(// THIS IS WHERE YOU CHANGE THE DEFAULT FIELDS
'fields' => array('email' => 'email', 'pwd' => 'pwd'),
'passwordHasher' => 'Blowfish'
)
)
)
);
public function isAuthorized($user) {
return true;
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect(array('controller'=>'users','action'=>'mind'));
} else {
$this->Session->setFlash('login failed');
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
this is my userscontroller.php.
my appcontroller.php
public $components = array('Auth', 'RequestHandler');
public $pageTitle;
public function beforeFilter() {
$this->Auth->allow('index');
parent::beforeFilter();
}
my user.php
<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public $name = 'User';
var $useTable = 'fvf_users';
public function beforeSave($options = array()) {
if (isset($this->data['User']['pwd'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data['User']['pwd'] = $passwordHasher->hash(
$this->data['User']['pwd']
);
}
return true;
}
}
please help me,my login page not worked.it will redirected to the same login page only.
i used the password hasing for blowfish method. please help me.thanks in advance.
which part is mistake in my code.what is the default password encryption method in cakephp.
Cakephp LOGIN not working...:(
Your fields array in config is incorrect. Assuming you db fields are email and pwd. The array needs to be 'fields' => array('username' => 'email', 'password' => 'pwd'). Your login form field names would also be email and pwd.

CakePHP: AUTH->LOGIN()... whether user/pass wrong...it redirects to index page [duplicate]

This question already has answers here:
Login Script in 2.4.2 is not working
(2 answers)
Closed 8 years ago.
my problem is : 'when i enter wrong username-password combination, it still redirects me to index page, while it should be redirected to login page again '.. what is the problem ??Pl help me... i am atteching my code...
here is AppController:
class AppController extends Controller {
public $components=array('DebugKit.Toolbar',
'Session','Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'login'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'index')
));
public function beforeFilter(){
$this->Auth->allow('index','register');
}
}
here is UsersController:
class UsersController extends AppController
{
public $name='Users';
public $uses=array('user');
public $helpers = array('Html', 'Form','Session');
public function beforeFilter() {
parent::beforeFilter();
}
public function index()
{
}
public function login() {
if ($this->request->is('post')) {
/* login and redirect to url set in app controller */
if ($this->Auth->login($this->request->data)) {
$this->Session->setFlash(__('Successful!!!'));
$this->Session->write('user',$this->data['user']['username'],time()+3600);
return $this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
// else{ echo "fail";exit;}
}
public function logout() {
/* logout and redirect to url set in app controller */
$this->Session->destroy();
return $this->redirect($this->Auth->logout());
}
public function register() {
if ($this->request->is('post')) {
$this->user->create();
if ($this->user->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('controller' => 'users','action' => 'index'));
}
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
here is User Model:
<?php
class User extends AppModel
{
var $name='User';
var $validate=array(
'username'=> array(
'rule'=>'notEmpty',
'message'=>'Enter Your USername'),
'password'=>array(
'rule'=>'notEmpty',
'message'=>'Enter your Password'
),
'Confirm_password'=>array(
'rule'=>'match',
'message'=>'password not match, try again'
)
);
public function match(){
if($this->data['user']['password']===$this->data['user']['Confirm_password'])
{
return true;
}
return false;
}
public function beforeSave($options = array()) {
/* password hashing */
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
?>
here is my login ctp:
<h2>LOGIN-MYSITE</h2>
<?php echo $this->Form->create('user',array('action'=>'login'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('LOGIN');
?>
Remove 'index' from
$this->Auth->allow('index','register');
Keep
$this->Auth->allow('register');

Auth login action not generating query in CakePHP

I have a simple login form, just like the Cake Blog Tutorial.
It works like a charm when I use 'UsersController' and 'User' model naming conventions, passing the rights queries in debug.
But when I change it to other name, Alunos in my case, it generates no QUERY and give me 'Incorrect username and/or password.'.
My login.ctp
<H1> Login </H1>
<?php
debug($this->data);
echo $this->Form->create('Aluno', array('action' => 'login'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>
My AppController
<?php
class AppController extends Controller {
public $components = array (
'Session',
'Auth' => array (
'loginAction' => array ('controller'=>'alunos', 'action'=>'login'),
'loginRedirect'=>array ('controller'=>'alunos', 'action'=>'inicio'),
'logoutRedirect'=>array ('controller'=>'alunos', 'action'=>'index'),
'authError'=>"Ops, você não está autorizado a fazer isso.",
'authorize'=>array('Controller'),
)
);
public function isAuthorized($user) {
return true;
}
public function beforeFilter() {
$this->Auth->allow('index', 'add');
$this->set('logged_in', $this->Auth->loggedIn());
$this->set('current_user', $this->Auth->user());
}
}
And my 'AlunosController.php' (see that its not USERSController, like common codes)
<?php
class AlunosController extends AppController {
public $name = 'Alunos';
public function beforeFilter(){
parent::beforeFilter();
}
public function index() {}
public function login(){
debug($this->Auth->login());
if ($this->request->is('post')) {
if ($this->Auth->login()){
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Incorrect username and/or password.');
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
public function add() {
debug($this->Auth->login());
if($this->request->is('post')) {
if ($this->Aluno->save($this->request->data)) {
$this->Session->setFlash('Cadastrado.');
}else {
$this->Session->setFlash('Falha no cadastro.');
}
}
}
public function inicio() {
debug($this->Auth->login());
}
}
?>
My debug($this->data) in login.ctp result:
array(
'Aluno' => array(
'password' => '*****',
'username' => 'anyuser'
)
)
What am I doing wrong?
Add this code to your app controller:
function beforeFilter() {
$this->Auth->userModel = 'Aluno'; <-- Should be singular. My mistake
parent::beforeFilter();
}
UPDATE FOR CAKE2
// Place in beforeFilter() of AppController.php
$this->Auth->authenticate = array(
'Form' => array(
'userModel' => 'Aluno'
)
);
Your problem is because you are not telling cake what to use for a user table. This is why your first instance works, and the second does not.
Change this:
echo $this->Form->create('Aluno', array('action' => 'login'));
to:
echo $this->Form->create('Alunos', array('url' => 'alunos/login'));
To call Alunos Controller's login() method.

Resources