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) { I think that if I add the following code: Quote:
if (GalleryUtilities::isA($item, 'GalleryDataItem') && 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: :/usr/obj/usr/src/sys/GENERIC i386 |
|
Dayo
Joined: 2005-11-04
Posts: 1642 |
Posted: Fri, 2011-01-28 10:32 |
darkyto wrote:
Hello! 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. -- |
|
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. -- |
|
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 |
|
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. |
|
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? -- |
|
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? 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. -- |
|
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. -- |
|
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. -- |
|
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! |
|