diff --git a/ktapi/KTAPIAcl.inc.php b/ktapi/KTAPIAcl.inc.php index 18b1671..066c6e2 100644 --- a/ktapi/KTAPIAcl.inc.php +++ b/ktapi/KTAPIAcl.inc.php @@ -1252,6 +1252,15 @@ final class KTAPI_RoleAllocation extends KTAPI_AllocationBase $map['role']['role'][$roleId] = $role->Name; $allocation = $type . 'Allocation'; + if (!array_key_exists($roleId, $map['role'][$allocation])) + { + $map['role'][$allocation][$roleId] = array(); + } + if (array_key_exists($memberId, $map['role'][$allocation][$roleId])) + { + // if the key exists, we don't have to do anything. + return; + } $map['role'][$allocation][$roleId][$memberId] = $memberId; $this->changed = true; @@ -1284,7 +1293,7 @@ final class KTAPI_RoleAllocation extends KTAPI_AllocationBase } public - function doesRoleHasMember(KTAPI_Role $role, KTAPI_Member $member) + function doesRoleHaveMember(KTAPI_Role $role, KTAPI_Member $member) { $map = & $this->map; @@ -1294,6 +1303,11 @@ final class KTAPI_RoleAllocation extends KTAPI_AllocationBase $type = $this->_getMemberType($member); $allocation = $type . 'Allocation'; + if (!array_key_exists($roleId, $map['role'][$allocation])) + { + return false; + } + $array = & $map['role'][$allocation][$roleId]; return (array_key_exists($memberId, $array)); @@ -1341,56 +1355,46 @@ final class KTAPI_RoleAllocation extends KTAPI_AllocationBase { $roleId = $role->Id; + $object = $this->folderItem->getObject(); + $objectId = $object->getId(); + $parentId = $object->getParentID(); + // FIXME do we need to check that this role _isn't_ allocated? - $oRoleAllocation = new RoleAllocation(); - $oRoleAllocation->setFolderId($this->oFolder->getId()); - $oRoleAllocation->setRoleId($role_id); + $roleAllocation = new RoleAllocation(); + $roleAllocation->setFolderId($objectId); + $roleAllocation->setRoleId($roleId); // create a new permission descriptor. // FIXME we really want to duplicate the original (if it exists) - $aAllowed = array(); // no-op, for now. - $this->startTransaction(); - - $oRoleAllocation->setAllowed($aAllowed); - $res = $oRoleAllocation->create(); - - if (PEAR::isError($res) || ($res == false)) { - $this->errorRedirectToMain(_kt('Failed to create the role allocation.') . print_r($res, true), sprintf('fFolderId=%d', $this->oFolder->getId())); - } + $allowed = array(); // no-op, for now. + $roleAllocation->setAllowed($allowed); + $res = $roleAllocation->create(); $this->_logTransaction(_kt('Override parent allocation'), 'ktcore.transactions.role_allocations_change'); + // inherit parent permissions - $oParentAllocation = RoleAllocation::getAllocationsForFolderAndRole($this->oFolder->getParentID(), $role_id); - if (!is_null($oParentAllocation) && !PEAR::isError($oParentAllocation)) + $parentAllocation = RoleAllocation::getAllocationsForFolderAndRole($parentId, $roleId); + if (!is_null($parentAllocation) && !PEAR::isError($parentAllocation)) { - $oPD = $oParentAllocation->getPermissionDescriptor(); - - $aAllowed = $oPD->getAllowed(); - $userids=$aAllowed['user']; - $groupids=$aAllowed['group']; + $descriptor = $parentAllocation->getPermissionDescriptor(); - // now lets update for the new allocation - $oPD = $oRoleAllocation->getPermissionDescriptor(); + $allowed = $descriptor->getAllowed(); - $aAllowed = $oPD->getAllowed(); + $allowed = array( + 'user' => $allowed['user'], + 'group' => $allowed['group'], + ); - $aAllowed['user'] = $userids; - $aAllowed['group'] = $groupids; + $roleAllocation->setAllowed($allowed); + $res = $roleAllocation->update(); - $oRoleAllocation->setAllowed($aAllowed); - $res = $oRoleAllocation->update(); - - if (PEAR::isError($res) || ($res == false)) - { - $this->errorRedirectToMain(_kt('Failed to create the role allocation.') . print_r($res, true), sprintf('fFolderId=%d', $this->oFolder->getId())); - } } // regenerate permissions - $this->renegeratePermissionsForRole($oRoleAllocation->getRoleId()); + $this->renegeratePermissionsForRole($roleId); } /** @@ -1400,26 +1404,50 @@ final class KTAPI_RoleAllocation extends KTAPI_AllocationBase public function inheritAllocation() { + if (!$this->canInheritRoleAllocation()) + { + return; + } + + $this->_logTransaction(_kt('Use parent allocation'), 'ktcore.transactions.role_allocations_change'); + foreach($this->map['role']['role'] as $roleId=>$roleName) { - $this->inheritRoleAllocation(KTAPI_Role::getById($roleId)); + $this->inheritRoleAllocation(KTAPI_Role::getById($roleId), false); } } + public + function canInheritRoleAllocation() + { + $object = $this->folderItem->getObject(); + $objectId = $object->getId(); + + return ($objectId != 1); + } + /** * Inherit the role associations from the parent. * * @param KTAPI_Role $role */ public - function inheritRoleAllocation(KTAPI_Role $role) + function inheritRoleAllocation(KTAPI_Role $role, $log = true) { - $roleId = $role->Id; - $this->_logTransaction(_kt('Use parent allocation'), 'ktcore.transactions.role_allocations_change'); + if (!$this->canInheritRoleAllocation()) + { + return; + } $object = $this->folderItem->getObject(); $objectId = $object->getId(); + $roleId = $role->Id; + if ($log) + { + $this->_logTransaction(_kt('Use parent allocation'), 'ktcore.transactions.role_allocations_change'); + } + $roleAllocation = RoleAllocation::getAllocationsForFolderAndRole($objectId, $roleId); $res = $oRoleAllocation->delete(); @@ -1510,9 +1538,50 @@ final class KTAPI_RoleAllocation extends KTAPI_AllocationBase public function save() { + if (!$this->changed) + { + // we don't have to do anything if nothing has changed. + return; + } - } + $map = & $this->map; + $folderId = $this->folderItem->getObject()->getId(); + + foreach($map['role']['role'] as $roleId => $roleName) + { + $roleAllocation = RoleAllocation::getAllocationsForFolderAndRole($folderId, $roleId); + + $allowed = array(); + $userIds = array(); + $groupIds = array(); + if (array_key_exists($roleId, $map['role']['userAllocation'])) + { + foreach($map['role']['userAllocation'][$roleId] as $userId) + { + $userIds[] = $userId; + } + } + if (array_key_exists($roleId, $map['role']['groupAllocation'])) + { + foreach($map['role']['groupAllocation'][$roleId] as $groupId) + { + $groupIds[] = $groupId; + } + } + + $allowed['user'] = $userIds; + $allowed['group'] = $groupIds; + + if (is_null($roleAllocation)) + { + $roleAllocation = $this->overrideRoleAllocation(KTAPI_Role::getById($roleId)); + } + + $roleAllocation->setAllowed($allowed); + $roleAllocation->update(); + } + } } ?> \ No newline at end of file diff --git a/ktapi/KTAPIDocument.inc.php b/ktapi/KTAPIDocument.inc.php index ad9c81a..d975e91 100644 --- a/ktapi/KTAPIDocument.inc.php +++ b/ktapi/KTAPIDocument.inc.php @@ -1930,6 +1930,113 @@ class KTAPI_Document extends KTAPI_FolderItem return $this->document; } + public function isSubscribed() + { + $subscriptionType = SubscriptionEvent::subTypes('Document'); + $user = $this->ktapi->get_user(); + $document = $this->document; + + return Subscription::exists($user->getId(), $document->getId(), $subscriptionType); + } + + public function unsubscribe() + { + if (!$this->isSubscribed()) + { + return; + } + + $subscriptionType = SubscriptionEvent::subTypes('Document'); + $user = $this->ktapi->get_user(); + $document = $this->document; + + $subscription = & Subscription::getByIDs($user->getId(), $document->getId(), $subscriptionType); + $subscription->delete(); + } + + public function subscribe() + { + if ($this->isSubscribed()) + { + return; + } + + $subscriptionType = SubscriptionEvent::subTypes('Document'); + $user = $this->ktapi->get_user(); + $document = $this->document; + + $subscription = new Subscription($user->getId(), $document->getId(), $subscriptionType); + $subscription->create(); + } + + + public function isImmutable() + { + return $this->document->getImmutable(); + } + + public function immute() + { + $this->document->setImmutable(true); + $this->document->update(); + } + + public function unimmute() + { + $this->document->setImmutable(false); + $this->document->update(); + } + + public function email($members, $title, $comment, $attachDocument = true) + { + if (empty($members)) + { + return; + } + + $userIds = array(); + $groupIds = array(); + $emailAddrs = array(); + + foreach($members as $member) + { + if ($member instanceof KTAPI_User) + { + $userIds[] = $member->Id; + } + elseif ($member instanceof KTAPI_Group) + { + $groupIds[] = $member->Id; + } + elseif (is_string($member)) + { + $emailAddrs[] = $member; + } + } + + $config = KTConfig::getSingleton(); + $allowAttachment = $config->get('email/allowAttachment', false); + $allowEmailAddresses = $oConfig->get('email/allowEmailAddresses', false); + + $emailErrors = array(); + $userEmails = array(); + + sendGroupEmails($groupIds, $userEmails, $emailErrors); + + sendUserEmails($userIds, $userEmails, $emailErrors); + + if ($attachDocument) + { + sendManualEmails($aEmailAddresses, $userEmails, $emailErrors); + } + else + { + sendExternalEmails($aEmailAddresses, $this->document->getID(), $this->document->getName(), $comment, $emailErrors); + } + + sendEmail($aListEmails, $this->document->getID(), $this->document->getName(), $comment, (boolean)$fAttachDocument, $aEmailErrors); + + } } ?> diff --git a/ktapi/KTAPIFolder.inc.php b/ktapi/KTAPIFolder.inc.php index 28fd1e1..1787d52 100644 --- a/ktapi/KTAPIFolder.inc.php +++ b/ktapi/KTAPIFolder.inc.php @@ -959,6 +959,46 @@ class KTAPI_Folder extends KTAPI_FolderItem { return $this->folder; } + + public function isSubscribed() + { + $subscriptionType = SubscriptionEvent::subTypes('Folder'); + $user = $this->ktapi->get_user(); + $folder = $this->folder; + + return Subscription::exists($user->getId(), $folder->getId(), $subscriptionType); + } + + public function unsubscribe() + { + if (!$this->isSubscribed()) + { + return; + } + + $subscriptionType = SubscriptionEvent::subTypes('Folder'); + $user = $this->ktapi->get_user(); + $folder = $this->folder; + + $subscription = & Subscription::getByIDs($user->getId(), $folder->getId(), $subscriptionType); + $subscription->delete(); + } + + public function subscribe() + { + if ($this->isSubscribed()) + { + return; + } + + $subscriptionType = SubscriptionEvent::subTypes('Folder '); + $user = $this->ktapi->get_user(); + $folder = $this->folder; + + $subscription = new Subscription($user->getId(), $folder->getId(), $subscriptionType); + $subscription->create(); + } + } ?> diff --git a/ktapi/ktapi.inc.php b/ktapi/ktapi.inc.php index 26fde3c..39353f9 100644 --- a/ktapi/ktapi.inc.php +++ b/ktapi/ktapi.inc.php @@ -70,6 +70,13 @@ abstract class KTAPI_FolderItem } public abstract function getObject(); + + public abstract function isSubscribed(); + + public abstract function unsubscribe(); + + public abstract function subscribe(); + } class KTAPI_Error extends PEAR_Error