help change code of jensperms module

darkyto

Joined: 2010-08-18
Posts: 16
Posted: Fri, 2011-01-28 09:48

Hello!

I have downloaded the module "jensperms" because I needed the owner permissions to be auto set after a photo upload. Now I want to make it possible the users create albums but with specific auto-set permissions.

I saw that the following part in the module is setting the permissions for a photo ($item):

Quote:
function handleEvent($event) {
if ($event->getEventName() == 'GalleryEntity::save') {
$item = $event->getEntity();
if (GalleryUtilities::isA($item, 'GalleryDataItem') &&
$item->testPersistentFlag(STORAGE_FLAG_NEWLY_CREATED)) {
/* Set permissions */
global $gallery;
$userId = $gallery->getActiveUserId();
$ret = GalleryCoreApi::addUserPermission(
$item->getId(), $userId, 'core.all');
if ($ret) {
return array($ret->wrap(__FILE__, __LINE__), null);
}
}
}
return array(null, null);
}

I think that if I add the following code:

Quote:
if (GalleryUtilities::isA($item, 'GalleryDataItem') &&
$item->testPersistentFlag(STORAGE_FLAG_NEWLY_CREATED)) {
/* Set permissions */
global $gallery;
$userId = $gallery->getActiveUserId();
$ret = GalleryCoreApi::addUserPermission(
$item->getId(), $userId, 'core.all');
if ($ret) {
return array($ret->wrap(__FILE__, __LINE__), null);
}
}

right before closing the following IF statement:

Quote:
if ($event->getEventName() == 'GalleryEntity::save') {

will do the work. I KNOW that I need to change the code that should be inserted but I don't know with what variables I should change "$item", "GalleryDataItem". I think if I change these two with the ones connected to Album it will set permissions for album.

The other part is that I want the user to have permissions to only EDIT the album and can't delete or set permissions for it nor deleting photos that are not owned by him/her.

I hope I described my problem clear enough :)

Here is the gallery info:
Gallery URL = http://www.bggallery.eu/main.php
Gallery version = 2.3 core 1.3.0
API = Core 7.54, Module 3.9, Theme 2.6, Embed 1.5
PHP version = 5.2.11 apache2handler
Webserver = Apache/2.2.13 (FreeBSD) mod_ssl/2.2.13 OpenSSL/0.9.8k DAV/2 PHP/5.2.11 with Suhosin-Patch
Database = mysqlt 5.0.86, lock.system=flock
Toolkits = ArchiveUpload, Dcraw, Exif, Ffmpeg, ImageMagick, jpegtran, LinkItemToolkit, Thumbnail, Gd, SquareThumb
Acceleration = none/900, none/0
Operating system = FreeBSD www.bggallery.eu 8.0-RELEASE FreeBSD 8.0-RELEASE #0: Sat Nov 21 15:48:17 UTC 2009

:/usr/obj/usr/src/sys/GENERIC i386
Default theme = matrix
gettext = enabled
Locale = en_US
Browser = Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10
Rows in GalleryAccessMap table = 24
Rows in GalleryAccessSubscriberMap table = 349
Rows in GalleryUser table = 9
Rows in GalleryItem table = 345
Rows in GalleryAlbumItem table = 20
Rows in GalleryCacheMap table = 0

 
Dayo

Joined: 2005-11-04
Posts: 1642
Posted: Fri, 2011-01-28 10:32
darkyto wrote:
Hello!
...
I hope I described my problem clear enough :)
...

Not really :)

I think I sort of understand what you want to do but the actual query is lost somewhere along the line.

Can you repeat what it is you want to do without the code example and gallery Info since these are now in the original post.

Just something along the lines of: I want to assign xyz type permissions to abc type users when def type action takes place.

--
dakanji.com

 
darkyto

Joined: 2010-08-18
Posts: 16
Posted: Fri, 2011-01-28 11:08

I will grant permissions to registered users to create albums. As the user will become owner of the album, he/she will have the possibility to edit the album, edit the permissions and delete the album. I want to use the mentioned module in the following way: when a user creates an album he,she will be the owner, but I want the only thing that user can do is to edit the album. I want to forbid the user to change permissions or delete the album. I need this, because other users can upload photos in this user's album and if the owner wants to delete the album, every photo in it will be deleted too. Is it clearer now, or I should try again? :)

 
Dayo

Joined: 2005-11-04
Posts: 1642
Posted: Fri, 2011-01-28 19:04

I don't know what the jensperms module is (guessing it is an earlier form of the useralbums module) but change the handle event function to ...

