mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
respect admin / user choice about birthday calendars in corresponding hooks
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
This commit is contained in:
parent
d59b3392ab
commit
ef6f41a16c
2 changed files with 160 additions and 7 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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') {
|
||||
|
|
|
|||
Loading…
Reference in a new issue