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 | |
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')
-rw-r--r-- | apps/dav/lib/CalDAV/BirthdayService.php | 52 | ||||
-rw-r--r-- | apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php | 115 |
2 files changed, 160 insertions, 7 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; + } + } diff --git a/apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php b/apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php index 72b3c57bea6..867168033a4 100644 --- a/apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php +++ b/apps/dav/tests/unit/CardDAV/BirthdayServiceTest.php @@ -28,6 +28,7 @@ use OCA\DAV\CalDAV\BirthdayService; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\DAV\GroupPrincipalBackend; +use OCP\IConfig; use Sabre\VObject\Component\VCalendar; use Sabre\VObject\Reader; use Test\TestCase; @@ -42,15 +43,19 @@ class BirthdayServiceTest extends TestCase { private $cardDav; /** @var GroupPrincipalBackend | \PHPUnit_Framework_MockObject_MockObject */ private $groupPrincipalBackend; + /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ + private $config; public function setUp() { parent::setUp(); - $this->calDav = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock(); - $this->cardDav = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock(); - $this->groupPrincipalBackend = $this->getMockBuilder(GroupPrincipalBackend::class)->disableOriginalConstructor()->getMock(); + $this->calDav = $this->createMock(CalDavBackend::class); + $this->cardDav = $this->createMock(CardDavBackend::class); + $this->groupPrincipalBackend = $this->createMock(GroupPrincipalBackend::class); + $this->config = $this->createMock(IConfig::class); - $this->service = new BirthdayService($this->calDav, $this->cardDav, $this->groupPrincipalBackend); + $this->service = new BirthdayService($this->calDav, $this->cardDav, + $this->groupPrincipalBackend, $this->config); } /** @@ -71,7 +76,52 @@ class BirthdayServiceTest extends TestCase { } } + public function testOnCardDeleteGloballyDisabled() { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('no')); + + $this->cardDav->expects($this->never())->method('getAddressBookById'); + + $this->service->onCardDeleted(666, 'gump.vcf'); + } + + public function testOnCardDeleteUserDisabled() { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('yes')); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with('user01', 'dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('no')); + + $this->cardDav->expects($this->once())->method('getAddressBookById') + ->with(666) + ->willReturn([ + 'principaluri' => 'principals/users/user01', + 'uri' => 'default' + ]); + $this->cardDav->expects($this->once())->method('getShares')->willReturn([]); + $this->calDav->expects($this->never())->method('getCalendarByUri'); + $this->calDav->expects($this->never())->method('deleteCalendarObject'); + + $this->service->onCardDeleted(666, 'gump.vcf'); + } + public function testOnCardDeleted() { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('yes')); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with('user01', 'dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('yes')); + $this->cardDav->expects($this->once())->method('getAddressBookById') ->with(666) ->willReturn([ @@ -91,10 +141,65 @@ class BirthdayServiceTest extends TestCase { $this->service->onCardDeleted(666, 'gump.vcf'); } + public function testOnCardChangedGloballyDisabled() { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('no')); + + $this->cardDav->expects($this->never())->method('getAddressBookById'); + + $service = $this->getMockBuilder(BirthdayService::class) + ->setMethods(['buildDateFromContact', 'birthdayEvenChanged']) + ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config]) + ->getMock(); + + $service->onCardChanged(666, 'gump.vcf', ''); + } + + public function testOnCardChangedUserDisabled() { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('yes')); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with('user01', 'dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('no')); + + $this->cardDav->expects($this->once())->method('getAddressBookById') + ->with(666) + ->willReturn([ + 'principaluri' => 'principals/users/user01', + 'uri' => 'default' + ]); + $this->cardDav->expects($this->once())->method('getShares')->willReturn([]); + $this->calDav->expects($this->never())->method('getCalendarByUri'); + + /** @var BirthdayService | \PHPUnit_Framework_MockObject_MockObject $service */ + $service = $this->getMockBuilder(BirthdayService::class) + ->setMethods(['buildDateFromContact', 'birthdayEvenChanged']) + ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config]) + ->getMock(); + + $service->onCardChanged(666, 'gump.vcf', ''); + } + /** * @dataProvider providesCardChanges */ public function testOnCardChanged($expectedOp) { + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('yes')); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with('user01', 'dav', 'generateBirthdayCalendar', 'yes') + ->will($this->returnValue('yes')); + $this->cardDav->expects($this->once())->method('getAddressBookById') ->with(666) ->willReturn([ @@ -111,7 +216,7 @@ class BirthdayServiceTest extends TestCase { /** @var BirthdayService | \PHPUnit_Framework_MockObject_MockObject $service */ $service = $this->getMockBuilder(BirthdayService::class) ->setMethods(['buildDateFromContact', 'birthdayEvenChanged']) - ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend]) + ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend, $this->config]) ->getMock(); if ($expectedOp === 'delete') { |