function handleEvent($event) {
	global $gallery;

	$entity = $event->getEntity();
	$entityId = $entity->getId();
	$userId = $gallery->getActiveUserId();
	list ($ret, $coreParams) = GalleryCoreApi::fetchAllPluginParameters('module', 'core');
	if ($ret) {
		return $ret;
	}
	$guestGroupId = $coreParams['id.everybodyGroup'];
	$registeredGroupId = $coreParams['id.allUserGroup'];

	switch ($event->getEventName()) {
		case 'GalleryEntity::save':
			if (GalleryUtilities::isA($entity, 'GalleryDataItem' && $entity->testPersistentFlag(STORAGE_FLAG_NEWLY_CREATED)) {
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.all');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addGroupPermission($entityId, $guestGroupId, 'core.viewResizes');
				if ($ret) {
					return array($ret, null);
				}
			} elseif (GalleryUtilities::isA($entity, 'GalleryAlbumItem' && $entity->testPersistentFlag(STORAGE_FLAG_NEWLY_CREATED)) { 
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.viewAll');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.addAlbumItem');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.addDataItem');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.edit');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addGroupPermission($entityId, $registeredGroupId, 'core.addDataItem');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addGroupPermission($entityId, $guestGroupId, 'core.view');
				if ($ret) {
					return array($ret, null);
				}
			}
			break;
		case 'GalleryEntity::delete':
		
			break
		case 'Gallery::Login':
		
			break
		default:
		
			break;
	}
	return array(null, null);
}

I changed it to a switch to allow adding other events later if you want.

You can play with the various permission types as listed below:

core.all				->		All access
core.view				->		View item
core.viewResizes			->		View resized
core.viewSource				->		View original version
core.viewAll				->		View all versions
core.addAlbumItem			->		Add sub-album
core.addDataItem			->		Add sub-item
core.edit				->		Edit item
core.changePermissions			->		Change item permissions
core.delete				->		Delete item

Let us know if it works.

--
dakanji.com

 
darkyto

Joined: 2010-08-18
Posts: 16
Posted: Fri, 2011-01-28 19:04

I have tried your code. I think that it will work if it is alone in a module, but I need to combine it with the quoted code below. Actually the quoted code below is the whole module which sets automaticaly full permissions to the owner of the uploaded photo. I can try to write some code, but I need to see how the gallery identifies the Album as an Album.

@Dayo - Thanks for the help! These permissions are one part of the things I needed :)

Quote:
<?php
class JensPermsModule extends GalleryModule {

function JensPermsModule() {
global $gallery;

$this->setId('jensperms'); /* this must match the directory name */
$this->setName('Jens Permissions'); /* this is the module name */
$this->setDescription($gallery->i18n('Automatically set permissions for uploaded file'));
$this->setVersion('0.9.0');
$this->setGroup('gallery', $gallery->i18n('Gallery'));
$this->setCallbacks('registerEventListeners');
$this->setRequiredCoreApi(array(7, 0));
$this->setRequiredModuleApi(array(3, 0));
}

/**
* @see GalleryModule::registerEventListeners()
*/
function registerEventListeners() {
GalleryCoreApi::registerEventListener('GalleryEntity::save',
new JensPermsModule(), true); /* true=disableForUnitTest */
}

/**
* Event handler for GalleryEntity::save event
*
* @param object GalleryEvent the event
* @return object GalleryStatus a status code
*/
function handleEvent($event) {
if ($event->getEventName() == 'GalleryEntity::save') {
$item = $event->getEntity();
if (GalleryUtilities::isA($item, 'GalleryDataItem') &&
$item->testPersistentFlag(STORAGE_FLAG_NEWLY_CREATED)) {
/* Set permissions */
global $gallery;
$userId = $gallery->getActiveUserId();
$ret = GalleryCoreApi::addUserPermission(
$item->getId(), $userId, 'core.all');
if ($ret) {
return array($ret->wrap(__FILE__, __LINE__), null);
}
}
}
return array(null, null);
}
}
?>

 
Dayo

Joined: 2005-11-04
Posts: 1642
Posted: Fri, 2011-01-28 20:55

Try this instead ...

<?php
class JensPermsModule extends GalleryModule {
	function JensPermsModule() {
		global $gallery;
		
		$this->setId('jensperms'); /* this must match the directory name */
		$this->setName('Auto Permissions'); /* this is the module name */
		$this->setDescription($gallery->i18n('Auto permissions for new items'));
		$this->setVersion('0.9.1');
		$this->setGroup('gallery', $gallery->i18n('Gallery'));
		$this->setCallbacks('');
		$this->setRequiredCoreApi(array(7, 54));
		$this->setRequiredModuleApi(array(3, 9));
	}

	/**
	* @see GalleryModule::performFactoryRegistrations
	*/
	function performFactoryRegistrations() {
		$ret = GalleryCoreApi::registerFactoryImplementation(
			'GalleryEventListener', 'JensPermsModule', 'JensPermsModule', 
			'modules/jensperms/module.inc', 'jensperms', 
			array('GalleryEntity::save'));
		if ($ret) {
			return $ret;
		}
	}
	
