Commit 728b370eb3940e04f0bbb69f87e71deedf8e3d24

Authored by nbm
1 parent 6efc8214

Implement support for the permission_folder_id - how to calculate it,

and how to update it on all a folder's children when it needs to be
changed.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3119 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 111 additions and 2 deletions
lib/foldermanagement/Folder.inc
... ... @@ -48,7 +48,8 @@ class Folder extends KTEntity {
48 48 var $sFullPath;
49 49 /** whether to inherit parent permissions or not */
50 50 var $bInheritParentPermission;
51   -
  51 + /** which folder I get permissions from (eases search lookups) */
  52 + var $iPermissionFolderID;
52 53  
53 54 /**
54 55 * Folder class constructor
... ... @@ -215,6 +216,16 @@ class Folder extends KTEntity {
215 216 function setInheritParentPermission($bNewValue) {
216 217 $this->bInheritParentPermissions = $bNewValue;
217 218 }
  219 +
  220 + /**
  221 + * Get the permission folder id
  222 + *
  223 + * @return integer permission folder id
  224 + *
  225 + */
  226 + function getPermissionFolderID() {
  227 + return $this->iPermissionFolderID;
  228 + }
218 229  
219 230 /**
220 231 * Recursive function to generate a comma delimited string containing
... ... @@ -285,6 +296,7 @@ class Folder extends KTEntity {
285 296 'full_path' => $this->sFullPath,
286 297 'parent_folder_ids' => $this->sParentFolderIDs,
287 298 'inherit_parent_folder_permission' => $this->bInheritParentPermissions,
  299 + 'permission_folder_id' => $this->iPermissionFolderID,
288 300 );
289 301 }
290 302  
... ... @@ -681,6 +693,103 @@ class Folder extends KTEntity {
681 693 // no documents
682 694 return false;
683 695 }
684   - }
  696 + }
  697 +
  698 + function calculatePermissionFolder() {
  699 + global $default;
  700 + $oInheritedFolder = $this;
  701 + while ($bFoundPermissions !== true) {
  702 + /*ok*/$aCheckQuery = array('SELECT id FROM groups_folders_link WHERE folder_id = ? LIMIT 1', $oInheritedFolder->getID());
  703 + if (count(DBUtil::getResultArrayKey($aCheckQuery, 'id')) == 0) {
  704 + $default->log->debug('No direct permissions on folder ' . $oInheritedFolder->getID());
  705 + $bInherited = true;
  706 + $oInheritedFolder =& Folder::get($oInheritedFolder->getParentID());
  707 + if ($oInheritedFolder === false) {
  708 + break;
  709 + }
  710 + // if our parent knows the permission folder, use that.
  711 + if ($oInheritedFolder->getPermissionFolderID()) {
  712 + $this->iPermissionFolderID = $oInheritedFolder->getPermissionFolderID();
  713 + }
  714 + $default->log->debug('... trying parent: ' . $oInheritedFolder->getID());
  715 + } else {
  716 + $default->log->debug('Found direct permissions on folder ' . $oInheritedFolder->getID());
  717 + $this->iPermissionFolderID = $oInheritedFolder->getID();
  718 + return;
  719 + }
  720 + }
  721 +
  722 + $default->log->error('No permissions whatsoever for folder ' . $this->getID());
  723 + // 0, which can never exist, for non-existent. null for not set yet (database upgrade).
  724 + $this->iPermissionFolderID = 0;
  725 + }
  726 +
  727 + // {{{ copyPermissionsFromFolder()
  728 + function copyPermissionsFromFolder ($oFolder) {
  729 + $sQuery = DBUtil::compactQuery("
  730 + SELECT
  731 + GFL.group_id AS group_id,
  732 + GFL.can_read AS can_read,
  733 + GFL.can_write AS can_write
  734 + FROM
  735 + $default->groups_folders_table AS GFL
  736 + WHERE GFL.folder_id = ?");
  737 + $aParams = array($oFolder->getID());
  738 + $aPermissions = DBUtil::getResultArray(array($sQuery, $aParams));
  739 +
  740 + if (PEAR::isError($aPermissions)) {
  741 + return $aPermissions;
  742 + }
  743 +
  744 + foreach ($aPermissions as $aRow) {
  745 + $aRow['folder_id'] = $this->getID();
  746 + $res = DBUtil::autoInsert($default->groups_folders_table, $aRow);
  747 + if (PEAR::isError($res)) {
  748 + return $res;
  749 + }
  750 + }
  751 +
  752 + $this->updatePermissions();
  753 + }
  754 + // }}}
  755 +
  756 + // {{{ create()
  757 + function create () {
  758 + $this->calculatePermissionFolder();
  759 + return parent::create();
  760 + }
  761 + // }}}
  762 +
  763 + // {{{ updatePermissions()
  764 + function updatePermissions () {
  765 + $this->calculatePermissionFolder();
  766 + // XXX: Non-explicit update... Have to update, or the children
  767 + // wouldn't have access to it for the shortcut...
  768 + $this->update();
  769 +
  770 + $aChildren = Folder::getList(array('parent_id = ?', $this->getID()));
  771 + foreach ($aChildren as $oChild) {
  772 + $oChild->updatePermissions();
  773 + }
  774 + }
  775 + // }}}
  776 +
  777 + function addPermission ($oGroup, $bCanRead, $bCanWrite) {
  778 + $oGroupFolderLink = & new GroupFolderLink($this->getID(), $oGroup->getID(), $bCanRead, $bCanWrite);
  779 +
  780 + if ($oGroupFolderLink->exists()) {
  781 + return new PEAR_Error(_("A folder access entry for the selected folder and group already exists."));
  782 + }
  783 +
  784 + if (!$oGroupFolderLink->create()) {
  785 + //otherwise display an error message
  786 + return new PEAR_Error(_("A folder access entry for the selected folder and group already exists."));
  787 + }
  788 +
  789 + $this->updatePermissions();
  790 + return true;
  791 + }
  792 +
685 793 }
  794 +
686 795 ?>
... ...