Ejemplo n.º 1
0
 protected function shareFileOrFolderWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId)
 {
     // Members of the new group
     $affectedUsers = array();
     // User performing the share
     $this->shareNotificationForSharer('shared_sharing_group_self', $shareWith, $fileSource, $itemType);
     $this->shareNotificationForOriginalOwners($this->currentUser, 'reshared_sharing_group_by', $shareWith, $fileSource, $itemType);
     $usersInGroup = \OCA\Sharing_Group\Data::readGroupUsers($shareWith);
     foreach ($usersInGroup as $user) {
         $affectedUsers[$user] = $fileTarget;
     }
     // Remove the triggering user, we already managed his notifications
     unset($affectedUsers[$this->currentUser]);
     if (empty($affectedUsers)) {
         return;
     }
     $filteredStreamUsersInGroup = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', Files_Sharing::TYPE_SHARED);
     $filteredEmailUsersInGroup = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', Files_Sharing::TYPE_SHARED);
     $affectedUsers = $this->fixPathsForShareExceptions($affectedUsers, $shareId);
     foreach ($affectedUsers as $user => $path) {
         if (empty($filteredStreamUsersInGroup[$user]) && empty($filteredEmailUsersInGroup[$user])) {
             continue;
         }
         $this->addNotificationsForUser($user, 'shared_with_by', array($path, $this->currentUser), $fileSource, $path, $itemType === 'file', !empty($filteredStreamUsersInGroup[$user]), !empty($filteredEmailUsersInGroup[$user]) ? $filteredEmailUsersInGroup[$user] : 0);
     }
 }
 protected function shareFileOrFolderWithGroup($params)
 {
     // User performing the share
     $subject = 'shared_sharing_group_self';
     $this->shareNotificationForSharer($subject, $params['shareWith'], $params['fileSource'], $params['itemType']);
     // Members of the new group
     $affectedUsers = array();
     $usersInGroup = Data::readGroupUsers($params['shareWith']);
     foreach ($usersInGroup as $user) {
         $affectedUsers[$user] = $params['fileTarget'];
     }
     // Remove the triggering user, we already managed his notifications
     unset($affectedUsers[$this->currentUser]);
     if (empty($affectedUsers)) {
         return;
     }
     $filteredStreamUsersInGroup = $this->userSettings->filterUsersBySetting($usersInGroup, 'stream', Files_Sharing::TYPE_SHARED);
     $filteredEmailUsersInGroup = $this->userSettings->filterUsersBySetting($usersInGroup, 'email', Files_Sharing::TYPE_SHARED);
     // Check when there was a naming conflict and the target is different
     // for some of the users
     $query = DB::prepare('SELECT `share_with`, `file_target` FROM `*PREFIX*share` WHERE `parent` = ? ');
     $result = $query->execute(array($params['id']));
     if (DB::isError($result)) {
         Util::writeLog('OCA\\Activity\\Hooks::shareFileOrFolderWithGroup', DB::getErrorMessage($result), Util::ERROR);
     } else {
         while ($row = $result->fetchRow()) {
             $affectedUsers[$row['share_with']] = $row['file_target'];
         }
     }
     foreach ($affectedUsers as $user => $path) {
         if (empty($filteredStreamUsersInGroup[$user]) && empty($filteredEmailUsersInGroup[$user])) {
             continue;
         }
         $this->addNotificationsForUser($user, 'shared_with_by', array($path, $this->currentUser), $path, $params['itemType'] === 'file', !empty($filteredStreamUsersInGroup[$user]), !empty($filteredEmailUsersInGroup[$user]) ? $filteredEmailUsersInGroup[$user] : 0);
     }
 }
