0

I was trying to work with Cake PHP Auth Section in cake tutorials with my project. But it doesn't work.

I have created three tables 'admin_users' with fields(id,username,password,group_id,created,modified) , 'admin_groups'(id,name,created, modified) and 'users'(id,username,password,created,modified);

I tried normal users will be put in users table and admin users are in 'admin_users' table and grouped the admin users by 'admin_groups' for permission acess in admin side.

When I try to path '[rootpath]/users/login' gives an error Database table 'admins' for model Admin was not found. But I dont created a model for admin.

After I maually created the admins table again error "Database table 'users_admin' for model UsersAdmin was not found". Here also dont created a model for UsersAdmin.

In Users Controller

class UsersController extends AppController {

    var $name = 'Users';
    var $uses = array();
    var $components = array('Auth','Session');


    function login() {
         $this->set('title_for_layout', 'Login');
         /*if ($this->Auth->user()) {
             $this->redirect($this->Auth->redirect());
         }*/
     }

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

2 Answers 2

1

The problem is probably related to your database structure.

A bit of topic first, but if you start your project from scratch, you should probably use only one table (users) to store all users, and link them to another table (groups) to give them different rights. It could be a direct link with a field group_id in the users table (which will allow you later to use the core ACL component), or a many to many relation with a table users_groups between users and groups.

That said, there is something that Cake can't understand out of the box: you called the foreign key between admin_users and admin_groups "group_id". With this name, Cake will try to find a table called groups, not admin_groups. If you want to keep with admin_groups, the foreign key name should be admin_group_id.

Correct this first, and maybe this will resolve some of the problems you get. You may also will have to correct the AdminUser model to reflect this update and clean the cache if you are not in debug mode.

Sign up to request clarification or add additional context in comments.

Comments

0

Try like this

var $name = 'Users';
var $layout = 'main';

function beforeFilter(){

   $this->Auth->authenticate = ClassRegistry::init('User');
    parent::beforeFilter(); 
}

function index(){

    $this->set('title_for_layout', 'test site');
}


function login(){   

    Security::setHash('md5');       
    $this->set('title_for_layout', 'test site');

}


function welcome(){

}


function logout(){

    $this->set('title_for_layout', 'test site');

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

 }
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.