Adding users to G2 Admin Group

axman

Joined: 2002-09-07
Posts: 57
Posted: Thu, 2006-08-31 03:29

I am using the following hunk of code to create users in Gallery2 via events in cmsms:

Quote:
require_once($this->GetPreference('g2_embed_php'));

$ret = GalleryEmbed::init(array('fullInit' => true));
if ($ret) {
print $ret->getAsHtml();
exit;
}

$ret = GalleryEmbed::isExternalIdMapped($username,'GalleryUser');
if ($ret) {
$ret = GalleryEmbed::createUser($username,array(
'username' => $username,
'email' => $email,
'fullname' => $fullname,
'creationtimestamp' => mktime()));
if ($ret) {
print "An error occurred during the user creation <br>";
print $ret->getAsHtml();
exit;
}
}
$g2data = GalleryEmbed::handleRequest();

I want to add a check that if the CMSMS user is an admin, they are added to the Gallery2 admin group. How can this be accomplished? From what i've seen, there is no way to directly interact with the internal Gallery 2 groups. Any info is appreciated. Thanks!

 
axman

Joined: 2002-09-07
Posts: 57
Posted: Wed, 2006-09-06 01:38

Does anyone have any information on this? Or if its possible?

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Wed, 2006-09-06 03:06

this is my code from geeklog bridge for adding admin:

function G2B_addUserToGroup( $GL_uid )
{
    list( $ret, $user ) = GalleryCoreApi::loadEntityByExternalId( $GL_uid, 'GalleryUser' ); //$GL_uid - geeklog uid
    $G2_uid = $user->getId(); 
    $ret = GalleryCoreApi::addUserToGroup( $G2_uid, 3); //G2 admin groupid is 3
    if ( $ret )
    {
	COM_errorLog( 'Failed to add user to admin group for GL uid ['.$GL_uid.']' );
	COM_errorLog( '   Here is the error message from G2:' );
	COM_errorLog( $ret->getAsText() );
	return false;
    }
    return true;

hope this helps

-s

 
axman

Joined: 2002-09-07
Posts: 57
Posted: Fri, 2006-09-08 03:03

Do i have to require anything else other than embed.php?

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Fri, 2006-09-08 03:53

no, but you must init gallery before calling any gallery functions.

In my geeklog/gallery2 bridge, everytime I involve gallery (showing sidebarblocks, random imageblock, photo viewing...) I call my init function which looks something like this:

require_once( $_G2B_CONF['G2_embed_path'] );

	$ret = GalleryEmbed::init( array(
		   'embedUri' => $_G2B_CONF['embedUri'],
		   'g2Uri' => $_G2B_CONF['g2Uri'],
		   'loginRedirect' => $_G2B_CONF['login_redirect'],
		   'activeUserId' => G2B_GetG2UserFromGL(),
		   'activeLanguage' => G2B_MapLanguage( $_CONF['language'] ),
		   'fullInit' => $full ) );

then I check to see if that user is mapped to gallery already:

if ( $ret )
{
	if ( $ret->getErrorCode() & ERROR_MISSING_OBJECT )
	{
		$errStr = G2B_CreateG2User( G2B_GetG2UserFromGL() ); // if the activeUserId is "ERROR_MISSING_OBJECT" then create a new G2 user
		
		if ( $errStr )
		{
			echo $errStr;
			
			return( false );
		}

create a new G2 user if he/she does not already exsist:

function G2B_CreateG2User( $GL_uid )
{
	global $_TABLES;
	
	$ret = G2B_G2_init();

	// make sure we are don't already have an user with the same name                                                                                                                    
	$ret = GalleryEmbed::isExternalIdMapped( $GL_uid, 'GalleryUser' );

	if ( !($ret && ( $ret->getErrorCode() & ERROR_MISSING_OBJECT )) )
			return;

	$sql = "SELECT uid, username, fullname, passwd, email, language, UNIX_TIMESTAMP( regdate ) AS registration_ts
				FROM {$_TABLES['users']} WHERE uid ='{$GL_uid}' LIMIT 1";
	$res = DB_query( $sql );
	$row = DB_fetchArray( $res );

	$args['fullname'] = $row['fullname'];
	$args['username'] = $row['username'];
	$args['hashedpassword'] = $row['passwd'];
	$args['hashmethod'] = 'md5';
	$args['email'] = $row['email'];
	$args['creationtimestamp']= $row['registration_ts'];
	$args['language'] = G2B_MapLanguage( $row['language'] );

	$res = GalleryEmbed::createUser( $GL_uid, $args );
	
	if (( $res) && ($res->getErrorCode() & ERROR_COLLISION ))
	{
                     GL_addExternalIdMap($GL_uid);
		return false;
           }		
	

	$debug = "username: [{$args['username']}]<br>
			fullname: [{$args['fullname']}]<br>
			hashedpassword: [{$args['hashedpassword']}]<br>
			email: [{$args['email']}]<br>
			creationtimestamp: [{$args['creationtimestamp']}]<br>
	";
	
	if ( $res )
		return $debug . '<br>Failed to create G2 user with extId ['.$GL_uid.' - '.G2B_GetG2UserFromGL().'] 
						Here is the error message from G2: <br>' . $res->getAsHtml();
}

function GL_addExternalIdMap($GL_uid)
{
	global $_TABLES;
	
	          // Fetch username based on $GL_uid
    $sql = "SELECT username FROM {$_TABLES['users']} WHERE uid ='{$GL_uid}' LIMIT 1";
	$result = DB_query( $sql );
	$A = DB_fetchArray( $result );
	
	list( $ret, $user ) = GalleryCoreApi::fetchUserByUserName($A['username']);

	$res = GalleryEmbed::addExternalIdMapEntry($GL_uid, $user->getId(), 'GalleryUser');
	if ( $res )
    {
	          COM_errorLog( 'Failed to add externalIdMap for GL usename ['.$A['username'].']' );
	          COM_errorLog( '   Here is the error message from G2:' );
	          COM_errorLog( $res->getAsText() );
	          return false;
           }
}

and finally, if I'm going to add him/her to admin group, I use the function from above.

Know that this code is Geeklog specific, and should be used as a reference/example only, it is riddled with geeklog functions calls.

hope this helps

-s