You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Backend.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  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\CardDAV\Activity;
  25. use OCA\DAV\CardDAV\Activity\Provider\Addressbook;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IManager as IActivityManager;
  28. use OCP\App\IAppManager;
  29. use OCP\IGroup;
  30. use OCP\IGroupManager;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use Sabre\CardDAV\Plugin;
  35. use Sabre\VObject\Reader;
  36. class Backend {
  37. /** @var IActivityManager */
  38. protected $activityManager;
  39. /** @var IGroupManager */
  40. protected $groupManager;
  41. /** @var IUserSession */
  42. protected $userSession;
  43. /** @var IAppManager */
  44. protected $appManager;
  45. /** @var IUserManager */
  46. protected $userManager;
  47. public function __construct(IActivityManager $activityManager,
  48. IGroupManager $groupManager,
  49. IUserSession $userSession,
  50. IAppManager $appManager,
  51. IUserManager $userManager) {
  52. $this->activityManager = $activityManager;
  53. $this->groupManager = $groupManager;
  54. $this->userSession = $userSession;
  55. $this->appManager = $appManager;
  56. $this->userManager = $userManager;
  57. }
  58. /**
  59. * Creates activities when an addressbook was creates
  60. *
  61. * @param array $addressbookData
  62. */
  63. public function onAddressbookCreate(array $addressbookData): void {
  64. $this->triggerAddressbookActivity(Addressbook::SUBJECT_ADD, $addressbookData);
  65. }
  66. /**
  67. * Creates activities when a calendar was updated
  68. *
  69. * @param array $addressbookData
  70. * @param array $shares
  71. * @param array $properties
  72. */
  73. public function onAddressbookUpdate(array $addressbookData, array $shares, array $properties): void {
  74. $this->triggerAddressbookActivity(Addressbook::SUBJECT_UPDATE, $addressbookData, $shares, $properties);
  75. }
  76. /**
  77. * Creates activities when a calendar was deleted
  78. *
  79. * @param array $addressbookData
  80. * @param array $shares
  81. */
  82. public function onAddressbookDelete(array $addressbookData, array $shares): void {
  83. $this->triggerAddressbookActivity(Addressbook::SUBJECT_DELETE, $addressbookData, $shares);
  84. }
  85. /**
  86. * Creates activities for all related users when a calendar was touched
  87. *
  88. * @param string $action
  89. * @param array $addressbookData
  90. * @param array $shares
  91. * @param array $changedProperties
  92. */
  93. protected function triggerAddressbookActivity(string $action, array $addressbookData, array $shares = [], array $changedProperties = []): void {
  94. if (!isset($addressbookData['principaluri'])) {
  95. return;
  96. }
  97. $principalUri = $addressbookData['principaluri'];
  98. // We are not interested in changes from the system addressbook
  99. if ($principalUri === 'principals/system/system') {
  100. return;
  101. }
  102. $principal = explode('/', $principalUri);
  103. $owner = array_pop($principal);
  104. $currentUser = $this->userSession->getUser();
  105. if ($currentUser instanceof IUser) {
  106. $currentUser = $currentUser->getUID();
  107. } else {
  108. $currentUser = $owner;
  109. }
  110. $event = $this->activityManager->generateEvent();
  111. $event->setApp('dav')
  112. ->setObject('addressbook', (int) $addressbookData['id'])
  113. ->setType('contacts')
  114. ->setAuthor($currentUser);
  115. $changedVisibleInformation = array_intersect([
  116. '{DAV:}displayname',
  117. '{' . Plugin::NS_CARDDAV . '}addressbook-description',
  118. ], array_keys($changedProperties));
  119. if (empty($shares) || ($action === Addressbook::SUBJECT_UPDATE && empty($changedVisibleInformation))) {
  120. $users = [$owner];
  121. } else {
  122. $users = $this->getUsersForShares($shares);
  123. $users[] = $owner;
  124. }
  125. foreach ($users as $user) {
  126. if ($action === Addressbook::SUBJECT_DELETE && !$this->userManager->userExists($user)) {
  127. // Avoid creating addressbook_delete activities for deleted users
  128. continue;
  129. }
  130. $event->setAffectedUser($user)
  131. ->setSubject(
  132. $user === $currentUser ? $action . '_self' : $action,
  133. [
  134. 'actor' => $currentUser,
  135. 'addressbook' => [
  136. 'id' => (int) $addressbookData['id'],
  137. 'uri' => $addressbookData['uri'],
  138. 'name' => $addressbookData['{DAV:}displayname'],
  139. ],
  140. ]
  141. );
  142. $this->activityManager->publish($event);
  143. }
  144. }
  145. /**
  146. * Creates activities for all related users when an addressbook was (un-)shared
  147. *
  148. * @param array $addressbookData
  149. * @param array $shares
  150. * @param array $add
  151. * @param array $remove
  152. */
  153. public function onAddressbookUpdateShares(array $addressbookData, array $shares, array $add, array $remove): void {
  154. $principal = explode('/', $addressbookData['principaluri']);
  155. $owner = $principal[2];
  156. $currentUser = $this->userSession->getUser();
  157. if ($currentUser instanceof IUser) {
  158. $currentUser = $currentUser->getUID();
  159. } else {
  160. $currentUser = $owner;
  161. }
  162. $event = $this->activityManager->generateEvent();
  163. $event->setApp('dav')
  164. ->setObject('addressbook', (int) $addressbookData['id'])
  165. ->setType('contacts')
  166. ->setAuthor($currentUser);
  167. foreach ($remove as $principal) {
  168. // principal:principals/users/test
  169. $parts = explode(':', $principal, 2);
  170. if ($parts[0] !== 'principal') {
  171. continue;
  172. }
  173. $principal = explode('/', $parts[1]);
  174. if ($principal[1] === 'users') {
  175. $this->triggerActivityUser(
  176. $principal[2],
  177. $event,
  178. $addressbookData,
  179. Addressbook::SUBJECT_UNSHARE_USER,
  180. Addressbook::SUBJECT_DELETE . '_self'
  181. );
  182. if ($owner !== $principal[2]) {
  183. $parameters = [
  184. 'actor' => $event->getAuthor(),
  185. 'addressbook' => [
  186. 'id' => (int) $addressbookData['id'],
  187. 'uri' => $addressbookData['uri'],
  188. 'name' => $addressbookData['{DAV:}displayname'],
  189. ],
  190. 'user' => $principal[2],
  191. ];
  192. if ($owner === $event->getAuthor()) {
  193. $subject = Addressbook::SUBJECT_UNSHARE_USER . '_you';
  194. } elseif ($principal[2] === $event->getAuthor()) {
  195. $subject = Addressbook::SUBJECT_UNSHARE_USER . '_self';
  196. } else {
  197. $event->setAffectedUser($event->getAuthor())
  198. ->setSubject(Addressbook::SUBJECT_UNSHARE_USER . '_you', $parameters);
  199. $this->activityManager->publish($event);
  200. $subject = Addressbook::SUBJECT_UNSHARE_USER . '_by';
  201. }
  202. $event->setAffectedUser($owner)
  203. ->setSubject($subject, $parameters);
  204. $this->activityManager->publish($event);
  205. }
  206. } elseif ($principal[1] === 'groups') {
  207. $this->triggerActivityGroup($principal[2], $event, $addressbookData, Addressbook::SUBJECT_UNSHARE_USER);
  208. $parameters = [
  209. 'actor' => $event->getAuthor(),
  210. 'addressbook' => [
  211. 'id' => (int) $addressbookData['id'],
  212. 'uri' => $addressbookData['uri'],
  213. 'name' => $addressbookData['{DAV:}displayname'],
  214. ],
  215. 'group' => $principal[2],
  216. ];
  217. if ($owner === $event->getAuthor()) {
  218. $subject = Addressbook::SUBJECT_UNSHARE_GROUP . '_you';
  219. } else {
  220. $event->setAffectedUser($event->getAuthor())
  221. ->setSubject(Addressbook::SUBJECT_UNSHARE_GROUP . '_you', $parameters);
  222. $this->activityManager->publish($event);
  223. $subject = Addressbook::SUBJECT_UNSHARE_GROUP . '_by';
  224. }
  225. $event->setAffectedUser($owner)
  226. ->setSubject($subject, $parameters);
  227. $this->activityManager->publish($event);
  228. }
  229. }
  230. foreach ($add as $share) {
  231. if ($this->isAlreadyShared($share['href'], $shares)) {
  232. continue;
  233. }
  234. // principal:principals/users/test
  235. $parts = explode(':', $share['href'], 2);
  236. if ($parts[0] !== 'principal') {
  237. continue;
  238. }
  239. $principal = explode('/', $parts[1]);
  240. if ($principal[1] === 'users') {
  241. $this->triggerActivityUser($principal[2], $event, $addressbookData, Addressbook::SUBJECT_SHARE_USER);
  242. if ($owner !== $principal[2]) {
  243. $parameters = [
  244. 'actor' => $event->getAuthor(),
  245. 'addressbook' => [
  246. 'id' => (int) $addressbookData['id'],
  247. 'uri' => $addressbookData['uri'],
  248. 'name' => $addressbookData['{DAV:}displayname'],
  249. ],
  250. 'user' => $principal[2],
  251. ];
  252. if ($owner === $event->getAuthor()) {
  253. $subject = Addressbook::SUBJECT_SHARE_USER . '_you';
  254. } else {
  255. $event->setAffectedUser($event->getAuthor())
  256. ->setSubject(Addressbook::SUBJECT_SHARE_USER . '_you', $parameters);
  257. $this->activityManager->publish($event);
  258. $subject = Addressbook::SUBJECT_SHARE_USER . '_by';
  259. }
  260. $event->setAffectedUser($owner)
  261. ->setSubject($subject, $parameters);
  262. $this->activityManager->publish($event);
  263. }
  264. } elseif ($principal[1] === 'groups') {
  265. $this->triggerActivityGroup($principal[2], $event, $addressbookData, Addressbook::SUBJECT_SHARE_USER);
  266. $parameters = [
  267. 'actor' => $event->getAuthor(),
  268. 'addressbook' => [
  269. 'id' => (int) $addressbookData['id'],
  270. 'uri' => $addressbookData['uri'],
  271. 'name' => $addressbookData['{DAV:}displayname'],
  272. ],
  273. 'group' => $principal[2],
  274. ];
  275. if ($owner === $event->getAuthor()) {
  276. $subject = Addressbook::SUBJECT_SHARE_GROUP . '_you';
  277. } else {
  278. $event->setAffectedUser($event->getAuthor())
  279. ->setSubject(Addressbook::SUBJECT_SHARE_GROUP . '_you', $parameters);
  280. $this->activityManager->publish($event);
  281. $subject = Addressbook::SUBJECT_SHARE_GROUP . '_by';
  282. }
  283. $event->setAffectedUser($owner)
  284. ->setSubject($subject, $parameters);
  285. $this->activityManager->publish($event);
  286. }
  287. }
  288. }
  289. /**
  290. * Checks if a calendar is already shared with a principal
  291. *
  292. * @param string $principal
  293. * @param array[] $shares
  294. * @return bool
  295. */
  296. protected function isAlreadyShared(string $principal, array $shares): bool {
  297. foreach ($shares as $share) {
  298. if ($principal === $share['href']) {
  299. return true;
  300. }
  301. }
  302. return false;
  303. }
  304. /**
  305. * Creates the given activity for all members of the given group
  306. *
  307. * @param string $gid
  308. * @param IEvent $event
  309. * @param array $properties
  310. * @param string $subject
  311. */
  312. protected function triggerActivityGroup(string $gid, IEvent $event, array $properties, string $subject): void {
  313. $group = $this->groupManager->get($gid);
  314. if ($group instanceof IGroup) {
  315. foreach ($group->getUsers() as $user) {
  316. // Exclude current user
  317. if ($user->getUID() !== $event->getAuthor()) {
  318. $this->triggerActivityUser($user->getUID(), $event, $properties, $subject);
  319. }
  320. }
  321. }
  322. }
  323. /**
  324. * Creates the given activity for the given user
  325. *
  326. * @param string $user
  327. * @param IEvent $event
  328. * @param array $properties
  329. * @param string $subject
  330. * @param string $subjectSelf
  331. */
  332. protected function triggerActivityUser(string $user, IEvent $event, array $properties, string $subject, string $subjectSelf = ''): void {
  333. $event->setAffectedUser($user)
  334. ->setSubject(
  335. $user === $event->getAuthor() && $subjectSelf ? $subjectSelf : $subject,
  336. [
  337. 'actor' => $event->getAuthor(),
  338. 'addressbook' => [
  339. 'id' => (int) $properties['id'],
  340. 'uri' => $properties['uri'],
  341. 'name' => $properties['{DAV:}displayname'],
  342. ],
  343. ]
  344. );
  345. $this->activityManager->publish($event);
  346. }
  347. /**
  348. * Creates activities when a card was created/updated/deleted
  349. *
  350. * @param string $action
  351. * @param array $addressbookData
  352. * @param array $shares
  353. * @param array $cardData
  354. */
  355. public function triggerCardActivity(string $action, array $addressbookData, array $shares, array $cardData): void {
  356. if (!isset($addressbookData['principaluri'])) {
  357. return;
  358. }
  359. $principalUri = $addressbookData['principaluri'];
  360. // We are not interested in changes from the system addressbook
  361. if ($principalUri === 'principals/system/system') {
  362. return;
  363. }
  364. $principal = explode('/', $principalUri);
  365. $owner = array_pop($principal);
  366. $currentUser = $this->userSession->getUser();
  367. if ($currentUser instanceof IUser) {
  368. $currentUser = $currentUser->getUID();
  369. } else {
  370. $currentUser = $owner;
  371. }
  372. $card = $this->getCardNameAndId($cardData);
  373. $event = $this->activityManager->generateEvent();
  374. $event->setApp('dav')
  375. ->setObject('addressbook', (int) $addressbookData['id'])
  376. ->setType('contacts')
  377. ->setAuthor($currentUser);
  378. $users = $this->getUsersForShares($shares);
  379. $users[] = $owner;
  380. // Users for share can return the owner itself if the calendar is published
  381. foreach (array_unique($users) as $user) {
  382. $params = [
  383. 'actor' => $event->getAuthor(),
  384. 'addressbook' => [
  385. 'id' => (int) $addressbookData['id'],
  386. 'uri' => $addressbookData['uri'],
  387. 'name' => $addressbookData['{DAV:}displayname'],
  388. ],
  389. 'card' => [
  390. 'id' => $card['id'],
  391. 'name' => $card['name'],
  392. ],
  393. ];
  394. $event->setAffectedUser($user)
  395. ->setSubject(
  396. $user === $currentUser ? $action . '_self' : $action,
  397. $params
  398. );
  399. $this->activityManager->publish($event);
  400. }
  401. }
  402. /**
  403. * @param array $cardData
  404. * @return string[]
  405. */
  406. protected function getCardNameAndId(array $cardData): array {
  407. $vObject = Reader::read($cardData['carddata']);
  408. return ['id' => (string) $vObject->UID, 'name' => (string) ($vObject->FN ?? '')];
  409. }
  410. /**
  411. * Get all users that have access to a given calendar
  412. *
  413. * @param array $shares
  414. * @return string[]
  415. */
  416. protected function getUsersForShares(array $shares): array {
  417. $users = $groups = [];
  418. foreach ($shares as $share) {
  419. $principal = explode('/', $share['{http://owncloud.org/ns}principal']);
  420. if ($principal[1] === 'users') {
  421. $users[] = $principal[2];
  422. } elseif ($principal[1] === 'groups') {
  423. $groups[] = $principal[2];
  424. }
  425. }
  426. if (!empty($groups)) {
  427. foreach ($groups as $gid) {
  428. $group = $this->groupManager->get($gid);
  429. if ($group instanceof IGroup) {
  430. foreach ($group->getUsers() as $user) {
  431. $users[] = $user->getUID();
  432. }
  433. }
  434. }
  435. }
  436. return array_unique($users);
  437. }
  438. }