Completed
Pull Request — master (#32303)
by Victor
09:58
created

Address::getCloudId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\FederatedFileSharing;
23
24
/**
25
 * Class Address
26
 *
27
 * @package OCA\FederatedFileSharing
28
 */
29
class Address {
30
	/**
31
	 * @var string
32
	 */
33
	protected $cloudId;
34
35
	/**
36
	 * @var string
37
	 */
38
	protected $displayName;
39
40
	/**
41
	 * Address constructor.
42
	 *
43
	 * @param string $cloudId
44
	 * @param string $displayName
45
	 */
46
	public function __construct($cloudId, $displayName = '') {
47
		$this->cloudId = $cloudId;
48
		$this->displayName = $displayName;
49
	}
50
51
	/**
52
	 * Get user federated id
53
	 *
54
	 * @return string
55
	 */
56
	public function getCloudId() {
57
		$hostName = $this->getHostName();
58
		$userId = $this->getUserId();
59
		return "{$userId}@{$hostName}";
60
	}
61
62
	/**
63
	 * Get user id
64
	 *
65
	 * @return string
66
	 */
67
	public function getUserId() {
68
		// userId is everything except the last part
69
		$parts = \explode('@', $this->cloudId);
70
		\array_pop($parts);
71
		return \implode('@', $parts);
72
	}
73
74
	/**
75
	 * Get user host without protocol and trailing slash
76
	 *
77
	 * @return string
78
	 */
79
	public function getHostName() {
80
		// hostname is the last part
81
		$parts = \explode('@', $this->cloudId);
82
		$rawHostName = \array_pop($parts);
83
		if ($fileNamePosition = \strpos($rawHostName, '/index.php')) {
84
			$rawHostName = \substr($rawHostName, 0, $fileNamePosition);
85
		}
86
87
		$normalizedHostname = \rtrim(
88
			\strtolower($rawHostName),
89
			'/'
90
		);
91
92
		// replace all characters before :// and :// itself
93
		return \preg_replace('|^(.*?://)|', '', $normalizedHostname);
94
	}
95
96
	/**
97
	 * Get user display name, fallback to userId if it is empty
98
	 *
99
	 * @return string
100
	 */
101
	public function getDisplayName() {
102
		return ($this->displayName !== '') ? $this->displayName : $this->getUserId();
103
	}
104
105
	/**
106
	 * Checks if the user and host is the same with another address
107
	 *
108
	 * @param Address $address
109
	 *
110
	 * @return bool
111
	 */
112
	public function equalTo(Address $address) {
113
		$thisUserId = $this->translateUid($this->getUserId());
114
		$otherUserId = $this->translateUid($address->getUserId());
115
		return $this->getHostName() === $address->getHostName()
116
			&& $thisUserId === $otherUserId;
117
	}
118
119
	/**
120
	 * @param string $uid
121
	 * @return mixed
122
	 */
123
	protected function translateUid($uid) {
124
		// FIXME this should be a method in the user management instead
125
		// Move to a helper instead of C&P meanwhile?
126
		\OCP\Util::emitHook(
127
			'\OCA\Files_Sharing\API\Server2Server',
128
			'preLoginNameUsedAsUserName',
129
			['uid' => &$uid]
130
		);
131
		return $uid;
132
	}
133
}
134