commit
348d97dfd6
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
|
||||
*
|
||||
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
namespace OC\Share20;
|
||||
|
||||
use OCP\Share\IShare;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\GenericEvent;
|
||||
|
||||
class LegacyHooks {
|
||||
/** @var EventDispatcher */
|
||||
private $eventDispatcher;
|
||||
|
||||
/**
|
||||
* LegacyHooks constructor.
|
||||
*
|
||||
* @param EventDispatcher $eventDispatcher
|
||||
*/
|
||||
public function __construct(EventDispatcher $eventDispatcher) {
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
|
||||
$this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']);
|
||||
$this->eventDispatcher->addListener('OCP\Share::postUnshare', [$this, 'postUnshare']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GenericEvent $e
|
||||
*/
|
||||
public function preUnshare(GenericEvent $e) {
|
||||
/** @var IShare $share */
|
||||
$share = $e->getSubject();
|
||||
|
||||
$formatted = $this->formatHookParams($share);
|
||||
\OC_Hook::emit('OCP\Share', 'pre_unshare', $formatted);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GenericEvent $e
|
||||
*/
|
||||
public function postUnshare(GenericEvent $e) {
|
||||
/** @var IShare $share */
|
||||
$share = $e->getSubject();
|
||||
|
||||
$formatted = $this->formatHookParams($share);
|
||||
|
||||
/** @var IShare[] $deletedShares */
|
||||
$deletedShares = $e->getArgument('deletedShares');
|
||||
|
||||
$formattedDeletedShares = array_map(function($share) {
|
||||
return $this->formatHookParams($share);
|
||||
}, $deletedShares);
|
||||
|
||||
$formatted['deletedShares'] = $formattedDeletedShares;
|
||||
|
||||
\OC_Hook::emit('OCP\Share', 'pre_unshare', $formatted);
|
||||
}
|
||||
|
||||
private function formatHookParams(IShare $share) {
|
||||
// Prepare hook
|
||||
$shareType = $share->getShareType();
|
||||
$sharedWith = '';
|
||||
if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
|
||||
$shareType === \OCP\Share::SHARE_TYPE_GROUP ||
|
||||
$shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
|
||||
$sharedWith = $share->getSharedWith();
|
||||
}
|
||||
|
||||
$hookParams = [
|
||||
'id' => $share->getId(),
|
||||
'itemType' => $share->getNodeType(),
|
||||
'itemSource' => $share->getNodeId(),
|
||||
'shareType' => $shareType,
|
||||
'shareWith' => $sharedWith,
|
||||
'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
|
||||
'uidOwner' => $share->getSharedBy(),
|
||||
'fileSource' => $share->getNodeId(),
|
||||
'fileTarget' => $share->getTarget()
|
||||
];
|
||||
return $hookParams;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
|
||||
*
|
||||
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
namespace Test\Share20;
|
||||
|
||||
use OC\Share20\LegacyHooks;
|
||||
use OC\Share20\Manager;
|
||||
use OCP\Files\File;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\GenericEvent;
|
||||
use Test\TestCase;
|
||||
|
||||
class LegacyHooksTest extends TestCase {
|
||||
|
||||
/** @var LegacyHooks */
|
||||
private $hooks;
|
||||
|
||||
/** @var EventDispatcher */
|
||||
private $eventDispatcher;
|
||||
|
||||
/** @var Manager */
|
||||
private $manager;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->eventDispatcher = new EventDispatcher();
|
||||
$this->hooks = new LegacyHooks($this->eventDispatcher);
|
||||
$this->manager = \OC::$server->getShareManager();
|
||||
}
|
||||
|
||||
public function testPreUnshare() {
|
||||
$path = $this->createMock(File::class);
|
||||
$path->method('getId')->willReturn(1);
|
||||
|
||||
$share = $this->manager->newShare();
|
||||
$share->setId(42)
|
||||
->setProviderId('prov')
|
||||
->setShareType(\OCP\Share::SHARE_TYPE_USER)
|
||||
->setSharedWith('awesomeUser')
|
||||
->setSharedBy('sharedBy')
|
||||
->setNode($path)
|
||||
->setTarget('myTarget');
|
||||
|
||||
$hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock();
|
||||
\OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
|
||||
|
||||
$hookListnerExpectsPre = [
|
||||
'id' => 42,
|
||||
'itemType' => 'file',
|
||||
'itemSource' => 1,
|
||||
'shareType' => \OCP\Share::SHARE_TYPE_USER,
|
||||
'shareWith' => 'awesomeUser',
|
||||
'itemparent' => null,
|
||||
'uidOwner' => 'sharedBy',
|
||||
'fileSource' => 1,
|
||||
'fileTarget' => 'myTarget',
|
||||
];
|
||||
|
||||
$hookListner
|
||||
->expects($this->exactly(1))
|
||||
->method('pre')
|
||||
->with($hookListnerExpectsPre);
|
||||
|
||||
$event = new GenericEvent($share);
|
||||
$this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
|
||||
}
|
||||
|
||||
public function testPostUnshare() {
|
||||
$path = $this->createMock(File::class);
|
||||
$path->method('getId')->willReturn(1);
|
||||
|
||||
$share = $this->manager->newShare();
|
||||
$share->setId(42)
|
||||
->setProviderId('prov')
|
||||
->setShareType(\OCP\Share::SHARE_TYPE_USER)
|
||||
->setSharedWith('awesomeUser')
|
||||
->setSharedBy('sharedBy')
|
||||
->setNode($path)
|
||||
->setTarget('myTarget');
|
||||
|
||||
$hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
|
||||
\OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'post');
|
||||
|
||||
$hookListnerExpectsPost = [
|
||||
'id' => 42,
|
||||
'itemType' => 'file',
|
||||
'itemSource' => 1,
|
||||
'shareType' => \OCP\Share::SHARE_TYPE_USER,
|
||||
'shareWith' => 'awesomeUser',
|
||||
'itemparent' => null,
|
||||
'uidOwner' => 'sharedBy',
|
||||
'fileSource' => 1,
|
||||
'fileTarget' => 'myTarget',
|
||||
'deletedShares' => [
|
||||
[
|
||||
'id' => 42,
|
||||
'itemType' => 'file',
|
||||
'itemSource' => 1,
|
||||
'shareType' => \OCP\Share::SHARE_TYPE_USER,
|
||||
'shareWith' => 'awesomeUser',
|
||||
'itemparent' => null,
|
||||
'uidOwner' => 'sharedBy',
|
||||
'fileSource' => 1,
|
||||
'fileTarget' => 'myTarget',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$hookListner
|
||||
->expects($this->exactly(1))
|
||||
->method('post')
|
||||
->with($hookListnerExpectsPost);
|
||||
|
||||
$event = new GenericEvent($share);
|
||||
$event->setArgument('deletedShares', [$share]);
|
||||
$this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue