Completed
Push — master ( 80b21c...5129a7 )
by Joas
29:44 queued 15s
created
apps/files_sharing/lib/MountProvider.php 2 patches
Indentation   +246 added lines, -246 removed lines patch added patch discarded remove patch
@@ -22,250 +22,250 @@
 block discarded – undo
22 22
 use Psr\Log\LoggerInterface;
23 23
 
24 24
 class MountProvider implements IMountProvider {
25
-	/**
26
-	 * @param IConfig $config
27
-	 * @param IManager $shareManager
28
-	 * @param LoggerInterface $logger
29
-	 */
30
-	public function __construct(
31
-		protected IConfig $config,
32
-		protected IManager $shareManager,
33
-		protected LoggerInterface $logger,
34
-		protected IEventDispatcher $eventDispatcher,
35
-		protected ICacheFactory $cacheFactory,
36
-		protected IMountManager $mountManager,
37
-	) {
38
-	}
39
-
40
-	/**
41
-	 * Get all mountpoints applicable for the user and check for shares where we need to update the etags
42
-	 *
43
-	 * @param IUser $user
44
-	 * @param IStorageFactory $loader
45
-	 * @return IMountPoint[]
46
-	 */
47
-	public function getMountsForUser(IUser $user, IStorageFactory $loader) {
48
-		$shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_USER, null, -1);
49
-		$shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1));
50
-		$shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_CIRCLE, null, -1));
51
-		$shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_ROOM, null, -1));
52
-		$shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_DECK, null, -1));
53
-		$shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_SCIENCEMESH, null, -1));
54
-
55
-
56
-		// filter out excluded shares and group shares that includes self
57
-		$shares = array_filter($shares, function (IShare $share) use ($user) {
58
-			return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
59
-		});
60
-
61
-		$superShares = $this->buildSuperShares($shares, $user);
62
-
63
-		$otherMounts = $this->mountManager->getAll();
64
-		$mounts = [];
65
-		$view = new View('/' . $user->getUID() . '/files');
66
-		$ownerViews = [];
67
-		$sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
68
-		/** @var CappedMemoryCache<bool> $folderExistCache */
69
-		$foldersExistCache = new CappedMemoryCache();
70
-
71
-		$validShareCache = $this->cacheFactory->createLocal('share-valid-mountpoint-max');
72
-		$maxValidatedShare = $validShareCache->get($user->getUID()) ?? 0;
73
-		$newMaxValidatedShare = $maxValidatedShare;
74
-
75
-		foreach ($superShares as $share) {
76
-			try {
77
-				/** @var IShare $parentShare */
78
-				$parentShare = $share[0];
79
-
80
-				if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED &&
81
-					($parentShare->getShareType() === IShare::TYPE_GROUP ||
82
-						$parentShare->getShareType() === IShare::TYPE_USERGROUP ||
83
-						$parentShare->getShareType() === IShare::TYPE_USER)) {
84
-					continue;
85
-				}
86
-
87
-				$owner = $parentShare->getShareOwner();
88
-				if (!isset($ownerViews[$owner])) {
89
-					$ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
90
-				}
91
-				$shareId = (int)$parentShare->getId();
92
-				$mount = new SharedMount(
93
-					'\OCA\Files_Sharing\SharedStorage',
94
-					array_merge($mounts, $otherMounts),
95
-					[
96
-						'user' => $user->getUID(),
97
-						// parent share
98
-						'superShare' => $parentShare,
99
-						// children/component of the superShare
100
-						'groupedShares' => $share[1],
101
-						'ownerView' => $ownerViews[$owner],
102
-						'sharingDisabledForUser' => $sharingDisabledForUser
103
-					],
104
-					$loader,
105
-					$view,
106
-					$foldersExistCache,
107
-					$this->eventDispatcher,
108
-					$user,
109
-					($shareId <= $maxValidatedShare),
110
-				);
111
-
112
-				$newMaxValidatedShare = max($shareId, $newMaxValidatedShare);
113
-
114
-				$event = new ShareMountedEvent($mount);
115
-				$this->eventDispatcher->dispatchTyped($event);
116
-
117
-				$mounts[$mount->getMountPoint()] = $mount;
118
-				foreach ($event->getAdditionalMounts() as $additionalMount) {
119
-					$mounts[$additionalMount->getMountPoint()] = $additionalMount;
120
-				}
121
-			} catch (\Exception $e) {
122
-				$this->logger->error(
123
-					'Error while trying to create shared mount',
124
-					[
125
-						'app' => 'files_sharing',
126
-						'exception' => $e,
127
-					],
128
-				);
129
-			}
130
-		}
131
-
132
-		$validShareCache->set($user->getUID(), $newMaxValidatedShare, 24 * 60 * 60);
133
-
134
-		// array_filter removes the null values from the array
135
-		return array_values(array_filter($mounts));
136
-	}
137
-
138
-	/**
139
-	 * Groups shares by path (nodeId) and target path
140
-	 *
141
-	 * @param IShare[] $shares
142
-	 * @return IShare[][] array of grouped shares, each element in the
143
-	 *                    array is a group which itself is an array of shares
144
-	 */
145
-	private function groupShares(array $shares) {
146
-		$tmp = [];
147
-
148
-		foreach ($shares as $share) {
149
-			if (!isset($tmp[$share->getNodeId()])) {
150
-				$tmp[$share->getNodeId()] = [];
151
-			}
152
-			$tmp[$share->getNodeId()][] = $share;
153
-		}
154
-
155
-		$result = [];
156
-		// sort by stime, the super share will be based on the least recent share
157
-		foreach ($tmp as &$tmp2) {
158
-			@usort($tmp2, function ($a, $b) {
159
-				$aTime = $a->getShareTime()->getTimestamp();
160
-				$bTime = $b->getShareTime()->getTimestamp();
161
-				if ($aTime === $bTime) {
162
-					return $a->getId() < $b->getId() ? -1 : 1;
163
-				}
164
-				return $aTime < $bTime ? -1 : 1;
165
-			});
166
-			$result[] = $tmp2;
167
-		}
168
-
169
-		return array_values($result);
170
-	}
171
-
172
-	/**
173
-	 * Build super shares (virtual share) by grouping them by node id and target,
174
-	 * then for each group compute the super share and return it along with the matching
175
-	 * grouped shares. The most permissive permissions are used based on the permissions
176
-	 * of all shares within the group.
177
-	 *
178
-	 * @param IShare[] $allShares
179
-	 * @param IUser $user user
180
-	 * @return array Tuple of [superShare, groupedShares]
181
-	 */
182
-	private function buildSuperShares(array $allShares, IUser $user) {
183
-		$result = [];
184
-
185
-		$groupedShares = $this->groupShares($allShares);
186
-
187
-		/** @var IShare[] $shares */
188
-		foreach ($groupedShares as $shares) {
189
-			if (count($shares) === 0) {
190
-				continue;
191
-			}
192
-
193
-			$superShare = $this->shareManager->newShare();
194
-
195
-			// compute super share based on first entry of the group
196
-			$superShare->setId($shares[0]->getId())
197
-				->setShareOwner($shares[0]->getShareOwner())
198
-				->setNodeId($shares[0]->getNodeId())
199
-				->setShareType($shares[0]->getShareType())
200
-				->setTarget($shares[0]->getTarget());
201
-
202
-			// Gather notes from all the shares.
203
-			// Since these are readly available here, storing them
204
-			// enables the DAV FilesPlugin to avoid executing many
205
-			// DB queries to retrieve the same information.
206
-			$allNotes = implode("\n", array_map(function ($sh) {
207
-				return $sh->getNote();
208
-			}, $shares));
209
-			$superShare->setNote($allNotes);
210
-
211
-			// use most permissive permissions
212
-			// this covers the case where there are multiple shares for the same
213
-			// file e.g. from different groups and different permissions
214
-			$superPermissions = 0;
215
-			$superAttributes = $this->shareManager->newShare()->newAttributes();
216
-			$status = IShare::STATUS_PENDING;
217
-			foreach ($shares as $share) {
218
-				$superPermissions |= $share->getPermissions();
219
-				$status = max($status, $share->getStatus());
220
-				// update permissions
221
-				$superPermissions |= $share->getPermissions();
222
-
223
-				// update share permission attributes
224
-				$attributes = $share->getAttributes();
225
-				if ($attributes !== null) {
226
-					foreach ($attributes->toArray() as $attribute) {
227
-						if ($superAttributes->getAttribute($attribute['scope'], $attribute['key']) === true) {
228
-							// if super share attribute is already enabled, it is most permissive
229
-							continue;
230
-						}
231
-						// update supershare attributes with subshare attribute
232
-						$superAttributes->setAttribute($attribute['scope'], $attribute['key'], $attribute['value']);
233
-					}
234
-				}
235
-
236
-				// adjust target, for database consistency if needed
237
-				if ($share->getTarget() !== $superShare->getTarget()) {
238
-					$share->setTarget($superShare->getTarget());
239
-					try {
240
-						$this->shareManager->moveShare($share, $user->getUID());
241
-					} catch (\InvalidArgumentException $e) {
242
-						// ignore as it is not important and we don't want to
243
-						// block FS setup
244
-
245
-						// the subsequent code anyway only uses the target of the
246
-						// super share
247
-
248
-						// such issue can usually happen when dealing with
249
-						// null groups which usually appear with group backend
250
-						// caching inconsistencies
251
-						$this->logger->debug(
252
-							'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
253
-							['app' => 'files_sharing']
254
-						);
255
-					}
256
-				}
257
-				if (!is_null($share->getNodeCacheEntry())) {
258
-					$superShare->setNodeCacheEntry($share->getNodeCacheEntry());
259
-				}
260
-			}
261
-
262
-			$superShare->setPermissions($superPermissions);
263
-			$superShare->setStatus($status);
264
-			$superShare->setAttributes($superAttributes);
265
-
266
-			$result[] = [$superShare, $shares];
267
-		}
268
-
269
-		return $result;
270
-	}
25
+    /**
26
+     * @param IConfig $config
27
+     * @param IManager $shareManager
28
+     * @param LoggerInterface $logger
29
+     */
30
+    public function __construct(
31
+        protected IConfig $config,
32
+        protected IManager $shareManager,
33
+        protected LoggerInterface $logger,
34
+        protected IEventDispatcher $eventDispatcher,
35
+        protected ICacheFactory $cacheFactory,
36
+        protected IMountManager $mountManager,
37
+    ) {
38
+    }
39
+
40
+    /**
41
+     * Get all mountpoints applicable for the user and check for shares where we need to update the etags
42
+     *
43
+     * @param IUser $user
44
+     * @param IStorageFactory $loader
45
+     * @return IMountPoint[]
46
+     */
47
+    public function getMountsForUser(IUser $user, IStorageFactory $loader) {
48
+        $shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_USER, null, -1);
49
+        $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, -1));
50
+        $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_CIRCLE, null, -1));
51
+        $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_ROOM, null, -1));
52
+        $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_DECK, null, -1));
53
+        $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_SCIENCEMESH, null, -1));
54
+
55
+
56
+        // filter out excluded shares and group shares that includes self
57
+        $shares = array_filter($shares, function (IShare $share) use ($user) {
58
+            return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
59
+        });
60
+
61
+        $superShares = $this->buildSuperShares($shares, $user);
62
+
63
+        $otherMounts = $this->mountManager->getAll();
64
+        $mounts = [];
65
+        $view = new View('/' . $user->getUID() . '/files');
66
+        $ownerViews = [];
67
+        $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
68
+        /** @var CappedMemoryCache<bool> $folderExistCache */
69
+        $foldersExistCache = new CappedMemoryCache();
70
+
71
+        $validShareCache = $this->cacheFactory->createLocal('share-valid-mountpoint-max');
72
+        $maxValidatedShare = $validShareCache->get($user->getUID()) ?? 0;
73
+        $newMaxValidatedShare = $maxValidatedShare;
74
+
75
+        foreach ($superShares as $share) {
76
+            try {
77
+                /** @var IShare $parentShare */
78
+                $parentShare = $share[0];
79
+
80
+                if ($parentShare->getStatus() !== IShare::STATUS_ACCEPTED &&
81
+                    ($parentShare->getShareType() === IShare::TYPE_GROUP ||
82
+                        $parentShare->getShareType() === IShare::TYPE_USERGROUP ||
83
+                        $parentShare->getShareType() === IShare::TYPE_USER)) {
84
+                    continue;
85
+                }
86
+
87
+                $owner = $parentShare->getShareOwner();
88
+                if (!isset($ownerViews[$owner])) {
89
+                    $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
90
+                }
91
+                $shareId = (int)$parentShare->getId();
92
+                $mount = new SharedMount(
93
+                    '\OCA\Files_Sharing\SharedStorage',
94
+                    array_merge($mounts, $otherMounts),
95
+                    [
96
+                        'user' => $user->getUID(),
97
+                        // parent share
98
+                        'superShare' => $parentShare,
99
+                        // children/component of the superShare
100
+                        'groupedShares' => $share[1],
101
+                        'ownerView' => $ownerViews[$owner],
102
+                        'sharingDisabledForUser' => $sharingDisabledForUser
103
+                    ],
104
+                    $loader,
105
+                    $view,
106
+                    $foldersExistCache,
107
+                    $this->eventDispatcher,
108
+                    $user,
109
+                    ($shareId <= $maxValidatedShare),
110
+                );
111
+
112
+                $newMaxValidatedShare = max($shareId, $newMaxValidatedShare);
113
+
114
+                $event = new ShareMountedEvent($mount);
115
+                $this->eventDispatcher->dispatchTyped($event);
116
+
117
+                $mounts[$mount->getMountPoint()] = $mount;
118
+                foreach ($event->getAdditionalMounts() as $additionalMount) {
119
+                    $mounts[$additionalMount->getMountPoint()] = $additionalMount;
120
+                }
121
+            } catch (\Exception $e) {
122
+                $this->logger->error(
123
+                    'Error while trying to create shared mount',
124
+                    [
125
+                        'app' => 'files_sharing',
126
+                        'exception' => $e,
127
+                    ],
128
+                );
129
+            }
130
+        }
131
+
132
+        $validShareCache->set($user->getUID(), $newMaxValidatedShare, 24 * 60 * 60);
133
+
134
+        // array_filter removes the null values from the array
135
+        return array_values(array_filter($mounts));
136
+    }
137
+
138
+    /**
139
+     * Groups shares by path (nodeId) and target path
140
+     *
141
+     * @param IShare[] $shares
142
+     * @return IShare[][] array of grouped shares, each element in the
143
+     *                    array is a group which itself is an array of shares
144
+     */
145
+    private function groupShares(array $shares) {
146
+        $tmp = [];
147
+
148
+        foreach ($shares as $share) {
149
+            if (!isset($tmp[$share->getNodeId()])) {
150
+                $tmp[$share->getNodeId()] = [];
151
+            }
152
+            $tmp[$share->getNodeId()][] = $share;
153
+        }
154
+
155
+        $result = [];
156
+        // sort by stime, the super share will be based on the least recent share
157
+        foreach ($tmp as &$tmp2) {
158
+            @usort($tmp2, function ($a, $b) {
159
+                $aTime = $a->getShareTime()->getTimestamp();
160
+                $bTime = $b->getShareTime()->getTimestamp();
161
+                if ($aTime === $bTime) {
162
+                    return $a->getId() < $b->getId() ? -1 : 1;
163
+                }
164
+                return $aTime < $bTime ? -1 : 1;
165
+            });
166
+            $result[] = $tmp2;
167
+        }
168
+
169
+        return array_values($result);
170
+    }
171
+
172
+    /**
173
+     * Build super shares (virtual share) by grouping them by node id and target,
174
+     * then for each group compute the super share and return it along with the matching
175
+     * grouped shares. The most permissive permissions are used based on the permissions
176
+     * of all shares within the group.
177
+     *
178
+     * @param IShare[] $allShares
179
+     * @param IUser $user user
180
+     * @return array Tuple of [superShare, groupedShares]
181
+     */
182
+    private function buildSuperShares(array $allShares, IUser $user) {
183
+        $result = [];
184
+
185
+        $groupedShares = $this->groupShares($allShares);
186
+
187
+        /** @var IShare[] $shares */
188
+        foreach ($groupedShares as $shares) {
189
+            if (count($shares) === 0) {
190
+                continue;
191
+            }
192
+
193
+            $superShare = $this->shareManager->newShare();
194
+
195
+            // compute super share based on first entry of the group
196
+            $superShare->setId($shares[0]->getId())
197
+                ->setShareOwner($shares[0]->getShareOwner())
198
+                ->setNodeId($shares[0]->getNodeId())
199
+                ->setShareType($shares[0]->getShareType())
200
+                ->setTarget($shares[0]->getTarget());
201
+
202
+            // Gather notes from all the shares.
203
+            // Since these are readly available here, storing them
204
+            // enables the DAV FilesPlugin to avoid executing many
205
+            // DB queries to retrieve the same information.
206
+            $allNotes = implode("\n", array_map(function ($sh) {
207
+                return $sh->getNote();
208
+            }, $shares));
209
+            $superShare->setNote($allNotes);
210
+
211
+            // use most permissive permissions
212
+            // this covers the case where there are multiple shares for the same
213
+            // file e.g. from different groups and different permissions
214
+            $superPermissions = 0;
215
+            $superAttributes = $this->shareManager->newShare()->newAttributes();
216
+            $status = IShare::STATUS_PENDING;
217
+            foreach ($shares as $share) {
218
+                $superPermissions |= $share->getPermissions();
219
+                $status = max($status, $share->getStatus());
220
+                // update permissions
221
+                $superPermissions |= $share->getPermissions();
222
+
223
+                // update share permission attributes
224
+                $attributes = $share->getAttributes();
225
+                if ($attributes !== null) {
226
+                    foreach ($attributes->toArray() as $attribute) {
227
+                        if ($superAttributes->getAttribute($attribute['scope'], $attribute['key']) === true) {
228
+                            // if super share attribute is already enabled, it is most permissive
229
+                            continue;
230
+                        }
231
+                        // update supershare attributes with subshare attribute
232
+                        $superAttributes->setAttribute($attribute['scope'], $attribute['key'], $attribute['value']);
233
+                    }
234
+                }
235
+
236
+                // adjust target, for database consistency if needed
237
+                if ($share->getTarget() !== $superShare->getTarget()) {
238
+                    $share->setTarget($superShare->getTarget());
239
+                    try {
240
+                        $this->shareManager->moveShare($share, $user->getUID());
241
+                    } catch (\InvalidArgumentException $e) {
242
+                        // ignore as it is not important and we don't want to
243
+                        // block FS setup
244
+
245
+                        // the subsequent code anyway only uses the target of the
246
+                        // super share
247
+
248
+                        // such issue can usually happen when dealing with
249
+                        // null groups which usually appear with group backend
250
+                        // caching inconsistencies
251
+                        $this->logger->debug(
252
+                            'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
253
+                            ['app' => 'files_sharing']
254
+                        );
255
+                    }
256
+                }
257
+                if (!is_null($share->getNodeCacheEntry())) {
258
+                    $superShare->setNodeCacheEntry($share->getNodeCacheEntry());
259
+                }
260
+            }
261
+
262
+            $superShare->setPermissions($superPermissions);
263
+            $superShare->setStatus($status);
264
+            $superShare->setAttributes($superAttributes);
265
+
266
+            $result[] = [$superShare, $shares];
267
+        }
268
+
269
+        return $result;
270
+    }
271 271
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
 
