Deleting G2 users from embedded applications

jettyrat
jettyrat's picture

Joined: 2005-12-30
Posts: 32
Posted: Fri, 2005-12-30 04:02

Hi, maybe someone can help with this issue.

I'm toying around with an existing integration package and trying to avoid potential collision errors where a user exists in G2, but not in the embedding application during the integration and later the same user is created in the embedding application and a collision occurs when this user is externally mapped to gallery because it already exists as a gallery user.

After a couple weeks of digging through the G2 code, I finally came up with this piece of code, where $g2Id = the integer value of the user to be deleted in gallery. I finally got it running without errors, but nothing gets deleted as far as user items and the user itself.

Perhaps someone more knowledgeable with gallery could enlighten me as to how to go about deleting a G2 user from an external application without an existing external id mapping.

I hope this is the right place to post this and thanks for any insight!

list ($ret, $siteAdminGroupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
if ($ret->isError()) die($ret->getAsHtml());

list ($ret, $adminUsers) = GalleryCoreApi::fetchUsersForGroup($siteAdminGroupId, 2);
if ($ret->isError()) die($ret->getAsHtml());
if (empty($adminUsers)) die('No adminUsers were available');

$adminUsers = array_keys($adminUsers);
if ($adminUsers[0] == $g2Id && count($adminUsers) == 1) die('The only G2 admin cannot be deleted.');

$newOwnerId = ($adminUsers[0] != $g2Id) ? $adminUsers[0] : $adminUsers[1];

list ($ret, $entityId) = GalleryCoreApi::loadEntitiesById($newOwnerId);
if ($ret->isError()) die($ret->getAsHtml());

$gallery->setActiveUser($entityId);

$ret = GalleryCoreApi::deleteUserItems($g2Id);
if ($ret->isError()) die($ret->getAsHtml());

$ret = GalleryCoreApi::deleteEntityById($g2Id);
if ($ret->isError()) die($ret->getAsHtml());
 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-12-30 11:26

1. a) if you want to change the owner of all the items the user owns rather than deleting them, call:
$ret = GalleryCoreApi::remapOwnerId($userToDelete->getId(), $newOwnerId);

1. b) if you'd rather delete the items of the user, call
$ret = GalleryCoreApi::deleteUserItems($userToDelete->getId());

2. finally, you can call:
$ret = GalleryCoreApi::deleteEntityById($userToDelete->getId());

or
list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($userToDelete->getId());
// error handling
$ret = $userToDelete->delete();
// error handling
$ret = GalleryCoreApi::releaseLocks($lockId);
// error handling

btw:
you don't need to duplicate the code from GalleryUser::delete, the admin check etc. are all executed during the API calls.

also, you need to call GalleryEmbed::done(); at the end to commit the transaction.

 
jettyrat
jettyrat's picture

Joined: 2005-12-30
Posts: 32
Posted: Fri, 2005-12-30 19:55

Thanks valiant! Greatly appreciated and most helpful. I think my biggest problem was not calling GalleryEmbed::done() at the end and also some confusion on the remapping vs deleting issue. Brings up a couple more questions, if you don't mind.

1. If I'm looping through more than one user to delete, should GalleryEmbed::done() be called for each one or just once at the very end of the integration syncing?

2. Is one of these approaches better than the other?

Quote:
$ret = GalleryCoreApi::deleteEntityById($userToDelete->getId());

or
list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($userToDelete->getId());
// error handling
$ret = $userToDelete->delete();
// error handling
$ret = GalleryCoreApi::releaseLocks($lockId);
// error handling

Also, the phpbb2 integration I'm working with does not set an active G2 user at the time of integration, so I have to go through the gyrations of finding a valid admin, else I get ERROR_PERMISSION_DENIED when the delete hooks are called.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sat, 2005-12-31 14:48

1. call GalleryEmbed::done(); at the end of your G2 interactions. you don't have to call it after each deletion. just call it at the end of your g2 code. but it isn't necessary to call it if you just do GalleryEmbed::init + ::handleRequest.

2. nope, both do the same.

only an admin can delete users in g2. that's a protective measure...