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 Christoph Wurst <[email protected]> |
9
|
|
|
* @author Joas Schilling <[email protected]> |
10
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
11
|
|
|
* @author Lukas Reschke <[email protected]> |
12
|
|
|
* @author Morris Jobke <[email protected]> |
13
|
|
|
* @author Robin McCorkell <[email protected]> |
14
|
|
|
* @author Roeland Jago Douma <[email protected]> |
15
|
|
|
* @author Roger Szabo <[email protected]> |
16
|
|
|
* @author Thomas Müller <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @license AGPL-3.0 |
19
|
|
|
* |
20
|
|
|
* This code is free software: you can redistribute it and/or modify |
21
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
22
|
|
|
* as published by the Free Software Foundation. |
23
|
|
|
* |
24
|
|
|
* This program is distributed in the hope that it will be useful, |
25
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
26
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
27
|
|
|
* GNU Affero General Public License for more details. |
28
|
|
|
* |
29
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
30
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
namespace OCA\User_LDAP; |
35
|
|
|
|
36
|
|
|
use OCA\User_LDAP\Mapping\GroupMapping; |
37
|
|
|
use OCA\User_LDAP\Mapping\UserMapping; |
38
|
|
|
use OCA\User_LDAP\User\Manager; |
39
|
|
|
|
40
|
|
|
abstract class Proxy { |
41
|
|
|
private static $accesses = []; |
42
|
|
|
private $ldap = null; |
43
|
|
|
/** @var bool */ |
44
|
|
|
private $isSingleBackend; |
45
|
|
|
|
46
|
|
|
/** @var \OCP\ICache|null */ |
47
|
|
|
private $cache; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param ILDAPWrapper $ldap |
51
|
|
|
*/ |
52
|
|
|
public function __construct(ILDAPWrapper $ldap) { |
53
|
|
|
$this->ldap = $ldap; |
54
|
|
|
$memcache = \OC::$server->getMemCacheFactory(); |
55
|
|
|
if ($memcache->isAvailable()) { |
56
|
|
|
$this->cache = $memcache->createDistributed(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $configPrefix |
62
|
|
|
*/ |
63
|
|
|
private function addAccess($configPrefix) { |
64
|
|
|
static $ocConfig; |
65
|
|
|
static $fs; |
66
|
|
|
static $log; |
67
|
|
|
static $avatarM; |
68
|
|
|
static $userMap; |
69
|
|
|
static $groupMap; |
70
|
|
|
static $db; |
71
|
|
|
static $coreUserManager; |
72
|
|
|
static $coreNotificationManager; |
73
|
|
|
if ($fs === null) { |
74
|
|
|
$ocConfig = \OC::$server->getConfig(); |
75
|
|
|
$fs = new FilesystemHelper(); |
76
|
|
|
$log = new LogWrapper(); |
77
|
|
|
$avatarM = \OC::$server->getAvatarManager(); |
78
|
|
|
$db = \OC::$server->getDatabaseConnection(); |
79
|
|
|
$userMap = new UserMapping($db); |
80
|
|
|
$groupMap = new GroupMapping($db); |
81
|
|
|
$coreUserManager = \OC::$server->getUserManager(); |
82
|
|
|
$coreNotificationManager = \OC::$server->getNotificationManager(); |
83
|
|
|
} |
84
|
|
|
$userManager = |
85
|
|
|
new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db, |
86
|
|
|
$coreUserManager, $coreNotificationManager); |
87
|
|
|
$connector = new Connection($this->ldap, $configPrefix); |
88
|
|
|
$access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig, $coreUserManager); |
89
|
|
|
$access->setUserMapper($userMap); |
90
|
|
|
$access->setGroupMapper($groupMap); |
91
|
|
|
self::$accesses[$configPrefix] = $access; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $configPrefix |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
protected function getAccess($configPrefix) { |
99
|
|
|
if (!isset(self::$accesses[$configPrefix])) { |
100
|
|
|
$this->addAccess($configPrefix); |
101
|
|
|
} |
102
|
|
|
return self::$accesses[$configPrefix]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $uid |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function getUserCacheKey($uid) { |
110
|
|
|
return 'user-' . $uid . '-lastSeenOn'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $gid |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function getGroupCacheKey($gid) { |
118
|
|
|
return 'group-' . $gid . '-lastSeenOn'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $id |
123
|
|
|
* @param string $method |
124
|
|
|
* @param array $parameters |
125
|
|
|
* @param bool $passOnWhen |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $id |
132
|
|
|
* @param string $method |
133
|
|
|
* @param array $parameters |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
|
|
abstract protected function walkBackends($id, $method, $parameters); |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $id |
140
|
|
|
* @return Access |
141
|
|
|
*/ |
142
|
|
|
abstract public function getLDAPAccess($id); |
143
|
|
|
|
144
|
|
|
abstract protected function activeBackends(): int; |
145
|
|
|
|
146
|
|
|
protected function isSingleBackend(): bool { |
147
|
|
|
if ($this->isSingleBackend === null) { |
148
|
|
|
$this->isSingleBackend = $this->activeBackends() === 1; |
149
|
|
|
} |
150
|
|
|
return $this->isSingleBackend; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Takes care of the request to the User backend |
155
|
|
|
* |
156
|
|
|
* @param string $id |
157
|
|
|
* @param string $method string, the method of the user backend that shall be called |
158
|
|
|
* @param array $parameters an array of parameters to be passed |
159
|
|
|
* @param bool $passOnWhen |
160
|
|
|
* @return mixed, the result of the specified method |
161
|
|
|
*/ |
162
|
|
|
protected function handleRequest($id, $method, $parameters, $passOnWhen = false) { |
163
|
|
|
if (!$this->isSingleBackend()) { |
164
|
|
|
$result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
165
|
|
|
} |
166
|
|
|
if (!isset($result) || $result === $passOnWhen) { |
167
|
|
|
$result = $this->walkBackends($id, $method, $parameters); |
168
|
|
|
} |
169
|
|
|
return $result; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string|null $key |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
private function getCacheKey($key) { |
177
|
|
|
$prefix = 'LDAP-Proxy-'; |
178
|
|
|
if ($key === null) { |
179
|
|
|
return $prefix; |
180
|
|
|
} |
181
|
|
|
return $prefix . hash('sha256', $key); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $key |
186
|
|
|
* @return mixed|null |
187
|
|
|
*/ |
188
|
|
|
public function getFromCache($key) { |
189
|
|
|
if ($this->cache === null) { |
190
|
|
|
return null; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$key = $this->getCacheKey($key); |
194
|
|
|
$value = $this->cache->get($key); |
195
|
|
|
if ($value === null) { |
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return json_decode(base64_decode($value)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $key |
204
|
|
|
* @param mixed $value |
205
|
|
|
*/ |
206
|
|
|
public function writeToCache($key, $value) { |
207
|
|
|
if ($this->cache === null) { |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
$key = $this->getCacheKey($key); |
211
|
|
|
$value = base64_encode(json_encode($value)); |
212
|
|
|
$this->cache->set($key, $value, 2592000); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function clearCache() { |
216
|
|
|
if ($this->cache === null) { |
217
|
|
|
return; |
218
|
|
|
} |
219
|
|
|
$this->cache->clear($this->getCacheKey(null)); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|