Wednesday, February 20, 2013

admin/login is not working shows "Invalid username or password, try again" and admin/logout is redirecting to users/login

Dear All,

I am a Newbie to Cake, please help me on this.

I have followed the tutorials provided by cake, and i have used the same
controller and models and views and created my admin section as like that of
user section which is provided by cake.

But i have changed the naming like

// app/Controller/AdminsController.php
<?php
class AdminsController extends AppController {

public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login','add');
}

public function index() {
$this->Admin->recursive = 0;
$this->set('admins', $this->paginate());
}

public function view($id = null) {
$this->Admin->id = $id;
if (!$this->Admin->exists()) {
throw new NotFoundException(__('Invalid admin'));
}
$this->set('admin', $this->Admin->read(null, $id));
}

public function add() {
if ($this->request->is('post')) {
$this->Admin->create();
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The Admin has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Admin could not be saved.
Please, try again.'));
}
}
}

public function edit($id = null) {
$this->Admin->id = $id;
if (!$this->Admin->exists()) {
throw new NotFoundException(__('Invalid Admin'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Admin->save($this->request->data)) {
$this->Session->setFlash(__('The Admin has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Admin could not be saved.
Please, try again.'));
}
} else {
$this->request->data = $this->Admin->read(null, $id);
unset($this->request->data['Admin']['password']);
}
}

public function delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Admin->id = $id;
if (!$this->Admin->exists()) {
throw new NotFoundException(__('Invalid Admin'));
}
if ($this->Admin->delete()) {
$this->Session->setFlash(__('Admin deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Admin was not deleted'));
$this->redirect(array('action' => 'index'));
}


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'));
}
}
}

public function logout() {
$this->redirect($this->Auth->logout());
}

}
?>


My app controller:
============

App::uses('Controller', 'Controller');

class AppController extends Controller {


public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'posts', 'action' => 'view',1),
//'logoutRedirect' => array('controller' => 'pages', 'action' =>
'display', 'home'),
'logoutRedirect' => array('controller' => 'posts',
'action' => 'view',2),
'authorize' => array('Controller') // Added this line
)
);


public function beforeFilter() {
$this->Auth->allow('index', 'view');
}

public function isAuthorized($admin) {
// Admin can access every action
if (isset($admin['role']) && $admin['role'] === 'admin') {
return true;
}

// Default deny
return false;
}
}

My admin model:
<?php
// app/Model/Admin.php
App::uses('AuthComponent', 'Controller/Component');
class Admin extends AppModel {
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
)
);


public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] =
AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
?>




But why i cant able to login and logout is redirecting to user/login, please
help me on this



--
View this message in context: http://cakephp.1045679.n5.nabble.com/admin-login-is-not-working-shows-Invalid-username-or-password-try-again-and-admin-logout-is-redirectn-tp5713754.html
Sent from the CakePHP mailing list archive at Nabble.com.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: