Auth component in cakephp supports only UsersController? - php

I might hav askd question related to this earlier but not satisfied by answers and no answer is working.....My doubt is little different , i have two controllers
1.UsersController.
2.MembersController.
My doubt is the Auth component is working wonders for UsersControllers, but the Auth is not working for MembersController. In simple terms whenever i try to use Auth component for my MembersController, instead of redirecting to Members view. It is displaying UsersController pages....And when i delete the UsersController i get below error...
Error: UsersController could not be found.
Is there any connection between Auth and Users. How to set Auth component for my MembersController......
This is how i am using it....
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}

In your App Controller
class AppController extends Controller {
public $components = array(
'Auth' => array(
'authorize' => 'actions',
'actionPath' => 'controllers/',
'loginAction' => array(
'controller' => 'members',
'action' => 'login',
'plugin' => false,
'admin' => false,
),
),
);
}

Related

CakePHP - Controller::redirect and Auth->logoutRedirect not working

My CakePHP 2.5.3 app lives in a subdomain (domain/project_name) and apache rewrite rules are working correctly.
After I set App.fullBaseUrl='domain/project_name' in app/Config/core.php, Router::fullBaseUrl() works fine but, all the $this->Controller->redirect and all AuthComponent redirect to http://domain/project_name/project_name/controller/action.
Has anyone else encountered this and how did you fix it?
Many thanks in advance!
This is pattern for redirecting after log out:
// app/Controller/AppController.php
class AppController extends Controller {
//...
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array( // <-- Let's focus at here.
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
//...
}
Source: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html#authentication-login-and-logout
In your problem context, check logoutRedirect configuration array.
If you want handle redirecting by other ways:
public function logout() {
return $this->redirect($this->Auth->logout());
}
Source: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html#authentication-login-and-logout

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 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'.

CakePHP AbstractPasswordHasher not found error

I am trying to make my own custom password hasher. I have read CakePHP 2.4 documentation and I have followed instructions
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#creating-custom-password-hasher-classes
This is my CustomPasswordHasher Class on Controller/Component/Auth
App::uses('CustomPasswordHasher', 'Controller/Component/Auth');
class CustomPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
return $password;
}
public function check($password, $hashedPassword) {
return $password === $this->hash($hashedPassword);
}
}
These are my components on UsersController
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'authenticate' => array(
'Form' => array(
'passwordHasher' => array(
'className' => 'Custom'
)
)
)
)
);
When im trying to log in a user i got this error:
Error: Class 'AbstractPasswordHasher' not found
File: C:\Users\Jonathan\Dropbox\Public\WebSites\umbrellaApp\app\Controller\Component\Auth\CustomPasswordHasher.php
Line: 5
Notice: If you want to customize this error message, create app\View\Errors\fatal_error.ctp
i've looked up at my core and i can see AbstracrPasswordHasher
can anyone help me?
Thanks
Looks like there's a mistake in the documentation, of course it should load AbstractPasswordHasher, not CustomPasswordHasher, which is the name of the class being created.
Your App::uses() call should look like this:
App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');

How to set authError redirect action in CakePHP?

Is it a way to customize the authError URL on CakePHP? If I look into the Auth component I've placed in the AppController i have a redirect action loginRedirect and logoutRedirect but i don't know if is it possible to set something like authErrorRedirect:
<?php
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'index'),
'authError' => 'You don\'t have the right to go there.',
// something like this
'authErrorRedirect' => array('controller' => 'users', 'action' => 'login'),
'authorize' => array(
'Controller',
'Actions' => array(
'actionPath' => 'controllers'
)
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
),
'Acl'
);
Can I set an authError redirect action?
No. If you follow the Simple Authentication and Authorization Application Example # CakePHP Cookbook v2.x documentation you should redirect in the login action where the login fails for 'Invalid username or password...'. These only makes sense if you want to redirect for a different action, in the following example login2
public function 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'));
$this->redirect(array('controller' => 'users', 'action' => 'login2'));
}
}
}

Categories