emApp, howto delete user with all his/her items?

chilll
chilll's picture

Joined: 2006-05-23
Posts: 15
Posted: Wed, 2006-06-07 13:50
Quote:
FAQ:
* What happens if a user is deleted in G2 (e.g. with GalleryEmbed::deleteUser())?

All items of the user get reassigned to a new owner, usually to the first admin in the admin list. The user itself is completely deleted.

What function (or API method) should I use if I want all user items to be deleted with user?

Thanks,

 
Oldiesmann
Oldiesmann's picture

Joined: 2005-05-18
Posts: 151
Posted: Wed, 2006-06-07 19:10

After calling GalleryEmbed::init, before you call GalleryEmbed::deleteUser(), use this code:

list($ret, $user) = GalleryCoreApi::loadEntityByExternalId('user_id', 'GalleryUser');
GalleryEmbed::checkActiveUser('your_user_id');
GalleryCoreApi::deleteUserItems($user->getid());

'user_id' = ID of the user being deleted
'your_user_id' = your User ID (or the User ID of another user who has Site Admin priveleges in Gallery) - this is required because you can't call deleteUserItems unless you're a site admin.

You can expand that to your liking (adding error handling, etc.).

The Oldiesmann
SMF Support Specialist
SMF+G2 Integration Project

 
chilll
chilll's picture

Joined: 2006-05-23
Posts: 15
Posted: Thu, 2006-06-08 13:22

Hi again,

Somewhy my code does not delete the user - can you tell me why?

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$conf['gallery2_embed_path']        = dirname(__FILE__).'/../g2/embed.php';
$conf['gallery2_uri']               = 'http://_g2/';
$conf['gallery2_embed_app_uri']     = 'http://_g2_myapi/entry.php';
$conf['gallery2_login_redirect']    = 'http://_emapp/';
$conf['gallery2_lang']              = 'en';
$conf['gallery2_active_user_name']  = 'admin';
$conf['gallery2_active_user_id']    = 6;

require_once($conf['gallery2_embed_path']);

$username = 'testxyz';

$ret = GalleryEmbed::init(array('g2Uri'            => $conf['gallery2_uri'],
                                'embedUri'         => $conf['gallery2_embed_app_uri'],
                                'loginRedirect'    => $conf['gallery2_login_redirect'],
                                'activeUserId'     => $conf['gallery2_active_user_name']));

list($ret, $user) = GalleryCoreApi::loadEntityByExternalId($username, 'GalleryUser');
GalleryEmbed::checkActiveUser($conf['gallery2_active_user_id']);
GalleryCoreApi::deleteUserItems($user->getid());
GalleryEmbed::deleteUser($user->getid());

echo $user->getid(); // returns valid user id
?>

Because $user->getid(); returns valid user id (number) that should be deleted, I suspect that the GalleryCoreApi::deleteUserItems($user->getid()); and GalleryEmbed::deleteUser($user->getid()); are not working. Or... do I have to execute or commit the changes somehow?

Thanks,

 
chilll
chilll's picture

Joined: 2006-05-23
Posts: 15
Posted: Fri, 2006-06-09 10:10

problem solved.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sat, 2006-06-10 09:19

just FYI:

the default policy of g2 is to reassign all items of the user to a new owner instead of deleting them.
the new owner is the first admin it finds by default.

the admin UI of g2 allows to specify another new owner. it also allows to specify that all items should be deleted.

so if you do an integration, decide what behavior you want and implement the code appropriately. be sure to communicate this to your users. :)

 
chilll
chilll's picture

Joined: 2006-05-23
Posts: 15
Posted: Mon, 2006-06-12 10:47

Hi again,

I don't fully understand how to deal with $ret errors? For example, the code below gives me PHP error:
Fatal error: Call to a member function isError() on a non-object in E:\Projects\g2_myapi\embed.php on line 26.

