Passed
Push — master ( b85c2d...d5c2e7 )
by Christoph
15:18 queued 13s
created

UserAddressBooks::createExtendedCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2016, ownCloud, Inc.
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Anna Larch <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
namespace OCA\DAV\CardDAV;
30
31
use OCA\DAV\AppInfo\PluginManager;
32
use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
33
use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
34
use OCA\Federation\TrustedServers;
35
use OCP\AppFramework\QueryException;
36
use OCP\IConfig;
37
use OCP\IGroupManager;
38
use OCP\IL10N;
39
use OCP\IRequest;
40
use OCP\IUser;
41
use OCP\IUserSession;
42
use Psr\Container\ContainerExceptionInterface;
43
use Psr\Container\NotFoundExceptionInterface;
44
use Sabre\CardDAV\Backend;
45
use Sabre\DAV\Exception\MethodNotAllowed;
46
use Sabre\CardDAV\IAddressBook;
47
use function array_map;
48
use Sabre\DAV\MkCol;
49
50
class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
51
	/** @var IL10N */
52
	protected $l10n;
53
54
	/** @var IConfig */
55
	protected $config;
56
57
	/** @var PluginManager */
58
	private $pluginManager;
59
	private ?IUser $user;
60
	private ?IGroupManager $groupManager;
61
62
	public function __construct(Backend\BackendInterface $carddavBackend,
63
								string $principalUri,
64
								PluginManager $pluginManager,
65
								?IUser $user,
66
								?IGroupManager $groupManager) {
67
		parent::__construct($carddavBackend, $principalUri);
68
		$this->pluginManager = $pluginManager;
69
		$this->user = $user;
70
		$this->groupManager = $groupManager;
71
	}
72
73
	/**
74
	 * Returns a list of address books
75
	 *
76
	 * @return IAddressBook[]
77
	 */
78
	public function getChildren() {
79
		if ($this->l10n === null) {
80
			$this->l10n = \OC::$server->getL10N('dav');
81
		}
82
		if ($this->config === null) {
83
			$this->config = \OC::$server->getConfig();
84
		}
85
86
		/** @var string|array $principal */
87
		$principal = $this->principalUri;
88
		$addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
0 ignored issues
show
Bug introduced by
$this->principalUri of type array is incompatible with the type string expected by parameter $principalUri of Sabre\CardDAV\Backend\Ba...etAddressBooksForUser(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
		$addressBooks = $this->carddavBackend->getAddressBooksForUser(/** @scrutinizer ignore-type */ $this->principalUri);
Loading history...
89
		// add the system address book
90
		$systemAddressBook = null;
91
		if (is_string($principal) && $principal !== 'principals/system/system' && $this->carddavBackend instanceof CardDavBackend) {
0 ignored issues
show
introduced by
The condition is_string($principal) is always false.
Loading history...
92
			$systemAddressBook = $this->carddavBackend->getAddressBooksByUri('principals/system/system', 'system');
93
			if ($systemAddressBook !== null) {
94
				$systemAddressBook['uri'] = SystemAddressbook::URI_SHARED;
95
			}
96
		}
97
		if (!is_null($systemAddressBook)) {
0 ignored issues
show
introduced by
The condition is_null($systemAddressBook) is always true.
Loading history...
98
			$addressBooks[] = $systemAddressBook;
99
		}
100
101
		$objects = [];
102
		if (!empty($addressBooks)) {
103
			/** @var IAddressBook[] $objects */
104
			$objects = array_map(function (array $addressBook) {
105
				$trustedServers = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $trustedServers is dead and can be removed.
Loading history...
106
				$request = null;
107
				try {
108
					$trustedServers = \OC::$server->get(TrustedServers::class);
109
					$request = \OC::$server->get(IRequest::class);
110
				} catch (NotFoundExceptionInterface | ContainerExceptionInterface $e) {
111
					// nothing to do, the request / trusted servers don't exist
112
				}
113
				if ($addressBook['principaluri'] === 'principals/system/system') {
114
					return new SystemAddressbook(
115
						$this->carddavBackend,
116
						$addressBook,
117
						$this->l10n,
118
						$this->config,
119
						\OCP\Server::get(IUserSession::class),
120
						$request,
121
						$trustedServers,
122
						$this->groupManager
123
					);
124
				}
125
126
				return new AddressBook($this->carddavBackend, $addressBook, $this->l10n);
127
			}, $addressBooks);
128
		}
129
		/** @var IAddressBook[][] $objectsFromPlugins */
130
		$objectsFromPlugins = array_map(function (IAddressBookProvider $plugin): array {
131
			return $plugin->fetchAllForAddressBookHome($this->principalUri);
0 ignored issues
show
Bug introduced by
$this->principalUri of type array is incompatible with the type string expected by parameter $principalUri of OCA\DAV\CardDAV\Integrat...AllForAddressBookHome(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
			return $plugin->fetchAllForAddressBookHome(/** @scrutinizer ignore-type */ $this->principalUri);
Loading history...
132
		}, $this->pluginManager->getAddressBookPlugins());
133
134
		return array_merge($objects, ...$objectsFromPlugins);
135
	}
136
137
	public function createExtendedCollection($name, MkCol $mkCol) {
138
		if (ExternalAddressBook::doesViolateReservedName($name)) {
139
			throw new MethodNotAllowed('The resource you tried to create has a reserved name');
140
		}
141
142
		parent::createExtendedCollection($name, $mkCol);
143
	}
144
145
	/**
146
	 * Returns a list of ACE's for this node.
147
	 *
148
	 * Each ACE has the following properties:
149
	 *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
150
	 *     currently the only supported privileges
151
	 *   * 'principal', a url to the principal who owns the node
152
	 *   * 'protected' (optional), indicating that this ACE is not allowed to
153
	 *      be updated.
154
	 *
155
	 * @return array
156
	 */
157
	public function getACL() {
158
		$acl = parent::getACL();
159
		if ($this->principalUri === 'principals/system/system') {
0 ignored issues
show
introduced by
The condition $this->principalUri === ...incipals/system/system' is always false.
Loading history...
160
			$acl[] = [
161
				'privilege' => '{DAV:}read',
162
				'principal' => '{DAV:}authenticated',
163
				'protected' => true,
164
			];
165
		}
166
167
		return $acl;
168
	}
169
}
170