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')
)
)
)
);
The AuthComponent is actually unaware of tables and such. By using a custom model, you can define
$tableon it to work on a different table. So, the first thing is to set up your plugin’s model to use the proper table:Then, tell your authentication methods to use the plugin’s model by using the dot syntax.
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
userstable, and looks for the user there.