Skip to content

Commit

Permalink
Merge pull request #29566 from owncloud/fedsharelog-beforeacceptorrej…
Browse files Browse the repository at this point in the history
…ect-stable10

[stable10] Trigger eventdispatcher for federated share
  • Loading branch information
Vincent Petry authored Nov 14, 2017
2 parents 913c884 + 62cbe95 commit 6dc5ddc
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 4 deletions.
14 changes: 13 additions & 1 deletion apps/federatedfilesharing/lib/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use OCP\IRequest;
use OCP\IUserManager;
use OCP\Share;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class RequestHandler
Expand Down Expand Up @@ -65,6 +67,9 @@ class RequestHandler {
/** @var IUserManager */
private $userManager;

/** @var EventDispatcherInterface */
private $eventDispatcher;

/** @var string */
private $shareTable = 'share';

Expand All @@ -78,14 +83,16 @@ class RequestHandler {
* @param Notifications $notifications
* @param AddressHandler $addressHandler
* @param IUserManager $userManager
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(FederatedShareProvider $federatedShareProvider,
IDBConnection $connection,
Share\IManager $shareManager,
IRequest $request,
Notifications $notifications,
AddressHandler $addressHandler,
IUserManager $userManager
IUserManager $userManager,
EventDispatcherInterface $eventDispatcher
) {
$this->federatedShareProvider = $federatedShareProvider;
$this->connection = $connection;
Expand All @@ -94,6 +101,7 @@ public function __construct(FederatedShareProvider $federatedShareProvider,
$this->notifications = $notifications;
$this->addressHandler = $addressHandler;
$this->userManager = $userManager;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -165,6 +173,10 @@ public function createShare($params) {
$sharedByFederatedId = $ownerFederatedId;
}

$event = new GenericEvent(null, ['name' => $name, 'targetuser' => $sharedByFederatedId,
'owner' => $owner, 'sharewith' => $shareWith,
'sharedby' => $sharedBy, 'remoteid' => $remoteId]);
$this->eventDispatcher->dispatch('\OCA\FederatedFileSharing::remote_shareReceived', $event);
\OC::$server->getActivityManager()->publishActivity(
Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')], '', [],
'', '', $shareWith, Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW);
Expand Down
15 changes: 13 additions & 2 deletions apps/federatedfilesharing/tests/RequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\IUserManager;
use OCP\Share\IShare;
use OC\HTTPHelper;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class RequestHandlerTest
Expand Down Expand Up @@ -110,7 +111,8 @@ protected function setUp() {
\OC::$server->getRequest(),
$this->notifications,
$this->addressHandler,
$this->userManager
$this->userManager,
\OC::$server->getEventDispatcher()
);

$this->connection = \OC::$server->getDatabaseConnection();
Expand Down Expand Up @@ -161,8 +163,16 @@ function testCreateShare() {
$_POST['shareWith'] = self::TEST_FILES_SHARING_API_USER2;
$_POST['remoteId'] = 1;

$called = array();
\OC::$server->getEventDispatcher()->addListener('\OCA\FederatedFileSharing::remote_shareReceived', function ($event) use (&$called) {
$called[] = '\OCA\FederatedFileSharing::remote_shareReceived';
array_push($called, $event);
});

$result = $this->s2s->createShare(null);

$this->assertTrue($called[1] instanceof GenericEvent);

$this->assertTrue($result->succeeded());

$query = \OCP\DB::prepare('SELECT * FROM `*PREFIX*share_external` WHERE `remote_id` = ?');
Expand Down Expand Up @@ -190,7 +200,8 @@ function testDeclineShare() {
\OC::$server->getRequest(),
$this->notifications,
$this->addressHandler,
$this->userManager
$this->userManager,
\OC::$server->getEventDispatcher()
]
)->setMethods(['executeDeclineShare', 'verifyShare'])->getMock();

Expand Down
10 changes: 10 additions & 0 deletions apps/files_sharing/lib/External/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCP\Share\Events\AcceptShare;
use OCP\Share\Events\DeclineShare;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class Manager {
const STORAGE = '\OCA\Files_Sharing\External\Storage';
Expand Down Expand Up @@ -205,6 +206,9 @@ public function acceptShare($id) {
AcceptShare::class, new AcceptShare($share)
);

$event = new GenericEvent(null, ['sharedItem' => $share['name'], 'shareAcceptedFrom' => $share['owner'],
'remoteUrl' => $share['remote']]);
$this->eventDispatcher->dispatch('remoteshare.accepted',$event);
\OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $share['remote']]);

$this->processNotification($id);
Expand Down Expand Up @@ -234,6 +238,10 @@ public function declineShare($id) {
new DeclineShare($share)
);

$event = new GenericEvent(null, ['sharedItem' => $share['name'], 'shareAcceptedFrom' => $share['owner'],
'remoteUrl' => $share['remote']]);
$this->eventDispatcher->dispatch('remoteshare.declined',$event);

$this->processNotification($id);
return true;
}
Expand Down Expand Up @@ -342,6 +350,8 @@ public function removeShare($mountPoint) {

if($result) {
$this->removeReShares($id);
$event = new GenericEvent(null, ['user' => $this->uid, 'targetmount' => $mountPoint]);
$this->eventDispatcher->dispatch('\OCA\Files_Sharing::unshareEvent', $event);
}

return $result;
Expand Down
97 changes: 97 additions & 0 deletions apps/files_sharing/tests/External/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace OCA\Files_Sharing\Tests\External;

use League\Flysystem\MountManager;
use OC\Files\Mount\MountPoint;
use OC\Files\Storage\StorageFactory;
use OCA\Files_Sharing\External\Manager;
use OCA\Files_Sharing\External\MountProvider;
Expand All @@ -32,6 +34,7 @@
use OCP\Share\Events\DeclineShare;
use OCP\Share\Events\ShareEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\Traits\UserTrait;

/**
Expand Down Expand Up @@ -130,12 +133,23 @@ public function testAddShare() {
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');

$called = array();
\OC::$server->getEventDispatcher()->addListener('remoteshare.accepted', function($event) use (&$called) {
$called[] = 'remoteshare.accepted';
array_push($called, $event);
});

$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(AcceptShare::class, $this->callback(function($event) use ($openShares) {
return $this->verifyShareEvent($event, $openShares[0], AcceptShare::class); }
));

$event = new GenericEvent(null, ['sharedItem' => '/SharedFolder', 'shareAcceptedFrom' => 'foobar', 'remoteUrl' => 'http://localhost']);
$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with('remoteshare.accepted', $event);

// Accept the first share
$this->manager->acceptShare($openShares[0]['id']);

Expand Down Expand Up @@ -173,6 +187,10 @@ public function testAddShare() {
return $this->verifyShareEvent($event, $openShares[1], DeclineShare::class); }
));

$event = new GenericEvent(null, ['sharedItem' => '/SharedFolder', 'shareAcceptedFrom' => 'foobar', 'remoteUrl' => 'http://localhost']);
$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with('remoteshare.declined', $event);
// Decline the third share
$this->manager->declineShare($openShares[1]['id']);

Expand Down Expand Up @@ -265,4 +283,83 @@ private function assertNotMount($mountPoint) {
private function getFullPath($path) {
return '/' . $this->uid . '/files' . $path;
}

public function testRemoveShare() {
/*$shareData1 = [
'remote' => 'http://localhost',
'token' => 'token1',
'password' => '',
'name' => '/SharedFolder',
'owner' => 'foobar',
'accepted' => false,
'user' => $this->uid,
];
$shareData2 = $shareData1;
$shareData2['token'] = 'token2';
$shareData3 = $shareData1;
$shareData3['token'] = 'token3';
// Add a share for "user"
$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
$openShares = $this->manager->getOpenShares();
$this->assertCount(1, $openShares);
$this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->setupMounts();
$this->assertNotMount('SharedFolder');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');*/


$this->mountManager = $this->createMock(\OC\Files\Mount\Manager::class);
$idbConnection = $this->createMock(\OCP\IDBConnection::class);
$prepare = $this->createMock(\Doctrine\DBAL\Driver\Statement::class);
$prepare->method('execute')
->willReturn(true);
$idbConnection->method('prepare')
->willReturn($prepare);
$storageFactory = $this->createMock(\OCP\Files\Storage\IStorageFactory::class);
$this->manager = new Manager(
$idbConnection,
$this->mountManager,
$storageFactory,
\OC::$server->getNotificationManager(),
\OC::$server->getEventDispatcher(),
$this->uid
);

$mountpointobj = $this->createMock(\OC\Files\Mount\MountPoint::class);
$this->mountManager->method('find')
->willReturn($mountpointobj);
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
$fileCache = $this->createMock(\OC\Files\Cache\Cache::class);
$storage->method('getCache')
->willReturn($fileCache);
$mountpointobj->method('getStorage')
->willReturn($storage);

$iqueryBuilder = $this->createMock(\OCP\DB\QueryBuilder\IQueryBuilder::class);
$iqueryBuilder->method('select')
->willReturn($iqueryBuilder);
$iqueryBuilder->method('from')
->willReturn($iqueryBuilder);
$iqueryBuilder->method('where')
->willReturn($iqueryBuilder);
$iqueryBuilder->method('delete')
->willReturn($iqueryBuilder);
$expressionBuilder = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class);
$iqueryBuilder->method('expr')
->willReturn($expressionBuilder);
$idbConnection->method('getQueryBuilder')
->willReturn($iqueryBuilder);

$called = array();
\OC::$server->getEventDispatcher()->addListener('\OCA\Files_Sharing::unshareEvent', function($event) use (&$called) {
$called[] = '\OCA\Files_Sharing::unshareEvent';
array_push($called, $event);
});

$this->manager->removeShare('/SharedFolder');
$this->assertSame('\OCA\Files_Sharing::unshareEvent', $called[0]);
$this->assertArrayHasKey('user', $called[1]);
}
}
3 changes: 2 additions & 1 deletion ocs/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
\OC::$server->getRequest(),
$notification,
$addressHandler,
\OC::$server->getUserManager()
\OC::$server->getUserManager(),
\OC::$server->getEventDispatcher()
);
API::register('post',
'/cloud/shares',
Expand Down

0 comments on commit 6dc5ddc

Please sign in to comment.