	/**
	* Event handler for GalleryEntity::save event
	*
	* @param object GalleryEvent the event
	* @return object GalleryStatus a status code
	*/
	function handleEvent($event) {
		global $gallery;
		
		$entity = $event->getEntity();
		$entityId = $entity->getId();
		$newItemFlag = $entity->testPersistentFlag(STORAGE_FLAG_NEWLY_CREATED);
		$userId = $gallery->getActiveUserId();
		list ($ret, $coreParams) = GalleryCoreApi::fetchAllPluginParameters('module', 'core');
		if ($ret) {
			return $ret;
		}
		$guestGroupId = $coreParams['id.everybodyGroup'];
		$registeredGroupId = $coreParams['id.allUserGroup'];
		
		if ($event->getEventName() == 'GalleryEntity::save') {
			if (GalleryUtilities::isA($entity, 'GalleryDataItem' && $newItemFlag) {
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.all');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addGroupPermission($entityId, $guestGroupId, 'core.viewResizes');
				if ($ret) {
					return array($ret, null);
				}
			} elseif (GalleryUtilities::isA($entity, 'GalleryAlbumItem' && $newItemFlag) { 
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.viewAll');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.addAlbumItem');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.addDataItem');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addUserPermission($entityId, $userId, 'core.edit');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addGroupPermission($entityId, $registeredGroupId, 'core.addDataItem');
				if ($ret) {
					return array($ret, null);
				}
				$ret = GalleryCoreApi::addGroupPermission($entityId, $guestGroupId, 'core.view');
				if ($ret) {
					return array($ret, null);
				}
			}
		}
		return array(null, null);
	}
}
?>

You need to be on G2.3.x to use this code.

PS. You might need to remove the "core.edit" permission if you don't want the user to delete items as looking at how it works, it seems that there may be a bug as it seems to bypass some other permission settings and allows the user to change permissions. Just a cursory look at it though so needs testing on your part.
--
dakanji.com

 
darkyto

Joined: 2010-08-18
Posts: 16
Posted: Fri, 2011-01-28 19:52

I tried this code. I am still getting error. I don't understand much PHP script and everything seems OK for me. :|

 
Dayo

Joined: 2005-11-04
Posts: 1642
Posted: Fri, 2011-01-28 19:55

"I am still getting error" is a bit vague. What error are you getting?

--
dakanji.com

 
darkyto

Joined: 2010-08-18
Posts: 16
Posted: Fri, 2011-01-28 19:57

Error 500. It comes from the browser. It's strange for me...but I am not good at coding.

 
Dayo

Joined: 2005-11-04
Posts: 1642
Posted: Fri, 2011-01-28 20:03

So try a few simple tests.

1. Do you get the error even if you remove the module?
2. Do you get the error with the original module before making changes?

Basically, you do a few logical eliminations to try to figure out where the problem is coming from as the error may not be related to this code.
Removing and adding stuff into a test environment to try to get an idea where an issue is coming from doesn't need coding knowledge.

--
dakanji.com

 
darkyto

Joined: 2010-08-18
Posts: 16
Posted: Fri, 2011-01-28 20:11

Well...here is the picture. I am changing the name of the original module. After that I can refresh the modules' list and everything is OK. Then I am changing the name of the "new" file of the module in which is the "combined" code. Refresh again and the error is there. I can't even activate the module. The Gallery script gives this error (I think) when it discovers it.

 
Dayo

Joined: 2005-11-04
Posts: 1642
Posted: Fri, 2011-01-28 20:12

PS: Have you looked at the userAlbum Module? http://codex.gallery2.org/Gallery2:Modules:useralbum

I don't think it does what you want out of the box but this can be amended as well and has other features.

--
dakanji.com

 
Dayo

Joined: 2005-11-04
Posts: 1642
Posted: Fri, 2011-01-28 20:15
darkyto wrote:
Well...here is the picture. I am changing the name of the original module. After that I can refresh the modules' list and everything is OK. Then I am changing the name of the "new" file of the module in which is the "combined" code. Refresh again and the error is there. I can't even activate the module. The Gallery script gives this error (I think) when it discovers it.

That wouldn't work without more work.

Just leave the original module as it is and only change the code in the module.inc to the one I gave you. Copy again as I made some changes.

Don't change any names.

--
dakanji.com

 
darkyto

Joined: 2010-08-18
Posts: 16
Posted: Fri, 2011-01-28 22:19

I copied the code you gave me. The module is consisting of only 1 file - module.inc. When I replace the original code with the modified the browser get's the error. I didn't change any names and I left the module as it should be (as much as possible).

I tried again with changes made and there is no change. I will try to change some of the code these days and tell you if something happens.

Thanks for the help! :)