Index: branches/unlabeled/unlabeled-1.17.2/kernel/units/general/helpers/permissions_helper.php =================================================================== diff -u -r5516 -r5858 --- branches/unlabeled/unlabeled-1.17.2/kernel/units/general/helpers/permissions_helper.php (.../permissions_helper.php) (revision 5516) +++ branches/unlabeled/unlabeled-1.17.2/kernel/units/general/helpers/permissions_helper.php (.../permissions_helper.php) (revision 5858) @@ -130,7 +130,7 @@ function CheckEventCategoryPermission(&$event, $event_perm_mapping) { // mapping between specific permissions and common permissions - $perm_mapping = Array('add' => 'ADD', 'edit' => 'MODIFY', 'delete' => 'DELETE', 'view' => 'VIEW'); + $perm_mapping = Array('add' => 'ADD', 'add.pending' => 'ADD.PENDING', 'edit' => 'MODIFY', 'edit.pending' => 'MODIFY.PENDING', 'delete' => 'DELETE', 'view' => 'VIEW'); $top_prefix = $event->getEventParam('top_prefix'); $event_handler =& $this->Application->recallObject($event->Prefix.'_EventHandler'); @@ -142,8 +142,6 @@ $id = $event_handler->getPassedID($event); } - $item_prefix = $this->Application->getUnitOption($top_prefix, 'PermItemPrefix'); - // 1. get primary category of category item $id_field = $this->Application->getUnitOption($top_prefix, 'IDField'); $table_name = $this->Application->getUnitOption($top_prefix, 'TableName'); @@ -155,19 +153,26 @@ } else { // item being edited -> check by it's primary category permissions - $sql = 'SELECT ci.CategoryId + $sql = 'SELECT ci.CategoryId, main_table.CreatedById FROM '.$table_name.' main_table LEFT JOIN '.$ci_table.' ci ON ci.ItemResourceId = main_table.ResourceId WHERE (main_table.'.$id_field.' = '.$id.') AND (ci.PrimaryCat = 1)'; - $category_id = $this->Conn->GetOne($sql); + $item_info = $this->Conn->GetRow($sql); + $category_id = $item_info['CategoryId']; + $owner_id = $item_info['CreatedById']; } - if ((substr($event->Name, 0, 9) == 'OnPreSave') || ($event->Name == 'OnSave')) { + $item_prefix = $this->Application->getUnitOption($top_prefix, 'PermItemPrefix'); + + if (substr($event->Name, 0, 9) == 'OnPreSave') { if ($event_handler->isNewItemCreate($event)) { - return $this->CheckPermission($item_prefix.'.ADD', 0, $category_id); + return $this->CheckPermission($item_prefix.'.ADD', 0, $category_id) || + $this->CheckPermission($item_prefix.'.ADD.PENDING', 0, $category_id); } else { - return $this->CheckPermission($item_prefix.'.ADD', 0, $category_id) || $this->CheckPermission($item_prefix.'.MODIFY', 0, $category_id); + return $this->CheckPermission($item_prefix.'.ADD', 0, $category_id) || + $this->CheckPermission($item_prefix.'.ADD.PENDING', 0, $category_id) || + $this->ModifyCheckPermission($owner_id, $category_id, $top_prefix); } } @@ -349,9 +354,7 @@ FROM '.$this->Application->getUnitOption('c', 'TableName').' WHERE CategoryId = '.$cat_id; $cat_hierarchy = $this->Conn->GetOne($sql); - $cat_hierarchy = explode('|', $cat_hierarchy); - array_shift($cat_hierarchy); - array_pop($cat_hierarchy); + $cat_hierarchy = explode('|', substr($cat_hierarchy, 1, -1)); $cat_hierarchy = array_reverse($cat_hierarchy); array_push($cat_hierarchy, 0); } @@ -372,6 +375,40 @@ $this->Application->setCache('permissions', $cache_key, $perm_value); return $perm_value; } + + /** + * Allows to check MODIFY & OWNER.MODFY +/- PENDING permission combinations on item + * + * @param int $owner_id user_id, that is owner of the item + * @param int $category_id primary category of item + * @param string $prefix prefix of item + * @return int {0 - no MODIFY permission, 1 - has MODIFY permission, 2 - has MODIFY.PENDING permission} + */ + function ModifyCheckPermission($owner_id, $category_id, $prefix) + { + $perm_prefix = $this->Application->getUnitOption($prefix, 'PermItemPrefix'); + + $live_modify = $this->CheckPermission($perm_prefix.'.MODIFY', ptCATEGORY, $category_id); + if ($live_modify) { + return 1; + } + else if ($this->CheckPermission($perm_prefix.'.MODIFY.PENDING', ptCATEGORY, $category_id)) { + return 2; + } + + if ($owner_id == $this->Application->GetVar('u_id')) { + // user is item's OWNER -> check this permissions first + $live_modify = $this->CheckPermission($perm_prefix.'.OWNER.MODIFY', ptCATEGORY, $category_id); + if ($live_modify) { + return 1; + } + else if ($this->CheckPermission($perm_prefix.'.OWNER.MODIFY.PENDING', ptCATEGORY, $category_id)) { + return 2; + } + } + + return 0; + } } ?> \ No newline at end of file