Defining a custom table for AuthComponent

I've created a simple CMS to manage several small websites built on CakePHP. After successfully migrating the plugin from 1.3 to 2.0 I'm running into a problem with the AuthComponent after updating Cake to the latest version 2.1.2.

The classnames of the plugin are all prefixed by the plugin's name to avoid duplicate classnames, as the application shares most of its tables with the plugin. So there are cases when I have a UsersController for the main application and a PluginNameUsersController for my CMS plugin (or a PostsController and a PluginNamePostsController). The plugin models rely on $useTable to find the correct database table (so PluginNamePostsController still uses posts).

Since upgrading to the 2.1.* branch of Cake the AuthComponent has ceased to work as it expects a non-existent pluginname_users table instead of referring to users.

Is it possible to define a custom table for the AuthComponent or is there any other method to get this working? Also, is this behaviour expected?

The component is configured in PluginNameAppController as follows:

  public $components = array(
    'Session', 'RequestHandler',
    'Auth'=> array(
      'loginAction' => array(
        'controller' => 'pluginname_users',
        'action' => 'login',
        'plugin' => 'pluginname'
      ),
      'loginRedirect' => array(
        'controller' => 'pluginname_posts',
        'action' => 'index',
        'plugin' => 'pluginname'
      ),
      'authenticate' => array(
        'Form' => array(
          'userModel' => 'PluginNameUser',
          'fields' => array('username', 'password')
        )
      )
    )
  );

Answers


The AuthComponent is actually unaware of tables and such. By using a custom model, you can define $table on it to work on a different table. So, the first thing is to set up your plugin's model to use the proper table:

class PluginNameUser extends PluginAppModel {
  public $table = 'users';
}

Then, tell your authentication methods to use the plugin's model by using the dot syntax.

'authenticate' => array(
   'Form' => array(
     'userModel' => 'PluginName.PluginNameUser',
     'fields' => array('username', 'password')
   )
 )

Now when FormAuthenticate tries to authenticate, it will try and find the user using that model, which it now knows is in PluginName. It loads the model, which you've set to use the users table, and looks for the user there.


Need Your Help

Is it possible to add arbitrary data to an ObjectifiedElement instance?

python lxml

I've set up a custom namespace lookup dictionary in order to map elements in XML files to subclasses of ObjectifiedElement. Now, I want to add some data to instances of these classes. But due to th...

Send E-mail if date has passed using php

php sql email

I'm trying to make an online sign up sheet system. I was just wondering if when the date of the event had passed would I be able to send a list of all the people who have signed up to the event (fr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.