Skip to content

Commit

Permalink
Adding symfony events for shares
Browse files Browse the repository at this point in the history
Adding before and after events for shares
using symfony's event dispatcher.

Signed-off-by: Sujith H <[email protected]>
  • Loading branch information
sharidas committed Feb 22, 2018
1 parent e1d0f2f commit 6aa850f
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 139 deletions.
226 changes: 125 additions & 101 deletions lib/private/Share/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OC\Group\Group;
use OCA\FederatedFileSharing\DiscoveryManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Events\EventEmitterTrait;
use OCP\IUser;
use OCP\IUserSession;
use OCP\IDBConnection;
Expand All @@ -60,6 +61,8 @@
*/
class Share extends Constants {

use EventEmitterTrait;

/** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask
* Construct permissions for share() and setPermissions with Or (|) e.g.
* Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE
Expand Down Expand Up @@ -996,12 +999,17 @@ public static function unshareAll($itemType, $itemSource) {
'itemSource' => $itemSource,
'shares' => $shares,
];
\OC_Hook::emit('OCP\Share', 'pre_unshareAll', $hookParams);
foreach ($shares as $share) {
self::unshareItem($share);
}
\OC_Hook::emit('OCP\Share', 'post_unshareAll', $hookParams);
return true;
return (new self)->emittingCall(function () use (&$hookParams, $shares) {
\OC_Hook::emit('OCP\Share', 'pre_unshareAll', $hookParams);
foreach ($shares as $share) {
self::unshareItem($share);
}
\OC_Hook::emit('OCP\Share', 'post_unshareAll', $hookParams);
return true;
}, [
'before' => ['share' => $hookParams],
'after' => ['share' => $hookParams]],
'file', 'unshareAll');
}
return false;
}
Expand Down Expand Up @@ -1532,11 +1540,17 @@ protected static function unshareItem(array $item, $newParent = null) {
$hookParams['fileTarget'] = $item['file_target'];
}

\OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams);
$deletedShares = Helper::delete($item['id'], false, null, $newParent);
$deletedShares[] = $hookParams;
$hookParams['deletedShares'] = $deletedShares;
\OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams);
(new self)->emittingCall(function () use (&$hookParams, &$item, &$newParent) {
\OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams);
$deletedShares = Helper::delete($item['id'], false, null, $newParent);
$deletedShares[] = $hookParams;
$hookParams['deletedShares'] = $deletedShares;
\OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams);
return true;
}, [
'before' => ['share' => $hookParams],
'after' => ['share' => &$hookParams],
], 'file', 'unshare');
if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) {
list(, $remote) = Helper::splitUserRemote($item['share_with']);
self::sendRemoteUnshare($remote, $item['id'], $item['token']);
Expand Down Expand Up @@ -2223,118 +2237,128 @@ private static function put($itemType, $itemSource, $shareType, $shareWith, $uid
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);
$postHookData = [];
return (new self)->emittingCall(function () use (&$preHookData, &$users, &$itemType,
&$isGroupShare, &$groupItemTarget, &$fileSource, &$itemSource,
&$shareType, &$uidOwner, &$suggestedItemTarget, &$parent,
&$parentFolder, &$suggestedItemTarget, &$suggestedFileTarget,
&$filePath, &$groupFileTarget, &$permissions, &$expirationDate,
&$token, &$shareWith, &$queriesToExecute, &$postHookData) {
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;
$userShareType = ($isGroupShare) ? self::$shareTypeGroupUserUnique : $shareType;

if ($sourceExists && $sourceExists['item_source'] === $itemSource) {
$fileTarget = $sourceExists['file_target'];
$itemTarget = $sourceExists['item_target'];
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;
}
// 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;
} 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 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 = Helper::generateTarget('file', $filePath, $userShareType,
$user, $uidOwner, $suggestedFileTarget, $parent);
$fileTarget = null;
}
} else {
$fileTarget = null;
}

} else {
} else {

// group share which doesn't exists until now, check if we need a unique target for this user
// 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);
$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;
}
// 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;
if (($itemTarget === $groupItemTarget) &&
(!isset($fileSource) || $fileTarget === $groupFileTarget)) {
continue;
}
}
}

$queriesToExecute[] = [
'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,
];

}
$queriesToExecute[] = [
'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::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
unset($queriesToExecute['groupShare']);
}
}

foreach ($queriesToExecute as $shareQuery) {
$shareQuery['parent'] = $parent;
$id = self::insertShare($shareQuery);
}
$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::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share');
unset($queriesToExecute['groupShare']);
}

$postHookData = [
'itemType' => $itemType,
'itemSource' => $itemSource,
'parent' => $parent,
'shareType' => $shareType,
'uidOwner' => $uidOwner,
'permissions' => $permissions,
'fileSource' => $fileSource,
'id' => $parent,
'token' => $token,
'expirationDate' => $expirationDate,
];
foreach ($queriesToExecute as $shareQuery) {
$shareQuery['parent'] = $parent;
$id = self::insertShare($shareQuery);
}

$postHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith;
$postHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget;
$postHookData['fileTarget'] = ($isGroupShare) ? $groupFileTarget : $fileTarget;
$postHookData = [
'itemType' => $itemType,
'itemSource' => $itemSource,
'parent' => $parent,
'shareType' => $shareType,
'uidOwner' => $uidOwner,
'permissions' => $permissions,
'fileSource' => $fileSource,
'id' => $parent,
'token' => $token,
'expirationDate' => $expirationDate,
];

\OC_Hook::emit('OCP\Share', 'post_shared', $postHookData);
$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;
return $id ? $id : false;
}, [
'before' => ['sharedata' => $preHookData],
'after' => ['sharedata' => &$postHookData]
], 'file', 'share');
}

/**
Expand Down
Loading

0 comments on commit 6aa850f

Please sign in to comment.