Hi All,
  I am facing a problem with cakePHP Authentication code. I have a
users table as

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL auto_increment,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `user_type` enum('A','N') NOT NULL COMMENT 'A:Admin, N:Normal',
  `status` enum('0','1','2') NOT NULL COMMENT '0: disabled, 1:valid,
2:deleted',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


On this table I have 2 models, 'Users' and 'Admin'. Their code is as

user.php
class User extends AppModel {
        var $name = 'User';
        var $displayField = 'email';

        function beforeFind(&$conditions) {
                // force to fetch only 'Normal' users
                if (is_array($conditions['conditions']) || empty($conditions
['conditions'])) {
                        $conditions['conditions'][$this->name.'.user_type'] = 
'N';
                        $conditions['conditions'][$this->name.'.status !='] = 
'2';
                } else {
                        $patterns[0] = "/(".$this->name.".user_type = 
'[A-Z]')?/";
                        $replacement[0] = $this->name.".user_type = 'N'";

                        $patterns[1] = "/(".$this->name.".status = '[0-9]')?/";
                        $replacement[1] = $this->name.".status != '2'";

                        $conditions['conditions'] = preg_replace($patterns, 
$replacement,
$conditions['conditions']);
                }
        }
}

admin.php
class Admin extends AppModel {
        var $name = 'Admin';
        var $useTable = 'users';
        var $displayField = 'email';

        function beforeFind(&$conditions) {
                // force to fetch only 'Admin' users
                if (is_array($conditions['conditions']) || empty($conditions
['conditions'])) {
                        $conditions['conditions'][$this->name.'.user_type'] = 
'A';
                } else {
                        $conditions['conditions'] = preg_replace("/(".$this-
>name.".user_type = '[A-Z]')?/", $this->name.".user_type = 'A'",
$conditions['conditions']);
                }
        }
}

I know I have to improve the 'else' condition statements.

In the controller I have the following code

user_controller.php
class UserController extends AppController {
        var $name = 'Users';
        var $uses = array('User', 'Thali');
        var $components = array('Auth');
        var $helpers = array('Javascript');

        function beforeFilter(){
                //Set up Auth Component
                $this->Auth->userModel = 'User';
                $this->Auth->fields =array('username' => 'email', 'password' =>
'password');
                $this->Auth->loginRedirect = array('controller' => 'user', 
'action'
=> 'index');
                $this->Auth->loginAction = array('controller' => 'user', 
'action' =>
'login');
                $this->Auth->allow('login');
                $this->Auth->authorize = 'controller';
                $this->Auth->userScope = array('User.status' => 1);
        }

        function login() {      }

        function logout() {
                $this->Session->del('thisUser');
                $this->redirect($this->Auth->logout());
        }

        function isAuthorized() {
                $thisUser = $this->Auth->user();
                $this->User->id = $thisUser['User']['id'];
                $this->Session->write('thisUser', $thisUser['User']);
                return true;
        }
}

admin_controller.php
class AdminController extends AppController {
        var $name = 'Admin';
        var $uses = array('Admin','User', 'Food', 'Thali', 'Order');
        var $layout = 'admin';
        var $components = array('Auth');

        function beforeFilter(){
                //Set up Auth Component
                $this->Auth->userModel = 'Admin';
                $this->Auth->fields =array('username' => 'email', 'password' =>
'password');
                $this->Auth->loginAction = array('controller' => 'admin', 
'action'
=> 'login');
                $this->Auth->loginRedirect = array('controller' => 'admin', 
'action'
=> 'index');
                $this->Auth->allow('login');
                $this->Auth->authorize = 'controller';
                $this->Auth->userScope = array('Admin.status' => 1);
// echo 'debug';
        }

        function login() {      }

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

        function isAuthorized() {
                return true;
        }
}

As you can see their is not much difference in the Authentication code
between the two controllers.
The Admin code works fine without any problem. But the User code does
not work as expected. After giving the login details for the user it
sometimes redirects to /localhost/ or /localhost/admin/login page.
But if I echo something in the beforeFilter() I get an header error,
after commenting this echo statement I run the code it works fine.

I did not want to create another table for admin as their would be
just a handful of them. And I also could not set the "Configure::write
('Routing.admin', 'admin');" in core.php as I need the user too login
and did not want to mix the admin and users code in a single file.

Could someone help in correcting my code as I am unable to make out
where I have gone wrong?


Regards,
Shashidhar.G

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to