function copy($oSrcFolder, $oDestFolder, $oUser, $sReason, $sDestFolderName = NULL, $copyAll = true) { $sDestFolderName = empty($sDestFolderName) ? $oSrcFolder->getName() : $sDestFolderName; if (KTFolderUtil::exists($oDestFolder, $sDestFolderName)) { return PEAR::raiseError(_kt("Folder with the same name already exists in the new parent folder")); } // // FIXME the failure cleanup code here needs some serious work. // $oPerm = KTPermission::getByName('ktcore.permissions.read'); $oBaseFolderPerm = KTPermission::getByName('ktcore.permissions.addFolder'); if (!KTPermissionUtil::userHasPermissionOnItem($oUser, $oBaseFolderPerm, $oDestFolder)) { return PEAR::raiseError(_kt('You are not allowed to create folders in the destination.')); } // Check if the source folder inherits its permissions // Get source PO id and its parent PO id $iSrcPoId = $oSrcFolder->getPermissionObjectID(); $oSrcParent = Folder::get($oSrcFolder->getParentID()); $iSrcParentPoId = $oSrcParent->getPermissionObjectID(); // If the folder defines its own permissions then we copy the permission object // If the source folder inherits permissions we must change it to inherit from the new parent folder $bInheritPermissions = false; if ($iSrcPoId == $iSrcParentPoId) { $bInheritPermissions = true; } $aFolderIds = array(); // of oFolder $aDocuments = array(); // of oDocument $aFailedDocuments = array(); // of String $aFailedFolders = array(); // of String $aRemainingFolders = array($oSrcFolder->getId()); DBUtil::startTransaction(); while (!empty($aRemainingFolders) && $copyAll) { $iFolderId = array_pop($aRemainingFolders); $oFolder = Folder::get($iFolderId); if (PEAR::isError($oFolder) || $oFolder == false) { DBUtil::rollback(); return PEAR::raiseError(sprintf(_kt('Failure resolving child folder with id = %d.'), $iFolderId)); } // don't just stop ... plough on. if (KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oFolder)) { $aFolderIds[] = $iFolderId; } else { $aFailedFolders[] = $oFolder->getName(); } // child documents $aChildDocs = Document::getList(array('folder_id = ?', array($iFolderId))); foreach ($aChildDocs as $oDoc) { if (KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oDoc)) { $aDocuments[] = $oDoc; } else { $aFailedDocuments[] = $oDoc->getName(); } } // child folders. $aCFIds = Folder::getList(array('parent_id = ?', array($iFolderId)), array('ids' => true)); $aRemainingFolders = kt_array_merge($aRemainingFolders, $aCFIds); } if (!empty($aFailedDocuments) || !empty($aFailedFolders)) { $sFD = ''; $sFF = ''; if (!empty($aFailedDocuments)) { $sFD = _kt('Documents: ') . implode(', ', $aFailedDocuments) . '. '; } if (!empty($aFailedFolders)) { $sFF = _kt('Folders: ') . implode(', ', $aFailedFolders) . '.'; } return PEAR::raiseError(_kt('You do not have permission to copy these items. ') . $sFD . $sFF); } // first we walk the tree, creating in the new location as we go. // essentially this is an "ok" pass. $oStorage =& KTStorageManagerUtil::getSingleton(); $aFolderMap = array(); $sTable = 'folders'; $sGetQuery = 'SELECT * FROM ' . $sTable . ' WHERE id = ? '; $aParams = array($oSrcFolder->getId()); $aRow = DBUtil::getOneResult(array($sGetQuery, $aParams)); unset($aRow['id']); $aRow['name'] = $sDestFolderName; $aRow['description'] = $sDestFolderName; $aRow['parent_id'] = $oDestFolder->getId(); $aRow['parent_folder_ids'] = sprintf('%s,%s', $oDestFolder->getParentFolderIDs(), $oDestFolder->getId()); $aRow['full_path'] = $oDestFolder->getFullPath() . '/' . $aRow['name']; $id = DBUtil::autoInsert($sTable, $aRow); if (PEAR::isError($id)) { DBUtil::rollback(); return $id; } $sSrcFolderId = $oSrcFolder->getId(); $aFolderMap[$sSrcFolderId]['parent_id'] = $id; $aFolderMap[$sSrcFolderId]['parent_folder_ids'] = $aRow['parent_folder_ids']; $aFolderMap[$sSrcFolderId]['full_path'] = $aRow['full_path']; $aFolderMap[$sSrcFolderId]['name'] = $aRow['name']; $oNewBaseFolder = Folder::get($id); $res = $oStorage->createFolder($oNewBaseFolder); if (PEAR::isError($res)) { // it doesn't exist, so rollback and raise.. DBUtil::rollback(); return $res; } $aRemainingFolders = Folder::getList(array('parent_id = ?', array($oSrcFolder->getId())), array('ids' => true)); while (!empty($aRemainingFolders) && $copyAll) { $iFolderId = array_pop($aRemainingFolders); $aParams = array($iFolderId); $aRow = DBUtil::getOneResult(array($sGetQuery, $aParams)); unset($aRow['id']); // since we are nested, we will have solved the parent first. $sPrevParentId = $aRow['parent_id']; $aRow['parent_id'] = $aFolderMap[$aRow['parent_id']]['parent_id']; $aRow['parent_folder_ids'] = sprintf('%s,%s', $aFolderMap[$sPrevParentId]['parent_folder_ids'], $aRow['parent_id']); $aRow['full_path'] = sprintf('%s/%s', $aFolderMap[$sPrevParentId]['full_path'], $aRow['name']); $id = DBUtil::autoInsert($sTable, $aRow); if (PEAR::isError($id)) { $oStorage->removeFolder($oNewBaseFolder); DBUtil::rollback(); return $id; } $aFolderMap[$iFolderId]['parent_id'] = $id; $aFolderMap[$iFolderId]['parent_folder_ids'] = $aRow['parent_folder_ids']; $aFolderMap[$iFolderId]['full_path'] = $aRow['full_path']; $aFolderMap[$iFolderId]['name'] = $aRow['name']; $oNewFolder = Folder::get($id); $res = $oStorage->createFolder($oNewFolder); if (PEAR::isError($res)) { // first delete, then rollback, then fail out. $oStorage->removeFolder($oNewBaseFolder); DBUtil::rollback(); return $res; } $aCFIds = Folder::getList(array('parent_id = ?', array($iFolderId)), array('ids' => true)); $aRemainingFolders = kt_array_merge($aRemainingFolders, $aCFIds); } // now we can go ahead. foreach ($aDocuments as $oDocument) { $oChildDestinationFolder = Folder::get($aFolderMap[$oDocument->getFolderID()]['parent_id']); $res = KTDocumentUtil::copy($oDocument, $oChildDestinationFolder); if (PEAR::isError($res) || $res === false) { $oStorage->removeFolder($oNewBaseFolder); DBUtil::rollback(); return PEAR::raiseError(_kt('Delete Aborted. Unexpected failure to copydocument: ') . $oDocument->getName() . $res->getMessage()); } } $sComment = sprintf(_kt("Folder copied from %s to %s"), $oSrcFolder->getFullPath(), $oDestFolder->getFullPath()); if ($sReason !== null) { $sComment .= sprintf(_kt(" (reason: %s)"), $sReason); } $oTransaction = KTFolderTransaction::createFromArray(array('folderid' => $oFolder->getId(), 'comment' => $sComment, 'transactionNS' => 'ktcore.transactions.copy', 'userid' => $oUser->getId(), 'ip' => Session::getClientIP())); // If the folder inherits its permissions then we set it to inherit from the new parent folder and update permissions // If it defines its own then copy the permission object over if ($bInheritPermissions) { $aOptions = array('evenifnotowner' => true); KTPermissionUtil::inheritPermissionObject($oNewBaseFolder, $aOptions); } else { KTPermissionUtil::copyPermissionObject($oNewBaseFolder); } // and store DBUtil::commit(); return true; }
function do_inheritPermissions() { $aOptions = array('redirect_to' => array('main', 'fFolderId=' . $this->oFolder->getId())); if (!KTBrowseUtil::inAdminMode($this->oUser, $this->oFolder)) { $this->oValidator->userHasPermissionOnItem($this->oUser, $this->_sEditShowPermission, $this->oFolder, $aOptions); } $oTransaction = KTFolderTransaction::createFromArray(array('folderid' => $this->oFolder->getId(), 'comment' => _kt('Inherit permissions from parent'), 'transactionNS' => 'ktcore.transactions.permissions_change', 'userid' => $_SESSION['userID'], 'ip' => Session::getClientIP())); $aOptions = array('defaultmessage' => _kt('Error updating permissions'), 'redirect_to' => array('edit', sprintf('fFolderId=%d', $this->oFolder->getId()))); $this->oValidator->notErrorFalse($oTransaction, $aOptions); KTPermissionUtil::inheritPermissionObject($this->oFolder); return $this->successRedirectTo('main', _kt('Permissions updated'), array('fFolderId' => $this->oFolder->getId())); }
/** * Force the current folder item to inherit permission from the parent. * * @author KnowledgeTree Team * @access public */ public function inheritAllocation() { $object = $this->_logTransaction(_kt('Inherit permissions from parent'), 'ktcore.transactions.permissions_change'); KTPermissionUtil::inheritPermissionObject($object); $this->_resolvePermissions(); }
<?php require_once "../../config/dmsDefaults.php"; require_once KT_LIB_DIR . '/foldermanagement/Folder.inc'; require_once KT_LIB_DIR . '/permissions/permissionutil.inc.php'; error_reporting(E_ALL); $oFolder = Folder::get(19); var_dump($oFolder->getPermissionObjectID()); $oFolder = Folder::get(20); var_dump($oFolder->getPermissionObjectID()); $oFolder = Folder::get(21); var_dump($oFolder->getPermissionObjectID()); $oFolder = Folder::get(22); var_dump($oFolder->getPermissionObjectID()); $oDocument = Document::get(123); var_dump($oDocument->getPermissionObjectID()); $oFolder = Folder::get(19); $res = KTPermissionUtil::inheritPermissionObject($oFolder); if (PEAR::isError($res)) { var_dump($res); } $oFolder = Folder::get(19); var_dump($oFolder->getPermissionObjectID()); $oFolder = Folder::get(20); var_dump($oFolder->getPermissionObjectID()); $oFolder = Folder::get(21); var_dump($oFolder->getPermissionObjectID()); $oFolder = Folder::get(22); var_dump($oFolder->getPermissionObjectID()); $oDocument = Document::get(123); var_dump($oDocument->getPermissionObjectID());