Completed
Pull Request — master (#315)
by Joas
04:21 queued 02:19
created

lib/CurrentUser.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Activity;
23
24
25
use OCP\IRequest;
26
use OCP\IUser;
27
use OCP\IUserSession;
28
use OCP\Share;
29
use OCP\Share\Exceptions\ShareNotFound;
30
use OCP\Share\IManager;
31
32
class CurrentUser {
33
34
	/** @var IUserSession */
35
	protected $userSession;
36
	/** @var IRequest */
37
	protected $request;
38
	/** @var IManager */
39
	protected $shareManager;
40
41
	/** @var string */
42
	protected $identifier;
43
	/** @var string|null */
44
	protected $cloudId;
45
	/** @var string|false|null */
46
	protected $sessionUser;
47
48
	/**
49
	 * @param IUserSession $userSession
50
	 * @param IRequest $request
51
	 * @param IManager $shareManager
52
	 */
53 35
	public function __construct(IUserSession $userSession, IRequest $request, IManager $shareManager) {
54 35
		$this->userSession = $userSession;
55 35
		$this->request = $request;
56 35
		$this->shareManager = $shareManager;
57 35
		$this->cloudId = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type string|null of property $cloudId.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58 35
		$this->sessionUser = false;
59 35
	}
60
61
	/**
62
	 * Get an identifier for the user, session or token
63
	 * @return string
64
	 */
65 4
	public function getUserIdentifier() {
66 4
		if ($this->identifier === null) {
67 3
			$this->identifier = $this->getUID();
68
69 3
			if ($this->identifier === null) {
70 2
				$this->identifier = $this->getCloudIDFromToken();
71
72 2
				if ($this->identifier === null) {
73
					// Nothing worked, fallback to empty string
74 1
					$this->identifier = '';
75
				}
76
			}
77
		}
78
79 4
		return $this->identifier;
80
	}
81
82
	/**
83
	 * Get the current user from the session
84
	 * @return string|null
85
	 */
86 22
	public function getUID() {
87 22
		if ($this->sessionUser === false) {
88 22
			$user = $this->userSession->getUser();
89 22
			if ($user instanceof IUser) {
0 ignored issues
show
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
90 2
				$this->sessionUser = (string) $user->getUID();
91
			} else {
92 20
				$this->sessionUser = null;
93
			}
94
		}
95
96 22
		return $this->sessionUser;
97
	}
98
99
	/**
100
	 * Get the current user from the session
101
	 * @return string|null
102
	 */
103
	public function getCloudId() {
104
		if ($this->cloudId === false) {
105
			$user = $this->userSession->getUser();
106
			if ($user instanceof IUser) {
0 ignored issues
show
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
107
				$this->cloudId = (string) $user->getCloudId();
108
			} else {
109
				$this->cloudId = $this->getCloudIDFromToken();
110
			}
111
		}
112
113
		return $this->cloudId;
114
	}
115
116
	/**
117
	 * Get the cloud ID from the sharing token
118
	 * @return string|null
119
	 */
120 4
	protected function getCloudIDFromToken() {
121 4
		if (!empty($this->request->server['PHP_AUTH_USER'])) {
122 3
			$token = $this->request->server['PHP_AUTH_USER'];
123
			try {
124 3
				$share = $this->shareManager->getShareByToken($token);
125 2
				if ($share->getShareType() === Share::SHARE_TYPE_REMOTE) {
126 2
					return $share->getSharedWith();
127
				}
128 1
			} catch (ShareNotFound $e) {
0 ignored issues
show
The class OCP\Share\Exceptions\ShareNotFound does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
129
				// No share, use the fallback
130
			}
131
		}
132
133 3
		return null;
134
	}
135
}
136