aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2016-09-28 21:42:55 +0200
committerGitHub <noreply@github.com>2016-09-28 21:42:55 +0200
commitef0760f84f7da2adbb44851aa8ff4f6fdbdf4720 (patch)
tree05966304139d36a5b4c55cde947a3d7baa53d05a /apps/dav
parent2eaa2791b75f7c2c8c7892a5354f6e93fedaf396 (diff)
parent6c8bcb38f1124d0be7cee770ab67730d90dbd783 (diff)
downloadnextcloud-server-ef0760f84f7da2adbb44851aa8ff4f6fdbdf4720.tar.gz
nextcloud-server-ef0760f84f7da2adbb44851aa8ff4f6fdbdf4720.zip
Merge pull request #1545 from nextcloud/delete-calendars-with-their-users
Delete calendars with their users
Diffstat (limited to 'apps/dav')
-rw-r--r--apps/dav/lib/CalDAV/CalDavBackend.php52
-rw-r--r--apps/dav/lib/DAV/Sharing/Backend.php8
-rw-r--r--apps/dav/lib/HookManager.php20
-rw-r--r--apps/dav/tests/unit/DAV/HookManagerTest.php88
4 files changed, 152 insertions, 16 deletions
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php
index 440188c13ca..d742061428b 100644
--- a/apps/dav/lib/CalDAV/CalDavBackend.php
+++ b/apps/dav/lib/CalDAV/CalDavBackend.php
@@ -300,6 +300,48 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
return array_values($calendars);
}
+ public function getUsersOwnCalendars($principalUri) {
+ $principalUri = $this->convertPrincipal($principalUri, true);
+ $fields = array_values($this->propertyMap);
+ $fields[] = 'id';
+ $fields[] = 'uri';
+ $fields[] = 'synctoken';
+ $fields[] = 'components';
+ $fields[] = 'principaluri';
+ $fields[] = 'transparent';
+ // Making fields a comma-delimited list
+ $query = $this->db->getQueryBuilder();
+ $query->select($fields)->from('calendars')
+ ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
+ ->orderBy('calendarorder', 'ASC');
+ $stmt = $query->execute();
+ $calendars = [];
+ while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ $components = [];
+ if ($row['components']) {
+ $components = explode(',',$row['components']);
+ }
+ $calendar = [
+ 'id' => $row['id'],
+ 'uri' => $row['uri'],
+ 'principaluri' => $this->convertPrincipal($row['principaluri'], false),
+ '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'),
+ '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0',
+ '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components),
+ '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'),
+ ];
+ foreach($this->propertyMap as $xmlName=>$dbName) {
+ $calendar[$xmlName] = $row[$dbName];
+ }
+ if (!isset($calendars[$calendar['id']])) {
+ $calendars[$calendar['id']] = $calendar;
+ }
+ }
+ $stmt->closeCursor();
+ return array_values($calendars);
+ }
+
+
private function getUserDisplayName($uid) {
if (!isset($this->userDisplayNames[$uid])) {
$user = $this->userManager->get($uid);
@@ -640,6 +682,16 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
}
/**
+ * Delete all of an user's shares
+ *
+ * @param string $principaluri
+ * @return void
+ */
+ function deleteAllSharesByUser($principaluri) {
+ $this->sharingBackend->deleteAllSharesByUser($principaluri);
+ }
+
+ /**
* Returns all calendar objects within a calendar.
*
* Every item contains an array with the following keys:
diff --git a/apps/dav/lib/DAV/Sharing/Backend.php b/apps/dav/lib/DAV/Sharing/Backend.php
index 006fe334ba2..f662d8e1b80 100644
--- a/apps/dav/lib/DAV/Sharing/Backend.php
+++ b/apps/dav/lib/DAV/Sharing/Backend.php
@@ -110,6 +110,14 @@ class Backend {
->execute();
}
+ public function deleteAllSharesByUser($principaluri) {
+ $query = $this->db->getQueryBuilder();
+ $query->delete('dav_shares')
+ ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri)))
+ ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType)))
+ ->execute();
+ }
+
/**
* @param IShareable $shareable
* @param string $element
diff --git a/apps/dav/lib/HookManager.php b/apps/dav/lib/HookManager.php
index 5a3e328f707..364db19d76a 100644
--- a/apps/dav/lib/HookManager.php
+++ b/apps/dav/lib/HookManager.php
@@ -46,6 +46,12 @@ class HookManager {
/** @var CardDavBackend */
private $cardDav;
+ /** @var array */
+ private $calendarsToDelete;
+
+ /** @var array */
+ private $addressBooksToDelete;
+
public function __construct(IUserManager $userManager,
SyncService $syncService,
CalDavBackend $calDav,
@@ -85,7 +91,10 @@ class HookManager {
}
public function preDeleteUser($params) {
- $this->usersToDelete[$params['uid']] = $this->userManager->get($params['uid']);
+ $uid = $params['uid'];
+ $this->usersToDelete[$uid] = $this->userManager->get($uid);
+ $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid);
+ $this->addressBooksToDelete = $this->cardDav->getAddressBooksForUser('principals/users/' . $uid);
}
public function postDeleteUser($params) {
@@ -93,6 +102,15 @@ class HookManager {
if (isset($this->usersToDelete[$uid])){
$this->syncService->deleteUser($this->usersToDelete[$uid]);
}
+
+ foreach ($this->calendarsToDelete as $calendar) {
+ $this->calDav->deleteCalendar($calendar['id']);
+ }
+ $this->calDav->deleteAllSharesByUser('principals/users/' . $uid);
+
+ foreach ($this->addressBooksToDelete as $addressBook) {
+ $this->cardDav->deleteAddressBook($addressBook['id']);
+ }
}
public function changeUser($params) {
diff --git a/apps/dav/tests/unit/DAV/HookManagerTest.php b/apps/dav/tests/unit/DAV/HookManagerTest.php
index 0db66bf76c1..5b7d4700a5f 100644
--- a/apps/dav/tests/unit/DAV/HookManagerTest.php
+++ b/apps/dav/tests/unit/DAV/HookManagerTest.php
@@ -28,29 +28,45 @@ use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\HookManager;
+use OCP\IL10N;
+use OCP\IUser;
use OCP\IUserManager;
use Test\TestCase;
class HookManagerTest extends TestCase {
+ /** @var IL10N */
+ private $l10n;
+
+ public function setUp() {
+ parent::setUp();
+ $this->l10n = $this->createMock(IL10N::class);
+ $this->l10n
+ ->expects($this->any())
+ ->method('t')
+ ->will($this->returnCallback(function ($text, $parameters = []) {
+ return vsprintf($text, $parameters);
+ }));
+ }
+
public function test() {
- $user = $this->getMockBuilder('\OCP\IUser')
+ $user = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$user->expects($this->once())->method('getUID')->willReturn('newUser');
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
- $userManager = $this->getMockBuilder('\OCP\IUserManager')
+ $userManager = $this->getMockBuilder(IUserManager::class)
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
- $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
+ $syncService = $this->getMockBuilder(SyncService::class)
->disableOriginalConstructor()
->getMock();
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
- $cal = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')
+ $cal = $this->getMockBuilder(CalDavBackend::class)
->disableOriginalConstructor()
->getMock();
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
@@ -59,7 +75,7 @@ class HookManagerTest extends TestCase {
'personal', ['{DAV:}displayname' => 'Personal']);
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
- $card = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
+ $card = $this->getMockBuilder(CardDavBackend::class)
->disableOriginalConstructor()
->getMock();
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
@@ -72,31 +88,31 @@ class HookManagerTest extends TestCase {
}
public function testWithExisting() {
- $user = $this->getMockBuilder('\OCP\IUser')
+ $user = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$user->expects($this->once())->method('getUID')->willReturn('newUser');
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
- $userManager = $this->getMockBuilder('\OCP\IUserManager')
+ $userManager = $this->getMockBuilder(IUserManager::class)
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
- $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
+ $syncService = $this->getMockBuilder(SyncService::class)
->disableOriginalConstructor()
->getMock();
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
- $cal = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')
+ $cal = $this->getMockBuilder(CalDavBackend::class)
->disableOriginalConstructor()
->getMock();
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1);
$cal->expects($this->never())->method('createCalendar');
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
- $card = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
+ $card = $this->getMockBuilder(CardDavBackend::class)
->disableOriginalConstructor()
->getMock();
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
@@ -107,24 +123,24 @@ class HookManagerTest extends TestCase {
}
public function testWithBirthdayCalendar() {
- $user = $this->getMockBuilder('\OCP\IUser')
+ $user = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$user->expects($this->once())->method('getUID')->willReturn('newUser');
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
- $userManager = $this->getMockBuilder('\OCP\IUserManager')
+ $userManager = $this->getMockBuilder(IUserManager::class)
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);
/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
- $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
+ $syncService = $this->getMockBuilder(SyncService::class)
->disableOriginalConstructor()
->getMock();
/** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
- $cal = $this->getMockBuilder('OCA\DAV\CalDAV\CalDavBackend')
+ $cal = $this->getMockBuilder(CalDavBackend::class)
->disableOriginalConstructor()
->getMock();
$cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
@@ -133,7 +149,7 @@ class HookManagerTest extends TestCase {
'personal', ['{DAV:}displayname' => 'Personal']);
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
- $card = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')
+ $card = $this->getMockBuilder(CardDavBackend::class)
->disableOriginalConstructor()
->getMock();
$card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
@@ -144,4 +160,46 @@ class HookManagerTest extends TestCase {
$hm = new HookManager($userManager, $syncService, $cal, $card);
$hm->postLogin(['uid' => 'newUser']);
}
+
+ public function testDeleteCalendar() {
+ $user = $this->getMockBuilder(IUser::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
+ $userManager = $this->getMockBuilder(IUserManager::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $userManager->expects($this->once())->method('get')->willReturn($user);
+
+ /** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
+ $syncService = $this->getMockBuilder(SyncService::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $syncService->expects($this->once())
+ ->method('deleteUser');
+
+ /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
+ $cal = $this->getMockBuilder(CalDavBackend::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $cal->expects($this->once())->method('getUsersOwnCalendars')->willReturn([
+ ['id' => 'personal']
+ ]);
+ $cal->expects($this->once())->method('deleteCalendar');
+ $cal->expects($this->once())->method('deleteAllSharesByUser');
+
+ /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
+ $card = $this->getMockBuilder(CardDavBackend::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $card->expects($this->once())->method('getAddressBooksForUser')->willReturn([
+ ['id' => 'personal']
+ ]);
+ $card->expects($this->once())->method('deleteAddressBook');
+
+ $hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n);
+ $hm->preDeleteUser(['uid' => 'newUser']);
+ $hm->postDeleteUser(['uid' => 'newUser']);
+ }
}