07.19
Been working like a maniac recently integrating a Joomla 1.5 powered website with a remote user source via RESTful interface. REST is a great thing, and makes it dead simple to scale and shard your applications at the network level. However Joomla has an attitude about primary keys, or more specifically JTable. So when it came time for me to stuff user ids from this remote REST source, JTable said “no way, amigo.”
JTable, in this specific instance, won’t let you change the values in the jos_users.id column. Unfortunately that was exactly what I needed to do. In the end, I had to hotrod my authentication plugin to check to see if the jos_users.id was the same as the id coming over from the REST calls – and if not, manually update jos_users and jos_acl_aro_map.
I created an authentication plugin that consumes the REST service. In the onAuthenticate method I take the results of the REST call and stuff them in the session thusly:
- $session =& JFactory::getSession();
- $session->set("user",$response,‘mynamespace’);
For the user plugin, I created an onAfterStoreUser method that takes the values stuffed in the session above, and compares them to the existing JUser object properties. Right now all I am really looking for is the jos_users.id as that is what I cannot take care of with the stock facilities provided by Joomla. The method looks strikingly like this:
- function onAfterStoreUser($user, $isnew, $success, $msg)
- {
- global $mainframe;
- // Get the correct user_id out of REST session object
- $session =& JFactory::getSession();
- $object = $session->get("user",‘empty object’,‘mynamespace’);
- $gid = $object->id;
- if($gid != $user[‘id’])
- {
- // Now fix Joomla’s broken IDs in jos_users
- $db =& JFactory::getDBO();
- $sql = ‘UPDATE #__users SET id = ‘ . $gid . ‘ WHERE id = ‘ . $user[‘id’];
- $session->set(‘auth_plugin_sql’, $sql);
- $db->setQuery($sql);
- $result = $db->query();
- // Now update the ACL mapping for this user as the id is different
- $sql_acl = ‘UPDATE #__core_acl_aro SET value = ‘ . $gid . ‘ WHERE value = ‘ . $user[‘id’];
- $db->setQuery($sql_acl);
- $result_acl = $db->query();
- }
- }
Now you have a system that fetches user data from a RESTful service, and populates the Joomla tables to make JUser happy.
Look ma, no core hacks!
One ugly ‘feature’ of this system is that on the first login, the user finds themselves in a state where they attempt to authenticate with one id and it gets updated. Since I have no way to tell JUser to reload itself, you have this strange experience of logging in twice and then everything is golden.
Interested in hearing how others have dealt with this problem.




No Comment.
Add Your Comment