Skip to content

Commit

Permalink
Adding new symfony events to replace old hooks
Browse files Browse the repository at this point in the history
Adding new symfony events for the following:
1) setPassword
2) setPassphrase

Signed-off-by: Sujith H <[email protected]>
  • Loading branch information
sharidas committed Feb 12, 2018
1 parent 1c8baeb commit 2f4cf9c
Show file tree
Hide file tree
Showing 17 changed files with 320 additions and 189 deletions.
6 changes: 4 additions & 2 deletions apps/dav/lib/Connector/Sabre/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public function __construct($view, $info, $shareManager = null, Request $request
*/
public function put($data) {
$path = $this->fileView->getAbsolutePath($this->path);
return $this->emittingCall(function () use (&$data) {
$emitPostEvent = false;
return $this->emittingCall(function () use (&$data, &$emitPostEvent) {
try {
$exists = $this->fileView->file_exists($this->path);
if ($this->info && $exists && !$this->info->isUpdateable()) {
Expand Down Expand Up @@ -251,10 +252,11 @@ public function put($data) {
throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage());
}

$emitPostEvent = true;
return '"' . $this->info->getEtag() . '"';
}, [
'before' => ['path' => $path],
'after' => ['path' => $path]],
'after' => ['path' => $path, 'processPostEvent' => &$emitPostEvent]],
'file', 'create');
}

Expand Down
45 changes: 20 additions & 25 deletions apps/dav/lib/HookManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\Util;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;

class HookManager {

Expand All @@ -50,6 +52,9 @@ class HookManager {
/** @var IL10N */
private $l10n;

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

/** @var array */
private $calendarsToDelete;

Expand All @@ -60,47 +65,37 @@ public function __construct(IUserManager $userManager,
SyncService $syncService,
CalDavBackend $calDav,
CardDavBackend $cardDav,
IL10N $l10n) {
IL10N $l10n,
EventDispatcher $eventDispatcher) {
$this->userManager = $userManager;
$this->syncService = $syncService;
$this->calDav = $calDav;
$this->cardDav = $cardDav;
$this->l10n = $l10n;
$this->eventDispatcher = $eventDispatcher;
}

public function setup() {
Util::connectHook('OC_User',
'post_createUser',
$this,
'postCreateUser');
Util::connectHook('OC_User',
'pre_deleteUser',
$this,
'preDeleteUser');
Util::connectHook('OC_User',
'post_deleteUser',
$this,
'postDeleteUser');
Util::connectHook('OC_User',
'changeUser',
$this,
'changeUser');
$this->eventDispatcher->addListener('user.aftercreateuser', [$this, 'postCreateUser']);
$this->eventDispatcher->addListener('user.beforedelete', [$this, 'preDeleteUser']);
$this->eventDispatcher->addListener('user.afterdelete', [$this, 'postDeleteUser']);
$this->eventDispatcher->addListener('user.beforefeaturechange', [$this, 'changeUser']);
}

public function postCreateUser($params) {
$user = $this->userManager->get($params['uid']);
public function postCreateUser(GenericEvent $params) {
$user = $this->userManager->get($params->getArgument('uid'));
$this->syncService->updateUser($user);
}

public function preDeleteUser($params) {
$uid = $params['uid'];
public function preDeleteUser(GenericEvent $params) {
$uid = $params->getArgument('uid');
$this->usersToDelete[$uid] = $this->userManager->get($uid);
$this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid);
$this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid);
}

public function postDeleteUser($params) {
$uid = $params['uid'];
public function postDeleteUser(GenericEvent $params) {
$uid = $params->getArgument('uid');
if (isset($this->usersToDelete[$uid])){
$this->syncService->deleteUser($this->usersToDelete[$uid]);
}
Expand All @@ -115,8 +110,8 @@ public function postDeleteUser($params) {
}
}

public function changeUser($params) {
$user = $params['user'];
public function changeUser(GenericEvent $params) {
$user = $params->getArgument('user');
$this->syncService->updateUser($user);
}

Expand Down
20 changes: 14 additions & 6 deletions apps/dav/tests/unit/DAV/HookManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@
use OCA\DAV\HookManager;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;

/**
* Class HookManagerTest
*
* @group DB
* @package OCA\DAV\Tests\unit\DAV
*/
class HookManagerTest extends TestCase {

/** @var L10N */
Expand Down Expand Up @@ -89,7 +96,7 @@ public function test() {
'principals/users/newUser',
'contacts', ['{DAV:}displayname' => $this->l10n->t('Contacts')]);

$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n);
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n, \OC::$server->getEventDispatcher());
$hm->firstLogin($user);
}

Expand Down Expand Up @@ -127,7 +134,7 @@ public function testWithExisting() {
]);
$card->expects($this->never())->method('createAddressBook');

$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n);
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n, \OC::$server->getEventDispatcher());
$hm->firstLogin($user);
}

Expand Down Expand Up @@ -171,7 +178,7 @@ public function testWithBirthdayCalendar() {
'principals/users/newUser',
'contacts', ['{DAV:}displayname' => $this->l10n->t('Contacts')]);

$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n);
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n, \OC::$server->getEventDispatcher());
$hm->firstLogin($user);
}

