for my site I need to have two different auth session because one login is for admin section (like worpdress admin "wp-admi" access), and one login is for account login into the site.
So I have thought to duplicate AppController with AppAdminController but I retrieve some error.
This is my AppAdminController
App::uses('Controller', 'Controller');
class AppAdminController extends Controller {
public $helpers = array('Html', 'Form', 'Session'); //'DebugKit.Toolbar'
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'admin', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'admin', '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' => 'username')
)
)
)
);
public function afterFilter () {
}
public function beforeFilter () {
}
}
and I use it into AdminController:
App::uses('AppAdminController', 'Controller');
App::uses('AppController', 'Controller');
class AdminController extends AppAdminController {
public $name = 'Admin';
public $scaffold;
public $uses = 'users';
public $useDbConfig = 'admin';
public function beforeFilter () {
parent::beforeFilter();
$user_type = 'guest';
if($this->Session->read('is_logged')){
$auth_user = $this->Auth->user();
$user_type = $auth_user['group'];
}
}
}
I retrieve this error:
Call to a member function read() on a non-object
into this line:
if($this->Session->read('is_logged')){
How can I fix this?
Thanks
Related
I'm a beginner on cakePHP and I have a problem with Auth Component.
The function allow of Auth doesn't work for permission of all (*). The code is this:
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'ACL',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
'Session'
);
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('*');
}
}
All actions redirecting to the login action. The function allow really doesn't work as planned.
I resolve the problem... I used the function with parameter empty. It worked
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'ACL',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
'Session'
);
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow();
}
}
Thanks
On localhost i have no problem at all. i can login access all sites and go through the process of the site without a problem.
Now when i put it remotely i get logged out as soon as i change page (aka i am being redirected to my login view no matter what link i press)
My AppController loos like this:
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
'Session'
);
public $uses = array
(
'Category'
);
public $helpers = array('Html', 'Form', 'Session');
public function beforeFilter() {
$this->Auth->allow('*');
//Configure AuthComponent
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'home', 'action' => 'index');
$this->set('menu_categories', $this->Category->find('all'));
}
}
Please tell me if you need more information (also if you wish to try this issue go to my page (my domain
log in as test password test123
I know it's an overbeaten question, but I couldn't find the solution in all the answers. Perhaps you can help me. I'm trying to log a user in and I'm getting the "Username/password invalid" error with correct data. It' my first experience with cakePHP.
To the code.
Model:
App::uses('AppModel','Model');
Class User extends AppModel {
public $useTable = 'Users';
public $hasMany = array(
'Costumer' => array(
'className' => 'Costumer',
'foreignKey' => 'users_id',
'order' => 'Costumer.name ASC'
)
);
//Suppressed the validation code, don't think it's important here
public function beforeSave($options = array()){
if (!empty($this->data['User']['pwd'])) {
$this->data['User']['passwd'] = Security::hash($this->data['User']['pwd']);
}
}
}
Controller:
App::uses('AppController', 'Controller');
class UsersController extends AppController{
public $helpers = array('Html', 'Form');
public $name = 'Users';
public $components = array('Auth','Session');
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('add');
}
public function login(){
//Tests
$userEmail = $this->User->findByEmail($this->request->data['User']['email']);
$userPass = $this->User->findByPasswd(Security::hash($this->request->data['User']['passwd']));
die(var_dump($userEmail, $userPass));
if ($this->request->is('post')) {
$this->request->data['User']['passwd'] = Security::hash($this->request->data['User']['passwd']);
if ($this->Auth->login()){
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
}
}
}
View:
<div class="row" style="margin-top: 40px;">
<div class="col-lg-8">
</div>
<div id="login" class="col-lg-2" style="background-color: #eee">
<h3>Conecte-se.</h3>
<?php
echo $this->Form->create('User', array(
'label' => 'login',
'class' => 'form-horizontal form-group'
)
);
echo $this->Form->input('email', array(
'label' => 'E-mail',
'class' => 'form-control',
)
);
echo $this->Form->input('passwd', array(
'label' => 'Senha',
'class' => 'form-control',
)
);
echo '<br />';
echo $this->Form->end(array(
'label' => 'Entrar',
'class' => 'btn btn-success'
)
);
?>
</div>
And my AppController.php has:
class AppController extends Controller {
//public $components = array('DebugKit.Toolbar');
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'passwd'),
'passwordHasher' => 'Blowfish'
)
),
'authError' => 'Para visualizar esta página, você precisa estar logado.'
)
);
public function beforeFilter(){
$this->Auth->allow('display');
$this->set('authUser', $this->Auth->user());
}
}
The crazy thing is, both UsersController's lines
$userEmail = $this->User->findByEmail($this->request->data['User']['email']);
and
$userPass = $this->User->findByPasswd(Security::hash($this->request->data['User']['passwd']));
return the user I'm trying to login, so it doesn't seem to be data error.
Guys! What am I missing here?
Thanks.
EDIT
Since I haven't found any way for doing this in an "elegant" way, I wrote a dummy workaround. It manually checks request->data values against database and manually logs the user in. That's a temporary solution, I'll be back to it later.
public function login(){
if ($this->request->is('post')) {
$user = $this->User->findByEmail($this->request->data['User']['email']);
if (!empty($user) && ($user['User']['passwd'] == Security::hash($this->request->data['User']['passwd']))){
$this->Auth->login($this->request->data);
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
}
}
}
I'm unfamiliar with the passwordHasher property of the Auth component and unfamiliar with Blowfish.
You should use Cake's built in password hasher
User model
public function beforeSave() {
if (isset($this->data['User']['passwd'])) {
$this->data['User']['passwd'] = AuthComponent::password($this->data['User']['passwd']);
}
return true;
}
AppController
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'homemk'),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'passwd'),
)
),
'authError' => 'Para visualizar esta página, você precisa estar logado.'
)
);
UsersController
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()){
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('E-mail e/ou usuário incorretos, tente novamente.'));
} // end if cannot log in
} // end if no form submitted
} // end login
I use the CakePHP Auth componenet in my web site. The code works fine in windows but after uploading it to linux online host, it give the message
Authorization adapter "actions" was not found. CakePHP
Any idea regarding the problem ?
<?php
App::uses('AppController', 'Controller');
class AppController extends Controller {
public $mobile;
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers/'),
),
),
'Session',
'RequestHandler',
);
public $helpers = array('Html', 'Form', 'Session', 'Js' => array('Jquery'));
public function beforeFilter() {
parent::beforeFilter();
// print_r($this->request); die;
if ($this->request->is('post') && isset($this->request['data']['access_token'])) {
App::uses('User', 'Model');
$this->User = new User();
// print_r($this->request['data']['access_token']);die;
$this->mobile = $this->User->authenticateMobile($this->request['data']['access_token']);
}
// print_r('APPCONT');
// print_r($this->request);
// die;
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'User'),
//'ChangeEg',
'Form'
);
//Configure AuthComponent
$this->Auth->authorize = 'actions';
$this->Auth->loginError = "Wrong credentials";
$this->Auth->authError = "This part of the website is protected.";
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'feedbacks', 'action' => 'add');
$this->Auth->logoutRedirect = array('controller' => 'home', 'action' => 'index');
}
}
I had de same problem, was a CamelCased mistake,
instead write $this->Auth->authorize = 'Controller';
i wrote $this->Auth->authorize = 'controller'.
In windows works ok, but in Linux not.
see this post https://groups.google.com/forum/?fromgroups#!topic/cake-php/wZGFoJ4ayx0
I'm using CakePHP 2.2, here is the link to tutorial that I used: link
Very important: I turned off Inflector
I don't care about ACL(it works :D), my AUTH doesn't work... $this->Auth->login() returns false...
Users controller:
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $helpers = array('Html','Form');
public $components = array('Auth' => array('authenticate' => array('form' => array('fields' => array('username' => 'login')))),'Session');
function beforeFilter() {
//$this->Auth->allow('logout', 'view');
$this->Auth->allow('*');
parent::beforeFilter();
}
function login() {
if ($this->Auth->login())
{
$this->redirect($this->Auth->redirect());
} else
{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
App controller:
App::uses('Alc', 'Controller', 'Controller');
class AppController extends Controller {
public $components = array('Auth'=>array('authorize' => array('Actions' => array('actionPath' => 'controllers'))), 'Session');
public $helpers = array('Html', 'Form', 'Session');
function beforeFilter() {
$this->Auth->userModel = 'Users';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->allow('index', 'view', 'admin', 'edit', 'login', 'logout', 'add');
$this->Auth->logoutRedirect = array('controller' => 'novosti', 'action' => 'index');
$this->Auth->loginRedirect = array('controller' => 'novosti', 'action' => 'index');
}
Users Model:
App::uses('AuthComponent', 'Controller/Component');
class Users extends AppModel {
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data['Users']['password'] = AuthComponent::password($this->data['Users']['password']);
}
return true;
}
public function bindNode($user) {
return array('model' => 'Groups', 'foreign_key' => $user['Users']['groups_id']);
}
View file:
<?php
echo $this->Session->flash('auth');
echo $this->Form->create('Users', array('action' => 'login'));
echo $this->Form->inputs(array(
'legend' => __('Login', true),
'Login',
'password'
));
echo $this->Form->end('Login');
?>
NO SQL DUMPS AVAILABLE
I went to lib/controller/components/Authcomponents.php and to lib/controller/components/auth/* and look though all those files.... and changed all Auth.User to Auth.Users; also looked though setting variables and everywhere I found I changed the model name from User to Users, and also for login fields a changed from username to Login
if (debug($this->Auth->login()))
Debug doesn't returning anything so this line will always fail.
Your usename is field is Login but the default is username and you haven't configured Auth for this.
public $components = [
'Auth' => [
'authenticate' => [
'Form' => [
'userModel' => 'Users',
'fields' => [
'username' => 'Login'
],
],
],
],
];
In your beforeSave you're using Users key rather than User. Model is singular.
Add <?php echo $this->element('sql_dump'); ?> and look at the query generated. Make sure it is correct and the password matches with your database value.
Just some stuff I noticed.
Try this in login view:
<?php
echo $this->Session->flash('auth');
echo $this->Form->create('Users', array('action' => 'login'));
echo $this->Form->inputs(array(
'legend' => __('Login', true),
'username',
'password'
));
echo $this->Form->end('Login');
?>
Auth accepts default authorize fileds as username/password if you want to use login then override auth authorization like this in controller:
$this->Auth->fields = array('username' => 'login', 'password' => 'password');
or in controller you can do this:
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array('username' => 'login', 'password' =>'password')
)
);
if($this->Auth->login($this->request->data['Users'])){
......your code...
}