Completed
Push — master ( 815338...ed050b )
by Joas
22:38
created
apps/workflowengine/lib/Entity/File.php 1 patch
Indentation   +212 added lines, -212 removed lines patch added patch discarded remove patch
@@ -32,216 +32,216 @@
 block discarded – undo
32 32
 use OCP\WorkflowEngine\IRuleMatcher;
33 33
 
34 34
 class File implements IEntity, IDisplayText, IUrl, IIcon, IContextPortation {
35
-	private const EVENT_NAMESPACE = '\OCP\Files::';
36
-	protected ?string $eventName = null;
37
-	protected ?Event $event = null;
38
-	private ?Node $node = null;
39
-	private ?IUser $actingUser = null;
40
-
41
-	public function __construct(
42
-		protected IL10N $l10n,
43
-		protected IURLGenerator $urlGenerator,
44
-		protected IRootFolder $root,
45
-		private IUserSession $userSession,
46
-		private ISystemTagManager $tagManager,
47
-		private IUserManager $userManager,
48
-		private UserMountCache $userMountCache,
49
-		private IMountManager $mountManager,
50
-	) {
51
-	}
52
-
53
-	public function getName(): string {
54
-		return $this->l10n->t('File');
55
-	}
56
-
57
-	public function getIcon(): string {
58
-		return $this->urlGenerator->imagePath('core', 'categories/files.svg');
59
-	}
60
-
61
-	public function getEvents(): array {
62
-		return [
63
-			new GenericEntityEvent($this->l10n->t('File created'), self::EVENT_NAMESPACE . 'postCreate'),
64
-			new GenericEntityEvent($this->l10n->t('File updated'), self::EVENT_NAMESPACE . 'postWrite'),
65
-			new GenericEntityEvent($this->l10n->t('File renamed'), self::EVENT_NAMESPACE . 'postRename'),
66
-			new GenericEntityEvent($this->l10n->t('File deleted'), self::EVENT_NAMESPACE . 'postDelete'),
67
-			new GenericEntityEvent($this->l10n->t('File accessed'), self::EVENT_NAMESPACE . 'postTouch'),
68
-			new GenericEntityEvent($this->l10n->t('File copied'), self::EVENT_NAMESPACE . 'postCopy'),
69
-			new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN),
70
-		];
71
-	}
72
-
73
-	public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void {
74
-		if (!$event instanceof GenericEvent && !$event instanceof MapperEvent) {
75
-			return;
76
-		}
77
-		$this->eventName = $eventName;
78
-		$this->event = $event;
79
-		$this->actingUser = $this->actingUser ?? $this->userSession->getUser();
80
-		try {
81
-			$node = $this->getNode();
82
-			$ruleMatcher->setEntitySubject($this, $node);
83
-			$ruleMatcher->setFileInfo($node->getStorage(), $node->getInternalPath());
84
-		} catch (NotFoundException $e) {
85
-			// pass
86
-		}
87
-	}
88
-
89
-	public function isLegitimatedForUserId(string $userId): bool {
90
-		try {
91
-			$node = $this->getNode();
92
-			if ($node->getOwner()?->getUID() === $userId) {
93
-				return true;
94
-			}
95
-
96
-			if ($this->eventName === self::EVENT_NAMESPACE . 'postDelete') {
97
-				// At postDelete, the file no longer exists. Check for parent folder instead.
98
-				$fileId = $node->getParentId();
99
-			} else {
100
-				$fileId = $node->getId();
101
-			}
102
-
103
-			$mountInfos = $this->userMountCache->getMountsForFileId($fileId, $userId);
104
-			foreach ($mountInfos as $mountInfo) {
105
-				$mount = $this->mountManager->getMountFromMountInfo($mountInfo);
106
-				if ($mount && $mount->getStorage() && !empty($mount->getStorage()->getCache()->get($fileId))) {
107
-					return true;
108
-				}
109
-			}
110
-			return false;
111
-		} catch (NotFoundException $e) {
112
-			return false;
113
-		}
114
-	}
115
-
116
-	/**
117
-	 * @throws NotFoundException
118
-	 */
119
-	protected function getNode(): Node {
120
-		if ($this->node) {
121
-			return $this->node;
122
-		}
123
-		if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) {
124
-			throw new NotFoundException();
125
-		}
126
-		switch ($this->eventName) {
127
-			case self::EVENT_NAMESPACE . 'postCreate':
128
-			case self::EVENT_NAMESPACE . 'postWrite':
129
-			case self::EVENT_NAMESPACE . 'postDelete':
130
-			case self::EVENT_NAMESPACE . 'postTouch':
131
-				return $this->event->getSubject();
132
-			case self::EVENT_NAMESPACE . 'postRename':
133
-			case self::EVENT_NAMESPACE . 'postCopy':
134
-				return $this->event->getSubject()[1];
135
-			case MapperEvent::EVENT_ASSIGN:
136
-				if (!$this->event instanceof MapperEvent || $this->event->getObjectType() !== 'files') {
137
-					throw new NotFoundException();
138
-				}
139
-				$nodes = $this->root->getById((int)$this->event->getObjectId());
140
-				if (is_array($nodes) && isset($nodes[0])) {
141
-					$this->node = $nodes[0];
142
-					return $this->node;
143
-				}
144
-				break;
145
-		}
146
-		throw new NotFoundException();
147
-	}
148
-
149
-	public function getDisplayText(int $verbosity = 0): string {
150
-		try {
151
-			$node = $this->getNode();
152
-		} catch (NotFoundException $e) {
153
-			return '';
154
-		}
155
-
156
-		$options = [
157
-			$this->actingUser ? $this->actingUser->getDisplayName() : $this->l10n->t('Someone'),
158
-			$node->getName()
159
-		];
160
-
161
-		switch ($this->eventName) {
162
-			case self::EVENT_NAMESPACE . 'postCreate':
163
-				return $this->l10n->t('%s created %s', $options);
164
-			case self::EVENT_NAMESPACE . 'postWrite':
165
-				return $this->l10n->t('%s modified %s', $options);
166
-			case self::EVENT_NAMESPACE . 'postDelete':
167
-				return $this->l10n->t('%s deleted %s', $options);
168
-			case self::EVENT_NAMESPACE . 'postTouch':
169
-				return $this->l10n->t('%s accessed %s', $options);
170
-			case self::EVENT_NAMESPACE . 'postRename':
171
-				return $this->l10n->t('%s renamed %s', $options);
172
-			case self::EVENT_NAMESPACE . 'postCopy':
173
-				return $this->l10n->t('%s copied %s', $options);
174
-			case MapperEvent::EVENT_ASSIGN:
175
-				$tagNames = [];
176
-				if ($this->event instanceof MapperEvent) {
177
-					$tagIDs = $this->event->getTags();
178
-					$tagObjects = $this->tagManager->getTagsByIds($tagIDs);
179
-					foreach ($tagObjects as $systemTag) {
180
-						if ($systemTag->isUserVisible()) {
181
-							$tagNames[] = $systemTag->getName();
182
-						}
183
-					}
184
-				}
185
-				$filename = array_pop($options);
186
-				$tagString = implode(', ', $tagNames);
187
-				if ($tagString === '') {
188
-					return '';
189
-				}
190
-				array_push($options, $tagString, $filename);
191
-				return $this->l10n->t('%1$s assigned %2$s to %3$s', $options);
192
-			default:
193
-				return '';
194
-		}
195
-	}
196
-
197
-	public function getUrl(): string {
198
-		try {
199
-			return $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $this->getNode()->getId()]);
200
-		} catch (InvalidPathException|NotFoundException) {
201
-			return '';
202
-		}
203
-	}
204
-
205
-	/**
206
-	 * @inheritDoc
207
-	 */
208
-	public function exportContextIDs(): array {
209
-		$nodeOwner = $this->getNode()->getOwner();
210
-		$actingUserId = null;
211
-		if ($this->actingUser instanceof IUser) {
212
-			$actingUserId = $this->actingUser->getUID();
213
-		} elseif ($this->userSession->getUser() instanceof IUser) {
214
-			$actingUserId = $this->userSession->getUser()->getUID();
215
-		}
216
-		return [
217
-			'eventName' => $this->eventName,
218
-			'nodeId' => $this->getNode()->getId(),
219
-			'nodeOwnerId' => $nodeOwner ? $nodeOwner->getUID() : null,
220
-			'actingUserId' => $actingUserId,
221
-		];
222
-	}
223
-
224
-	/**
225
-	 * @inheritDoc
226
-	 */
227
-	public function importContextIDs(array $contextIDs): void {
228
-		$this->eventName = $contextIDs['eventName'];
229
-		if ($contextIDs['nodeOwnerId'] !== null) {
230
-			$userFolder = $this->root->getUserFolder($contextIDs['nodeOwnerId']);
231
-			$nodes = $userFolder->getById($contextIDs['nodeId']);
232
-		} else {
233
-			$nodes = $this->root->getById($contextIDs['nodeId']);
234
-		}
235
-		$this->node = $nodes[0] ?? null;
236
-		if ($contextIDs['actingUserId']) {
237
-			$this->actingUser = $this->userManager->get($contextIDs['actingUserId']);
238
-		}
239
-	}
240
-
241
-	/**
242
-	 * @inheritDoc
243
-	 */
244
-	public function getIconUrl(): string {
245
-		return $this->getIcon();
246
-	}
35
+    private const EVENT_NAMESPACE = '\OCP\Files::';
36
+    protected ?string $eventName = null;
37
+    protected ?Event $event = null;
38
+    private ?Node $node = null;
39
+    private ?IUser $actingUser = null;
40
+
41
+    public function __construct(
42
+        protected IL10N $l10n,
43
+        protected IURLGenerator $urlGenerator,
44
+        protected IRootFolder $root,
45
+        private IUserSession $userSession,
46
+        private ISystemTagManager $tagManager,
47
+        private IUserManager $userManager,
48
+        private UserMountCache $userMountCache,
49
+        private IMountManager $mountManager,
50
+    ) {
51
+    }
52
+
53
+    public function getName(): string {
54
+        return $this->l10n->t('File');
55
+    }
56
+
57
+    public function getIcon(): string {
58
+        return $this->urlGenerator->imagePath('core', 'categories/files.svg');
59
+    }
60
+
61
+    public function getEvents(): array {
62
+        return [
63
+            new GenericEntityEvent($this->l10n->t('File created'), self::EVENT_NAMESPACE . 'postCreate'),
64
+            new GenericEntityEvent($this->l10n->t('File updated'), self::EVENT_NAMESPACE . 'postWrite'),
65
+            new GenericEntityEvent($this->l10n->t('File renamed'), self::EVENT_NAMESPACE . 'postRename'),
66
+            new GenericEntityEvent($this->l10n->t('File deleted'), self::EVENT_NAMESPACE . 'postDelete'),
67
+            new GenericEntityEvent($this->l10n->t('File accessed'), self::EVENT_NAMESPACE . 'postTouch'),
68
+            new GenericEntityEvent($this->l10n->t('File copied'), self::EVENT_NAMESPACE . 'postCopy'),
69
+            new GenericEntityEvent($this->l10n->t('Tag assigned'), MapperEvent::EVENT_ASSIGN),
70
+        ];
71
+    }
72
+
73
+    public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void {
74
+        if (!$event instanceof GenericEvent && !$event instanceof MapperEvent) {
75
+            return;
76
+        }
77
+        $this->eventName = $eventName;
78
+        $this->event = $event;
79
+        $this->actingUser = $this->actingUser ?? $this->userSession->getUser();
80
+        try {
81
+            $node = $this->getNode();
82
+            $ruleMatcher->setEntitySubject($this, $node);
83
+            $ruleMatcher->setFileInfo($node->getStorage(), $node->getInternalPath());
84
+        } catch (NotFoundException $e) {
85
+            // pass
86
+        }
87
+    }
88
+
89
+    public function isLegitimatedForUserId(string $userId): bool {
90
+        try {
91
+            $node = $this->getNode();
92
+            if ($node->getOwner()?->getUID() === $userId) {
93
+                return true;
94
+            }
95
+
96
+            if ($this->eventName === self::EVENT_NAMESPACE . 'postDelete') {
97
+                // At postDelete, the file no longer exists. Check for parent folder instead.
98
+                $fileId = $node->getParentId();
99
+            } else {
100
+                $fileId = $node->getId();
101
+            }
102
+
103
+            $mountInfos = $this->userMountCache->getMountsForFileId($fileId, $userId);
104
+            foreach ($mountInfos as $mountInfo) {
105
+                $mount = $this->mountManager->getMountFromMountInfo($mountInfo);
106
+                if ($mount && $mount->getStorage() && !empty($mount->getStorage()->getCache()->get($fileId))) {
107
+                    return true;
108
+                }
109
+            }
110
+            return false;
111
+        } catch (NotFoundException $e) {
112
+            return false;
113
+        }
114
+    }
115
+
116
+    /**
117
+     * @throws NotFoundException
118
+     */
119
+    protected function getNode(): Node {
120
+        if ($this->node) {
121
+            return $this->node;
122
+        }
123
+        if (!$this->event instanceof GenericEvent && !$this->event instanceof MapperEvent) {
124
+            throw new NotFoundException();
125
+        }
126
+        switch ($this->eventName) {
127
+            case self::EVENT_NAMESPACE . 'postCreate':
128
+            case self::EVENT_NAMESPACE . 'postWrite':
129
+            case self::EVENT_NAMESPACE . 'postDelete':
130
+            case self::EVENT_NAMESPACE . 'postTouch':
131
+                return $this->event->getSubject();
132
+            case self::EVENT_NAMESPACE . 'postRename':
133
+            case self::EVENT_NAMESPACE . 'postCopy':
134
+                return $this->event->getSubject()[1];
135
+            case MapperEvent::EVENT_ASSIGN:
136
+                if (!$this->event instanceof MapperEvent || $this->event->getObjectType() !== 'files') {
137
+                    throw new NotFoundException();
138
+                }
139
+                $nodes = $this->root->getById((int)$this->event->getObjectId());
140
+                if (is_array($nodes) && isset($nodes[0])) {
141
+                    $this->node = $nodes[0];
142
+                    return $this->node;
143
+                }
144
+                break;
145
+        }
146
+        throw new NotFoundException();
147
+    }
148
+
149
+    public function getDisplayText(int $verbosity = 0): string {
150
+        try {
151
+            $node = $this->getNode();
152
+        } catch (NotFoundException $e) {
153
+            return '';
154
+        }
155
+
156
+        $options = [
157
+            $this->actingUser ? $this->actingUser->getDisplayName() : $this->l10n->t('Someone'),
158
+            $node->getName()
159
+        ];
160
+
161
+        switch ($this->eventName) {
162
+            case self::EVENT_NAMESPACE . 'postCreate':
163
+                return $this->l10n->t('%s created %s', $options);
164
+            case self::EVENT_NAMESPACE . 'postWrite':
165
+                return $this->l10n->t('%s modified %s', $options);
166
+            case self::EVENT_NAMESPACE . 'postDelete':
167
+                return $this->l10n->t('%s deleted %s', $options);
168
+            case self::EVENT_NAMESPACE . 'postTouch':
169
+                return $this->l10n->t('%s accessed %s', $options);
170
+            case self::EVENT_NAMESPACE . 'postRename':
171
+                return $this->l10n->t('%s renamed %s', $options);
172
+            case self::EVENT_NAMESPACE . 'postCopy':
173
+                return $this->l10n->t('%s copied %s', $options);
174
+            case MapperEvent::EVENT_ASSIGN:
175
+                $tagNames = [];
176
+                if ($this->event instanceof MapperEvent) {
177
+                    $tagIDs = $this->event->getTags();
178
+                    $tagObjects = $this->tagManager->getTagsByIds($tagIDs);
179
+                    foreach ($tagObjects as $systemTag) {
180
+                        if ($systemTag->isUserVisible()) {
181
+                            $tagNames[] = $systemTag->getName();
182
+                        }
183
+                    }
184
+                }
185
+                $filename = array_pop($options);
186
+                $tagString = implode(', ', $tagNames);
187
+                if ($tagString === '') {
188
+                    return '';
189
+                }
190
+                array_push($options, $tagString, $filename);
191
+                return $this->l10n->t('%1$s assigned %2$s to %3$s', $options);
192
+            default:
193
+                return '';
194
+        }
195
+    }
196
+
197
+    public function getUrl(): string {
198
+        try {
199
+            return $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $this->getNode()->getId()]);
200
+        } catch (InvalidPathException|NotFoundException) {
201
+            return '';
202
+        }
203
+    }
204
+
205
+    /**
206
+     * @inheritDoc
207
+     */
208
+    public function exportContextIDs(): array {
209
+        $nodeOwner = $this->getNode()->getOwner();
210
+        $actingUserId = null;
211
+        if ($this->actingUser instanceof IUser) {
212
+            $actingUserId = $this->actingUser->getUID();
213
+        } elseif ($this->userSession->getUser() instanceof IUser) {
214
+            $actingUserId = $this->userSession->getUser()->getUID();
215
+        }
216
+        return [
217
+            'eventName' => $this->eventName,
218
+            'nodeId' => $this->getNode()->getId(),
219
+            'nodeOwnerId' => $nodeOwner ? $nodeOwner->getUID() : null,
220
+            'actingUserId' => $actingUserId,
221
+        ];
222
+    }
223
+
224
+    /**
225
+     * @inheritDoc
226
+     */
227
+    public function importContextIDs(array $contextIDs): void {
228
+        $this->eventName = $contextIDs['eventName'];
229
+        if ($contextIDs['nodeOwnerId'] !== null) {
230
+            $userFolder = $this->root->getUserFolder($contextIDs['nodeOwnerId']);
231
+            $nodes = $userFolder->getById($contextIDs['nodeId']);
232
+        } else {
233
+            $nodes = $this->root->getById($contextIDs['nodeId']);
234
+        }
235
+        $this->node = $nodes[0] ?? null;
236
+        if ($contextIDs['actingUserId']) {
237
+            $this->actingUser = $this->userManager->get($contextIDs['actingUserId']);
238
+        }
239
+    }
240
+
241
+    /**
242
+     * @inheritDoc
243
+     */
244
+    public function getIconUrl(): string {
245
+        return $this->getIcon();
246
+    }
247 247
 }
Please login to merge, or discard this patch.