Ejemplo n.º 3
0
 /**
  * Put shared item into the database
  * @param string $itemType Item type
  * @param string $itemSource Item source
  * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  * @param string $shareWith User or group the item is being shared with
  * @param string $uidOwner User that is the owner of shared item
  * @param int $permissions CRUDS permissions
  * @param boolean|array $parentFolder Parent folder target (optional)
  * @param string $token (optional)
  * @param string $itemSourceName name of the source item (optional)
  * @param \DateTime $expirationDate (optional)
  * @throws \Exception
  * @return mixed id of the new share or false
  */
 private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder = null, $token = null, $itemSourceName = null, \DateTime $expirationDate = null)
 {
     $queriesToExecute = array();
     $suggestedItemTarget = null;
     $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = '';
     $groupItemTarget = $itemTarget = $fileSource = $parent = 0;
     $result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate);
     if (!empty($result)) {
         $parent = $result['parent'];
         $itemSource = $result['itemSource'];
         $fileSource = $result['fileSource'];
         $suggestedItemTarget = $result['suggestedItemTarget'];
         $suggestedFileTarget = $result['suggestedFileTarget'];
         $filePath = $result['filePath'];
     }
     $isGroupShare = false;
     if ($shareType == self::SHARE_TYPE_GROUP) {
         $isGroupShare = true;
         if (isset($shareWith['users'])) {
             $users = $shareWith['users'];
         } else {
             $users = \OC_Group::usersInGroup($shareWith['group']);
         }
         // remove current user from list
         if (in_array(\OCP\User::getUser(), $users)) {
             unset($users[array_search(\OCP\User::getUser(), $users)]);
         }
         $groupItemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget);
         $groupFileTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith['group'], $uidOwner, $filePath);
         // add group share to table and remember the id as parent
         $queriesToExecute['groupShare'] = array('itemType' => $itemType, 'itemSource' => $itemSource, 'itemTarget' => $groupItemTarget, 'shareType' => $shareType, 'shareWith' => $shareWith['group'], 'uidOwner' => $uidOwner, 'permissions' => $permissions, 'shareTime' => time(), 'fileSource' => $fileSource, 'fileTarget' => $groupFileTarget, 'token' => $token, 'parent' => $parent, 'expiration' => $expirationDate);
     } else {
         if ($shareType == self::SHARE_TYPE_SHARING_GROUP) {
             $isGroupShare = true;
             if (isset($shareWith['users'])) {
                 $users = $shareWith['users'];
             } else {
                 $users = \OCA\Sharing_Group\Data::readGroupUsers($shareWith['group']);
             }
             if (in_array(\OCP\User::getUser(), $users)) {
                 unset($users[array_search(\OCP\User::getUser(), $users)]);
             }
             $groupItemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget);
             $groupFileTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith['group'], $uidOwner, $filePath);
             // add group share to table and remember the id as parent
             $queriesToExecute['groupShare'] = array('itemType' => $itemType, 'itemSource' => $itemSource, 'itemTarget' => $groupItemTarget, 'shareType' => $shareType, 'shareWith' => $shareWith['group'], 'uidOwner' => $uidOwner, 'permissions' => $permissions, 'shareTime' => time(), 'fileSource' => $fileSource, 'fileTarget' => $groupFileTarget, 'token' => $token, 'parent' => $parent, 'expiration' => $expirationDate);
         } else {
             $users = array($shareWith);
             $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $suggestedItemTarget);
         }
     }
     $run = true;
     $error = '';
     $preHookData = array('itemType' => $itemType, 'itemSource' => $itemSource, 'shareType' => $shareType, 'uidOwner' => $uidOwner, 'permissions' => $permissions, 'fileSource' => $fileSource, 'expiration' => $expirationDate, 'token' => $token, 'run' => &$run, 'error' => &$error);
     $preHookData['itemTarget'] = $isGroupShare ? $groupItemTarget : $itemTarget;
     $preHookData['shareWith'] = $isGroupShare ? $shareWith['group'] : $shareWith;
     \OC_Hook::emit('OCP\\Share', 'pre_shared', $preHookData);
     if ($run === false) {
         throw new \Exception($error);
     }
     foreach ($users as $user) {
         $sourceId = $itemType === 'file' || $itemType === 'folder' ? $fileSource : $itemSource;
         $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user);
         $userShareType = $isGroupShare ? self::$shareTypeGroupUserUnique : $shareType;
         if ($sourceExists && $sourceExists['item_source'] === $itemSource) {
             $fileTarget = $sourceExists['file_target'];
             $itemTarget = $sourceExists['item_target'];
             // for group shares we don't need a additional entry if the target is the same
             if ($isGroupShare && $groupItemTarget === $itemTarget) {
                 continue;
             }
         } elseif (!$sourceExists && !$isGroupShare) {
             $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user, $uidOwner, $suggestedItemTarget, $parent);
             if (isset($fileSource)) {
                 if ($parentFolder) {
                     if ($parentFolder === true) {
                         $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user, $uidOwner, $suggestedFileTarget, $parent);
                         if ($fileTarget != $groupFileTarget) {
                             $parentFolders[$user]['folder'] = $fileTarget;
                         }
                     } else {
                         if (isset($parentFolder[$user])) {
                             $fileTarget = $parentFolder[$user]['folder'] . $itemSource;
                             $parent = $parentFolder[$user]['id'];
                         }
                     }
                 } else {
                     $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user, $uidOwner, $suggestedFileTarget, $parent);
                 }
             } else {
                 $fileTarget = null;
             }
         } else {
             // group share which doesn't exists until now, check if we need a unique target for this user
             $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user, $uidOwner, $suggestedItemTarget, $parent);
             // do we also need a file target
             if (isset($fileSource)) {
                 $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user, $uidOwner, $suggestedFileTarget, $parent);
             } else {
                 $fileTarget = null;
             }
             if ($itemTarget === $groupItemTarget && (!isset($fileSource) || $fileTarget === $groupFileTarget)) {
                 continue;
             }
         }
         $queriesToExecute[] = array('itemType' => $itemType, 'itemSource' => $itemSource, 'itemTarget' => $itemTarget, 'shareType' => $userShareType, 'shareWith' => $user, 'uidOwner' => $uidOwner, 'permissions' => $permissions, 'shareTime' => time(), 'fileSource' => $fileSource, 'fileTarget' => $fileTarget, 'token' => $token, 'parent' => $parent, 'expiration' => $expirationDate);
     }
     $id = false;
     if ($isGroupShare) {
         $id = self::insertShare($queriesToExecute['groupShare']);
         // Save this id, any extra rows for this group share will need to reference it
         $parent = \OC_DB::insertid('*PREFIX*share');
         unset($queriesToExecute['groupShare']);
     }
     foreach ($queriesToExecute as $shareQuery) {
         $shareQuery['parent'] = $parent;
         $id = self::insertShare($shareQuery);
     }
     $postHookData = array('itemType' => $itemType, 'itemSource' => $itemSource, 'parent' => $parent, 'shareType' => $shareType, 'uidOwner' => $uidOwner, 'permissions' => $permissions, 'fileSource' => $fileSource, 'id' => $parent, 'token' => $token, 'expirationDate' => $expirationDate);
     $postHookData['shareWith'] = $isGroupShare ? $shareWith['group'] : $shareWith;
     $postHookData['itemTarget'] = $isGroupShare ? $groupItemTarget : $itemTarget;
     $postHookData['fileTarget'] = $isGroupShare ? $groupFileTarget : $fileTarget;
     \OC_Hook::emit('OCP\\Share', 'post_shared', $postHookData);
     return $id ? $id : false;
 }