Expand Down Expand Up @@ -212,9 +219,10 @@ public function testDeleteCalendar() {
]);
$card->expects($this->once())->method('deleteAddressBook')->with('personal');

$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n);
$hm->preDeleteUser(['uid' => 'newUser']);
$hm->postDeleteUser(['uid' => 'newUser']);
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n, \OC::$server->getEventDispatcher());
$params = new GenericEvent(null, ['uid' => 'newUser']);
$hm->preDeleteUser($params);
$hm->postDeleteUser($params);
}

}
8 changes: 5 additions & 3 deletions apps/files_sharing/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
class Helper {

public static function registerHooks() {
\OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook');
\OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren');
$hooks = new Hooks();
$updater = new Updater();
\OC::$server->getEventDispatcher()->addListener('file.afterrename', [$updater, 'renameHook']);
\OC::$server->getEventDispatcher()->addListener('file.afterdelete', [$hooks, 'unshareChildren']);

\OCP\Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser');
\OC::$server->getEventDispatcher()->addListener('user.afterdelete', [$hooks, 'deleteUser']);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions apps/files_sharing/lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@

use OC\Files\Filesystem;
use OCA\FederatedFileSharing\DiscoveryManager;
use Symfony\Component\EventDispatcher\GenericEvent;

class Hooks {

public static function deleteUser($params) {
public static function deleteUser(GenericEvent $params) {
$discoveryManager = new DiscoveryManager(
\OC::$server->getMemCacheFactory(),
\OC::$server->getHTTPClientService()
Expand All @@ -41,13 +42,13 @@ public static function deleteUser($params) {
\OC\Files\Filesystem::getLoader(),
\OC::$server->getNotificationManager(),
\OC::$server->getEventDispatcher(),
$params['uid']);
$params->getArgument('uid'));

$manager->removeUserShares($params['uid']);
$manager->removeUserShares($params->getArgument('uid'));
}

public static function unshareChildren($params) {
$path = Filesystem::getView()->getAbsolutePath($params['path']);
public static function unshareChildren(GenericEvent $params) {
$path = Filesystem::getView()->getAbsolutePath($params->getArgument('path'));
$view = new \OC\Files\View('/');

// find share mount points within $path and unmount them
Expand Down
20 changes: 16 additions & 4 deletions apps/files_sharing/lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@

namespace OCA\Files_Sharing;

use Symfony\Component\EventDispatcher\GenericEvent;

class Updater {

/**
* @param array $params
*/
static public function renameHook($params) {
self::renameChildren($params['oldpath'], $params['newpath']);
self::moveShareToShare($params['newpath']);
static public function renameHook(GenericEvent $params) {
if ($params->getArgument('processPostEvent') === true) {
self::renameChildren($params->getArgument('oldpath'), $params->getArgument('newpath'));
self::moveShareToShare($params->getArgument('newpath'));
}
}

/**
Expand All @@ -52,7 +56,15 @@ static private function moveShareToShare($path) {
return;
}

$src = $userFolder->get($path);
/**
* What if the path is not relative to $userFolder So as a precaution
* we could also try to get the src from the RootFolder.
*/
try {
$src = $userFolder->get($path);
} catch (\Exception $e) {
$src = \OC::$server->getRootFolder()->get($path);
}

$shareManager = \OC::$server->getShareManager();

Expand Down
24 changes: 16 additions & 8 deletions apps/files_versions/lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,20 @@

namespace OCA\Files_Versions;

use OC\Files\Filesystem;
use Symfony\Component\EventDispatcher\GenericEvent;

class Hooks {

public static function connectHooks() {
$hooks = new Hooks();
// Listen to write signals
\OCP\Util::connectHook('OC_Filesystem', 'write', 'OCA\Files_Versions\Hooks', 'write_hook');
// Listen to delete and rename signals
\OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Files_Versions\Hooks', 'remove_hook');
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Versions\Hooks', 'pre_remove_hook');
\OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Files_Versions\Hooks', 'rename_hook');
\OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Files_Versions\Hooks', 'copy_hook');
\OC::$server->getEventDispatcher()->addListener('file.afterrename', [$hooks, 'rename_hook']);
\OC::$server->getEventDispatcher()->addListener('file.aftercopy', [$hooks, 'copy_hook']);
\OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
\OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');

Expand Down Expand Up @@ -98,11 +102,13 @@ public static function pre_remove_hook($params) {
* This function is connected to the rename signal of OC_Filesystem and adjust the name and location
* of the stored versions along the actual file
*/
public static function rename_hook($params) {
public static function rename_hook(GenericEvent $params) {

if (\OCP\App::isEnabled('files_versions')) {
$oldpath = $params['oldpath'];
$newpath = $params['newpath'];
$oldpath = $params->getArgument('oldpath');
$oldpath = Filesystem::getView()->getRelativePath($oldpath);
$newpath = $params->getArgument('newpath');
$newpath = Filesystem::getView()->getRelativePath($newpath);
if($oldpath<>'' && $newpath<>'') {
Storage::renameOrCopy($oldpath, $newpath, 'rename');
}
Expand All @@ -116,11 +122,13 @@ public static function rename_hook($params) {
* This function is connected to the copy signal of OC_Filesystem and copies the
* the stored versions to the new location
*/
public static function copy_hook($params) {
public static function copy_hook(GenericEvent $params) {

if (\OCP\App::isEnabled('files_versions')) {
$oldpath = $params['oldpath'];
$newpath = $params['newpath'];
$oldpath = $params->getArgument('oldpath');
$oldpath = Filesystem::getView()->getRelativePath($oldpath);
$newpath = $params->getArgument('newpath');
$newpath = Filesystem::getView()->getRelativePath($newpath);
if($oldpath<>'' && $newpath<>'') {
Storage::renameOrCopy($oldpath, $newpath, 'copy');
}
Expand Down
8 changes: 5 additions & 3 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,11 @@ private static function registerEncryptionWrapper() {
private static function registerEncryptionHooks() {
$enabled = self::$server->getEncryptionManager()->isEnabled();
if ($enabled) {
\OCP\Util::connectHook('OCP\Share', 'post_shared', 'OC\Encryption\HookManager', 'postShared');
\OCP\Util::connectHook('OCP\Share', 'post_unshare', 'OC\Encryption\HookManager', 'postUnshared');
\OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OC\Encryption\HookManager', 'postRename');
$eventDispatcher = OC::$server->getEventDispatcher();
$hookManager = new \OC\Encryption\HookManager();
$eventDispatcher->addListener('file.aftercreateshare', [$hookManager, 'postShared']);
$eventDispatcher->addListener('file.afterunshare',[$hookManager, 'postUnshared']);
$eventDispatcher->addListener('file.afterrename', [$hookManager, 'postRename']);
\OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', 'OC\Encryption\HookManager', 'postRestore');
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/private/Encryption/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OC\Files\Filesystem;
use \OC\Files\Mount;
use \OC\Files\View;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* update encrypted files, e.g. because a file was shared
Expand Down Expand Up @@ -81,10 +82,10 @@ public function __construct(
*
* @param array $params
*/
public function postShared($params) {
public function postShared(GenericEvent $params) {
if ($this->encryptionManager->isEnabled()) {
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
$path = Filesystem::getPath($params['fileSource']);
if ($params->getArgument('itemType') === 'file' || $params->getArgument('itemType') === 'folder') {
$path = Filesystem::getPath($params->getArgument('fileSource'));
list($owner, $ownerPath) = $this->getOwnerPath($path);
$absPath = '/' . $owner . '/files/' . $ownerPath;
$this->update($absPath);
Expand All @@ -97,10 +98,10 @@ public function postShared($params) {
*
* @param array $params
*/
public function postUnshared($params) {
public function postUnshared(GenericEvent $params) {
if ($this->encryptionManager->isEnabled()) {
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
$path = Filesystem::getPath($params['fileSource']);
if ($params->getArgument('itemType') === 'file' || $params->getArgument('itemType') === 'folder') {
$path = Filesystem::getPath($params->getArgument('fileSource'));
list($owner, $ownerPath) = $this->getOwnerPath($path);
$absPath = '/' . $owner . '/files/' . $ownerPath;
$this->update($absPath);
Expand Down
Loading

0 comments on commit 2f4cf9c

Please sign in to comment.