Backend.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV\Activity;
  25. use OCA\DAV\CalDAV\Activity\Provider\Calendar;
  26. use OCA\DAV\CalDAV\Activity\Provider\Event;
  27. use OCA\DAV\CalDAV\CalDavBackend;
  28. use OCP\Activity\IEvent;
  29. use OCP\Activity\IManager as IActivityManager;
  30. use OCP\IGroup;
  31. use OCP\IGroupManager;
  32. use OCP\IUser;
  33. use OCP\IUserSession;
  34. use Sabre\VObject\Reader;
  35. /**
  36. * Class Backend
  37. *
  38. * @package OCA\DAV\CalDAV\Activity
  39. */
  40. class Backend {
  41. /** @var IActivityManager */
  42. protected $activityManager;
  43. /** @var IGroupManager */
  44. protected $groupManager;
  45. /** @var IUserSession */
  46. protected $userSession;
  47. /**
  48. * @param IActivityManager $activityManager
  49. * @param IGroupManager $groupManager
  50. * @param IUserSession $userSession
  51. */
  52. public function __construct(IActivityManager $activityManager, IGroupManager $groupManager, IUserSession $userSession) {
  53. $this->activityManager = $activityManager;
  54. $this->groupManager = $groupManager;
  55. $this->userSession = $userSession;
  56. }
  57. /**
  58. * Creates activities when a calendar was creates
  59. *
  60. * @param array $calendarData
  61. */
  62. public function onCalendarAdd(array $calendarData) {
  63. $this->triggerCalendarActivity(Calendar::SUBJECT_ADD, $calendarData);
  64. }
  65. /**
  66. * Creates activities when a calendar was updated
  67. *
  68. * @param array $calendarData
  69. * @param array $shares
  70. * @param array $properties
  71. */
  72. public function onCalendarUpdate(array $calendarData, array $shares, array $properties) {
  73. $this->triggerCalendarActivity(Calendar::SUBJECT_UPDATE, $calendarData, $shares, $properties);
  74. }
  75. /**
  76. * Creates activities when a calendar was deleted
  77. *
  78. * @param array $calendarData
  79. * @param array $shares
  80. */
  81. public function onCalendarDelete(array $calendarData, array $shares) {
  82. $this->triggerCalendarActivity(Calendar::SUBJECT_DELETE, $calendarData, $shares);
  83. }
  84. /**
  85. * Creates activities when a calendar was (un)published
  86. *
  87. * @param array $calendarData
  88. * @param bool $publishStatus
  89. */
  90. public function onCalendarPublication(array $calendarData, $publishStatus) {
  91. $this->triggerCalendarActivity($publishStatus ? Calendar::SUBJECT_PUBLISH : Calendar::SUBJECT_UNPUBLISH, $calendarData);
  92. }
  93. /**
  94. * Creates activities for all related users when a calendar was touched
  95. *
  96. * @param string $action
  97. * @param array $calendarData
  98. * @param array $shares
  99. * @param array $changedProperties
  100. */
  101. protected function triggerCalendarActivity($action, array $calendarData, array $shares = [], array $changedProperties = []) {
  102. if (!isset($calendarData['principaluri'])) {
  103. return;
  104. }
  105. $principal = explode('/', $calendarData['principaluri']);
  106. $owner = array_pop($principal);
  107. $currentUser = $this->userSession->getUser();
  108. if ($currentUser instanceof IUser) {
  109. $currentUser = $currentUser->getUID();
  110. } else {
  111. $currentUser = $owner;
  112. }
  113. $event = $this->activityManager->generateEvent();
  114. $event->setApp('dav')
  115. ->setObject('calendar', (int) $calendarData['id'])
  116. ->setType('calendar')
  117. ->setAuthor($currentUser);
  118. $changedVisibleInformation = array_intersect([
  119. '{DAV:}displayname',
  120. '{http://apple.com/ns/ical/}calendar-color'
  121. ], array_keys($changedProperties));
  122. if (empty($shares) || ($action === Calendar::SUBJECT_UPDATE && empty($changedVisibleInformation))) {
  123. $users = [$owner];
  124. } else {
  125. $users = $this->getUsersForShares($shares);
  126. $users[] = $owner;
  127. }
  128. foreach ($users as $user) {
  129. $event->setAffectedUser($user)
  130. ->setSubject(
  131. $user === $currentUser ? $action . '_self' : $action,
  132. [
  133. 'actor' => $currentUser,
  134. 'calendar' => [
  135. 'id' => (int) $calendarData['id'],
  136. 'uri' => $calendarData['uri'],
  137. 'name' => $calendarData['{DAV:}displayname'],
  138. ],
  139. ]
  140. );
  141. $this->activityManager->publish($event);
  142. }
  143. }
  144. /**
  145. * Creates activities for all related users when a calendar was (un-)shared
  146. *
  147. * @param array $calendarData
  148. * @param array $shares
  149. * @param array $add
  150. * @param array $remove
  151. */
  152. public function onCalendarUpdateShares(array $calendarData, array $shares, array $add, array $remove) {
  153. $principal = explode('/', $calendarData['principaluri']);
  154. $owner = $principal[2];
  155. $currentUser = $this->userSession->getUser();
  156. if ($currentUser instanceof IUser) {
  157. $currentUser = $currentUser->getUID();
  158. } else {
  159. $currentUser = $owner;
  160. }
  161. $event = $this->activityManager->generateEvent();
  162. $event->setApp('dav')
  163. ->setObject('calendar', (int) $calendarData['id'])
  164. ->setType('calendar')
  165. ->setAuthor($currentUser);
  166. foreach ($remove as $principal) {
  167. // principal:principals/users/test
  168. $parts = explode(':', $principal, 2);
  169. if ($parts[0] !== 'principal') {
  170. continue;
  171. }
  172. $principal = explode('/', $parts[1]);
  173. if ($principal[1] === 'users') {
  174. $this->triggerActivityUser(
  175. $principal[2],
  176. $event,
  177. $calendarData,
  178. Calendar::SUBJECT_UNSHARE_USER,
  179. Calendar::SUBJECT_DELETE . '_self'
  180. );
  181. if ($owner !== $principal[2]) {
  182. $parameters = [
  183. 'actor' => $event->getAuthor(),
  184. 'calendar' => [
  185. 'id' => (int) $calendarData['id'],
  186. 'uri' => $calendarData['uri'],
  187. 'name' => $calendarData['{DAV:}displayname'],
  188. ],
  189. 'user' => $principal[2],
  190. ];
  191. if ($owner === $event->getAuthor()) {
  192. $subject = Calendar::SUBJECT_UNSHARE_USER . '_you';
  193. } else if ($principal[2] === $event->getAuthor()) {
  194. $subject = Calendar::SUBJECT_UNSHARE_USER . '_self';
  195. } else {
  196. $event->setAffectedUser($event->getAuthor())
  197. ->setSubject(Calendar::SUBJECT_UNSHARE_USER . '_you', $parameters);
  198. $this->activityManager->publish($event);
  199. $subject = Calendar::SUBJECT_UNSHARE_USER . '_by';
  200. }
  201. $event->setAffectedUser($owner)
  202. ->setSubject($subject, $parameters);
  203. $this->activityManager->publish($event);
  204. }
  205. } else if ($principal[1] === 'groups') {
  206. $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_UNSHARE_USER);
  207. $parameters = [
  208. 'actor' => $event->getAuthor(),
  209. 'calendar' => [
  210. 'id' => (int) $calendarData['id'],
  211. 'uri' => $calendarData['uri'],
  212. 'name' => $calendarData['{DAV:}displayname'],
  213. ],
  214. 'group' => $principal[2],
  215. ];
  216. if ($owner === $event->getAuthor()) {
  217. $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_you';
  218. } else {
  219. $event->setAffectedUser($event->getAuthor())
  220. ->setSubject(Calendar::SUBJECT_UNSHARE_GROUP . '_you', $parameters);
  221. $this->activityManager->publish($event);
  222. $subject = Calendar::SUBJECT_UNSHARE_GROUP . '_by';
  223. }
  224. $event->setAffectedUser($owner)
  225. ->setSubject($subject, $parameters);
  226. $this->activityManager->publish($event);
  227. }
  228. }
  229. foreach ($add as $share) {
  230. if ($this->isAlreadyShared($share['href'], $shares)) {
  231. continue;
  232. }
  233. // principal:principals/users/test
  234. $parts = explode(':', $share['href'], 2);
  235. if ($parts[0] !== 'principal') {
  236. continue;
  237. }
  238. $principal = explode('/', $parts[1]);
  239. if ($principal[1] === 'users') {
  240. $this->triggerActivityUser($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER);
  241. if ($owner !== $principal[2]) {
  242. $parameters = [
  243. 'actor' => $event->getAuthor(),
  244. 'calendar' => [
  245. 'id' => (int) $calendarData['id'],
  246. 'uri' => $calendarData['uri'],
  247. 'name' => $calendarData['{DAV:}displayname'],
  248. ],
  249. 'user' => $principal[2],
  250. ];
  251. if ($owner === $event->getAuthor()) {
  252. $subject = Calendar::SUBJECT_SHARE_USER . '_you';
  253. } else {
  254. $event->setAffectedUser($event->getAuthor())
  255. ->setSubject(Calendar::SUBJECT_SHARE_USER . '_you', $parameters);
  256. $this->activityManager->publish($event);
  257. $subject = Calendar::SUBJECT_SHARE_USER . '_by';
  258. }
  259. $event->setAffectedUser($owner)
  260. ->setSubject($subject, $parameters);
  261. $this->activityManager->publish($event);
  262. }
  263. } else if ($principal[1] === 'groups') {
  264. $this->triggerActivityGroup($principal[2], $event, $calendarData, Calendar::SUBJECT_SHARE_USER);
  265. $parameters = [
  266. 'actor' => $event->getAuthor(),
  267. 'calendar' => [
  268. 'id' => (int) $calendarData['id'],
  269. 'uri' => $calendarData['uri'],
  270. 'name' => $calendarData['{DAV:}displayname'],
  271. ],
  272. 'group' => $principal[2],
  273. ];
  274. if ($owner === $event->getAuthor()) {
  275. $subject = Calendar::SUBJECT_SHARE_GROUP . '_you';
  276. } else {
  277. $event->setAffectedUser($event->getAuthor())
  278. ->setSubject(Calendar::SUBJECT_SHARE_GROUP . '_you', $parameters);
  279. $this->activityManager->publish($event);
  280. $subject = Calendar::SUBJECT_SHARE_GROUP . '_by';
  281. }
  282. $event->setAffectedUser($owner)
  283. ->setSubject($subject, $parameters);
  284. $this->activityManager->publish($event);
  285. }
  286. }
  287. }
  288. /**
  289. * Checks if a calendar is already shared with a principal
  290. *
  291. * @param string $principal
  292. * @param array[] $shares
  293. * @return bool
  294. */
  295. protected function isAlreadyShared($principal, $shares) {
  296. foreach ($shares as $share) {
  297. if ($principal === $share['href']) {
  298. return true;
  299. }
  300. }
  301. return false;
  302. }
  303. /**
  304. * Creates the given activity for all members of the given group
  305. *
  306. * @param string $gid
  307. * @param IEvent $event
  308. * @param array $properties
  309. * @param string $subject
  310. */
  311. protected function triggerActivityGroup($gid, IEvent $event, array $properties, $subject) {
  312. $group = $this->groupManager->get($gid);
  313. if ($group instanceof IGroup) {
  314. foreach ($group->getUsers() as $user) {
  315. // Exclude current user
  316. if ($user->getUID() !== $event->getAuthor()) {
  317. $this->triggerActivityUser($user->getUID(), $event, $properties, $subject);
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * Creates the given activity for the given user
  324. *
  325. * @param string $user
  326. * @param IEvent $event
  327. * @param array $properties
  328. * @param string $subject
  329. * @param string $subjectSelf
  330. */
  331. protected function triggerActivityUser($user, IEvent $event, array $properties, $subject, $subjectSelf = '') {
  332. $event->setAffectedUser($user)
  333. ->setSubject(
  334. $user === $event->getAuthor() && $subjectSelf ? $subjectSelf : $subject,
  335. [
  336. 'actor' => $event->getAuthor(),
  337. 'calendar' => [
  338. 'id' => (int) $properties['id'],
  339. 'uri' => $properties['uri'],
  340. 'name' => $properties['{DAV:}displayname'],
  341. ],
  342. ]
  343. );
  344. $this->activityManager->publish($event);
  345. }
  346. /**
  347. * Creates activities when a calendar object was created/updated/deleted
  348. *
  349. * @param string $action
  350. * @param array $calendarData
  351. * @param array $shares
  352. * @param array $objectData
  353. */
  354. public function onTouchCalendarObject($action, array $calendarData, array $shares, array $objectData) {
  355. if (!isset($calendarData['principaluri'])) {
  356. return;
  357. }
  358. $principal = explode('/', $calendarData['principaluri']);
  359. $owner = array_pop($principal);
  360. $currentUser = $this->userSession->getUser();
  361. if ($currentUser instanceof IUser) {
  362. $currentUser = $currentUser->getUID();
  363. } else {
  364. $currentUser = $owner;
  365. }
  366. $classification = $objectData['classification'] ?? CalDavBackend::CLASSIFICATION_PUBLIC;
  367. $object = $this->getObjectNameAndType($objectData);
  368. $action = $action . '_' . $object['type'];
  369. if ($object['type'] === 'todo' && strpos($action, Event::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'COMPLETED') {
  370. $action .= '_completed';
  371. } else if ($object['type'] === 'todo' && strpos($action, Event::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'NEEDS-ACTION') {
  372. $action .= '_needs_action';
  373. }
  374. $event = $this->activityManager->generateEvent();
  375. $event->setApp('dav')
  376. ->setObject('calendar', (int) $calendarData['id'])
  377. ->setType($object['type'] === 'event' ? 'calendar_event' : 'calendar_todo')
  378. ->setAuthor($currentUser);
  379. $users = $this->getUsersForShares($shares);
  380. $users[] = $owner;
  381. foreach ($users as $user) {
  382. if ($classification === CalDavBackend::CLASSIFICATION_PRIVATE && $user !== $owner) {
  383. // Private events are only shown to the owner
  384. continue;
  385. }
  386. $event->setAffectedUser($user)
  387. ->setSubject(
  388. $user === $currentUser ? $action . '_self' : $action,
  389. [
  390. 'actor' => $event->getAuthor(),
  391. 'calendar' => [
  392. 'id' => (int) $calendarData['id'],
  393. 'uri' => $calendarData['uri'],
  394. 'name' => $calendarData['{DAV:}displayname'],
  395. ],
  396. 'object' => [
  397. 'id' => $object['id'],
  398. 'name' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $owner ? 'Busy' : $object['name'],
  399. 'classified' => $classification === CalDavBackend::CLASSIFICATION_CONFIDENTIAL && $user !== $owner,
  400. ],
  401. ]
  402. );
  403. $this->activityManager->publish($event);
  404. }
  405. }
  406. /**
  407. * @param array $objectData
  408. * @return string[]|bool
  409. */
  410. protected function getObjectNameAndType(array $objectData) {
  411. $vObject = Reader::read($objectData['calendardata']);
  412. $component = $componentType = null;
  413. foreach($vObject->getComponents() as $component) {
  414. if (in_array($component->name, ['VEVENT', 'VTODO'])) {
  415. $componentType = $component->name;
  416. break;
  417. }
  418. }
  419. if (!$componentType) {
  420. // Calendar objects must have a VEVENT or VTODO component
  421. return false;
  422. }
  423. if ($componentType === 'VEVENT') {
  424. return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'event'];
  425. }
  426. return ['id' => (string) $component->UID, 'name' => (string) $component->SUMMARY, 'type' => 'todo', 'status' => (string) $component->STATUS];
  427. }
  428. /**
  429. * Get all users that have access to a given calendar
  430. *
  431. * @param array $shares
  432. * @return string[]
  433. */
  434. protected function getUsersForShares(array $shares) {
  435. $users = $groups = [];
  436. foreach ($shares as $share) {
  437. $prinical = explode('/', $share['{http://owncloud.org/ns}principal']);
  438. if ($prinical[1] === 'users') {
  439. $users[] = $prinical[2];
  440. } else if ($prinical[1] === 'groups') {
  441. $groups[] = $prinical[2];
  442. }
  443. }
  444. if (!empty($groups)) {
  445. foreach ($groups as $gid) {
  446. $group = $this->groupManager->get($gid);
  447. if ($group instanceof IGroup) {
  448. foreach ($group->getUsers() as $user) {
  449. $users[] = $user->getUID();
  450. }
  451. }
  452. }
  453. }
  454. return array_unique($users);
  455. }
  456. }