55 55
 
56 56
 		// filter out excluded shares and group shares that includes self
57
-		$shares = array_filter($shares, function (IShare $share) use ($user) {
57
+		$shares = array_filter($shares, function(IShare $share) use ($user) {
58 58
 			return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID();
59 59
 		});
60 60
 
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 
63 63
 		$otherMounts = $this->mountManager->getAll();
64 64
 		$mounts = [];
65
-		$view = new View('/' . $user->getUID() . '/files');
65
+		$view = new View('/'.$user->getUID().'/files');
66 66
 		$ownerViews = [];
67 67
 		$sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID());
68 68
 		/** @var CappedMemoryCache<bool> $folderExistCache */
@@ -86,9 +86,9 @@  discard block
 block discarded – undo
86 86
 
87 87
 				$owner = $parentShare->getShareOwner();
88 88
 				if (!isset($ownerViews[$owner])) {
89
-					$ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files');
89
+					$ownerViews[$owner] = new View('/'.$parentShare->getShareOwner().'/files');
90 90
 				}
91
-				$shareId = (int)$parentShare->getId();
91
+				$shareId = (int) $parentShare->getId();
92 92
 				$mount = new SharedMount(
93 93
 					'\OCA\Files_Sharing\SharedStorage',
94 94
 					array_merge($mounts, $otherMounts),
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
 		$result = [];
156 156
 		// sort by stime, the super share will be based on the least recent share
157 157
 		foreach ($tmp as &$tmp2) {
158
-			@usort($tmp2, function ($a, $b) {
158
+			@usort($tmp2, function($a, $b) {
159 159
 				$aTime = $a->getShareTime()->getTimestamp();
160 160
 				$bTime = $b->getShareTime()->getTimestamp();
161 161
 				if ($aTime === $bTime) {
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 			// Since these are readly available here, storing them
204 204
 			// enables the DAV FilesPlugin to avoid executing many
205 205
 			// DB queries to retrieve the same information.
206
-			$allNotes = implode("\n", array_map(function ($sh) {
206
+			$allNotes = implode("\n", array_map(function($sh) {
207 207
 				return $sh->getNote();
208 208
 			}, $shares));
209 209
 			$superShare->setNote($allNotes);
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 						// null groups which usually appear with group backend
250 250
 						// caching inconsistencies
251 251
 						$this->logger->debug(
252
-							'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(),
252
+							'Could not adjust share target for share '.$share->getId().' to make it consistent: '.$e->getMessage(),
253 253
 							['app' => 'files_sharing']
254 254
 						);
255 255
 					}
Please login to merge, or discard this patch.