Skip to content

Commit 1e60b73

Browse files
committed
Adding new symfony events to replace old hooks
Adding new symfony events for the following: 1) setPassword 2) setPassphrase Signed-off-by: Sujith H <sharidasan@owncloud.com>
1 parent 1c8baeb commit 1e60b73

File tree

17 files changed

+351
-189
lines changed

17 files changed

+351
-189
lines changed

apps/dav/lib/Connector/Sabre/File.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public function __construct($view, $info, $shareManager = null, Request $request
112112
*/
113113
public function put($data) {
114114
$path = $this->fileView->getAbsolutePath($this->path);
115-
return $this->emittingCall(function () use (&$data) {
115+
$emitPostEvent = false;
116+
return $this->emittingCall(function () use (&$data, &$emitPostEvent) {
116117
try {
117118
$exists = $this->fileView->file_exists($this->path);
118119
if ($this->info && $exists && !$this->info->isUpdateable()) {
@@ -251,10 +252,11 @@ public function put($data) {
251252
throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage());
252253
}
253254

255+
$emitPostEvent = true;
254256
return '"' . $this->info->getEtag() . '"';
255257
}, [
256258
'before' => ['path' => $path],
257-
'after' => ['path' => $path]],
259+
'after' => ['path' => $path, 'processPostEvent' => &$emitPostEvent]],
258260
'file', 'create');
259261
}
260262

apps/dav/lib/HookManager.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
use OCP\IUser;
3030
use OCP\IUserManager;
3131
use OCP\Util;
32+
use Symfony\Component\EventDispatcher\EventDispatcher;
33+
use Symfony\Component\EventDispatcher\GenericEvent;
3234

3335
class HookManager {
3436

@@ -50,6 +52,9 @@ class HookManager {
5052
/** @var IL10N */
5153
private $l10n;
5254

55+
/** @var EventDispatcher */
56+
private $eventDispatcher;
57+
5358
/** @var array */
5459
private $calendarsToDelete;
5560

@@ -60,47 +65,37 @@ public function __construct(IUserManager $userManager,
6065
SyncService $syncService,
6166
CalDavBackend $calDav,
6267
CardDavBackend $cardDav,
63-
IL10N $l10n) {
68+
IL10N $l10n,
69+
EventDispatcher $eventDispatcher) {
6470
$this->userManager = $userManager;
6571
$this->syncService = $syncService;
6672
$this->calDav = $calDav;
6773
$this->cardDav = $cardDav;
6874
$this->l10n = $l10n;
75+
$this->eventDispatcher = $eventDispatcher;
6976
}
7077

7178
public function setup() {
72-
Util::connectHook('OC_User',
73-
'post_createUser',
74-
$this,
75-
'postCreateUser');
76-
Util::connectHook('OC_User',
77-
'pre_deleteUser',
78-
$this,
79-
'preDeleteUser');
80-
Util::connectHook('OC_User',
81-
'post_deleteUser',
82-
$this,
83-
'postDeleteUser');
84-
Util::connectHook('OC_User',
85-
'changeUser',
86-
$this,
87-
'changeUser');
79+
$this->eventDispatcher->addListener('user.aftercreateuser', [$this, 'postCreateUser']);
80+
$this->eventDispatcher->addListener('user.beforedelete', [$this, 'preDeleteUser']);
81+
$this->eventDispatcher->addListener('user.afterdelete', [$this, 'postDeleteUser']);
82+
$this->eventDispatcher->addListener('user.beforefeaturechange', [$this, 'changeUser']);
8883
}
8984

90-
public function postCreateUser($params) {
91-
$user = $this->userManager->get($params['uid']);
85+
public function postCreateUser(GenericEvent $params) {
86+
$user = $this->userManager->get($params->getArgument('uid'));
9287
$this->syncService->updateUser($user);
9388
}
9489

95-
public function preDeleteUser($params) {
96-
$uid = $params['uid'];
90+
public function preDeleteUser(GenericEvent $params) {
91+
$uid = $params->getArgument('uid');
9792
$this->usersToDelete[$uid] = $this->userManager->get($uid);
9893
$this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid);
9994
$this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid);
10095
}
10196

102-
public function postDeleteUser($params) {
103-
$uid = $params['uid'];
97+
public function postDeleteUser(GenericEvent $params) {
98+
$uid = $params->getArgument('uid');
10499
if (isset($this->usersToDelete[$uid])){
105100
$this->syncService->deleteUser($this->usersToDelete[$uid]);
106101
}
@@ -115,8 +110,8 @@ public function postDeleteUser($params) {
115110
}
116111
}
117112

118-
public function changeUser($params) {
119-
$user = $params['user'];
113+
public function changeUser(GenericEvent $params) {
114+
$user = $params->getArgument('user');
120115
$this->syncService->updateUser($user);
121116
}
122117

apps/dav/tests/unit/DAV/HookManagerTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@
3131
use OCA\DAV\HookManager;
3232
use OCP\IUser;
3333
use OCP\IUserManager;
34+
use Symfony\Component\EventDispatcher\GenericEvent;
3435
use Test\TestCase;
3536

37+
/**
38+
* Class HookManagerTest
39+
*
40+
* @group DB
41+
* @package OCA\DAV\Tests\unit\DAV
42+
*/
3643
class HookManagerTest extends TestCase {
3744

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

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

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

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

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

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

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

215-
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n);
216-
$hm->preDeleteUser(['uid' => 'newUser']);
217-
$hm->postDeleteUser(['uid' => 'newUser']);
222+
$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n, \OC::$server->getEventDispatcher());
223+
$params = new GenericEvent(null, ['uid' => 'newUser']);
224+
$hm->preDeleteUser($params);
225+
$hm->postDeleteUser($params);
218226
}
219227

220228
}

apps/files_sharing/lib/Helper.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
class Helper {
3939

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

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

4749
/**

apps/files_sharing/lib/Hooks.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727

2828
use OC\Files\Filesystem;
2929
use OCA\FederatedFileSharing\DiscoveryManager;
30+
use Symfony\Component\EventDispatcher\GenericEvent;
3031

3132
class Hooks {
3233

33-
public static function deleteUser($params) {
34+
public static function deleteUser(GenericEvent $params) {
3435
$discoveryManager = new DiscoveryManager(
3536
\OC::$server->getMemCacheFactory(),
3637
\OC::$server->getHTTPClientService()
@@ -41,13 +42,17 @@ public static function deleteUser($params) {
4142
\OC\Files\Filesystem::getLoader(),
4243
\OC::$server->getNotificationManager(),
4344
\OC::$server->getEventDispatcher(),
44-
$params['uid']);
45+
$params->getArgument('uid'));
4546

46-
$manager->removeUserShares($params['uid']);
47+
$manager->removeUserShares($params->getArgument('uid'));
4748
}
4849

4950
public static function unshareChildren($params) {
50-
$path = Filesystem::getView()->getAbsolutePath($params['path']);
51+
if ($params instanceof GenericEvent) {
52+
$path = Filesystem::getView()->getAbsolutePath($params->getArgument('path'));
53+
} else {
54+
$path = Filesystem::getView()->getAbsolutePath($params['path']);
55+
}
5156
$view = new \OC\Files\View('/');
5257

5358
// find share mount points within $path and unmount them

apps/files_sharing/lib/Updater.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,35 @@
2424

2525
namespace OCA\Files_Sharing;
2626

27+
use Symfony\Component\EventDispatcher\GenericEvent;
28+
2729
class Updater {
2830

2931
/**
3032
* @param array $params
3133
*/
32-
static public function renameHook($params) {
33-
self::renameChildren($params['oldpath'], $params['newpath']);
34-
self::moveShareToShare($params['newpath']);
34+
static public function renameHook(GenericEvent $params) {
35+
if ($params->getArgument('processPostEvent') === true) {
36+
/**
37+
* Before proceeding a check to see if event has to be
38+
* processed here, a weird hack, else return.
39+
*/
40+
try {
41+
\OC::$server->getUserFolder()->get($params->getArgument('newpath'));
42+
} catch (\Exception $e) {
43+
try {
44+
/*
45+
* Even if this is not working, then guess this is not,
46+
* the method to call. Hence return.
47+
*/
48+
\OC::$server->getRootFolder()->get($params->getArgument('newpath'));
49+
} catch (\Exception $e) {
50+
return;
51+
}
52+
}
53+
self::renameChildren($params->getArgument('oldpath'), $params->getArgument('newpath'));
54+
self::moveShareToShare($params->getArgument('newpath'));
55+
}
3556
}
3657

3758
/**
@@ -52,7 +73,15 @@ static private function moveShareToShare($path) {
5273
return;
5374
}
5475

55-
$src = $userFolder->get($path);
76+
/**
77+
* What if the path is not relative to $userFolder So as a precaution
78+
* we could also try to get the src from the RootFolder.
79+
*/
80+
try {
81+
$src = $userFolder->get($path);
82+
} catch (\Exception $e) {
83+
$src = \OC::$server->getRootFolder()->get($path);
84+
}
5685

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

apps/files_versions/lib/Hooks.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@
3232

3333
namespace OCA\Files_Versions;
3434

35+
use OC\Files\Filesystem;
36+
use Symfony\Component\EventDispatcher\GenericEvent;
37+
3538
class Hooks {
3639

3740
public static function connectHooks() {
41+
$hooks = new Hooks();
3842
// Listen to write signals
3943
\OCP\Util::connectHook('OC_Filesystem', 'write', 'OCA\Files_Versions\Hooks', 'write_hook');
4044
// Listen to delete and rename signals
4145
\OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Files_Versions\Hooks', 'remove_hook');
4246
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Versions\Hooks', 'pre_remove_hook');
43-
\OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Files_Versions\Hooks', 'rename_hook');
44-
\OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Files_Versions\Hooks', 'copy_hook');
47+
\OC::$server->getEventDispatcher()->addListener('file.afterrename', [$hooks, 'rename_hook']);
48+
\OC::$server->getEventDispatcher()->addListener('file.aftercopy', [$hooks, 'copy_hook']);
4549
\OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
4650
\OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
4751

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

103107
if (\OCP\App::isEnabled('files_versions')) {
104-
$oldpath = $params['oldpath'];
105-
$newpath = $params['newpath'];
108+
$oldpath = $params->getArgument('oldpath');
109+
$oldpath = Filesystem::getView()->getRelativePath($oldpath);
110+
$newpath = $params->getArgument('newpath');
111+
$newpath = Filesystem::getView()->getRelativePath($newpath);
106112
if($oldpath<>'' && $newpath<>'') {
107113
Storage::renameOrCopy($oldpath, $newpath, 'rename');
108114
}
@@ -116,11 +122,13 @@ public static function rename_hook($params) {
116122
* This function is connected to the copy signal of OC_Filesystem and copies the
117123
* the stored versions to the new location
118124
*/
119-
public static function copy_hook($params) {
125+
public static function copy_hook(GenericEvent $params) {
120126

121127
if (\OCP\App::isEnabled('files_versions')) {
122-
$oldpath = $params['oldpath'];
123-
$newpath = $params['newpath'];
128+
$oldpath = $params->getArgument('oldpath');
129+
$oldpath = Filesystem::getView()->getRelativePath($oldpath);
130+
$newpath = $params->getArgument('newpath');
131+
$newpath = Filesystem::getView()->getRelativePath($newpath);
124132
if($oldpath<>'' && $newpath<>'') {
125133
Storage::renameOrCopy($oldpath, $newpath, 'copy');
126134
}

lib/base.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,11 @@ private static function registerEncryptionWrapper() {
759759
private static function registerEncryptionHooks() {
760760
$enabled = self::$server->getEncryptionManager()->isEnabled();
761761
if ($enabled) {
762-
\OCP\Util::connectHook('OCP\Share', 'post_shared', 'OC\Encryption\HookManager', 'postShared');
763-
\OCP\Util::connectHook('OCP\Share', 'post_unshare', 'OC\Encryption\HookManager', 'postUnshared');
764-
\OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OC\Encryption\HookManager', 'postRename');
762+
$eventDispatcher = OC::$server->getEventDispatcher();
763+
$hookManager = new \OC\Encryption\HookManager();
764+
$eventDispatcher->addListener('file.aftercreateshare', [$hookManager, 'postShared']);
765+
$eventDispatcher->addListener('file.afterunshare',[$hookManager, 'postUnshared']);
766+
$eventDispatcher->addListener('file.afterrename', [$hookManager, 'postRename']);
765767
\OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', 'OC\Encryption\HookManager', 'postRestore');
766768
}
767769
}

lib/private/Encryption/Update.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OC\Files\Filesystem;
2727
use \OC\Files\Mount;
2828
use \OC\Files\View;
29+
use Symfony\Component\EventDispatcher\GenericEvent;
2930

3031
/**
3132
* update encrypted files, e.g. because a file was shared
@@ -81,10 +82,10 @@ public function __construct(
8182
*
8283
* @param array $params
8384
*/
84-
public function postShared($params) {
85+
public function postShared(GenericEvent $params) {
8586
if ($this->encryptionManager->isEnabled()) {
86-
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
87-
$path = Filesystem::getPath($params['fileSource']);
87+
if ($params->getArgument('itemType') === 'file' || $params->getArgument('itemType') === 'folder') {
88+
$path = Filesystem::getPath($params->getArgument('fileSource'));
8889
list($owner, $ownerPath) = $this->getOwnerPath($path);
8990
$absPath = '/' . $owner . '/files/' . $ownerPath;
9091
$this->update($absPath);
@@ -97,10 +98,10 @@ public function postShared($params) {
9798
*
9899
* @param array $params
99100
*/
100-
public function postUnshared($params) {
101+
public function postUnshared(GenericEvent $params) {
101102
if ($this->encryptionManager->isEnabled()) {
102-
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
103-
$path = Filesystem::getPath($params['fileSource']);
103+
if ($params->getArgument('itemType') === 'file' || $params->getArgument('itemType') === 'folder') {
104+
$path = Filesystem::getPath($params->getArgument('fileSource'));
104105
list($owner, $ownerPath) = $this->getOwnerPath($path);
105106
$absPath = '/' . $owner . '/files/' . $ownerPath;
106107
$this->update($absPath);

0 commit comments

Comments
 (0)