Problem: You'd like to share some data between two different drupal websites without changing any Drupal code or otherwise having to lift much of a finger. But you don't want to share information unless it meets certain requirements (for example, only users labeled "foo" in somedatabase.sometable) For our example, we'll use the users table.

Solution: Use MySQL5 Views (http://dev.mysql.com/doc/refman/5.0/en/views.html)

Our "master" users table resides in a database called "master". The database of the site that will have restricted access to our masters users table is called "banana". Assumming you're starting with a fresh instance of the Drupal schema in your database "banana", do this:

mysql> use banana;
mysql> drop table users;
mysql> CREATE VIEW users AS
mysql>  SELECT *
mysql>  FROM master.users 
mysql>  WHERE uid IN (
mysql>    SELECT uid
mysql>    FROM somedatabase.sometable
mysql>    WHERE uid = 0
mysql>     OR label = 'foo'
mysql>  ) 

Drupal will use banana.users just as it would a normal users table. No other modifications are necessary. Now only "foo" users will be included in the users table for your banana website.

Note: Drupal has a dependency that is not really documented. Every users table must have an entry that contains uid=0. It's a "stub" entry that Drupal needs to function properly. A workaround for this dependency is to include "user 0" in the results set that defines your view.

See, wasn't that easy?

Comments

greg.harvey’s picture

This was news to me until today, but see this comment from Damien Tournoud:
http://drupal.org/node/292367#comment-956572

With that behaviour supported by the core, MySQL Views are not required at all. =)

--
http://www.drupaler.co.uk/

eoneillPPH’s picture

Would a view be the way to handle that? If so, how?

In our case, the user database we'd like to "share" is on the same db server, but has its own structure. We're trying to allow our installed user base to be "logged in" when they come over to our new Drupal site(s). Been trying to force a user_save to accomplish this, but have been unable to get it to work right.

Also, I don't think we want to JUST use the external user table, but to allow those users to be automatically registered and logged in on our Drupal sites.

greg.harvey’s picture

A view could certainly do that - you could join your existing, non-Drupal users table on your Drupal users table within a view, as long as you can marry up the fields (e.g. every Drupal user table field has an equivalent in your non-Drupal app, or at least the Drupal field is permitted to be empty).

Or you could look at alternative shared sign-on solutions designed for wider integration, such as LDAP.

Or you could look at writing your own single sign-on around web services for disparate applications. I've seen this done effectively. In principle it's very simple. We used a system like this at Dow Jones, where a central web service was responsible for saying if someone had access to an app or not, and providing it responded accordingly each app handled it's own local sessions, etc.

--
http://www.drupaler.co.uk/

Harry Slaughter’s picture

Using $db_prefix is perfect if you want to share *all* the content in the alternate table. But if you want to limit what you see with some arbitrary parameter (say only users with the admin role), database views are probably the easiest way to go.

--
Devbee - http://devbee.net/

nicodv’s picture

Hi.

I have two different sites, with two different users tables (obvious) and both are imported from salesforce. I want to use both tables in one and only drupal installation... Is it possible? How?

Thanks in advance for any light you can throw in my path :)

THE VERY LITTLE AGENCY

Harry Slaughter’s picture

You could create a view that joins the two tables, but I'm guessing that's going to get expensive (though I don't know much about database tweaking/optimization that may alleviate this cost).

I'd be much more inclined to import the users into Drupal and maybe set up a scheduled sync if the source data changes regularly.

--
Devbee - http://devbee.net/