-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathBookmark.php
More file actions
130 lines (119 loc) · 3.26 KB
/
Copy pathBookmark.php
File metadata and controls
130 lines (119 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\Flow;
use OCA\Bookmarks\Db\BookmarkMapper;
use OCA\Bookmarks\Events\BeforeDeleteEvent;
use OCA\Bookmarks\Events\ChangeEvent;
use OCA\Bookmarks\Events\CreateEvent;
use OCA\Bookmarks\Events\MoveEvent;
use OCA\Bookmarks\Events\UpdateEvent;
use OCA\Bookmarks\Service\Authorizer;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\EventDispatcher\Event;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\WorkflowEngine\EntityContext\IUrl;
use OCP\WorkflowEngine\GenericEntityEvent;
use OCP\WorkflowEngine\IEntity;
use OCP\WorkflowEngine\IRuleMatcher;
class Bookmark implements IEntity, IUrl {
public const EVENT_DELETE = BeforeDeleteEvent::class;
public const EVENT_CREATE = CreateEvent::class;
public const EVENT_MOVE = MoveEvent::class;
public const EVENT_UPDATE = UpdateEvent::class;
/**
* @var IL10N
*/
private $l;
/**
* @var IURLGenerator
*/
private $urlGenerator;
/**
* @var BookmarkMapper
*/
private $bookmarkMapper;
/**
* @var \OCA\Bookmarks\Db\Bookmark
*/
private $bookmark;
/**
* @var Authorizer
*/
private $authorizer;
/**
* Bookmark constructor.
*
* @param IL10N $l
* @param IURLGenerator $urlGenerator
* @param BookmarkMapper $bookmarkMapper
* @param Authorizer $authorizer
*/
public function __construct(IL10N $l, IURLGenerator $urlGenerator, BookmarkMapper $bookmarkMapper, Authorizer $authorizer) {
$this->l = $l;
$this->urlGenerator = $urlGenerator;
$this->bookmarkMapper = $bookmarkMapper;
$this->authorizer = $authorizer;
}
/**
* @inheritDoc
*/
public function getName(): string {
return $this->l->t('Bookmark');
}
/**
* @inheritDoc
*/
public function getIcon(): string {
return $this->urlGenerator->imagePath('bookmarks', 'bookmarks-black.svg');
}
/**
* @inheritDoc
*/
public function getEvents(): array {
return [
new GenericEntityEvent($this->l->t('Bookmark deleted'), self::EVENT_DELETE),
new GenericEntityEvent($this->l->t('Bookmark created'), self::EVENT_CREATE),
new GenericEntityEvent($this->l->t('Bookmark moved'), self::EVENT_MOVE),
new GenericEntityEvent($this->l->t('Bookmark updated'), self::EVENT_UPDATE),
];
}
/**
* @inheritDoc
*/
public function prepareRuleMatcher(IRuleMatcher $ruleMatcher, string $eventName, Event $event): void {
if (!$event instanceof ChangeEvent) {
return;
}
if ($event->getType() !== 'bookmark') {
return;
}
try {
$this->bookmark = $this->bookmarkMapper->find($event->getId());
} catch (DoesNotExistException|MultipleObjectsReturnedException $e) {
return;
}
$ruleMatcher->setEntitySubject($this, $this->bookmark);
}
/**
* @inheritDoc
*/
public function isLegitimatedForUserId(string $userId): bool {
if ($this->bookmark->getUserId() === $userId) {
return true;
}
$permissions = $this->authorizer->getUserPermissionsForBookmark($userId, $this->bookmark->getId());
if (Authorizer::hasPermission(Authorizer::PERM_READ, $permissions)) {
return true;
}
return false;
}
public function getUrl(): string {
return $this->bookmark->getUrl();
}
}