Completed
Push — master ( a3c203...38961a )
by Morris
99:43 queued 81:59
created

HookManager::preUnassignedUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author John Molakvoæ (skjnldsv) <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author Thomas Citharel <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
namespace OCA\DAV;
28
29
use OCA\DAV\CalDAV\CalDavBackend;
30
use OCA\DAV\CardDAV\CardDavBackend;
31
use OCA\DAV\CardDAV\SyncService;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
use OCP\Util;
35
use Symfony\Component\EventDispatcher\EventDispatcher;
36
37
class HookManager {
38
39
	/** @var IUserManager */
40
	private $userManager;
41
42
	/** @var SyncService */
43
	private $syncService;
44
45
	/** @var IUser[] */
46
	private $usersToDelete = [];
47
48
	/** @var CalDavBackend */
49
	private $calDav;
50
51
	/** @var CardDavBackend */
52
	private $cardDav;
53
54
	/** @var array */
55
	private $calendarsToDelete = [];
56
57
	/** @var array */
58
	private $addressBooksToDelete = [];
59
60
	/** @var EventDispatcher */
61
	private $eventDispatcher;
62
63
	public function __construct(IUserManager $userManager,
64
								SyncService $syncService,
65
								CalDavBackend $calDav,
66
								CardDavBackend $cardDav,
67
								EventDispatcher $eventDispatcher) {
68
		$this->userManager = $userManager;
69
		$this->syncService = $syncService;
70
		$this->calDav = $calDav;
71
		$this->cardDav = $cardDav;
72
		$this->eventDispatcher = $eventDispatcher;
73
	}
74
75
	public function setup() {
76
		Util::connectHook('OC_User',
77
			'post_createUser',
78
			$this,
79
			'postCreateUser');
80
		\OC::$server->getUserManager()->listen('\OC\User', 'assignedUserId', function ($uid) {
81
			$this->postCreateUser(['uid' => $uid]);
82
		});
83
		Util::connectHook('OC_User',
84
			'pre_deleteUser',
85
			$this,
86
			'preDeleteUser');
87
		\OC::$server->getUserManager()->listen('\OC\User', 'preUnassignedUserId', [$this, 'preUnassignedUserId']);
88
		Util::connectHook('OC_User',
89
			'post_deleteUser',
90
			$this,
91
			'postDeleteUser');
92
		\OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', function ($uid) {
93
			$this->postDeleteUser(['uid' => $uid]);
94
		});
95
		\OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', [$this, 'postUnassignedUserId']);
96
		Util::connectHook('OC_User',
97
			'changeUser',
98
			$this,
99
			'changeUser');
100
	}
101
102
	public function postCreateUser($params) {
103
		$user = $this->userManager->get($params['uid']);
104
		$this->syncService->updateUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userManager->get($params['uid']) on line 103 can be null; however, OCA\DAV\CardDAV\SyncService::updateUser() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
105
	}
106
107
	public function preDeleteUser($params) {
108
		$uid = $params['uid'];
109
		$this->usersToDelete[$uid] = $this->userManager->get($uid);
110
		$this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid);
111
		$this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid);
112
	}
113
114
	public function preUnassignedUserId($uid) {
115
		$this->usersToDelete[$uid] = $this->userManager->get($uid);
116
	}
117
118
	public function postDeleteUser($params) {
119
		$uid = $params['uid'];
120
		if (isset($this->usersToDelete[$uid])){
121
			$this->syncService->deleteUser($this->usersToDelete[$uid]);
122
		}
123
124
		foreach ($this->calendarsToDelete as $calendar) {
125
			$this->calDav->deleteCalendar($calendar['id']);
126
		}
127
		$this->calDav->deleteAllSharesByUser('principals/users/' . $uid);
128
129
		foreach ($this->addressBooksToDelete as $addressBook) {
130
			$this->cardDav->deleteAddressBook($addressBook['id']);
131
		}
132
	}
133
134
	public function postUnassignedUserId($uid) {
135
		if (isset($this->usersToDelete[$uid])){
136
			$this->syncService->deleteUser($this->usersToDelete[$uid]);
137
		}
138
	}
139
140
	public function changeUser($params) {
141
		$user = $params['user'];
142
		$this->syncService->updateUser($user);
143
	}
144
145
	public function firstLogin(IUser $user = null) {
146
		if (!is_null($user)) {
147
			$principal = 'principals/users/' . $user->getUID();
148
			if ($this->calDav->getCalendarsForUserCount($principal) === 0) {
149
				try {
150
					$this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [
151
						'{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME,
152
					]);
153
				} catch (\Exception $ex) {
154
					\OC::$server->getLogger()->logException($ex);
0 ignored issues
show
Documentation introduced by
$ex is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
				}
156
			}
157
			if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) {
158
				try {
159
					$this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [
160
						'{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME,
161
					]);
162
				} catch (\Exception $ex) {
163
					\OC::$server->getLogger()->logException($ex);
0 ignored issues
show
Documentation introduced by
$ex is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
164
				}
165
			}
166
		}
167
	}
168
}
169