Completed
Push — master ( bb1af9...9932b7 )
by Lukas
77:15 queued 63:44
created

Proxy::isCached()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Christopher Schäpers <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Robin McCorkell <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OCA\User_LDAP;
32
33
use OCA\User_LDAP\Mapping\UserMapping;
34
use OCA\User_LDAP\Mapping\GroupMapping;
35
use OCA\User_LDAP\User\Manager;
36
37
abstract class Proxy {
38
	static private $accesses = array();
39
	private $ldap = null;
40
41
	/** @var \OCP\ICache|null */
42
	private $cache;
43
44
	/**
45
	 * @param ILDAPWrapper $ldap
46
	 */
47
	public function __construct(ILDAPWrapper $ldap) {
48
		$this->ldap = $ldap;
49
		$memcache = \OC::$server->getMemCacheFactory();
50
		if($memcache->isAvailable()) {
51
			$this->cache = $memcache->create();
52
		}
53
	}
54
55
	/**
56
	 * @param string $configPrefix
57
	 */
58
	private function addAccess($configPrefix) {
59
		static $ocConfig;
60
		static $fs;
61
		static $log;
62
		static $avatarM;
63
		static $userMap;
64
		static $groupMap;
65
		static $db;
66
		static $coreUserManager;
67
		static $coreNotificationManager;
68
		if($fs === null) {
69
			$ocConfig = \OC::$server->getConfig();
70
			$fs       = new FilesystemHelper();
71
			$log      = new LogWrapper();
72
			$avatarM  = \OC::$server->getAvatarManager();
73
			$db       = \OC::$server->getDatabaseConnection();
74
			$userMap  = new UserMapping($db);
75
			$groupMap = new GroupMapping($db);
76
			$coreUserManager = \OC::$server->getUserManager();
77
			$coreNotificationManager = \OC::$server->getNotificationManager();
78
		}
79
		$userManager =
80
			new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db,
81
				$coreUserManager, $coreNotificationManager);
82
		$connector = new Connection($this->ldap, $configPrefix);
83
		$access = new Access($connector, $this->ldap, $userManager, new Helper(\OC::$server->getConfig()));
84
		$access->setUserMapper($userMap);
85
		$access->setGroupMapper($groupMap);
86
		self::$accesses[$configPrefix] = $access;
87
	}
88
89
	/**
90
	 * @param string $configPrefix
91
	 * @return mixed
92
	 */
93
	protected function getAccess($configPrefix) {
94
		if(!isset(self::$accesses[$configPrefix])) {
95
			$this->addAccess($configPrefix);
96
		}
97
		return self::$accesses[$configPrefix];
98
	}
99
100
	/**
101
	 * @param string $uid
102
	 * @return string
103
	 */
104
	protected function getUserCacheKey($uid) {
105
		return 'user-'.$uid.'-lastSeenOn';
106
	}
107
108
	/**
109
	 * @param string $gid
110
	 * @return string
111
	 */
112
	protected function getGroupCacheKey($gid) {
113
		return 'group-'.$gid.'-lastSeenOn';
114
	}
115
116
	/**
117
	 * @param string $id
118
	 * @param string $method
119
	 * @param array $parameters
120
	 * @param bool $passOnWhen
121
	 * @return mixed
122
	 */
123
	abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
124
125
	/**
126
	 * @param string $id
127
	 * @param string $method
128
	 * @param array $parameters
129
	 * @return mixed
130
	 */
131
	abstract protected function walkBackends($id, $method, $parameters);
132
133
	/**
134
	 * @param string $id
135
	 * @return Access
136
	 */
137
	abstract public function getLDAPAccess($id);
138
139
	/**
140
	 * Takes care of the request to the User backend
141
	 * @param string $id
142
	 * @param string $method string, the method of the user backend that shall be called
143
	 * @param array $parameters an array of parameters to be passed
144
	 * @param bool $passOnWhen
145
	 * @return mixed, the result of the specified method
0 ignored issues
show
Documentation introduced by
The doc-type mixed, could not be parsed: Expected "|" or "end of type", but got "," at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
146
	 */
147
	protected function handleRequest($id, $method, $parameters, $passOnWhen = false) {
148
		$result = $this->callOnLastSeenOn($id,  $method, $parameters, $passOnWhen);
149
		if($result === $passOnWhen) {
150
			$result = $this->walkBackends($id, $method, $parameters);
151
		}
152
		return $result;
153
	}
154
155
	/**
156
	 * @param string|null $key
157
	 * @return string
158
	 */
159
	private function getCacheKey($key) {
160
		$prefix = 'LDAP-Proxy-';
161
		if($key === null) {
162
			return $prefix;
163
		}
164
		return $prefix.md5($key);
165
	}
166
167
	/**
168
	 * @param string $key
169
	 * @return mixed|null
170
	 */
171
	public function getFromCache($key) {
172
		if($this->cache === null) {
173
			return null;
174
		}
175
176
		$key = $this->getCacheKey($key);
177
		$value = $this->cache->get($key);
178
		if ($value === null) {
179
			return null;
180
		}
181
182
		return json_decode(base64_decode($value));
183
	}
184
185
	/**
186
	 * @param string $key
187
	 * @param mixed $value
188
	 */
189
	public function writeToCache($key, $value) {
190
		if($this->cache === null) {
191
			return;
192
		}
193
		$key   = $this->getCacheKey($key);
194
		$value = base64_encode(json_encode($value));
195
		$this->cache->set($key, $value, 2592000);
196
	}
197
198
	public function clearCache() {
199
		if($this->cache === null) {
200
			return;
201
		}
202
		$this->cache->clear($this->getCacheKey(null));
203
	}
204
}
205