<?php
// embed.php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$conf['gallery2_embed_path']        = dirname(__FILE__).'/../g2b/embed.php';
$conf['gallery2_uri']               = 'http://_g2/';
$conf['gallery2_embed_app_uri']     = 'http://_g2_myapi/entry.php';
$conf['gallery2_login_redirect']    = 'http://_emapp/';
$conf['gallery2_lang']              = 'en';
$conf['gallery2_active_user_name']  = 'admin';
$conf['gallery2_active_user_id']    = 6;

require_once($conf['gallery2_embed_path']);

$username = 'test';

$ret = GalleryEmbed::init(array('g2Uri'         => $conf['gallery2_uri'],
                                'embedUri'      => $conf['gallery2_embed_app_uri'],
                                'loginRedirect' => $conf['gallery2_login_redirect'],
                                'activeUserId'  => $conf['gallery2_active_user_name']));

list ($ret, $value) = GalleryCoreApi::loadEntityByExternalId($username, 'GalleryUser');

if ($ret->isError()) {
    print $ret->getAsHtml();
}
?>

Thanks,

 
geddes

Joined: 2006-05-31
Posts: 7
Posted: Mon, 2006-06-12 14:05

As far as I know (I am no expert) $ret is an instance of a gallery return object, which has a few methods, such as getAsHtml. One method it does not have is isError(). $ret itself acts as a sort of boolean, so all you need to do in your code is:

if ($ret)
    print $ret->getAsHtml();

The return object will return true if there has been an error of some sort. Then you can deal with it. I've been subscribing to this thread because I have written a little script with this exact same functionality in my G2 integration. Here it is, perhaps it will be helpful:

if($_POST['task']=='gogogoDelete')
{
if($_POST['reallyReally']!='true')
	print "<p>You didn't say 'really really' Press your back button and try again</p>";
else
	{
	// THE DELETION CODE
	
	//parse the accounts into an array
	$accountsToDelete = explode(',',$_POST['deletionBlock']);
	array_walk($accountsToDelete, 'trim_value');
	
	
	for($x=0; $x < count($accountsToDelete); $x++)
		{
		print ("<p> Processing " . $accountsToDelete[$x] . "...<ul>");
		list($ret, $user) = GalleryCoreApi::loadEntityByExternalId($accountsToDelete[$x], 'GalleryUser');
		if ($ret && $ret->getErrorCode='ERROR_MISSING_OBJECT')
			{
			print('<li><em>FAILED:</em> Unknown User ID!</li></ul>');
			}
		elseif($ret)
			{
			print('<li>real problem trying to find user</li></ul>');
			print('<p>' . $ret->getAsHtml() . '</p>');
			}
		else
			{
			//delete the stuff if neccessary
			
			if(isset($_POST['deleteStuff']) && $_POST['deleteStuff']=='true')
				{
				print('<li>Attempting to delete all photos and albums...');
				$ret = GalleryCoreApi::deleteUserItems($user->getid());										
				if($ret)
					print('<em>FAILED</em><br> ' . $ret->getAsHtml() . '</li>');
				else
					print('<u>success!</u></li>');
				}
			//delete the accounts
			
			print('<li>Attempting to delete account...');
			$ret = GalleryEmbed::deleteUser($accountsToDelete[$x]);										
			if($ret)
				print(' <em>FAILED</em><br> ' . $ret->getAsHtml() . '</li>');
			else
				print(' <u>success!</u></li>');
			
			}
		
		print('</ul></p>');
																						
		}
	
	
	
	
	
	
	}



}

I have an administration script which has a couple text entries. The first is a big textarea that the admin is supposed to paste the account names into, seperated by commas (name=deletionBlock). Then there are two checkboxes, one that says "Really do this? This will DELETE THE USERS" (name="reallyReally") and another that says "Also delete user's items"(name="deleteStuff"). The script works like a charm, all that stuff really does get deleted.

Good luck with your G2 integration,
- Geddes

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2006-06-13 01:07

$ret is null unless there was an error.
if ($ret) {
// something bad happened.
print $ret->getAsHtml();
exit;
}
// else everything is fine.