@@ -94,7 +94,7 @@ |
||
94 | 94 | $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes'); |
95 | 95 | |
96 | 96 | // add background job for each user |
97 | - $this->userManager->callForSeenUsers(function (IUser $user) { |
|
97 | + $this->userManager->callForSeenUsers(function(IUser $user) { |
|
98 | 98 | $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [ |
99 | 99 | 'userId' => $user->getUID(), |
100 | 100 | ]); |
@@ -37,82 +37,82 @@ |
||
37 | 37 | |
38 | 38 | class BirthdayCalendarController extends Controller { |
39 | 39 | |
40 | - /** |
|
41 | - * @var IDBConnection |
|
42 | - */ |
|
43 | - protected $db; |
|
40 | + /** |
|
41 | + * @var IDBConnection |
|
42 | + */ |
|
43 | + protected $db; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @var IConfig |
|
47 | - */ |
|
48 | - protected $config; |
|
45 | + /** |
|
46 | + * @var IConfig |
|
47 | + */ |
|
48 | + protected $config; |
|
49 | 49 | |
50 | - /** |
|
51 | - * @var IUserManager |
|
52 | - */ |
|
53 | - protected $userManager; |
|
50 | + /** |
|
51 | + * @var IUserManager |
|
52 | + */ |
|
53 | + protected $userManager; |
|
54 | 54 | |
55 | - /** |
|
56 | - * @var CalDavBackend |
|
57 | - */ |
|
58 | - protected $caldavBackend; |
|
55 | + /** |
|
56 | + * @var CalDavBackend |
|
57 | + */ |
|
58 | + protected $caldavBackend; |
|
59 | 59 | |
60 | - /** |
|
61 | - * @var IJobList |
|
62 | - */ |
|
63 | - protected $jobList; |
|
60 | + /** |
|
61 | + * @var IJobList |
|
62 | + */ |
|
63 | + protected $jobList; |
|
64 | 64 | |
65 | - /** |
|
66 | - * BirthdayCalendar constructor. |
|
67 | - * |
|
68 | - * @param string $appName |
|
69 | - * @param IRequest $request |
|
70 | - * @param IDBConnection $db |
|
71 | - * @param IConfig $config |
|
72 | - * @param IJobList $jobList |
|
73 | - * @param IUserManager $userManager |
|
74 | - * @param CalDavBackend $calDavBackend |
|
75 | - */ |
|
76 | - public function __construct($appName, IRequest $request, |
|
77 | - IDBConnection $db, IConfig $config, |
|
78 | - IJobList $jobList, |
|
79 | - IUserManager $userManager, |
|
80 | - CalDavBackend $calDavBackend) { |
|
81 | - parent::__construct($appName, $request); |
|
82 | - $this->db = $db; |
|
83 | - $this->config = $config; |
|
84 | - $this->userManager = $userManager; |
|
85 | - $this->jobList = $jobList; |
|
86 | - $this->caldavBackend = $calDavBackend; |
|
87 | - } |
|
65 | + /** |
|
66 | + * BirthdayCalendar constructor. |
|
67 | + * |
|
68 | + * @param string $appName |
|
69 | + * @param IRequest $request |
|
70 | + * @param IDBConnection $db |
|
71 | + * @param IConfig $config |
|
72 | + * @param IJobList $jobList |
|
73 | + * @param IUserManager $userManager |
|
74 | + * @param CalDavBackend $calDavBackend |
|
75 | + */ |
|
76 | + public function __construct($appName, IRequest $request, |
|
77 | + IDBConnection $db, IConfig $config, |
|
78 | + IJobList $jobList, |
|
79 | + IUserManager $userManager, |
|
80 | + CalDavBackend $calDavBackend) { |
|
81 | + parent::__construct($appName, $request); |
|
82 | + $this->db = $db; |
|
83 | + $this->config = $config; |
|
84 | + $this->userManager = $userManager; |
|
85 | + $this->jobList = $jobList; |
|
86 | + $this->caldavBackend = $calDavBackend; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * @return Response |
|
91 | - * @AuthorizedAdminSetting(settings=OCA\DAV\Settings\CalDAVSettings) |
|
92 | - */ |
|
93 | - public function enable() { |
|
94 | - $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes'); |
|
89 | + /** |
|
90 | + * @return Response |
|
91 | + * @AuthorizedAdminSetting(settings=OCA\DAV\Settings\CalDAVSettings) |
|
92 | + */ |
|
93 | + public function enable() { |
|
94 | + $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes'); |
|
95 | 95 | |
96 | - // add background job for each user |
|
97 | - $this->userManager->callForSeenUsers(function (IUser $user) { |
|
98 | - $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [ |
|
99 | - 'userId' => $user->getUID(), |
|
100 | - ]); |
|
101 | - }); |
|
96 | + // add background job for each user |
|
97 | + $this->userManager->callForSeenUsers(function (IUser $user) { |
|
98 | + $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [ |
|
99 | + 'userId' => $user->getUID(), |
|
100 | + ]); |
|
101 | + }); |
|
102 | 102 | |
103 | - return new JSONResponse([]); |
|
104 | - } |
|
103 | + return new JSONResponse([]); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @return Response |
|
108 | - * @AuthorizedAdminSetting(settings=OCA\DAV\Settings\CalDAVSettings) |
|
109 | - */ |
|
110 | - public function disable() { |
|
111 | - $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no'); |
|
106 | + /** |
|
107 | + * @return Response |
|
108 | + * @AuthorizedAdminSetting(settings=OCA\DAV\Settings\CalDAVSettings) |
|
109 | + */ |
|
110 | + public function disable() { |
|
111 | + $this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no'); |
|
112 | 112 | |
113 | - $this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class); |
|
114 | - $this->caldavBackend->deleteAllBirthdayCalendars(); |
|
113 | + $this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class); |
|
114 | + $this->caldavBackend->deleteAllBirthdayCalendars(); |
|
115 | 115 | |
116 | - return new JSONResponse([]); |
|
117 | - } |
|
116 | + return new JSONResponse([]); |
|
117 | + } |
|
118 | 118 | } |
@@ -113,12 +113,12 @@ discard block |
||
113 | 113 | $this->server = $server; |
114 | 114 | |
115 | 115 | $this->server->on('method:POST', [$this, 'httpPost']); |
116 | - $this->server->on('propFind', [$this, 'propFind']); |
|
116 | + $this->server->on('propFind', [$this, 'propFind']); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | public function propFind(PropFind $propFind, INode $node) { |
120 | 120 | if ($node instanceof Calendar) { |
121 | - $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) { |
|
121 | + $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function() use ($node) { |
|
122 | 122 | if ($node->getPublishStatus()) { |
123 | 123 | // We return the publish-url only if the calendar is published. |
124 | 124 | $token = $node->getPublishStatus(); |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | } |
129 | 129 | }); |
130 | 130 | |
131 | - $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function () use ($node) { |
|
131 | + $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function() use ($node) { |
|
132 | 132 | $canShare = (!$node->isSubscription() && $node->canWrite()); |
133 | 133 | $canPublish = (!$node->isSubscription() && $node->canWrite()); |
134 | 134 |
@@ -41,216 +41,216 @@ |
||
41 | 41 | use Sabre\HTTP\ResponseInterface; |
42 | 42 | |
43 | 43 | class PublishPlugin extends ServerPlugin { |
44 | - public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
45 | - |
|
46 | - /** |
|
47 | - * Reference to SabreDAV server object. |
|
48 | - * |
|
49 | - * @var \Sabre\DAV\Server |
|
50 | - */ |
|
51 | - protected $server; |
|
52 | - |
|
53 | - /** |
|
54 | - * Config instance to get instance secret. |
|
55 | - * |
|
56 | - * @var IConfig |
|
57 | - */ |
|
58 | - protected $config; |
|
59 | - |
|
60 | - /** |
|
61 | - * URL Generator for absolute URLs. |
|
62 | - * |
|
63 | - * @var IURLGenerator |
|
64 | - */ |
|
65 | - protected $urlGenerator; |
|
66 | - |
|
67 | - /** |
|
68 | - * PublishPlugin constructor. |
|
69 | - * |
|
70 | - * @param IConfig $config |
|
71 | - * @param IURLGenerator $urlGenerator |
|
72 | - */ |
|
73 | - public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
|
74 | - $this->config = $config; |
|
75 | - $this->urlGenerator = $urlGenerator; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * This method should return a list of server-features. |
|
80 | - * |
|
81 | - * This is for example 'versioning' and is added to the DAV: header |
|
82 | - * in an OPTIONS response. |
|
83 | - * |
|
84 | - * @return string[] |
|
85 | - */ |
|
86 | - public function getFeatures() { |
|
87 | - // May have to be changed to be detected |
|
88 | - return ['oc-calendar-publishing', 'calendarserver-sharing']; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Returns a plugin name. |
|
93 | - * |
|
94 | - * Using this name other plugins will be able to access other plugins |
|
95 | - * using Sabre\DAV\Server::getPlugin |
|
96 | - * |
|
97 | - * @return string |
|
98 | - */ |
|
99 | - public function getPluginName() { |
|
100 | - return 'oc-calendar-publishing'; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * This initializes the plugin. |
|
105 | - * |
|
106 | - * This function is called by Sabre\DAV\Server, after |
|
107 | - * addPlugin is called. |
|
108 | - * |
|
109 | - * This method should set up the required event subscriptions. |
|
110 | - * |
|
111 | - * @param Server $server |
|
112 | - */ |
|
113 | - public function initialize(Server $server) { |
|
114 | - $this->server = $server; |
|
115 | - |
|
116 | - $this->server->on('method:POST', [$this, 'httpPost']); |
|
117 | - $this->server->on('propFind', [$this, 'propFind']); |
|
118 | - } |
|
119 | - |
|
120 | - public function propFind(PropFind $propFind, INode $node) { |
|
121 | - if ($node instanceof Calendar) { |
|
122 | - $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) { |
|
123 | - if ($node->getPublishStatus()) { |
|
124 | - // We return the publish-url only if the calendar is published. |
|
125 | - $token = $node->getPublishStatus(); |
|
126 | - $publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri().'public-calendars/').$token; |
|
127 | - |
|
128 | - return new Publisher($publishUrl, true); |
|
129 | - } |
|
130 | - }); |
|
131 | - |
|
132 | - $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function () use ($node) { |
|
133 | - $canShare = (!$node->isSubscription() && $node->canWrite()); |
|
134 | - $canPublish = (!$node->isSubscription() && $node->canWrite()); |
|
135 | - |
|
136 | - if ($this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes') { |
|
137 | - $canShare = $canShare && ($node->getOwner() === $node->getPrincipalURI()); |
|
138 | - $canPublish = $canPublish && ($node->getOwner() === $node->getPrincipalURI()); |
|
139 | - } |
|
140 | - |
|
141 | - return new AllowedSharingModes($canShare, $canPublish); |
|
142 | - }); |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * We intercept this to handle POST requests on calendars. |
|
148 | - * |
|
149 | - * @param RequestInterface $request |
|
150 | - * @param ResponseInterface $response |
|
151 | - * |
|
152 | - * @return void|bool |
|
153 | - */ |
|
154 | - public function httpPost(RequestInterface $request, ResponseInterface $response) { |
|
155 | - $path = $request->getPath(); |
|
156 | - |
|
157 | - // Only handling xml |
|
158 | - $contentType = $request->getHeader('Content-Type'); |
|
159 | - if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { |
|
160 | - return; |
|
161 | - } |
|
162 | - |
|
163 | - // Making sure the node exists |
|
164 | - try { |
|
165 | - $node = $this->server->tree->getNodeForPath($path); |
|
166 | - } catch (NotFound $e) { |
|
167 | - return; |
|
168 | - } |
|
169 | - |
|
170 | - $requestBody = $request->getBodyAsString(); |
|
171 | - |
|
172 | - // If this request handler could not deal with this POST request, it |
|
173 | - // will return 'null' and other plugins get a chance to handle the |
|
174 | - // request. |
|
175 | - // |
|
176 | - // However, we already requested the full body. This is a problem, |
|
177 | - // because a body can only be read once. This is why we preemptively |
|
178 | - // re-populated the request body with the existing data. |
|
179 | - $request->setBody($requestBody); |
|
180 | - |
|
181 | - $this->server->xml->parse($requestBody, $request->getUrl(), $documentType); |
|
182 | - |
|
183 | - switch ($documentType) { |
|
184 | - |
|
185 | - case '{'.self::NS_CALENDARSERVER.'}publish-calendar': |
|
186 | - |
|
187 | - // We can only deal with IShareableCalendar objects |
|
188 | - if (!$node instanceof Calendar) { |
|
189 | - return; |
|
190 | - } |
|
191 | - $this->server->transactionType = 'post-publish-calendar'; |
|
192 | - |
|
193 | - // Getting ACL info |
|
194 | - $acl = $this->server->getPlugin('acl'); |
|
195 | - |
|
196 | - // If there's no ACL support, we allow everything |
|
197 | - if ($acl) { |
|
198 | - /** @var \Sabre\DAVACL\Plugin $acl */ |
|
199 | - $acl->checkPrivileges($path, '{DAV:}write'); |
|
200 | - |
|
201 | - $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes'; |
|
202 | - $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner(); |
|
203 | - if ($limitSharingToOwner && !$isOwner) { |
|
204 | - return; |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - $node->setPublishStatus(true); |
|
209 | - |
|
210 | - // iCloud sends back the 202, so we will too. |
|
211 | - $response->setStatus(202); |
|
212 | - |
|
213 | - // Adding this because sending a response body may cause issues, |
|
214 | - // and I wanted some type of indicator the response was handled. |
|
215 | - $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
216 | - |
|
217 | - // Breaking the event chain |
|
218 | - return false; |
|
219 | - |
|
220 | - case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar': |
|
221 | - |
|
222 | - // We can only deal with IShareableCalendar objects |
|
223 | - if (!$node instanceof Calendar) { |
|
224 | - return; |
|
225 | - } |
|
226 | - $this->server->transactionType = 'post-unpublish-calendar'; |
|
227 | - |
|
228 | - // Getting ACL info |
|
229 | - $acl = $this->server->getPlugin('acl'); |
|
230 | - |
|
231 | - // If there's no ACL support, we allow everything |
|
232 | - if ($acl) { |
|
233 | - /** @var \Sabre\DAVACL\Plugin $acl */ |
|
234 | - $acl->checkPrivileges($path, '{DAV:}write'); |
|
235 | - |
|
236 | - $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes'; |
|
237 | - $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner(); |
|
238 | - if ($limitSharingToOwner && !$isOwner) { |
|
239 | - return; |
|
240 | - } |
|
241 | - } |
|
242 | - |
|
243 | - $node->setPublishStatus(false); |
|
244 | - |
|
245 | - $response->setStatus(200); |
|
246 | - |
|
247 | - // Adding this because sending a response body may cause issues, |
|
248 | - // and I wanted some type of indicator the response was handled. |
|
249 | - $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
250 | - |
|
251 | - // Breaking the event chain |
|
252 | - return false; |
|
44 | + public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; |
|
45 | + |
|
46 | + /** |
|
47 | + * Reference to SabreDAV server object. |
|
48 | + * |
|
49 | + * @var \Sabre\DAV\Server |
|
50 | + */ |
|
51 | + protected $server; |
|
52 | + |
|
53 | + /** |
|
54 | + * Config instance to get instance secret. |
|
55 | + * |
|
56 | + * @var IConfig |
|
57 | + */ |
|
58 | + protected $config; |
|
59 | + |
|
60 | + /** |
|
61 | + * URL Generator for absolute URLs. |
|
62 | + * |
|
63 | + * @var IURLGenerator |
|
64 | + */ |
|
65 | + protected $urlGenerator; |
|
66 | + |
|
67 | + /** |
|
68 | + * PublishPlugin constructor. |
|
69 | + * |
|
70 | + * @param IConfig $config |
|
71 | + * @param IURLGenerator $urlGenerator |
|
72 | + */ |
|
73 | + public function __construct(IConfig $config, IURLGenerator $urlGenerator) { |
|
74 | + $this->config = $config; |
|
75 | + $this->urlGenerator = $urlGenerator; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * This method should return a list of server-features. |
|
80 | + * |
|
81 | + * This is for example 'versioning' and is added to the DAV: header |
|
82 | + * in an OPTIONS response. |
|
83 | + * |
|
84 | + * @return string[] |
|
85 | + */ |
|
86 | + public function getFeatures() { |
|
87 | + // May have to be changed to be detected |
|
88 | + return ['oc-calendar-publishing', 'calendarserver-sharing']; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Returns a plugin name. |
|
93 | + * |
|
94 | + * Using this name other plugins will be able to access other plugins |
|
95 | + * using Sabre\DAV\Server::getPlugin |
|
96 | + * |
|
97 | + * @return string |
|
98 | + */ |
|
99 | + public function getPluginName() { |
|
100 | + return 'oc-calendar-publishing'; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * This initializes the plugin. |
|
105 | + * |
|
106 | + * This function is called by Sabre\DAV\Server, after |
|
107 | + * addPlugin is called. |
|
108 | + * |
|
109 | + * This method should set up the required event subscriptions. |
|
110 | + * |
|
111 | + * @param Server $server |
|
112 | + */ |
|
113 | + public function initialize(Server $server) { |
|
114 | + $this->server = $server; |
|
115 | + |
|
116 | + $this->server->on('method:POST', [$this, 'httpPost']); |
|
117 | + $this->server->on('propFind', [$this, 'propFind']); |
|
118 | + } |
|
119 | + |
|
120 | + public function propFind(PropFind $propFind, INode $node) { |
|
121 | + if ($node instanceof Calendar) { |
|
122 | + $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) { |
|
123 | + if ($node->getPublishStatus()) { |
|
124 | + // We return the publish-url only if the calendar is published. |
|
125 | + $token = $node->getPublishStatus(); |
|
126 | + $publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri().'public-calendars/').$token; |
|
127 | + |
|
128 | + return new Publisher($publishUrl, true); |
|
129 | + } |
|
130 | + }); |
|
131 | + |
|
132 | + $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function () use ($node) { |
|
133 | + $canShare = (!$node->isSubscription() && $node->canWrite()); |
|
134 | + $canPublish = (!$node->isSubscription() && $node->canWrite()); |
|
135 | + |
|
136 | + if ($this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes') { |
|
137 | + $canShare = $canShare && ($node->getOwner() === $node->getPrincipalURI()); |
|
138 | + $canPublish = $canPublish && ($node->getOwner() === $node->getPrincipalURI()); |
|
139 | + } |
|
140 | + |
|
141 | + return new AllowedSharingModes($canShare, $canPublish); |
|
142 | + }); |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * We intercept this to handle POST requests on calendars. |
|
148 | + * |
|
149 | + * @param RequestInterface $request |
|
150 | + * @param ResponseInterface $response |
|
151 | + * |
|
152 | + * @return void|bool |
|
153 | + */ |
|
154 | + public function httpPost(RequestInterface $request, ResponseInterface $response) { |
|
155 | + $path = $request->getPath(); |
|
156 | + |
|
157 | + // Only handling xml |
|
158 | + $contentType = $request->getHeader('Content-Type'); |
|
159 | + if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { |
|
160 | + return; |
|
161 | + } |
|
162 | + |
|
163 | + // Making sure the node exists |
|
164 | + try { |
|
165 | + $node = $this->server->tree->getNodeForPath($path); |
|
166 | + } catch (NotFound $e) { |
|
167 | + return; |
|
168 | + } |
|
169 | + |
|
170 | + $requestBody = $request->getBodyAsString(); |
|
171 | + |
|
172 | + // If this request handler could not deal with this POST request, it |
|
173 | + // will return 'null' and other plugins get a chance to handle the |
|
174 | + // request. |
|
175 | + // |
|
176 | + // However, we already requested the full body. This is a problem, |
|
177 | + // because a body can only be read once. This is why we preemptively |
|
178 | + // re-populated the request body with the existing data. |
|
179 | + $request->setBody($requestBody); |
|
180 | + |
|
181 | + $this->server->xml->parse($requestBody, $request->getUrl(), $documentType); |
|
182 | + |
|
183 | + switch ($documentType) { |
|
184 | + |
|
185 | + case '{'.self::NS_CALENDARSERVER.'}publish-calendar': |
|
186 | + |
|
187 | + // We can only deal with IShareableCalendar objects |
|
188 | + if (!$node instanceof Calendar) { |
|
189 | + return; |
|
190 | + } |
|
191 | + $this->server->transactionType = 'post-publish-calendar'; |
|
192 | + |
|
193 | + // Getting ACL info |
|
194 | + $acl = $this->server->getPlugin('acl'); |
|
195 | + |
|
196 | + // If there's no ACL support, we allow everything |
|
197 | + if ($acl) { |
|
198 | + /** @var \Sabre\DAVACL\Plugin $acl */ |
|
199 | + $acl->checkPrivileges($path, '{DAV:}write'); |
|
200 | + |
|
201 | + $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes'; |
|
202 | + $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner(); |
|
203 | + if ($limitSharingToOwner && !$isOwner) { |
|
204 | + return; |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + $node->setPublishStatus(true); |
|
209 | + |
|
210 | + // iCloud sends back the 202, so we will too. |
|
211 | + $response->setStatus(202); |
|
212 | + |
|
213 | + // Adding this because sending a response body may cause issues, |
|
214 | + // and I wanted some type of indicator the response was handled. |
|
215 | + $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
216 | + |
|
217 | + // Breaking the event chain |
|
218 | + return false; |
|
219 | + |
|
220 | + case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar': |
|
221 | + |
|
222 | + // We can only deal with IShareableCalendar objects |
|
223 | + if (!$node instanceof Calendar) { |
|
224 | + return; |
|
225 | + } |
|
226 | + $this->server->transactionType = 'post-unpublish-calendar'; |
|
227 | + |
|
228 | + // Getting ACL info |
|
229 | + $acl = $this->server->getPlugin('acl'); |
|
230 | + |
|
231 | + // If there's no ACL support, we allow everything |
|
232 | + if ($acl) { |
|
233 | + /** @var \Sabre\DAVACL\Plugin $acl */ |
|
234 | + $acl->checkPrivileges($path, '{DAV:}write'); |
|
235 | + |
|
236 | + $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes'; |
|
237 | + $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner(); |
|
238 | + if ($limitSharingToOwner && !$isOwner) { |
|
239 | + return; |
|
240 | + } |
|
241 | + } |
|
242 | + |
|
243 | + $node->setPublishStatus(false); |
|
244 | + |
|
245 | + $response->setStatus(200); |
|
246 | + |
|
247 | + // Adding this because sending a response body may cause issues, |
|
248 | + // and I wanted some type of indicator the response was handled. |
|
249 | + $response->setHeader('X-Sabre-Status', 'everything-went-well'); |
|
250 | + |
|
251 | + // Breaking the event chain |
|
252 | + return false; |
|
253 | 253 | |
254 | - } |
|
255 | - } |
|
254 | + } |
|
255 | + } |
|
256 | 256 | } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | } |
45 | 45 | |
46 | 46 | public function createFile($name, $data = null) { |
47 | - throw new Forbidden('Permission denied to create file (filename ' . $name . ')'); |
|
47 | + throw new Forbidden('Permission denied to create file (filename '.$name.')'); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | public function createDirectory($name) { |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | } |
60 | 60 | |
61 | 61 | public function getChildren(): array { |
62 | - return array_map(function ($node) { |
|
62 | + return array_map(function($node) { |
|
63 | 63 | return new UploadFolder($node, $this->cleanupService); |
64 | 64 | }, $this->impl()->getChildren()); |
65 | 65 | } |
@@ -92,10 +92,10 @@ discard block |
||
92 | 92 | $rootView = new View(); |
93 | 93 | $user = \OC::$server->getUserSession()->getUser(); |
94 | 94 | Filesystem::initMountPoints($user->getUID()); |
95 | - if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) { |
|
96 | - $rootView->mkdir('/' . $user->getUID() . '/uploads'); |
|
95 | + if (!$rootView->file_exists('/'.$user->getUID().'/uploads')) { |
|
96 | + $rootView->mkdir('/'.$user->getUID().'/uploads'); |
|
97 | 97 | } |
98 | - $view = new View('/' . $user->getUID() . '/uploads'); |
|
98 | + $view = new View('/'.$user->getUID().'/uploads'); |
|
99 | 99 | $rootInfo = $view->getFileInfo(''); |
100 | 100 | return new Directory($view, $rootInfo); |
101 | 101 | } |
@@ -33,70 +33,70 @@ |
||
33 | 33 | |
34 | 34 | class UploadHome implements ICollection { |
35 | 35 | |
36 | - /** @var array */ |
|
37 | - private $principalInfo; |
|
38 | - /** @var CleanupService */ |
|
39 | - private $cleanupService; |
|
36 | + /** @var array */ |
|
37 | + private $principalInfo; |
|
38 | + /** @var CleanupService */ |
|
39 | + private $cleanupService; |
|
40 | 40 | |
41 | - public function __construct(array $principalInfo, CleanupService $cleanupService) { |
|
42 | - $this->principalInfo = $principalInfo; |
|
43 | - $this->cleanupService = $cleanupService; |
|
44 | - } |
|
41 | + public function __construct(array $principalInfo, CleanupService $cleanupService) { |
|
42 | + $this->principalInfo = $principalInfo; |
|
43 | + $this->cleanupService = $cleanupService; |
|
44 | + } |
|
45 | 45 | |
46 | - public function createFile($name, $data = null) { |
|
47 | - throw new Forbidden('Permission denied to create file (filename ' . $name . ')'); |
|
48 | - } |
|
46 | + public function createFile($name, $data = null) { |
|
47 | + throw new Forbidden('Permission denied to create file (filename ' . $name . ')'); |
|
48 | + } |
|
49 | 49 | |
50 | - public function createDirectory($name) { |
|
51 | - $this->impl()->createDirectory($name); |
|
50 | + public function createDirectory($name) { |
|
51 | + $this->impl()->createDirectory($name); |
|
52 | 52 | |
53 | - // Add a cleanup job |
|
54 | - $this->cleanupService->addJob($name); |
|
55 | - } |
|
53 | + // Add a cleanup job |
|
54 | + $this->cleanupService->addJob($name); |
|
55 | + } |
|
56 | 56 | |
57 | - public function getChild($name): UploadFolder { |
|
58 | - return new UploadFolder($this->impl()->getChild($name), $this->cleanupService); |
|
59 | - } |
|
57 | + public function getChild($name): UploadFolder { |
|
58 | + return new UploadFolder($this->impl()->getChild($name), $this->cleanupService); |
|
59 | + } |
|
60 | 60 | |
61 | - public function getChildren(): array { |
|
62 | - return array_map(function ($node) { |
|
63 | - return new UploadFolder($node, $this->cleanupService); |
|
64 | - }, $this->impl()->getChildren()); |
|
65 | - } |
|
61 | + public function getChildren(): array { |
|
62 | + return array_map(function ($node) { |
|
63 | + return new UploadFolder($node, $this->cleanupService); |
|
64 | + }, $this->impl()->getChildren()); |
|
65 | + } |
|
66 | 66 | |
67 | - public function childExists($name): bool { |
|
68 | - return !is_null($this->getChild($name)); |
|
69 | - } |
|
67 | + public function childExists($name): bool { |
|
68 | + return !is_null($this->getChild($name)); |
|
69 | + } |
|
70 | 70 | |
71 | - public function delete() { |
|
72 | - $this->impl()->delete(); |
|
73 | - } |
|
71 | + public function delete() { |
|
72 | + $this->impl()->delete(); |
|
73 | + } |
|
74 | 74 | |
75 | - public function getName() { |
|
76 | - [,$name] = \Sabre\Uri\split($this->principalInfo['uri']); |
|
77 | - return $name; |
|
78 | - } |
|
75 | + public function getName() { |
|
76 | + [,$name] = \Sabre\Uri\split($this->principalInfo['uri']); |
|
77 | + return $name; |
|
78 | + } |
|
79 | 79 | |
80 | - public function setName($name) { |
|
81 | - throw new Forbidden('Permission denied to rename this folder'); |
|
82 | - } |
|
80 | + public function setName($name) { |
|
81 | + throw new Forbidden('Permission denied to rename this folder'); |
|
82 | + } |
|
83 | 83 | |
84 | - public function getLastModified() { |
|
85 | - return $this->impl()->getLastModified(); |
|
86 | - } |
|
84 | + public function getLastModified() { |
|
85 | + return $this->impl()->getLastModified(); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @return Directory |
|
90 | - */ |
|
91 | - private function impl() { |
|
92 | - $rootView = new View(); |
|
93 | - $user = \OC::$server->getUserSession()->getUser(); |
|
94 | - Filesystem::initMountPoints($user->getUID()); |
|
95 | - if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) { |
|
96 | - $rootView->mkdir('/' . $user->getUID() . '/uploads'); |
|
97 | - } |
|
98 | - $view = new View('/' . $user->getUID() . '/uploads'); |
|
99 | - $rootInfo = $view->getFileInfo(''); |
|
100 | - return new Directory($view, $rootInfo); |
|
101 | - } |
|
88 | + /** |
|
89 | + * @return Directory |
|
90 | + */ |
|
91 | + private function impl() { |
|
92 | + $rootView = new View(); |
|
93 | + $user = \OC::$server->getUserSession()->getUser(); |
|
94 | + Filesystem::initMountPoints($user->getUID()); |
|
95 | + if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) { |
|
96 | + $rootView->mkdir('/' . $user->getUID() . '/uploads'); |
|
97 | + } |
|
98 | + $view = new View('/' . $user->getUID() . '/uploads'); |
|
99 | + $rootInfo = $view->getFileInfo(''); |
|
100 | + return new Directory($view, $rootInfo); |
|
101 | + } |
|
102 | 102 | } |
@@ -33,33 +33,33 @@ |
||
33 | 33 | * Verify that the public link share is valid |
34 | 34 | */ |
35 | 35 | class PublicLinkCheckPlugin extends ServerPlugin { |
36 | - /** |
|
37 | - * @var FileInfo |
|
38 | - */ |
|
39 | - private $fileInfo; |
|
36 | + /** |
|
37 | + * @var FileInfo |
|
38 | + */ |
|
39 | + private $fileInfo; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param FileInfo $fileInfo |
|
43 | - */ |
|
44 | - public function setFileInfo($fileInfo) { |
|
45 | - $this->fileInfo = $fileInfo; |
|
46 | - } |
|
41 | + /** |
|
42 | + * @param FileInfo $fileInfo |
|
43 | + */ |
|
44 | + public function setFileInfo($fileInfo) { |
|
45 | + $this->fileInfo = $fileInfo; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * This initializes the plugin. |
|
50 | - * |
|
51 | - * @param \Sabre\DAV\Server $server Sabre server |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function initialize(\Sabre\DAV\Server $server) { |
|
56 | - $server->on('beforeMethod:*', [$this, 'beforeMethod']); |
|
57 | - } |
|
48 | + /** |
|
49 | + * This initializes the plugin. |
|
50 | + * |
|
51 | + * @param \Sabre\DAV\Server $server Sabre server |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function initialize(\Sabre\DAV\Server $server) { |
|
56 | + $server->on('beforeMethod:*', [$this, 'beforeMethod']); |
|
57 | + } |
|
58 | 58 | |
59 | - public function beforeMethod(RequestInterface $request, ResponseInterface $response) { |
|
60 | - // verify that the owner didn't have his share permissions revoked |
|
61 | - if ($this->fileInfo && !$this->fileInfo->isShareable()) { |
|
62 | - throw new NotFound(); |
|
63 | - } |
|
64 | - } |
|
59 | + public function beforeMethod(RequestInterface $request, ResponseInterface $response) { |
|
60 | + // verify that the owner didn't have his share permissions revoked |
|
61 | + if ($this->fileInfo && !$this->fileInfo->isShareable()) { |
|
62 | + throw new NotFound(); |
|
63 | + } |
|
64 | + } |
|
65 | 65 | } |
@@ -96,7 +96,7 @@ |
||
96 | 96 | $output->writeln("Start birthday calendar sync for all users ..."); |
97 | 97 | $p = new ProgressBar($output); |
98 | 98 | $p->start(); |
99 | - $this->userManager->callForSeenUsers(function ($user) use ($p) { |
|
99 | + $this->userManager->callForSeenUsers(function($user) use ($p) { |
|
100 | 100 | $p->advance(); |
101 | 101 | |
102 | 102 | $userId = $user->getUID(); |
@@ -38,87 +38,87 @@ |
||
38 | 38 | |
39 | 39 | class SyncBirthdayCalendar extends Command { |
40 | 40 | |
41 | - /** @var BirthdayService */ |
|
42 | - private $birthdayService; |
|
43 | - |
|
44 | - /** @var IConfig */ |
|
45 | - private $config; |
|
46 | - |
|
47 | - /** @var IUserManager */ |
|
48 | - private $userManager; |
|
49 | - |
|
50 | - /** |
|
51 | - * @param IUserManager $userManager |
|
52 | - * @param IConfig $config |
|
53 | - * @param BirthdayService $birthdayService |
|
54 | - */ |
|
55 | - public function __construct(IUserManager $userManager, IConfig $config, |
|
56 | - BirthdayService $birthdayService) { |
|
57 | - parent::__construct(); |
|
58 | - $this->birthdayService = $birthdayService; |
|
59 | - $this->config = $config; |
|
60 | - $this->userManager = $userManager; |
|
61 | - } |
|
62 | - |
|
63 | - protected function configure() { |
|
64 | - $this |
|
65 | - ->setName('dav:sync-birthday-calendar') |
|
66 | - ->setDescription('Synchronizes the birthday calendar') |
|
67 | - ->addArgument('user', |
|
68 | - InputArgument::OPTIONAL, |
|
69 | - 'User for whom the birthday calendar will be synchronized'); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @param InputInterface $input |
|
74 | - * @param OutputInterface $output |
|
75 | - */ |
|
76 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
77 | - $this->verifyEnabled(); |
|
78 | - |
|
79 | - $user = $input->getArgument('user'); |
|
80 | - if (!is_null($user)) { |
|
81 | - if (!$this->userManager->userExists($user)) { |
|
82 | - throw new \InvalidArgumentException("User <$user> in unknown."); |
|
83 | - } |
|
84 | - |
|
85 | - // re-enable the birthday calendar in case it's called directly with a user name |
|
86 | - $isEnabled = $this->config->getUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes'); |
|
87 | - if ($isEnabled !== 'yes') { |
|
88 | - $this->config->setUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes'); |
|
89 | - $output->writeln("Re-enabling birthday calendar for $user"); |
|
90 | - } |
|
91 | - |
|
92 | - $output->writeln("Start birthday calendar sync for $user"); |
|
93 | - $this->birthdayService->syncUser($user); |
|
94 | - return 0; |
|
95 | - } |
|
96 | - $output->writeln("Start birthday calendar sync for all users ..."); |
|
97 | - $p = new ProgressBar($output); |
|
98 | - $p->start(); |
|
99 | - $this->userManager->callForSeenUsers(function ($user) use ($p) { |
|
100 | - $p->advance(); |
|
101 | - |
|
102 | - $userId = $user->getUID(); |
|
103 | - $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes'); |
|
104 | - if ($isEnabled !== 'yes') { |
|
105 | - return; |
|
106 | - } |
|
107 | - |
|
108 | - /** @var IUser $user */ |
|
109 | - $this->birthdayService->syncUser($user->getUID()); |
|
110 | - }); |
|
111 | - |
|
112 | - $p->finish(); |
|
113 | - $output->writeln(''); |
|
114 | - return 0; |
|
115 | - } |
|
116 | - |
|
117 | - protected function verifyEnabled() { |
|
118 | - $isEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'); |
|
119 | - |
|
120 | - if ($isEnabled !== 'yes') { |
|
121 | - throw new \InvalidArgumentException('Birthday calendars are disabled'); |
|
122 | - } |
|
123 | - } |
|
41 | + /** @var BirthdayService */ |
|
42 | + private $birthdayService; |
|
43 | + |
|
44 | + /** @var IConfig */ |
|
45 | + private $config; |
|
46 | + |
|
47 | + /** @var IUserManager */ |
|
48 | + private $userManager; |
|
49 | + |
|
50 | + /** |
|
51 | + * @param IUserManager $userManager |
|
52 | + * @param IConfig $config |
|
53 | + * @param BirthdayService $birthdayService |
|
54 | + */ |
|
55 | + public function __construct(IUserManager $userManager, IConfig $config, |
|
56 | + BirthdayService $birthdayService) { |
|
57 | + parent::__construct(); |
|
58 | + $this->birthdayService = $birthdayService; |
|
59 | + $this->config = $config; |
|
60 | + $this->userManager = $userManager; |
|
61 | + } |
|
62 | + |
|
63 | + protected function configure() { |
|
64 | + $this |
|
65 | + ->setName('dav:sync-birthday-calendar') |
|
66 | + ->setDescription('Synchronizes the birthday calendar') |
|
67 | + ->addArgument('user', |
|
68 | + InputArgument::OPTIONAL, |
|
69 | + 'User for whom the birthday calendar will be synchronized'); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @param InputInterface $input |
|
74 | + * @param OutputInterface $output |
|
75 | + */ |
|
76 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
77 | + $this->verifyEnabled(); |
|
78 | + |
|
79 | + $user = $input->getArgument('user'); |
|
80 | + if (!is_null($user)) { |
|
81 | + if (!$this->userManager->userExists($user)) { |
|
82 | + throw new \InvalidArgumentException("User <$user> in unknown."); |
|
83 | + } |
|
84 | + |
|
85 | + // re-enable the birthday calendar in case it's called directly with a user name |
|
86 | + $isEnabled = $this->config->getUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes'); |
|
87 | + if ($isEnabled !== 'yes') { |
|
88 | + $this->config->setUserValue($user, 'dav', 'generateBirthdayCalendar', 'yes'); |
|
89 | + $output->writeln("Re-enabling birthday calendar for $user"); |
|
90 | + } |
|
91 | + |
|
92 | + $output->writeln("Start birthday calendar sync for $user"); |
|
93 | + $this->birthdayService->syncUser($user); |
|
94 | + return 0; |
|
95 | + } |
|
96 | + $output->writeln("Start birthday calendar sync for all users ..."); |
|
97 | + $p = new ProgressBar($output); |
|
98 | + $p->start(); |
|
99 | + $this->userManager->callForSeenUsers(function ($user) use ($p) { |
|
100 | + $p->advance(); |
|
101 | + |
|
102 | + $userId = $user->getUID(); |
|
103 | + $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes'); |
|
104 | + if ($isEnabled !== 'yes') { |
|
105 | + return; |
|
106 | + } |
|
107 | + |
|
108 | + /** @var IUser $user */ |
|
109 | + $this->birthdayService->syncUser($user->getUID()); |
|
110 | + }); |
|
111 | + |
|
112 | + $p->finish(); |
|
113 | + $output->writeln(''); |
|
114 | + return 0; |
|
115 | + } |
|
116 | + |
|
117 | + protected function verifyEnabled() { |
|
118 | + $isEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'); |
|
119 | + |
|
120 | + if ($isEnabled !== 'yes') { |
|
121 | + throw new \InvalidArgumentException('Birthday calendars are disabled'); |
|
122 | + } |
|
123 | + } |
|
124 | 124 | } |
@@ -146,8 +146,8 @@ discard block |
||
146 | 146 | } |
147 | 147 | |
148 | 148 | private function entitiesToArray(array $entities) { |
149 | - return array_map(function (IEntity $entity) { |
|
150 | - $events = array_map(function (IEntityEvent $entityEvent) { |
|
149 | + return array_map(function(IEntity $entity) { |
|
150 | + $events = array_map(function(IEntityEvent $entityEvent) { |
|
151 | 151 | return [ |
152 | 152 | 'eventName' => $entityEvent->getEventName(), |
153 | 153 | 'displayName' => $entityEvent->getDisplayName() |
@@ -164,11 +164,11 @@ discard block |
||
164 | 164 | } |
165 | 165 | |
166 | 166 | private function operatorsToArray(array $operators) { |
167 | - $operators = array_filter($operators, function (IOperation $operator) { |
|
167 | + $operators = array_filter($operators, function(IOperation $operator) { |
|
168 | 168 | return $operator->isAvailableForScope($this->getScope()); |
169 | 169 | }); |
170 | 170 | |
171 | - return array_map(function (IOperation $operator) { |
|
171 | + return array_map(function(IOperation $operator) { |
|
172 | 172 | return [ |
173 | 173 | 'id' => get_class($operator), |
174 | 174 | 'icon' => $operator->getIcon(), |
@@ -182,11 +182,11 @@ discard block |
||
182 | 182 | } |
183 | 183 | |
184 | 184 | private function checksToArray(array $checks) { |
185 | - $checks = array_filter($checks, function (ICheck $check) { |
|
185 | + $checks = array_filter($checks, function(ICheck $check) { |
|
186 | 186 | return $check->isAvailableForScope($this->getScope()); |
187 | 187 | }); |
188 | 188 | |
189 | - return array_map(function (ICheck $check) { |
|
189 | + return array_map(function(ICheck $check) { |
|
190 | 190 | return [ |
191 | 191 | 'id' => get_class($check), |
192 | 192 | 'supportedEntities' => $check->supportedEntities(), |
@@ -48,145 +48,145 @@ |
||
48 | 48 | use OCP\WorkflowEngine\ISpecificOperation; |
49 | 49 | |
50 | 50 | abstract class ASettings implements ISettings { |
51 | - private IL10N $l10n; |
|
52 | - private string $appName; |
|
53 | - private IEventDispatcher $eventDispatcher; |
|
54 | - protected Manager $manager; |
|
55 | - private IInitialState $initialStateService; |
|
56 | - private IConfig $config; |
|
57 | - private IURLGenerator $urlGenerator; |
|
58 | - |
|
59 | - public function __construct( |
|
60 | - string $appName, |
|
61 | - IL10N $l, |
|
62 | - IEventDispatcher $eventDispatcher, |
|
63 | - Manager $manager, |
|
64 | - IInitialState $initialStateService, |
|
65 | - IConfig $config, |
|
66 | - IURLGenerator $urlGenerator |
|
67 | - ) { |
|
68 | - $this->appName = $appName; |
|
69 | - $this->l10n = $l; |
|
70 | - $this->eventDispatcher = $eventDispatcher; |
|
71 | - $this->manager = $manager; |
|
72 | - $this->initialStateService = $initialStateService; |
|
73 | - $this->config = $config; |
|
74 | - $this->urlGenerator = $urlGenerator; |
|
75 | - } |
|
76 | - |
|
77 | - abstract public function getScope(): int; |
|
78 | - |
|
79 | - /** |
|
80 | - * @return TemplateResponse |
|
81 | - */ |
|
82 | - public function getForm(): TemplateResponse { |
|
83 | - // @deprecated in 20.0.0: retire this one in favor of the typed event |
|
84 | - $this->eventDispatcher->dispatch( |
|
85 | - 'OCP\WorkflowEngine::loadAdditionalSettingScripts', |
|
86 | - new LoadSettingsScriptsEvent() |
|
87 | - ); |
|
88 | - $this->eventDispatcher->dispatchTyped(new LoadSettingsScriptsEvent()); |
|
89 | - |
|
90 | - $entities = $this->manager->getEntitiesList(); |
|
91 | - $this->initialStateService->provideInitialState( |
|
92 | - 'entities', |
|
93 | - $this->entitiesToArray($entities) |
|
94 | - ); |
|
95 | - |
|
96 | - $operators = $this->manager->getOperatorList(); |
|
97 | - $this->initialStateService->provideInitialState( |
|
98 | - 'operators', |
|
99 | - $this->operatorsToArray($operators) |
|
100 | - ); |
|
101 | - |
|
102 | - $checks = $this->manager->getCheckList(); |
|
103 | - $this->initialStateService->provideInitialState( |
|
104 | - 'checks', |
|
105 | - $this->checksToArray($checks) |
|
106 | - ); |
|
107 | - |
|
108 | - $this->initialStateService->provideInitialState( |
|
109 | - 'scope', |
|
110 | - $this->getScope() |
|
111 | - ); |
|
112 | - |
|
113 | - $this->initialStateService->provideInitialState( |
|
114 | - 'appstoreenabled', |
|
115 | - $this->config->getSystemValueBool('appstoreenabled', true) |
|
116 | - ); |
|
117 | - |
|
118 | - $this->initialStateService->provideInitialState( |
|
119 | - 'doc-url', |
|
120 | - $this->urlGenerator->linkToDocs('admin-workflowengine') |
|
121 | - ); |
|
122 | - |
|
123 | - return new TemplateResponse(Application::APP_ID, 'settings', [], 'blank'); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @return string the section ID, e.g. 'sharing' |
|
128 | - */ |
|
129 | - public function getSection(): ?string { |
|
130 | - return 'workflow'; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @return int whether the form should be rather on the top or bottom of |
|
135 | - * the admin section. The forms are arranged in ascending order of the |
|
136 | - * priority values. It is required to return a value between 0 and 100. |
|
137 | - * |
|
138 | - * E.g.: 70 |
|
139 | - */ |
|
140 | - public function getPriority(): int { |
|
141 | - return 0; |
|
142 | - } |
|
143 | - |
|
144 | - private function entitiesToArray(array $entities) { |
|
145 | - return array_map(function (IEntity $entity) { |
|
146 | - $events = array_map(function (IEntityEvent $entityEvent) { |
|
147 | - return [ |
|
148 | - 'eventName' => $entityEvent->getEventName(), |
|
149 | - 'displayName' => $entityEvent->getDisplayName() |
|
150 | - ]; |
|
151 | - }, $entity->getEvents()); |
|
152 | - |
|
153 | - return [ |
|
154 | - 'id' => get_class($entity), |
|
155 | - 'icon' => $entity->getIcon(), |
|
156 | - 'name' => $entity->getName(), |
|
157 | - 'events' => $events, |
|
158 | - ]; |
|
159 | - }, $entities); |
|
160 | - } |
|
161 | - |
|
162 | - private function operatorsToArray(array $operators) { |
|
163 | - $operators = array_filter($operators, function (IOperation $operator) { |
|
164 | - return $operator->isAvailableForScope($this->getScope()); |
|
165 | - }); |
|
166 | - |
|
167 | - return array_map(function (IOperation $operator) { |
|
168 | - return [ |
|
169 | - 'id' => get_class($operator), |
|
170 | - 'icon' => $operator->getIcon(), |
|
171 | - 'name' => $operator->getDisplayName(), |
|
172 | - 'description' => $operator->getDescription(), |
|
173 | - 'fixedEntity' => $operator instanceof ISpecificOperation ? $operator->getEntityId() : '', |
|
174 | - 'isComplex' => $operator instanceof IComplexOperation, |
|
175 | - 'triggerHint' => $operator instanceof IComplexOperation ? $operator->getTriggerHint() : '', |
|
176 | - ]; |
|
177 | - }, $operators); |
|
178 | - } |
|
179 | - |
|
180 | - private function checksToArray(array $checks) { |
|
181 | - $checks = array_filter($checks, function (ICheck $check) { |
|
182 | - return $check->isAvailableForScope($this->getScope()); |
|
183 | - }); |
|
184 | - |
|
185 | - return array_map(function (ICheck $check) { |
|
186 | - return [ |
|
187 | - 'id' => get_class($check), |
|
188 | - 'supportedEntities' => $check->supportedEntities(), |
|
189 | - ]; |
|
190 | - }, $checks); |
|
191 | - } |
|
51 | + private IL10N $l10n; |
|
52 | + private string $appName; |
|
53 | + private IEventDispatcher $eventDispatcher; |
|
54 | + protected Manager $manager; |
|
55 | + private IInitialState $initialStateService; |
|
56 | + private IConfig $config; |
|
57 | + private IURLGenerator $urlGenerator; |
|
58 | + |
|
59 | + public function __construct( |
|
60 | + string $appName, |
|
61 | + IL10N $l, |
|
62 | + IEventDispatcher $eventDispatcher, |
|
63 | + Manager $manager, |
|
64 | + IInitialState $initialStateService, |
|
65 | + IConfig $config, |
|
66 | + IURLGenerator $urlGenerator |
|
67 | + ) { |
|
68 | + $this->appName = $appName; |
|
69 | + $this->l10n = $l; |
|
70 | + $this->eventDispatcher = $eventDispatcher; |
|
71 | + $this->manager = $manager; |
|
72 | + $this->initialStateService = $initialStateService; |
|
73 | + $this->config = $config; |
|
74 | + $this->urlGenerator = $urlGenerator; |
|
75 | + } |
|
76 | + |
|
77 | + abstract public function getScope(): int; |
|
78 | + |
|
79 | + /** |
|
80 | + * @return TemplateResponse |
|
81 | + */ |
|
82 | + public function getForm(): TemplateResponse { |
|
83 | + // @deprecated in 20.0.0: retire this one in favor of the typed event |
|
84 | + $this->eventDispatcher->dispatch( |
|
85 | + 'OCP\WorkflowEngine::loadAdditionalSettingScripts', |
|
86 | + new LoadSettingsScriptsEvent() |
|
87 | + ); |
|
88 | + $this->eventDispatcher->dispatchTyped(new LoadSettingsScriptsEvent()); |
|
89 | + |
|
90 | + $entities = $this->manager->getEntitiesList(); |
|
91 | + $this->initialStateService->provideInitialState( |
|
92 | + 'entities', |
|
93 | + $this->entitiesToArray($entities) |
|
94 | + ); |
|
95 | + |
|
96 | + $operators = $this->manager->getOperatorList(); |
|
97 | + $this->initialStateService->provideInitialState( |
|
98 | + 'operators', |
|
99 | + $this->operatorsToArray($operators) |
|
100 | + ); |
|
101 | + |
|
102 | + $checks = $this->manager->getCheckList(); |
|
103 | + $this->initialStateService->provideInitialState( |
|
104 | + 'checks', |
|
105 | + $this->checksToArray($checks) |
|
106 | + ); |
|
107 | + |
|
108 | + $this->initialStateService->provideInitialState( |
|
109 | + 'scope', |
|
110 | + $this->getScope() |
|
111 | + ); |
|
112 | + |
|
113 | + $this->initialStateService->provideInitialState( |
|
114 | + 'appstoreenabled', |
|
115 | + $this->config->getSystemValueBool('appstoreenabled', true) |
|
116 | + ); |
|
117 | + |
|
118 | + $this->initialStateService->provideInitialState( |
|
119 | + 'doc-url', |
|
120 | + $this->urlGenerator->linkToDocs('admin-workflowengine') |
|
121 | + ); |
|
122 | + |
|
123 | + return new TemplateResponse(Application::APP_ID, 'settings', [], 'blank'); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @return string the section ID, e.g. 'sharing' |
|
128 | + */ |
|
129 | + public function getSection(): ?string { |
|
130 | + return 'workflow'; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @return int whether the form should be rather on the top or bottom of |
|
135 | + * the admin section. The forms are arranged in ascending order of the |
|
136 | + * priority values. It is required to return a value between 0 and 100. |
|
137 | + * |
|
138 | + * E.g.: 70 |
|
139 | + */ |
|
140 | + public function getPriority(): int { |
|
141 | + return 0; |
|
142 | + } |
|
143 | + |
|
144 | + private function entitiesToArray(array $entities) { |
|
145 | + return array_map(function (IEntity $entity) { |
|
146 | + $events = array_map(function (IEntityEvent $entityEvent) { |
|
147 | + return [ |
|
148 | + 'eventName' => $entityEvent->getEventName(), |
|
149 | + 'displayName' => $entityEvent->getDisplayName() |
|
150 | + ]; |
|
151 | + }, $entity->getEvents()); |
|
152 | + |
|
153 | + return [ |
|
154 | + 'id' => get_class($entity), |
|
155 | + 'icon' => $entity->getIcon(), |
|
156 | + 'name' => $entity->getName(), |
|
157 | + 'events' => $events, |
|
158 | + ]; |
|
159 | + }, $entities); |
|
160 | + } |
|
161 | + |
|
162 | + private function operatorsToArray(array $operators) { |
|
163 | + $operators = array_filter($operators, function (IOperation $operator) { |
|
164 | + return $operator->isAvailableForScope($this->getScope()); |
|
165 | + }); |
|
166 | + |
|
167 | + return array_map(function (IOperation $operator) { |
|
168 | + return [ |
|
169 | + 'id' => get_class($operator), |
|
170 | + 'icon' => $operator->getIcon(), |
|
171 | + 'name' => $operator->getDisplayName(), |
|
172 | + 'description' => $operator->getDescription(), |
|
173 | + 'fixedEntity' => $operator instanceof ISpecificOperation ? $operator->getEntityId() : '', |
|
174 | + 'isComplex' => $operator instanceof IComplexOperation, |
|
175 | + 'triggerHint' => $operator instanceof IComplexOperation ? $operator->getTriggerHint() : '', |
|
176 | + ]; |
|
177 | + }, $operators); |
|
178 | + } |
|
179 | + |
|
180 | + private function checksToArray(array $checks) { |
|
181 | + $checks = array_filter($checks, function (ICheck $check) { |
|
182 | + return $check->isAvailableForScope($this->getScope()); |
|
183 | + }); |
|
184 | + |
|
185 | + return array_map(function (ICheck $check) { |
|
186 | + return [ |
|
187 | + 'id' => get_class($check), |
|
188 | + 'supportedEntities' => $check->supportedEntities(), |
|
189 | + ]; |
|
190 | + }, $checks); |
|
191 | + } |
|
192 | 192 | } |
@@ -72,7 +72,7 @@ |
||
72 | 72 | * @return IProvider[] |
73 | 73 | */ |
74 | 74 | public function getPrimaryProviders(): array { |
75 | - return array_filter($this->providers, function (IProvider $provider) { |
|
75 | + return array_filter($this->providers, function(IProvider $provider) { |
|
76 | 76 | return !($provider instanceof BackupCodesProvider); |
77 | 77 | }); |
78 | 78 | } |
@@ -33,49 +33,49 @@ |
||
33 | 33 | * Contains all two-factor provider information for the two-factor login challenge |
34 | 34 | */ |
35 | 35 | class ProviderSet { |
36 | - /** @var IProvider */ |
|
37 | - private $providers; |
|
36 | + /** @var IProvider */ |
|
37 | + private $providers; |
|
38 | 38 | |
39 | - /** @var bool */ |
|
40 | - private $providerMissing; |
|
39 | + /** @var bool */ |
|
40 | + private $providerMissing; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param IProvider[] $providers |
|
44 | - * @param bool $providerMissing |
|
45 | - */ |
|
46 | - public function __construct(array $providers, bool $providerMissing) { |
|
47 | - $this->providers = []; |
|
48 | - foreach ($providers as $provider) { |
|
49 | - $this->providers[$provider->getId()] = $provider; |
|
50 | - } |
|
51 | - $this->providerMissing = $providerMissing; |
|
52 | - } |
|
42 | + /** |
|
43 | + * @param IProvider[] $providers |
|
44 | + * @param bool $providerMissing |
|
45 | + */ |
|
46 | + public function __construct(array $providers, bool $providerMissing) { |
|
47 | + $this->providers = []; |
|
48 | + foreach ($providers as $provider) { |
|
49 | + $this->providers[$provider->getId()] = $provider; |
|
50 | + } |
|
51 | + $this->providerMissing = $providerMissing; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @param string $providerId |
|
56 | - * @return IProvider|null |
|
57 | - */ |
|
58 | - public function getProvider(string $providerId) { |
|
59 | - return $this->providers[$providerId] ?? null; |
|
60 | - } |
|
54 | + /** |
|
55 | + * @param string $providerId |
|
56 | + * @return IProvider|null |
|
57 | + */ |
|
58 | + public function getProvider(string $providerId) { |
|
59 | + return $this->providers[$providerId] ?? null; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @return IProvider[] |
|
64 | - */ |
|
65 | - public function getProviders(): array { |
|
66 | - return $this->providers; |
|
67 | - } |
|
62 | + /** |
|
63 | + * @return IProvider[] |
|
64 | + */ |
|
65 | + public function getProviders(): array { |
|
66 | + return $this->providers; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @return IProvider[] |
|
71 | - */ |
|
72 | - public function getPrimaryProviders(): array { |
|
73 | - return array_filter($this->providers, function (IProvider $provider) { |
|
74 | - return !($provider instanceof BackupCodesProvider); |
|
75 | - }); |
|
76 | - } |
|
69 | + /** |
|
70 | + * @return IProvider[] |
|
71 | + */ |
|
72 | + public function getPrimaryProviders(): array { |
|
73 | + return array_filter($this->providers, function (IProvider $provider) { |
|
74 | + return !($provider instanceof BackupCodesProvider); |
|
75 | + }); |
|
76 | + } |
|
77 | 77 | |
78 | - public function isProviderMissing(): bool { |
|
79 | - return $this->providerMissing; |
|
80 | - } |
|
78 | + public function isProviderMissing(): bool { |
|
79 | + return $this->providerMissing; |
|
80 | + } |
|
81 | 81 | } |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | $result->closeCursor(); |
242 | 242 | |
243 | 243 | if ($all) { |
244 | - return ((int)$row[0] === \count($objIds)); |
|
244 | + return ((int) $row[0] === \count($objIds)); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | return (bool) $row; |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | if (\count($tags) !== \count($tagIds)) { |
260 | 260 | // at least one tag missing, bail out |
261 | 261 | $foundTagIds = array_map( |
262 | - function (ISystemTag $tag) { |
|
262 | + function(ISystemTag $tag) { |
|
263 | 263 | return $tag->getId(); |
264 | 264 | }, |
265 | 265 | $tags |
@@ -39,235 +39,235 @@ |
||
39 | 39 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
40 | 40 | |
41 | 41 | class SystemTagObjectMapper implements ISystemTagObjectMapper { |
42 | - public const RELATION_TABLE = 'systemtag_object_mapping'; |
|
43 | - |
|
44 | - /** @var ISystemTagManager */ |
|
45 | - protected $tagManager; |
|
46 | - |
|
47 | - /** @var IDBConnection */ |
|
48 | - protected $connection; |
|
49 | - |
|
50 | - /** @var EventDispatcherInterface */ |
|
51 | - protected $dispatcher; |
|
52 | - |
|
53 | - /** |
|
54 | - * Constructor. |
|
55 | - * |
|
56 | - * @param IDBConnection $connection database connection |
|
57 | - * @param ISystemTagManager $tagManager system tag manager |
|
58 | - * @param EventDispatcherInterface $dispatcher |
|
59 | - */ |
|
60 | - public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) { |
|
61 | - $this->connection = $connection; |
|
62 | - $this->tagManager = $tagManager; |
|
63 | - $this->dispatcher = $dispatcher; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - public function getTagIdsForObjects($objIds, string $objectType): array { |
|
70 | - if (!\is_array($objIds)) { |
|
71 | - $objIds = [$objIds]; |
|
72 | - } elseif (empty($objIds)) { |
|
73 | - return []; |
|
74 | - } |
|
75 | - |
|
76 | - $query = $this->connection->getQueryBuilder(); |
|
77 | - $query->select(['systemtagid', 'objectid']) |
|
78 | - ->from(self::RELATION_TABLE) |
|
79 | - ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
80 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
81 | - ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
82 | - ->setParameter('objecttype', $objectType) |
|
83 | - ->addOrderBy('objectid', 'ASC') |
|
84 | - ->addOrderBy('systemtagid', 'ASC'); |
|
85 | - |
|
86 | - $mapping = []; |
|
87 | - foreach ($objIds as $objId) { |
|
88 | - $mapping[$objId] = []; |
|
89 | - } |
|
90 | - |
|
91 | - $result = $query->execute(); |
|
92 | - while ($row = $result->fetch()) { |
|
93 | - $objectId = $row['objectid']; |
|
94 | - $mapping[$objectId][] = $row['systemtagid']; |
|
95 | - } |
|
96 | - |
|
97 | - $result->closeCursor(); |
|
98 | - |
|
99 | - return $mapping; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * {@inheritdoc} |
|
104 | - */ |
|
105 | - public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0, string $offset = ''): array { |
|
106 | - if (!\is_array($tagIds)) { |
|
107 | - $tagIds = [$tagIds]; |
|
108 | - } |
|
109 | - |
|
110 | - $this->assertTagsExist($tagIds); |
|
111 | - |
|
112 | - $query = $this->connection->getQueryBuilder(); |
|
113 | - $query->selectDistinct('objectid') |
|
114 | - ->from(self::RELATION_TABLE) |
|
115 | - ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
116 | - ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType))); |
|
117 | - |
|
118 | - if ($limit) { |
|
119 | - if (\count($tagIds) !== 1) { |
|
120 | - throw new \InvalidArgumentException('Limit is only allowed with a single tag'); |
|
121 | - } |
|
122 | - |
|
123 | - $query->setMaxResults($limit) |
|
124 | - ->orderBy('objectid', 'ASC'); |
|
125 | - |
|
126 | - if ($offset !== '') { |
|
127 | - $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset))); |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - $objectIds = []; |
|
132 | - |
|
133 | - $result = $query->execute(); |
|
134 | - while ($row = $result->fetch()) { |
|
135 | - $objectIds[] = $row['objectid']; |
|
136 | - } |
|
137 | - |
|
138 | - return $objectIds; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * {@inheritdoc} |
|
143 | - */ |
|
144 | - public function assignTags(string $objId, string $objectType, $tagIds) { |
|
145 | - if (!\is_array($tagIds)) { |
|
146 | - $tagIds = [$tagIds]; |
|
147 | - } |
|
148 | - |
|
149 | - $this->assertTagsExist($tagIds); |
|
150 | - |
|
151 | - $query = $this->connection->getQueryBuilder(); |
|
152 | - $query->insert(self::RELATION_TABLE) |
|
153 | - ->values([ |
|
154 | - 'objectid' => $query->createNamedParameter($objId), |
|
155 | - 'objecttype' => $query->createNamedParameter($objectType), |
|
156 | - 'systemtagid' => $query->createParameter('tagid'), |
|
157 | - ]); |
|
158 | - |
|
159 | - $tagsAssigned = []; |
|
160 | - foreach ($tagIds as $tagId) { |
|
161 | - try { |
|
162 | - $query->setParameter('tagid', $tagId); |
|
163 | - $query->execute(); |
|
164 | - $tagsAssigned[] = $tagId; |
|
165 | - } catch (UniqueConstraintViolationException $e) { |
|
166 | - // ignore existing relations |
|
167 | - } |
|
168 | - } |
|
169 | - |
|
170 | - if (empty($tagsAssigned)) { |
|
171 | - return; |
|
172 | - } |
|
173 | - |
|
174 | - $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent( |
|
175 | - MapperEvent::EVENT_ASSIGN, |
|
176 | - $objectType, |
|
177 | - $objId, |
|
178 | - $tagsAssigned |
|
179 | - )); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * {@inheritdoc} |
|
184 | - */ |
|
185 | - public function unassignTags(string $objId, string $objectType, $tagIds) { |
|
186 | - if (!\is_array($tagIds)) { |
|
187 | - $tagIds = [$tagIds]; |
|
188 | - } |
|
189 | - |
|
190 | - $this->assertTagsExist($tagIds); |
|
191 | - |
|
192 | - $query = $this->connection->getQueryBuilder(); |
|
193 | - $query->delete(self::RELATION_TABLE) |
|
194 | - ->where($query->expr()->eq('objectid', $query->createParameter('objectid'))) |
|
195 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
196 | - ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
197 | - ->setParameter('objectid', $objId) |
|
198 | - ->setParameter('objecttype', $objectType) |
|
199 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
200 | - ->execute(); |
|
201 | - |
|
202 | - $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent( |
|
203 | - MapperEvent::EVENT_UNASSIGN, |
|
204 | - $objectType, |
|
205 | - $objId, |
|
206 | - $tagIds |
|
207 | - )); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * {@inheritdoc} |
|
212 | - */ |
|
213 | - public function haveTag($objIds, string $objectType, string $tagId, bool $all = true): bool { |
|
214 | - $this->assertTagsExist([$tagId]); |
|
215 | - |
|
216 | - if (!\is_array($objIds)) { |
|
217 | - $objIds = [$objIds]; |
|
218 | - } |
|
219 | - |
|
220 | - $query = $this->connection->getQueryBuilder(); |
|
221 | - |
|
222 | - if (!$all) { |
|
223 | - // If we only need one entry, we make the query lighter, by not |
|
224 | - // counting the elements |
|
225 | - $query->select('*') |
|
226 | - ->setMaxResults(1); |
|
227 | - } else { |
|
228 | - $query->select($query->func()->count($query->expr()->literal(1))); |
|
229 | - } |
|
230 | - |
|
231 | - $query->from(self::RELATION_TABLE) |
|
232 | - ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
233 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
234 | - ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid'))) |
|
235 | - ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
236 | - ->setParameter('tagid', $tagId) |
|
237 | - ->setParameter('objecttype', $objectType); |
|
238 | - |
|
239 | - $result = $query->execute(); |
|
240 | - $row = $result->fetch(\PDO::FETCH_NUM); |
|
241 | - $result->closeCursor(); |
|
242 | - |
|
243 | - if ($all) { |
|
244 | - return ((int)$row[0] === \count($objIds)); |
|
245 | - } |
|
246 | - |
|
247 | - return (bool) $row; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Asserts that all the given tag ids exist. |
|
252 | - * |
|
253 | - * @param string[] $tagIds tag ids to check |
|
254 | - * |
|
255 | - * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist |
|
256 | - */ |
|
257 | - private function assertTagsExist($tagIds) { |
|
258 | - $tags = $this->tagManager->getTagsByIds($tagIds); |
|
259 | - if (\count($tags) !== \count($tagIds)) { |
|
260 | - // at least one tag missing, bail out |
|
261 | - $foundTagIds = array_map( |
|
262 | - function (ISystemTag $tag) { |
|
263 | - return $tag->getId(); |
|
264 | - }, |
|
265 | - $tags |
|
266 | - ); |
|
267 | - $missingTagIds = array_diff($tagIds, $foundTagIds); |
|
268 | - throw new TagNotFoundException( |
|
269 | - 'Tags not found', 0, null, $missingTagIds |
|
270 | - ); |
|
271 | - } |
|
272 | - } |
|
42 | + public const RELATION_TABLE = 'systemtag_object_mapping'; |
|
43 | + |
|
44 | + /** @var ISystemTagManager */ |
|
45 | + protected $tagManager; |
|
46 | + |
|
47 | + /** @var IDBConnection */ |
|
48 | + protected $connection; |
|
49 | + |
|
50 | + /** @var EventDispatcherInterface */ |
|
51 | + protected $dispatcher; |
|
52 | + |
|
53 | + /** |
|
54 | + * Constructor. |
|
55 | + * |
|
56 | + * @param IDBConnection $connection database connection |
|
57 | + * @param ISystemTagManager $tagManager system tag manager |
|
58 | + * @param EventDispatcherInterface $dispatcher |
|
59 | + */ |
|
60 | + public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) { |
|
61 | + $this->connection = $connection; |
|
62 | + $this->tagManager = $tagManager; |
|
63 | + $this->dispatcher = $dispatcher; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + public function getTagIdsForObjects($objIds, string $objectType): array { |
|
70 | + if (!\is_array($objIds)) { |
|
71 | + $objIds = [$objIds]; |
|
72 | + } elseif (empty($objIds)) { |
|
73 | + return []; |
|
74 | + } |
|
75 | + |
|
76 | + $query = $this->connection->getQueryBuilder(); |
|
77 | + $query->select(['systemtagid', 'objectid']) |
|
78 | + ->from(self::RELATION_TABLE) |
|
79 | + ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
80 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
81 | + ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
82 | + ->setParameter('objecttype', $objectType) |
|
83 | + ->addOrderBy('objectid', 'ASC') |
|
84 | + ->addOrderBy('systemtagid', 'ASC'); |
|
85 | + |
|
86 | + $mapping = []; |
|
87 | + foreach ($objIds as $objId) { |
|
88 | + $mapping[$objId] = []; |
|
89 | + } |
|
90 | + |
|
91 | + $result = $query->execute(); |
|
92 | + while ($row = $result->fetch()) { |
|
93 | + $objectId = $row['objectid']; |
|
94 | + $mapping[$objectId][] = $row['systemtagid']; |
|
95 | + } |
|
96 | + |
|
97 | + $result->closeCursor(); |
|
98 | + |
|
99 | + return $mapping; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * {@inheritdoc} |
|
104 | + */ |
|
105 | + public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0, string $offset = ''): array { |
|
106 | + if (!\is_array($tagIds)) { |
|
107 | + $tagIds = [$tagIds]; |
|
108 | + } |
|
109 | + |
|
110 | + $this->assertTagsExist($tagIds); |
|
111 | + |
|
112 | + $query = $this->connection->getQueryBuilder(); |
|
113 | + $query->selectDistinct('objectid') |
|
114 | + ->from(self::RELATION_TABLE) |
|
115 | + ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
116 | + ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType))); |
|
117 | + |
|
118 | + if ($limit) { |
|
119 | + if (\count($tagIds) !== 1) { |
|
120 | + throw new \InvalidArgumentException('Limit is only allowed with a single tag'); |
|
121 | + } |
|
122 | + |
|
123 | + $query->setMaxResults($limit) |
|
124 | + ->orderBy('objectid', 'ASC'); |
|
125 | + |
|
126 | + if ($offset !== '') { |
|
127 | + $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset))); |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + $objectIds = []; |
|
132 | + |
|
133 | + $result = $query->execute(); |
|
134 | + while ($row = $result->fetch()) { |
|
135 | + $objectIds[] = $row['objectid']; |
|
136 | + } |
|
137 | + |
|
138 | + return $objectIds; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * {@inheritdoc} |
|
143 | + */ |
|
144 | + public function assignTags(string $objId, string $objectType, $tagIds) { |
|
145 | + if (!\is_array($tagIds)) { |
|
146 | + $tagIds = [$tagIds]; |
|
147 | + } |
|
148 | + |
|
149 | + $this->assertTagsExist($tagIds); |
|
150 | + |
|
151 | + $query = $this->connection->getQueryBuilder(); |
|
152 | + $query->insert(self::RELATION_TABLE) |
|
153 | + ->values([ |
|
154 | + 'objectid' => $query->createNamedParameter($objId), |
|
155 | + 'objecttype' => $query->createNamedParameter($objectType), |
|
156 | + 'systemtagid' => $query->createParameter('tagid'), |
|
157 | + ]); |
|
158 | + |
|
159 | + $tagsAssigned = []; |
|
160 | + foreach ($tagIds as $tagId) { |
|
161 | + try { |
|
162 | + $query->setParameter('tagid', $tagId); |
|
163 | + $query->execute(); |
|
164 | + $tagsAssigned[] = $tagId; |
|
165 | + } catch (UniqueConstraintViolationException $e) { |
|
166 | + // ignore existing relations |
|
167 | + } |
|
168 | + } |
|
169 | + |
|
170 | + if (empty($tagsAssigned)) { |
|
171 | + return; |
|
172 | + } |
|
173 | + |
|
174 | + $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent( |
|
175 | + MapperEvent::EVENT_ASSIGN, |
|
176 | + $objectType, |
|
177 | + $objId, |
|
178 | + $tagsAssigned |
|
179 | + )); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * {@inheritdoc} |
|
184 | + */ |
|
185 | + public function unassignTags(string $objId, string $objectType, $tagIds) { |
|
186 | + if (!\is_array($tagIds)) { |
|
187 | + $tagIds = [$tagIds]; |
|
188 | + } |
|
189 | + |
|
190 | + $this->assertTagsExist($tagIds); |
|
191 | + |
|
192 | + $query = $this->connection->getQueryBuilder(); |
|
193 | + $query->delete(self::RELATION_TABLE) |
|
194 | + ->where($query->expr()->eq('objectid', $query->createParameter('objectid'))) |
|
195 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
196 | + ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
197 | + ->setParameter('objectid', $objId) |
|
198 | + ->setParameter('objecttype', $objectType) |
|
199 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
200 | + ->execute(); |
|
201 | + |
|
202 | + $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent( |
|
203 | + MapperEvent::EVENT_UNASSIGN, |
|
204 | + $objectType, |
|
205 | + $objId, |
|
206 | + $tagIds |
|
207 | + )); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * {@inheritdoc} |
|
212 | + */ |
|
213 | + public function haveTag($objIds, string $objectType, string $tagId, bool $all = true): bool { |
|
214 | + $this->assertTagsExist([$tagId]); |
|
215 | + |
|
216 | + if (!\is_array($objIds)) { |
|
217 | + $objIds = [$objIds]; |
|
218 | + } |
|
219 | + |
|
220 | + $query = $this->connection->getQueryBuilder(); |
|
221 | + |
|
222 | + if (!$all) { |
|
223 | + // If we only need one entry, we make the query lighter, by not |
|
224 | + // counting the elements |
|
225 | + $query->select('*') |
|
226 | + ->setMaxResults(1); |
|
227 | + } else { |
|
228 | + $query->select($query->func()->count($query->expr()->literal(1))); |
|
229 | + } |
|
230 | + |
|
231 | + $query->from(self::RELATION_TABLE) |
|
232 | + ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
233 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
234 | + ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid'))) |
|
235 | + ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
236 | + ->setParameter('tagid', $tagId) |
|
237 | + ->setParameter('objecttype', $objectType); |
|
238 | + |
|
239 | + $result = $query->execute(); |
|
240 | + $row = $result->fetch(\PDO::FETCH_NUM); |
|
241 | + $result->closeCursor(); |
|
242 | + |
|
243 | + if ($all) { |
|
244 | + return ((int)$row[0] === \count($objIds)); |
|
245 | + } |
|
246 | + |
|
247 | + return (bool) $row; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Asserts that all the given tag ids exist. |
|
252 | + * |
|
253 | + * @param string[] $tagIds tag ids to check |
|
254 | + * |
|
255 | + * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist |
|
256 | + */ |
|
257 | + private function assertTagsExist($tagIds) { |
|
258 | + $tags = $this->tagManager->getTagsByIds($tagIds); |
|
259 | + if (\count($tags) !== \count($tagIds)) { |
|
260 | + // at least one tag missing, bail out |
|
261 | + $foundTagIds = array_map( |
|
262 | + function (ISystemTag $tag) { |
|
263 | + return $tag->getId(); |
|
264 | + }, |
|
265 | + $tags |
|
266 | + ); |
|
267 | + $missingTagIds = array_diff($tagIds, $foundTagIds); |
|
268 | + throw new TagNotFoundException( |
|
269 | + 'Tags not found', 0, null, $missingTagIds |
|
270 | + ); |
|
271 | + } |
|
272 | + } |
|
273 | 273 | } |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | * @since 16.0.0 |
127 | 127 | */ |
128 | 128 | public function addResource(IResource $resource): void { |
129 | - array_map(function (IResource $r) use ($resource) { |
|
129 | + array_map(function(IResource $r) use ($resource) { |
|
130 | 130 | if ($this->isSameResource($r, $resource)) { |
131 | 131 | throw new ResourceException('Already part of the collection'); |
132 | 132 | } |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | * @since 16.0.0 |
159 | 159 | */ |
160 | 160 | public function removeResource(IResource $resource): void { |
161 | - $this->resources = array_filter($this->getResources(), function (IResource $r) use ($resource) { |
|
161 | + $this->resources = array_filter($this->getResources(), function(IResource $r) use ($resource) { |
|
162 | 162 | return !$this->isSameResource($r, $resource); |
163 | 163 | }); |
164 | 164 |
@@ -37,194 +37,194 @@ |
||
37 | 37 | use OCP\IUser; |
38 | 38 | |
39 | 39 | class Collection implements ICollection { |
40 | - /** @var IManager|Manager */ |
|
41 | - protected $manager; |
|
42 | - |
|
43 | - /** @var IDBConnection */ |
|
44 | - protected $connection; |
|
45 | - |
|
46 | - /** @var int */ |
|
47 | - protected $id; |
|
48 | - |
|
49 | - /** @var string */ |
|
50 | - protected $name; |
|
51 | - |
|
52 | - /** @var IUser|null */ |
|
53 | - protected $userForAccess; |
|
54 | - |
|
55 | - /** @var bool|null */ |
|
56 | - protected $access; |
|
57 | - |
|
58 | - /** @var IResource[] */ |
|
59 | - protected $resources; |
|
60 | - |
|
61 | - public function __construct( |
|
62 | - IManager $manager, |
|
63 | - IDBConnection $connection, |
|
64 | - int $id, |
|
65 | - string $name, |
|
66 | - ?IUser $userForAccess = null, |
|
67 | - ?bool $access = null |
|
68 | - ) { |
|
69 | - $this->manager = $manager; |
|
70 | - $this->connection = $connection; |
|
71 | - $this->id = $id; |
|
72 | - $this->name = $name; |
|
73 | - $this->userForAccess = $userForAccess; |
|
74 | - $this->access = $access; |
|
75 | - $this->resources = []; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @return int |
|
80 | - * @since 16.0.0 |
|
81 | - */ |
|
82 | - public function getId(): int { |
|
83 | - return $this->id; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @return string |
|
88 | - * @since 16.0.0 |
|
89 | - */ |
|
90 | - public function getName(): string { |
|
91 | - return $this->name; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string $name |
|
96 | - * @since 16.0.0 |
|
97 | - */ |
|
98 | - public function setName(string $name): void { |
|
99 | - $query = $this->connection->getQueryBuilder(); |
|
100 | - $query->update(Manager::TABLE_COLLECTIONS) |
|
101 | - ->set('name', $query->createNamedParameter($name)) |
|
102 | - ->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))); |
|
103 | - $query->execute(); |
|
104 | - |
|
105 | - $this->name = $name; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @return IResource[] |
|
110 | - * @since 16.0.0 |
|
111 | - */ |
|
112 | - public function getResources(): array { |
|
113 | - if (empty($this->resources)) { |
|
114 | - $this->resources = $this->manager->getResourcesByCollectionForUser($this, $this->userForAccess); |
|
115 | - } |
|
116 | - |
|
117 | - return $this->resources; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Adds a resource to a collection |
|
122 | - * |
|
123 | - * @param IResource $resource |
|
124 | - * @throws ResourceException when the resource is already part of the collection |
|
125 | - * @since 16.0.0 |
|
126 | - */ |
|
127 | - public function addResource(IResource $resource): void { |
|
128 | - array_map(function (IResource $r) use ($resource) { |
|
129 | - if ($this->isSameResource($r, $resource)) { |
|
130 | - throw new ResourceException('Already part of the collection'); |
|
131 | - } |
|
132 | - }, $this->getResources()); |
|
133 | - |
|
134 | - $this->resources[] = $resource; |
|
135 | - |
|
136 | - $query = $this->connection->getQueryBuilder(); |
|
137 | - $query->insert(Manager::TABLE_RESOURCES) |
|
138 | - ->values([ |
|
139 | - 'collection_id' => $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT), |
|
140 | - 'resource_type' => $query->createNamedParameter($resource->getType()), |
|
141 | - 'resource_id' => $query->createNamedParameter($resource->getId()), |
|
142 | - ]); |
|
143 | - |
|
144 | - try { |
|
145 | - $query->execute(); |
|
146 | - } catch (ConstraintViolationException $e) { |
|
147 | - throw new ResourceException('Already part of the collection'); |
|
148 | - } |
|
149 | - |
|
150 | - $this->manager->invalidateAccessCacheForCollection($this); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Removes a resource from a collection |
|
155 | - * |
|
156 | - * @param IResource $resource |
|
157 | - * @since 16.0.0 |
|
158 | - */ |
|
159 | - public function removeResource(IResource $resource): void { |
|
160 | - $this->resources = array_filter($this->getResources(), function (IResource $r) use ($resource) { |
|
161 | - return !$this->isSameResource($r, $resource); |
|
162 | - }); |
|
163 | - |
|
164 | - $query = $this->connection->getQueryBuilder(); |
|
165 | - $query->delete(Manager::TABLE_RESOURCES) |
|
166 | - ->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))) |
|
167 | - ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType()))) |
|
168 | - ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))); |
|
169 | - $query->execute(); |
|
170 | - |
|
171 | - if (empty($this->resources)) { |
|
172 | - $this->removeCollection(); |
|
173 | - } else { |
|
174 | - $this->manager->invalidateAccessCacheForCollection($this); |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Can a user/guest access the collection |
|
180 | - * |
|
181 | - * @param IUser|null $user |
|
182 | - * @return bool |
|
183 | - * @since 16.0.0 |
|
184 | - */ |
|
185 | - public function canAccess(?IUser $user): bool { |
|
186 | - if ($user instanceof IUser) { |
|
187 | - return $this->canUserAccess($user); |
|
188 | - } |
|
189 | - return $this->canGuestAccess(); |
|
190 | - } |
|
191 | - |
|
192 | - protected function canUserAccess(IUser $user): bool { |
|
193 | - if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
194 | - return $this->access; |
|
195 | - } |
|
196 | - |
|
197 | - $access = $this->manager->canAccessCollection($this, $user); |
|
198 | - if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
199 | - $this->access = $access; |
|
200 | - } |
|
201 | - return $access; |
|
202 | - } |
|
203 | - |
|
204 | - protected function canGuestAccess(): bool { |
|
205 | - if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { |
|
206 | - return $this->access; |
|
207 | - } |
|
208 | - |
|
209 | - $access = $this->manager->canAccessCollection($this, null); |
|
210 | - if (!$this->userForAccess instanceof IUser) { |
|
211 | - $this->access = $access; |
|
212 | - } |
|
213 | - return $access; |
|
214 | - } |
|
215 | - |
|
216 | - protected function isSameResource(IResource $resource1, IResource $resource2): bool { |
|
217 | - return $resource1->getType() === $resource2->getType() && |
|
218 | - $resource1->getId() === $resource2->getId(); |
|
219 | - } |
|
220 | - |
|
221 | - protected function removeCollection(): void { |
|
222 | - $query = $this->connection->getQueryBuilder(); |
|
223 | - $query->delete(Manager::TABLE_COLLECTIONS) |
|
224 | - ->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))); |
|
225 | - $query->execute(); |
|
226 | - |
|
227 | - $this->manager->invalidateAccessCacheForCollection($this); |
|
228 | - $this->id = 0; |
|
229 | - } |
|
40 | + /** @var IManager|Manager */ |
|
41 | + protected $manager; |
|
42 | + |
|
43 | + /** @var IDBConnection */ |
|
44 | + protected $connection; |
|
45 | + |
|
46 | + /** @var int */ |
|
47 | + protected $id; |
|
48 | + |
|
49 | + /** @var string */ |
|
50 | + protected $name; |
|
51 | + |
|
52 | + /** @var IUser|null */ |
|
53 | + protected $userForAccess; |
|
54 | + |
|
55 | + /** @var bool|null */ |
|
56 | + protected $access; |
|
57 | + |
|
58 | + /** @var IResource[] */ |
|
59 | + protected $resources; |
|
60 | + |
|
61 | + public function __construct( |
|
62 | + IManager $manager, |
|
63 | + IDBConnection $connection, |
|
64 | + int $id, |
|
65 | + string $name, |
|
66 | + ?IUser $userForAccess = null, |
|
67 | + ?bool $access = null |
|
68 | + ) { |
|
69 | + $this->manager = $manager; |
|
70 | + $this->connection = $connection; |
|
71 | + $this->id = $id; |
|
72 | + $this->name = $name; |
|
73 | + $this->userForAccess = $userForAccess; |
|
74 | + $this->access = $access; |
|
75 | + $this->resources = []; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @return int |
|
80 | + * @since 16.0.0 |
|
81 | + */ |
|
82 | + public function getId(): int { |
|
83 | + return $this->id; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @return string |
|
88 | + * @since 16.0.0 |
|
89 | + */ |
|
90 | + public function getName(): string { |
|
91 | + return $this->name; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string $name |
|
96 | + * @since 16.0.0 |
|
97 | + */ |
|
98 | + public function setName(string $name): void { |
|
99 | + $query = $this->connection->getQueryBuilder(); |
|
100 | + $query->update(Manager::TABLE_COLLECTIONS) |
|
101 | + ->set('name', $query->createNamedParameter($name)) |
|
102 | + ->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))); |
|
103 | + $query->execute(); |
|
104 | + |
|
105 | + $this->name = $name; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @return IResource[] |
|
110 | + * @since 16.0.0 |
|
111 | + */ |
|
112 | + public function getResources(): array { |
|
113 | + if (empty($this->resources)) { |
|
114 | + $this->resources = $this->manager->getResourcesByCollectionForUser($this, $this->userForAccess); |
|
115 | + } |
|
116 | + |
|
117 | + return $this->resources; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Adds a resource to a collection |
|
122 | + * |
|
123 | + * @param IResource $resource |
|
124 | + * @throws ResourceException when the resource is already part of the collection |
|
125 | + * @since 16.0.0 |
|
126 | + */ |
|
127 | + public function addResource(IResource $resource): void { |
|
128 | + array_map(function (IResource $r) use ($resource) { |
|
129 | + if ($this->isSameResource($r, $resource)) { |
|
130 | + throw new ResourceException('Already part of the collection'); |
|
131 | + } |
|
132 | + }, $this->getResources()); |
|
133 | + |
|
134 | + $this->resources[] = $resource; |
|
135 | + |
|
136 | + $query = $this->connection->getQueryBuilder(); |
|
137 | + $query->insert(Manager::TABLE_RESOURCES) |
|
138 | + ->values([ |
|
139 | + 'collection_id' => $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT), |
|
140 | + 'resource_type' => $query->createNamedParameter($resource->getType()), |
|
141 | + 'resource_id' => $query->createNamedParameter($resource->getId()), |
|
142 | + ]); |
|
143 | + |
|
144 | + try { |
|
145 | + $query->execute(); |
|
146 | + } catch (ConstraintViolationException $e) { |
|
147 | + throw new ResourceException('Already part of the collection'); |
|
148 | + } |
|
149 | + |
|
150 | + $this->manager->invalidateAccessCacheForCollection($this); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Removes a resource from a collection |
|
155 | + * |
|
156 | + * @param IResource $resource |
|
157 | + * @since 16.0.0 |
|
158 | + */ |
|
159 | + public function removeResource(IResource $resource): void { |
|
160 | + $this->resources = array_filter($this->getResources(), function (IResource $r) use ($resource) { |
|
161 | + return !$this->isSameResource($r, $resource); |
|
162 | + }); |
|
163 | + |
|
164 | + $query = $this->connection->getQueryBuilder(); |
|
165 | + $query->delete(Manager::TABLE_RESOURCES) |
|
166 | + ->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))) |
|
167 | + ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType()))) |
|
168 | + ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))); |
|
169 | + $query->execute(); |
|
170 | + |
|
171 | + if (empty($this->resources)) { |
|
172 | + $this->removeCollection(); |
|
173 | + } else { |
|
174 | + $this->manager->invalidateAccessCacheForCollection($this); |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Can a user/guest access the collection |
|
180 | + * |
|
181 | + * @param IUser|null $user |
|
182 | + * @return bool |
|
183 | + * @since 16.0.0 |
|
184 | + */ |
|
185 | + public function canAccess(?IUser $user): bool { |
|
186 | + if ($user instanceof IUser) { |
|
187 | + return $this->canUserAccess($user); |
|
188 | + } |
|
189 | + return $this->canGuestAccess(); |
|
190 | + } |
|
191 | + |
|
192 | + protected function canUserAccess(IUser $user): bool { |
|
193 | + if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
194 | + return $this->access; |
|
195 | + } |
|
196 | + |
|
197 | + $access = $this->manager->canAccessCollection($this, $user); |
|
198 | + if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
199 | + $this->access = $access; |
|
200 | + } |
|
201 | + return $access; |
|
202 | + } |
|
203 | + |
|
204 | + protected function canGuestAccess(): bool { |
|
205 | + if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { |
|
206 | + return $this->access; |
|
207 | + } |
|
208 | + |
|
209 | + $access = $this->manager->canAccessCollection($this, null); |
|
210 | + if (!$this->userForAccess instanceof IUser) { |
|
211 | + $this->access = $access; |
|
212 | + } |
|
213 | + return $access; |
|
214 | + } |
|
215 | + |
|
216 | + protected function isSameResource(IResource $resource1, IResource $resource2): bool { |
|
217 | + return $resource1->getType() === $resource2->getType() && |
|
218 | + $resource1->getId() === $resource2->getId(); |
|
219 | + } |
|
220 | + |
|
221 | + protected function removeCollection(): void { |
|
222 | + $query = $this->connection->getQueryBuilder(); |
|
223 | + $query->delete(Manager::TABLE_COLLECTIONS) |
|
224 | + ->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))); |
|
225 | + $query->execute(); |
|
226 | + |
|
227 | + $this->manager->invalidateAccessCacheForCollection($this); |
|
228 | + $this->id = 0; |
|
229 | + } |
|
230 | 230 | } |