diff options
author | Georg Ehrke <developer@georgehrke.com> | 2017-11-11 01:27:48 +0100 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2017-11-11 02:15:57 +0100 |
commit | ef6f41a16ce2043cab812887f6ccfe790045ad90 (patch) | |
tree | ec99cfa9291ee9a111c0d7af36e399a63dee6f94 /apps/dav/lib | |
parent | d59b3392abf021d0289b5b2ea1a67bc99e8d89da (diff) | |
download | nextcloud-server-ef6f41a16ce2043cab812887f6ccfe790045ad90.tar.gz nextcloud-server-ef6f41a16ce2043cab812887f6ccfe790045ad90.zip |
respect admin / user choice about birthday calendars in corresponding hooks
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/CalDAV/BirthdayService.php | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/apps/dav/lib/CalDAV/BirthdayService.php b/apps/dav/lib/CalDAV/BirthdayService.php index aa902bacc53..62d218f0a2a 100644 --- a/apps/dav/lib/CalDAV/BirthdayService.php +++ b/apps/dav/lib/CalDAV/BirthdayService.php @@ -31,6 +31,7 @@ namespace OCA\DAV\CalDAV; use Exception; use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\DAV\GroupPrincipalBackend; +use OCP\IConfig; use Sabre\VObject\Component\VCalendar; use Sabre\VObject\Component\VCard; use Sabre\VObject\DateTimeParser; @@ -52,17 +53,22 @@ class BirthdayService { /** @var CardDavBackend */ private $cardDavBackEnd; + /** @var IConfig */ + private $config; + /** * BirthdayService constructor. * * @param CalDavBackend $calDavBackEnd * @param CardDavBackend $cardDavBackEnd * @param GroupPrincipalBackend $principalBackend + * @param IConfig $config; */ - public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend) { + public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config) { $this->calDavBackEnd = $calDavBackEnd; $this->cardDavBackEnd = $cardDavBackEnd; $this->principalBackend = $principalBackend; + $this->config = $config; } /** @@ -71,8 +77,11 @@ class BirthdayService { * @param string $cardData */ public function onCardChanged($addressBookId, $cardUri, $cardData) { + if (!$this->isGloballyEnabled()) { + return; + } + $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); - $book = $this->cardDavBackEnd->getAddressBookById($addressBookId); $targetPrincipals[] = $book['principaluri']; $datesToSync = [ @@ -81,6 +90,10 @@ class BirthdayService { ['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭"], ]; foreach ($targetPrincipals as $principalUri) { + if (!$this->isUserEnabled($principalUri)) { + continue; + } + $calendar = $this->ensureCalendarExists($principalUri); foreach ($datesToSync as $type) { $this->updateCalendar($cardUri, $cardData, $book, $calendar['id'], $type); @@ -93,10 +106,18 @@ class BirthdayService { * @param string $cardUri */ public function onCardDeleted($addressBookId, $cardUri) { + if (!$this->isGloballyEnabled()) { + return; + } + $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); $book = $this->cardDavBackEnd->getAddressBookById($addressBookId); $targetPrincipals[] = $book['principaluri']; foreach ($targetPrincipals as $principalUri) { + if (!$this->isUserEnabled($principalUri)) { + continue; + } + $calendar = $this->ensureCalendarExists($principalUri); foreach (['', '-death', '-anniversary'] as $tag) { $objectUri = $book['uri'] . '-' . $cardUri . $tag .'.ics'; @@ -293,4 +314,31 @@ class BirthdayService { } } + /** + * checks if the admin opted-out of birthday calendars + * + * @return bool + */ + private function isGloballyEnabled() { + $isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'); + return $isGloballyEnabled === 'yes'; + } + + /** + * checks if the user opted-out of birthday calendars + * + * @param $userPrincipal + * @return bool + */ + private function isUserEnabled($userPrincipal) { + if (strpos($userPrincipal, 'principals/users/') === 0) { + $userId = substr($userPrincipal, 17); + $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes'); + return $isEnabled === 'yes'; + } + + // not sure how we got here, just be on the safe side and return true + return true; + } + } |