@@ -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 | } |
@@ -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 |
@@ -38,237 +38,237 @@ |
||
38 | 38 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
39 | 39 | |
40 | 40 | class SystemTagObjectMapper implements ISystemTagObjectMapper { |
41 | - public const RELATION_TABLE = 'systemtag_object_mapping'; |
|
42 | - |
|
43 | - /** @var ISystemTagManager */ |
|
44 | - protected $tagManager; |
|
45 | - |
|
46 | - /** @var IDBConnection */ |
|
47 | - protected $connection; |
|
48 | - |
|
49 | - /** @var EventDispatcherInterface */ |
|
50 | - protected $dispatcher; |
|
51 | - |
|
52 | - /** |
|
53 | - * Constructor. |
|
54 | - * |
|
55 | - * @param IDBConnection $connection database connection |
|
56 | - * @param ISystemTagManager $tagManager system tag manager |
|
57 | - * @param EventDispatcherInterface $dispatcher |
|
58 | - */ |
|
59 | - public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) { |
|
60 | - $this->connection = $connection; |
|
61 | - $this->tagManager = $tagManager; |
|
62 | - $this->dispatcher = $dispatcher; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * {@inheritdoc} |
|
67 | - */ |
|
68 | - public function getTagIdsForObjects($objIds, string $objectType): array { |
|
69 | - if (!\is_array($objIds)) { |
|
70 | - $objIds = [$objIds]; |
|
71 | - } elseif (empty($objIds)) { |
|
72 | - return []; |
|
73 | - } |
|
74 | - |
|
75 | - $query = $this->connection->getQueryBuilder(); |
|
76 | - $query->select(['systemtagid', 'objectid']) |
|
77 | - ->from(self::RELATION_TABLE) |
|
78 | - ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
79 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
80 | - ->setParameter('objecttype', $objectType) |
|
81 | - ->addOrderBy('objectid', 'ASC') |
|
82 | - ->addOrderBy('systemtagid', 'ASC'); |
|
83 | - $chunks = array_chunk($objIds, 900, false); |
|
84 | - $mapping = []; |
|
85 | - foreach ($objIds as $objId) { |
|
86 | - $mapping[$objId] = []; |
|
87 | - } |
|
88 | - foreach ($chunks as $chunk) { |
|
89 | - $query->setParameter('objectids', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
|
90 | - $result = $query->executeQuery(); |
|
91 | - while ($row = $result->fetch()) { |
|
92 | - $objectId = $row['objectid']; |
|
93 | - $mapping[$objectId][] = $row['systemtagid']; |
|
94 | - } |
|
95 | - |
|
96 | - $result->closeCursor(); |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - return $mapping; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * {@inheritdoc} |
|
105 | - */ |
|
106 | - public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0, string $offset = ''): array { |
|
107 | - if (!\is_array($tagIds)) { |
|
108 | - $tagIds = [$tagIds]; |
|
109 | - } |
|
110 | - |
|
111 | - $this->assertTagsExist($tagIds); |
|
112 | - |
|
113 | - $query = $this->connection->getQueryBuilder(); |
|
114 | - $query->selectDistinct('objectid') |
|
115 | - ->from(self::RELATION_TABLE) |
|
116 | - ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
117 | - ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType))); |
|
118 | - |
|
119 | - if ($limit) { |
|
120 | - if (\count($tagIds) !== 1) { |
|
121 | - throw new \InvalidArgumentException('Limit is only allowed with a single tag'); |
|
122 | - } |
|
123 | - |
|
124 | - $query->setMaxResults($limit) |
|
125 | - ->orderBy('objectid', 'ASC'); |
|
126 | - |
|
127 | - if ($offset !== '') { |
|
128 | - $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset))); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - $objectIds = []; |
|
133 | - |
|
134 | - $result = $query->execute(); |
|
135 | - while ($row = $result->fetch()) { |
|
136 | - $objectIds[] = $row['objectid']; |
|
137 | - } |
|
138 | - |
|
139 | - return $objectIds; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * {@inheritdoc} |
|
144 | - */ |
|
145 | - public function assignTags(string $objId, string $objectType, $tagIds) { |
|
146 | - if (!\is_array($tagIds)) { |
|
147 | - $tagIds = [$tagIds]; |
|
148 | - } |
|
149 | - |
|
150 | - $this->assertTagsExist($tagIds); |
|
151 | - |
|
152 | - $query = $this->connection->getQueryBuilder(); |
|
153 | - $query->insert(self::RELATION_TABLE) |
|
154 | - ->values([ |
|
155 | - 'objectid' => $query->createNamedParameter($objId), |
|
156 | - 'objecttype' => $query->createNamedParameter($objectType), |
|
157 | - 'systemtagid' => $query->createParameter('tagid'), |
|
158 | - ]); |
|
159 | - |
|
160 | - $tagsAssigned = []; |
|
161 | - foreach ($tagIds as $tagId) { |
|
162 | - try { |
|
163 | - $query->setParameter('tagid', $tagId); |
|
164 | - $query->execute(); |
|
165 | - $tagsAssigned[] = $tagId; |
|
166 | - } catch (UniqueConstraintViolationException $e) { |
|
167 | - // ignore existing relations |
|
168 | - } |
|
169 | - } |
|
170 | - |
|
171 | - if (empty($tagsAssigned)) { |
|
172 | - return; |
|
173 | - } |
|
174 | - |
|
175 | - $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent( |
|
176 | - MapperEvent::EVENT_ASSIGN, |
|
177 | - $objectType, |
|
178 | - $objId, |
|
179 | - $tagsAssigned |
|
180 | - )); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * {@inheritdoc} |
|
185 | - */ |
|
186 | - public function unassignTags(string $objId, string $objectType, $tagIds) { |
|
187 | - if (!\is_array($tagIds)) { |
|
188 | - $tagIds = [$tagIds]; |
|
189 | - } |
|
190 | - |
|
191 | - $this->assertTagsExist($tagIds); |
|
192 | - |
|
193 | - $query = $this->connection->getQueryBuilder(); |
|
194 | - $query->delete(self::RELATION_TABLE) |
|
195 | - ->where($query->expr()->eq('objectid', $query->createParameter('objectid'))) |
|
196 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
197 | - ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
198 | - ->setParameter('objectid', $objId) |
|
199 | - ->setParameter('objecttype', $objectType) |
|
200 | - ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
201 | - ->execute(); |
|
202 | - |
|
203 | - $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent( |
|
204 | - MapperEvent::EVENT_UNASSIGN, |
|
205 | - $objectType, |
|
206 | - $objId, |
|
207 | - $tagIds |
|
208 | - )); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * {@inheritdoc} |
|
213 | - */ |
|
214 | - public function haveTag($objIds, string $objectType, string $tagId, bool $all = true): bool { |
|
215 | - $this->assertTagsExist([$tagId]); |
|
216 | - |
|
217 | - if (!\is_array($objIds)) { |
|
218 | - $objIds = [$objIds]; |
|
219 | - } |
|
220 | - |
|
221 | - $query = $this->connection->getQueryBuilder(); |
|
222 | - |
|
223 | - if (!$all) { |
|
224 | - // If we only need one entry, we make the query lighter, by not |
|
225 | - // counting the elements |
|
226 | - $query->select('*') |
|
227 | - ->setMaxResults(1); |
|
228 | - } else { |
|
229 | - $query->select($query->func()->count($query->expr()->literal(1))); |
|
230 | - } |
|
231 | - |
|
232 | - $query->from(self::RELATION_TABLE) |
|
233 | - ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
234 | - ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
235 | - ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid'))) |
|
236 | - ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
237 | - ->setParameter('tagid', $tagId) |
|
238 | - ->setParameter('objecttype', $objectType); |
|
239 | - |
|
240 | - $result = $query->execute(); |
|
241 | - $row = $result->fetch(\PDO::FETCH_NUM); |
|
242 | - $result->closeCursor(); |
|
243 | - |
|
244 | - if ($all) { |
|
245 | - return ((int)$row[0] === \count($objIds)); |
|
246 | - } |
|
247 | - |
|
248 | - return (bool) $row; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Asserts that all the given tag ids exist. |
|
253 | - * |
|
254 | - * @param string[] $tagIds tag ids to check |
|
255 | - * |
|
256 | - * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist |
|
257 | - */ |
|
258 | - private function assertTagsExist($tagIds) { |
|
259 | - $tags = $this->tagManager->getTagsByIds($tagIds); |
|
260 | - if (\count($tags) !== \count($tagIds)) { |
|
261 | - // at least one tag missing, bail out |
|
262 | - $foundTagIds = array_map( |
|
263 | - function (ISystemTag $tag) { |
|
264 | - return $tag->getId(); |
|
265 | - }, |
|
266 | - $tags |
|
267 | - ); |
|
268 | - $missingTagIds = array_diff($tagIds, $foundTagIds); |
|
269 | - throw new TagNotFoundException( |
|
270 | - 'Tags not found', 0, null, $missingTagIds |
|
271 | - ); |
|
272 | - } |
|
273 | - } |
|
41 | + public const RELATION_TABLE = 'systemtag_object_mapping'; |
|
42 | + |
|
43 | + /** @var ISystemTagManager */ |
|
44 | + protected $tagManager; |
|
45 | + |
|
46 | + /** @var IDBConnection */ |
|
47 | + protected $connection; |
|
48 | + |
|
49 | + /** @var EventDispatcherInterface */ |
|
50 | + protected $dispatcher; |
|
51 | + |
|
52 | + /** |
|
53 | + * Constructor. |
|
54 | + * |
|
55 | + * @param IDBConnection $connection database connection |
|
56 | + * @param ISystemTagManager $tagManager system tag manager |
|
57 | + * @param EventDispatcherInterface $dispatcher |
|
58 | + */ |
|
59 | + public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) { |
|
60 | + $this->connection = $connection; |
|
61 | + $this->tagManager = $tagManager; |
|
62 | + $this->dispatcher = $dispatcher; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * {@inheritdoc} |
|
67 | + */ |
|
68 | + public function getTagIdsForObjects($objIds, string $objectType): array { |
|
69 | + if (!\is_array($objIds)) { |
|
70 | + $objIds = [$objIds]; |
|
71 | + } elseif (empty($objIds)) { |
|
72 | + return []; |
|
73 | + } |
|
74 | + |
|
75 | + $query = $this->connection->getQueryBuilder(); |
|
76 | + $query->select(['systemtagid', 'objectid']) |
|
77 | + ->from(self::RELATION_TABLE) |
|
78 | + ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
79 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
80 | + ->setParameter('objecttype', $objectType) |
|
81 | + ->addOrderBy('objectid', 'ASC') |
|
82 | + ->addOrderBy('systemtagid', 'ASC'); |
|
83 | + $chunks = array_chunk($objIds, 900, false); |
|
84 | + $mapping = []; |
|
85 | + foreach ($objIds as $objId) { |
|
86 | + $mapping[$objId] = []; |
|
87 | + } |
|
88 | + foreach ($chunks as $chunk) { |
|
89 | + $query->setParameter('objectids', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
|
90 | + $result = $query->executeQuery(); |
|
91 | + while ($row = $result->fetch()) { |
|
92 | + $objectId = $row['objectid']; |
|
93 | + $mapping[$objectId][] = $row['systemtagid']; |
|
94 | + } |
|
95 | + |
|
96 | + $result->closeCursor(); |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + return $mapping; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * {@inheritdoc} |
|
105 | + */ |
|
106 | + public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0, string $offset = ''): array { |
|
107 | + if (!\is_array($tagIds)) { |
|
108 | + $tagIds = [$tagIds]; |
|
109 | + } |
|
110 | + |
|
111 | + $this->assertTagsExist($tagIds); |
|
112 | + |
|
113 | + $query = $this->connection->getQueryBuilder(); |
|
114 | + $query->selectDistinct('objectid') |
|
115 | + ->from(self::RELATION_TABLE) |
|
116 | + ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
117 | + ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType))); |
|
118 | + |
|
119 | + if ($limit) { |
|
120 | + if (\count($tagIds) !== 1) { |
|
121 | + throw new \InvalidArgumentException('Limit is only allowed with a single tag'); |
|
122 | + } |
|
123 | + |
|
124 | + $query->setMaxResults($limit) |
|
125 | + ->orderBy('objectid', 'ASC'); |
|
126 | + |
|
127 | + if ($offset !== '') { |
|
128 | + $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset))); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + $objectIds = []; |
|
133 | + |
|
134 | + $result = $query->execute(); |
|
135 | + while ($row = $result->fetch()) { |
|
136 | + $objectIds[] = $row['objectid']; |
|
137 | + } |
|
138 | + |
|
139 | + return $objectIds; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * {@inheritdoc} |
|
144 | + */ |
|
145 | + public function assignTags(string $objId, string $objectType, $tagIds) { |
|
146 | + if (!\is_array($tagIds)) { |
|
147 | + $tagIds = [$tagIds]; |
|
148 | + } |
|
149 | + |
|
150 | + $this->assertTagsExist($tagIds); |
|
151 | + |
|
152 | + $query = $this->connection->getQueryBuilder(); |
|
153 | + $query->insert(self::RELATION_TABLE) |
|
154 | + ->values([ |
|
155 | + 'objectid' => $query->createNamedParameter($objId), |
|
156 | + 'objecttype' => $query->createNamedParameter($objectType), |
|
157 | + 'systemtagid' => $query->createParameter('tagid'), |
|
158 | + ]); |
|
159 | + |
|
160 | + $tagsAssigned = []; |
|
161 | + foreach ($tagIds as $tagId) { |
|
162 | + try { |
|
163 | + $query->setParameter('tagid', $tagId); |
|
164 | + $query->execute(); |
|
165 | + $tagsAssigned[] = $tagId; |
|
166 | + } catch (UniqueConstraintViolationException $e) { |
|
167 | + // ignore existing relations |
|
168 | + } |
|
169 | + } |
|
170 | + |
|
171 | + if (empty($tagsAssigned)) { |
|
172 | + return; |
|
173 | + } |
|
174 | + |
|
175 | + $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent( |
|
176 | + MapperEvent::EVENT_ASSIGN, |
|
177 | + $objectType, |
|
178 | + $objId, |
|
179 | + $tagsAssigned |
|
180 | + )); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * {@inheritdoc} |
|
185 | + */ |
|
186 | + public function unassignTags(string $objId, string $objectType, $tagIds) { |
|
187 | + if (!\is_array($tagIds)) { |
|
188 | + $tagIds = [$tagIds]; |
|
189 | + } |
|
190 | + |
|
191 | + $this->assertTagsExist($tagIds); |
|
192 | + |
|
193 | + $query = $this->connection->getQueryBuilder(); |
|
194 | + $query->delete(self::RELATION_TABLE) |
|
195 | + ->where($query->expr()->eq('objectid', $query->createParameter('objectid'))) |
|
196 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
197 | + ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids'))) |
|
198 | + ->setParameter('objectid', $objId) |
|
199 | + ->setParameter('objecttype', $objectType) |
|
200 | + ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY) |
|
201 | + ->execute(); |
|
202 | + |
|
203 | + $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent( |
|
204 | + MapperEvent::EVENT_UNASSIGN, |
|
205 | + $objectType, |
|
206 | + $objId, |
|
207 | + $tagIds |
|
208 | + )); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * {@inheritdoc} |
|
213 | + */ |
|
214 | + public function haveTag($objIds, string $objectType, string $tagId, bool $all = true): bool { |
|
215 | + $this->assertTagsExist([$tagId]); |
|
216 | + |
|
217 | + if (!\is_array($objIds)) { |
|
218 | + $objIds = [$objIds]; |
|
219 | + } |
|
220 | + |
|
221 | + $query = $this->connection->getQueryBuilder(); |
|
222 | + |
|
223 | + if (!$all) { |
|
224 | + // If we only need one entry, we make the query lighter, by not |
|
225 | + // counting the elements |
|
226 | + $query->select('*') |
|
227 | + ->setMaxResults(1); |
|
228 | + } else { |
|
229 | + $query->select($query->func()->count($query->expr()->literal(1))); |
|
230 | + } |
|
231 | + |
|
232 | + $query->from(self::RELATION_TABLE) |
|
233 | + ->where($query->expr()->in('objectid', $query->createParameter('objectids'))) |
|
234 | + ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype'))) |
|
235 | + ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid'))) |
|
236 | + ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY) |
|
237 | + ->setParameter('tagid', $tagId) |
|
238 | + ->setParameter('objecttype', $objectType); |
|
239 | + |
|
240 | + $result = $query->execute(); |
|
241 | + $row = $result->fetch(\PDO::FETCH_NUM); |
|
242 | + $result->closeCursor(); |
|
243 | + |
|
244 | + if ($all) { |
|
245 | + return ((int)$row[0] === \count($objIds)); |
|
246 | + } |
|
247 | + |
|
248 | + return (bool) $row; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Asserts that all the given tag ids exist. |
|
253 | + * |
|
254 | + * @param string[] $tagIds tag ids to check |
|
255 | + * |
|
256 | + * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist |
|
257 | + */ |
|
258 | + private function assertTagsExist($tagIds) { |
|
259 | + $tags = $this->tagManager->getTagsByIds($tagIds); |
|
260 | + if (\count($tags) !== \count($tagIds)) { |
|
261 | + // at least one tag missing, bail out |
|
262 | + $foundTagIds = array_map( |
|
263 | + function (ISystemTag $tag) { |
|
264 | + return $tag->getId(); |
|
265 | + }, |
|
266 | + $tags |
|
267 | + ); |
|
268 | + $missingTagIds = array_diff($tagIds, $foundTagIds); |
|
269 | + throw new TagNotFoundException( |
|
270 | + 'Tags not found', 0, null, $missingTagIds |
|
271 | + ); |
|
272 | + } |
|
273 | + } |
|
274 | 274 | } |
@@ -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 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 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 | } |
@@ -38,7 +38,7 @@ |
||
38 | 38 | * @since 15.0.0 |
39 | 39 | */ |
40 | 40 | public function __construct($property) { |
41 | - parent::__construct('Property ' . $property . ' does not exist.'); |
|
41 | + parent::__construct('Property '.$property.' does not exist.'); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | } |
@@ -30,12 +30,12 @@ |
||
30 | 30 | * |
31 | 31 | */ |
32 | 32 | class PropertyDoesNotExistException extends \Exception { |
33 | - /** |
|
34 | - * Constructor |
|
35 | - * @param string $msg the error message |
|
36 | - * @since 15.0.0 |
|
37 | - */ |
|
38 | - public function __construct($property) { |
|
39 | - parent::__construct('Property ' . $property . ' does not exist.'); |
|
40 | - } |
|
33 | + /** |
|
34 | + * Constructor |
|
35 | + * @param string $msg the error message |
|
36 | + * @since 15.0.0 |
|
37 | + */ |
|
38 | + public function __construct($property) { |
|
39 | + parent::__construct('Property ' . $property . ' does not exist.'); |
|
40 | + } |
|
41 | 41 | } |
@@ -35,35 +35,35 @@ |
||
35 | 35 | * @since 8.1.0 |
36 | 36 | */ |
37 | 37 | class StreamResponse extends Response implements ICallbackResponse { |
38 | - /** @var string */ |
|
39 | - private $filePath; |
|
38 | + /** @var string */ |
|
39 | + private $filePath; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param string|resource $filePath the path to the file or a file handle which should be streamed |
|
43 | - * @since 8.1.0 |
|
44 | - */ |
|
45 | - public function __construct($filePath) { |
|
46 | - parent::__construct(); |
|
41 | + /** |
|
42 | + * @param string|resource $filePath the path to the file or a file handle which should be streamed |
|
43 | + * @since 8.1.0 |
|
44 | + */ |
|
45 | + public function __construct($filePath) { |
|
46 | + parent::__construct(); |
|
47 | 47 | |
48 | - $this->filePath = $filePath; |
|
49 | - } |
|
48 | + $this->filePath = $filePath; |
|
49 | + } |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * Streams the file using readfile |
|
54 | - * |
|
55 | - * @param IOutput $output a small wrapper that handles output |
|
56 | - * @since 8.1.0 |
|
57 | - */ |
|
58 | - public function callback(IOutput $output) { |
|
59 | - // handle caching |
|
60 | - if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) { |
|
61 | - if (!(is_resource($this->filePath) || file_exists($this->filePath))) { |
|
62 | - $output->setHttpResponseCode(Http::STATUS_NOT_FOUND); |
|
63 | - } elseif ($output->setReadfile($this->filePath) === false) { |
|
64 | - $output->setHttpResponseCode(Http::STATUS_BAD_REQUEST); |
|
65 | - } |
|
66 | - } |
|
67 | - } |
|
52 | + /** |
|
53 | + * Streams the file using readfile |
|
54 | + * |
|
55 | + * @param IOutput $output a small wrapper that handles output |
|
56 | + * @since 8.1.0 |
|
57 | + */ |
|
58 | + public function callback(IOutput $output) { |
|
59 | + // handle caching |
|
60 | + if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) { |
|
61 | + if (!(is_resource($this->filePath) || file_exists($this->filePath))) { |
|
62 | + $output->setHttpResponseCode(Http::STATUS_NOT_FOUND); |
|
63 | + } elseif ($output->setReadfile($this->filePath) === false) { |
|
64 | + $output->setHttpResponseCode(Http::STATUS_BAD_REQUEST); |
|
65 | + } |
|
66 | + } |
|
67 | + } |
|
68 | 68 | |
69 | 69 | } |