Backend.php 14 KB

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