I started off with a G2 standalone site but then decided I wanted to integrate with Drupal. Whilst gallery.module was excellent, I already had some users in G2 that I wanted to import, hence this patch.

The user login process is altered. 'hook_form_alter' is used to override the call to 'user_login_validate' when a user logs in.

If the username provided at login exists in Drupal, 'user_login_validate' is called and normal login resumes.

If the Drupal user doesn't exist an attempt is made to get a GalleryUser object via the username. If an object is returned and the password can be validated, the user is imported into Drupal.

gallery.module settings have been added to allow G2 groups to be imported as Drupal roles (names prefixed if desired) and imported users to be automatically put into specific roles (in addition to any existing mappings to G2 groups). G2 Admins can be added to different roles.

Unfortunately I couldn't perform a bulk user import as G2 uses a different password hashing system, so we have to grab the plaintext password at user login, validate that against G2 but then store the password using the Drupal hashing system.

Comments

kiz_0987’s picture

Thanks for the good work. I think that the gallery module really needs a bulk sync as the code for single user sync is horrible, due to all the potential error conditions. This could be reduced by a bulk sync or a dedicated sync manager I believe -- I hope that G2.3 will include some embed features to allow this.

For your code it would be good to have a bulk sync also, but with the passwords not being synced until the user logs in.

barkbarkuk’s picture

Version: master » 4.7.x-1.x-dev
StatusFileSize
new28.46 KB

For your code it would be good to have a bulk sync also, but with the passwords not being synced until the user logs in.

Agreed. It would be nice... AND... hopefully this patch does just that (replacing the original patch rather than supplementing it).

It's a bit hacky. The 'admin/user/gallery' page now has an 'Import all users' button that is displayed when a username exists in G2 but not in Drupal (We don't care about the anonymous user). This page kinda seemed like the most logical place to put the button, although maybe the settings page would be.

When 'Import all users' is clicked we 'import' all the usernames not in Drupal yet, specifying the password as ''.
When the Drupal user is created, md5('') is stored in the DB as the password hash. The gallery module's hook_user would normally attempt to update the G2 password at this point, but I've looked for a md5('') match and preserved the G2 password in this case.

As soon as is possible the password is modified in the Drupal DB to be 'G2PASS', which won't match the md5 of any password supplied just incase someone tries to supply it.

When a user attempts a login and the gallery import functionality is enabled, if a user exists in Drupal with the password hash set to 'G2PASS', the password is verified against G2 and if it matches the Drupal password is set accordingly.

I've not done anything more than a basic smoke test. The original patch functionality still works, so users that exist within G2 can log into Drupal without having to be specifically imported.

kiz_0987’s picture

Status: Active » Needs review
profix898’s picture

Status: Needs review » Fixed

A feature to bulk import users/groups from Gallery2 is included in the 5--2 branch of the module.

profix898’s picture

Version: 4.7.x-1.x-dev » 5.x-2.x-dev

A feature to bulk import users/groups from Gallery2 is included in the 5--2 branch of the module.

Anonymous’s picture

Status: Fixed » Closed (fixed)