aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2014-10-28 09:33:54 +0100
committerMorris Jobke <hey@morrisjobke.de>2014-10-28 09:33:54 +0100
commit4461e69873aac223fea410d8e78c3e7674541c17 (patch)
tree09d5ec0c89b02274c6832a58fef8ecb615459ef6
parent23873d80fe1bf7c47a991acbbcf52e68c9aebf83 (diff)
parent21d825ed6c11425d36a143f8ed63f1e3852d0aeb (diff)
downloadnextcloud-server-4461e69873aac223fea410d8e78c3e7674541c17.tar.gz
nextcloud-server-4461e69873aac223fea410d8e78c3e7674541c17.zip
Merge pull request #11791 from owncloud/webdav-getquotainfo-503-handling
Properly catch 503 storage not available in getQuotaInfo
-rw-r--r--lib/private/connector/sabre/directory.php18
1 files changed, 11 insertions, 7 deletions
diff --git a/lib/private/connector/sabre/directory.php b/lib/private/connector/sabre/directory.php
index 1b6d1f363b8..0d35c7d528e 100644
--- a/lib/private/connector/sabre/directory.php
+++ b/lib/private/connector/sabre/directory.php
@@ -205,13 +205,17 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node
* @return array
*/
public function getQuotaInfo() {
- $path = \OC\Files\Filesystem::getView()->getRelativePath($this->info->getPath());
- $storageInfo = OC_Helper::getStorageInfo($path);
- return array(
- $storageInfo['used'],
- $storageInfo['free']
- );
-
+ try {
+ $path = \OC\Files\Filesystem::getView()->getRelativePath($this->info->getPath());
+ $storageInfo = OC_Helper::getStorageInfo($path);
+ return array(
+ $storageInfo['used'],
+ $storageInfo['free']
+ );
+ }
+ catch (\OCP\Files\StorageNotAvailableException $e) {
+ return array(0, 0);
+ }
}
/**
ref='#n155'>155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
<?php
/**
 * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
 * @copyright 2017 Lukas Reschke <lukas@statuscode.ch>
 *
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
 * @author Georg Ehrke <oc.list@georgehrke.com>
 * @author Julius Härtl <jus@bitgrid.net>
 * @author Lukas Reschke <lukas@statuscode.ch>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 * @author Tobia De Koninck <tobia@ledfan.be>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OC\Contacts\ContactsMenu;

use OCP\Contacts\ContactsMenu\IContactsStore;
use OCP\Contacts\ContactsMenu\IEntry;
use OCP\Contacts\IManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;

class ContactsStore implements IContactsStore {

	/** @var IManager */
	private $contactsManager;

	/** @var IConfig */
	private $config;

	/** @var IUserManager */
	private $userManager;

	/** @var IGroupManager */
	private $groupManager;

	/**
	 * @param IManager $contactsManager
	 * @param IConfig $config
	 * @param IUserManager $userManager
	 * @param IGroupManager $groupManager
	 */
	public function __construct(IManager $contactsManager,
								IConfig $config,
								IUserManager $userManager,
								IGroupManager $groupManager) {
		$this->contactsManager = $contactsManager;
		$this->config = $config;
		$this->userManager = $userManager;
		$this->groupManager = $groupManager;
	}

	/**
	 * @param IUser $user
	 * @param string|null $filter
	 * @return IEntry[]
	 */
	public function getContacts(IUser $user, $filter, ?int $limit = null, ?int $offset = null) {
		$options = [];
		if ($limit !== null) {
			$options['limit'] = $limit;
		}
		if ($offset !== null) {
			$options['offset'] = $offset;
		}

		$allContacts = $this->contactsManager->search(
			$filter ?: '',
			[
				'FN',
				'EMAIL'
			],
			$options
		);

		$entries = array_map(function (array $contact) {
			return $this->contactArrayToEntry($contact);
		}, $allContacts);
		return $this->filterContacts(
			$user,
			$entries,
			$filter
		);
	}

	/**
	 * Filters the contacts. Applies 3 filters:
	 *  1. filter the current user
	 *  2. if the `shareapi_allow_share_dialog_user_enumeration` config option is
	 * enabled it will filter all local users
	 *  3. if the `shareapi_exclude_groups` config option is enabled and the
	 * current user is in an excluded group it will filter all local users.
	 *  4. if the `shareapi_only_share_with_group_members` config option is
	 * enabled it will filter all users which doens't have a common group
	 * with the current user.
	 *
	 * @param IUser $self
	 * @param Entry[] $entries
	 * @param string $filter
	 * @return Entry[] the filtered contacts
	 */
	private function filterContacts(IUser $self,
									array $entries,
									$filter) {
		$disallowEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') !== 'yes';
		$restrictEnumeration = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
		$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes';

		// whether to filter out local users
		$skipLocal = false;
		// whether to filter out all users which doesn't have the same group as the current user
		$ownGroupsOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes' || $restrictEnumeration;

		$selfGroups = $this->groupManager->getUserGroupIds($self);

		if ($excludedGroups) {
			$excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
			$decodedExcludeGroups = json_decode($excludedGroups, true);
			$excludeGroupsList = ($decodedExcludeGroups !== null) ? $decodedExcludeGroups : [];

			if (count(array_intersect($excludeGroupsList, $selfGroups)) !== 0) {
				// a group of the current user is excluded -> filter all local users
				$skipLocal = true;
			}
		}

		$selfUID = $self->getUID();

		return array_values(array_filter($entries, function (IEntry $entry) use ($self, $skipLocal, $ownGroupsOnly, $selfGroups, $selfUID, $disallowEnumeration, $filter) {
			if ($skipLocal && $entry->getProperty('isLocalSystemBook') === true) {
				return false;
			}

			// Prevent enumerating local users
			if ($disallowEnumeration && $entry->getProperty('isLocalSystemBook')) {
				$filterUser = true;

				$mailAddresses = $entry->getEMailAddresses();
				foreach ($mailAddresses as $mailAddress) {
					if ($mailAddress === $filter) {
						$filterUser = false;
						break;
					}
				}

				if ($entry->getProperty('UID') && $entry->getProperty('UID') === $filter) {
					$filterUser = false;
				}

				if ($filterUser) {
					return false;
				}
			}

			if ($ownGroupsOnly && $entry->getProperty('isLocalSystemBook') === true) {
				$uid = $this->userManager->get($entry->getProperty('UID'));

				if ($uid === null) {
					return false;
				}

				$contactGroups = $this->groupManager->getUserGroupIds($uid);
				if (count(array_intersect($contactGroups, $selfGroups)) === 0) {
					// no groups in common, so shouldn't see the contact
					return false;
				}
			}

			return $entry->getProperty('UID') !== $selfUID;
		}));
	}

	/**
	 * @param IUser $user
	 * @param integer $shareType
	 * @param string $shareWith
	 * @return IEntry|null
	 */
	public function findOne(IUser $user, $shareType, $shareWith) {
		switch ($shareType) {
			case 0:
			case 6:
				$filter = ['UID'];
				break;
			case 4:
				$filter = ['EMAIL'];
				break;
			default:
				return null;
		}

		$userId = $user->getUID();
		$allContacts = $this->contactsManager->search($shareWith, $filter);
		$contacts = array_filter($allContacts, function ($contact) use ($userId) {
			return $contact['UID'] !== $userId;
		});
		$match = null;

		foreach ($contacts as $contact) {
			if ($shareType === 4 && isset($contact['EMAIL'])) {
				if (in_array($shareWith, $contact['EMAIL'])) {
					$match = $contact;
					break;
				}
			}
			if ($shareType === 0 || $shareType === 6) {
				$isLocal = $contact['isLocalSystemBook'] ?? false;
				if ($contact['UID'] === $shareWith && $isLocal === true) {
					$match = $contact;
					break;
				}
			}
		}

		if ($match) {
			$match = $this->filterContacts($user, [$this->contactArrayToEntry($match)], $shareWith);
			if (count($match) === 1) {
				$match = $match[0];
			} else {
				$match = null;
			}
		}

		return $match;
	}

	/**
	 * @param array $contact
	 * @return Entry
	 */
	private function contactArrayToEntry(array $contact) {
		$entry = new Entry();

		if (isset($contact['id'])) {
			$entry->setId($contact['id']);
		}

		if (isset($contact['FN'])) {
			$entry->setFullName($contact['FN']);
		}

		$avatarPrefix = "VALUE=uri:";
		if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) {
			$entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix)));
		}

		if (isset($contact['EMAIL'])) {
			foreach ($contact['EMAIL'] as $email) {
				$entry->addEMailAddress($email);
			}
		}

		// Attach all other properties to the entry too because some
		// providers might make use of it.
		$entry->setProperties($contact);

		return $entry;
	}
}