Completed
Push — master ( d63bef...a1d44a )
by Lukas
26:43 queued 10:31
created

LegacyHooks::postShare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2017, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Share20;
24
25
use OCP\Files\File;
26
use OCP\Share\IShare;
27
use Symfony\Component\EventDispatcher\EventDispatcher;
28
use Symfony\Component\EventDispatcher\GenericEvent;
29
30
class LegacyHooks {
31
	/** @var EventDispatcher */
32
	private $eventDispatcher;
33
34
	/**
35
	 * LegacyHooks constructor.
36
	 *
37
	 * @param EventDispatcher $eventDispatcher
38
	 */
39
	public function __construct(EventDispatcher $eventDispatcher) {
40
		$this->eventDispatcher = $eventDispatcher;
41
42
		$this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']);
43
		$this->eventDispatcher->addListener('OCP\Share::postUnshare', [$this, 'postUnshare']);
44
		$this->eventDispatcher->addListener('OCP\Share::postUnshareFromSelf', [$this, 'postUnshareFromSelf']);
45
		$this->eventDispatcher->addListener('OCP\Share::preShare', [$this, 'preShare']);
46
		$this->eventDispatcher->addListener('OCP\Share::postShare', [$this, 'postShare']);
47
	}
48
49
	/**
50
	 * @param GenericEvent $e
51
	 */
52
	public function preUnshare(GenericEvent $e) {
53
		/** @var IShare $share */
54
		$share = $e->getSubject();
55
56
		$formatted = $this->formatHookParams($share);
57
		\OC_Hook::emit('OCP\Share', 'pre_unshare', $formatted);
58
	}
59
60
	/**
61
	 * @param GenericEvent $e
62
	 */
63
	public function postUnshare(GenericEvent $e) {
64
		/** @var IShare $share */
65
		$share = $e->getSubject();
66
67
		$formatted = $this->formatHookParams($share);
68
69
		/** @var IShare[] $deletedShares */
70
		$deletedShares = $e->getArgument('deletedShares');
71
72
		$formattedDeletedShares = array_map(function($share) {
73
			return $this->formatHookParams($share);
74
		}, $deletedShares);
75
76
		$formatted['deletedShares'] = $formattedDeletedShares;
77
78
		\OC_Hook::emit('OCP\Share', 'post_unshare', $formatted);
79
	}
80
81
	/**
82
	 * @param GenericEvent $e
83
	 */
84
	public function postUnshareFromSelf(GenericEvent $e) {
85
		/** @var IShare $share */
86
		$share = $e->getSubject();
87
88
		$formatted = $this->formatHookParams($share);
89
		$formatted['itemTarget'] = $formatted['fileTarget'];
90
		$formatted['unsharedItems'] = [$formatted];
91
92
		\OC_Hook::emit('OCP\Share', 'post_unshareFromSelf', $formatted);
93
	}
94
95
	private function formatHookParams(IShare $share) {
96
		// Prepare hook
97
		$shareType = $share->getShareType();
98
		$sharedWith = '';
99
		if ($shareType === \OCP\Share::SHARE_TYPE_USER ||
100
			$shareType === \OCP\Share::SHARE_TYPE_GROUP ||
101
			$shareType === \OCP\Share::SHARE_TYPE_REMOTE) {
102
			$sharedWith = $share->getSharedWith();
103
		}
104
105
		$hookParams = [
106
			'id' => $share->getId(),
107
			'itemType' => $share->getNodeType(),
108
			'itemSource' => $share->getNodeId(),
109
			'shareType' => $shareType,
110
			'shareWith' => $sharedWith,
111
			'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
112
			'uidOwner' => $share->getSharedBy(),
113
			'fileSource' => $share->getNodeId(),
114
			'fileTarget' => $share->getTarget()
115
		];
116
		return $hookParams;
117
	}
118
119
	public function preShare(GenericEvent $e) {
120
		/** @var IShare $share */
121
		$share = $e->getSubject();
122
123
		// Pre share hook
124
		$run = true;
125
		$error = '';
126
		$preHookData = [
127
			'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
128
			'itemSource' => $share->getNode()->getId(),
129
			'shareType' => $share->getShareType(),
130
			'uidOwner' => $share->getSharedBy(),
131
			'permissions' => $share->getPermissions(),
132
			'fileSource' => $share->getNode()->getId(),
133
			'expiration' => $share->getExpirationDate(),
134
			'token' => $share->getToken(),
135
			'itemTarget' => $share->getTarget(),
136
			'shareWith' => $share->getSharedWith(),
137
			'run' => &$run,
138
			'error' => &$error,
139
		];
140
		\OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData);
141
142
		if ($run === false) {
143
			$e->setArgument('error', $error);
144
			$e->stopPropagation();
145
		}
146
147
		return $e;
148
	}
149
150
	public function postShare(GenericEvent $e) {
151
		/** @var IShare $share */
152
		$share = $e->getSubject();
153
154
		$postHookData = [
155
			'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
156
			'itemSource' => $share->getNode()->getId(),
157
			'shareType' => $share->getShareType(),
158
			'uidOwner' => $share->getSharedBy(),
159
			'permissions' => $share->getPermissions(),
160
			'fileSource' => $share->getNode()->getId(),
161
			'expiration' => $share->getExpirationDate(),
162
			'token' => $share->getToken(),
163
			'id' => $share->getId(),
164
			'shareWith' => $share->getSharedWith(),
165
			'itemTarget' => $share->getTarget(),
166
			'fileTarget' => $share->getTarget(),
167
		];
168
169
		\OC_Hook::emit('OCP\Share', 'post_shared', $postHookData);
170
171
	}
172
}
173