1
0

Proxy.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Roger Szabo <roger.szabo@web.de>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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. namespace OCA\User_LDAP;
  34. use OCA\User_LDAP\Mapping\GroupMapping;
  35. use OCA\User_LDAP\Mapping\UserMapping;
  36. use OCA\User_LDAP\User\Manager;
  37. use OCP\IConfig;
  38. use OCP\IUserManager;
  39. use Psr\Log\LoggerInterface;
  40. abstract class Proxy {
  41. private static $accesses = [];
  42. private $ldap = null;
  43. /** @var bool */
  44. private $isSingleBackend;
  45. /** @var \OCP\ICache|null */
  46. private $cache;
  47. /**
  48. * @param ILDAPWrapper $ldap
  49. */
  50. public function __construct(ILDAPWrapper $ldap) {
  51. $this->ldap = $ldap;
  52. $memcache = \OC::$server->getMemCacheFactory();
  53. if ($memcache->isAvailable()) {
  54. $this->cache = $memcache->createDistributed();
  55. }
  56. }
  57. /**
  58. * @param string $configPrefix
  59. */
  60. private function addAccess(string $configPrefix): void {
  61. $ocConfig = \OC::$server->get(IConfig::class);
  62. $userMap = \OC::$server->get(UserMapping::class);
  63. $groupMap = \OC::$server->get(GroupMapping::class);
  64. $coreUserManager = \OC::$server->get(IUserManager::class);
  65. $logger = \OC::$server->get(LoggerInterface::class);
  66. $helper = \OC::$server->get(Helper::class);
  67. $userManager = \OC::$server->get(Manager::class);
  68. $connector = new Connection($this->ldap, $configPrefix);
  69. $access = new Access($connector, $this->ldap, $userManager, $helper, $ocConfig, $coreUserManager, $logger);
  70. $access->setUserMapper($userMap);
  71. $access->setGroupMapper($groupMap);
  72. self::$accesses[$configPrefix] = $access;
  73. }
  74. /**
  75. * @param string $configPrefix
  76. * @return mixed
  77. */
  78. protected function getAccess($configPrefix) {
  79. if (!isset(self::$accesses[$configPrefix])) {
  80. $this->addAccess($configPrefix);
  81. }
  82. return self::$accesses[$configPrefix];
  83. }
  84. /**
  85. * @param string $uid
  86. * @return string
  87. */
  88. protected function getUserCacheKey($uid) {
  89. return 'user-' . $uid . '-lastSeenOn';
  90. }
  91. /**
  92. * @param string $gid
  93. * @return string
  94. */
  95. protected function getGroupCacheKey($gid) {
  96. return 'group-' . $gid . '-lastSeenOn';
  97. }
  98. /**
  99. * @param string $id
  100. * @param string $method
  101. * @param array $parameters
  102. * @param bool $passOnWhen
  103. * @return mixed
  104. */
  105. abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  106. /**
  107. * @param string $id
  108. * @param string $method
  109. * @param array $parameters
  110. * @return mixed
  111. */
  112. abstract protected function walkBackends($id, $method, $parameters);
  113. /**
  114. * @param string $id
  115. * @return Access
  116. */
  117. abstract public function getLDAPAccess($id);
  118. abstract protected function activeBackends(): int;
  119. protected function isSingleBackend(): bool {
  120. if ($this->isSingleBackend === null) {
  121. $this->isSingleBackend = $this->activeBackends() === 1;
  122. }
  123. return $this->isSingleBackend;
  124. }
  125. /**
  126. * Takes care of the request to the User backend
  127. *
  128. * @param string $id
  129. * @param string $method string, the method of the user backend that shall be called
  130. * @param array $parameters an array of parameters to be passed
  131. * @param bool $passOnWhen
  132. * @return mixed, the result of the specified method
  133. */
  134. protected function handleRequest($id, $method, $parameters, $passOnWhen = false) {
  135. if (!$this->isSingleBackend()) {
  136. $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  137. }
  138. if (!isset($result) || $result === $passOnWhen) {
  139. $result = $this->walkBackends($id, $method, $parameters);
  140. }
  141. return $result;
  142. }
  143. /**
  144. * @param string|null $key
  145. * @return string
  146. */
  147. private function getCacheKey($key) {
  148. $prefix = 'LDAP-Proxy-';
  149. if ($key === null) {
  150. return $prefix;
  151. }
  152. return $prefix . hash('sha256', $key);
  153. }
  154. /**
  155. * @param string $key
  156. * @return mixed|null
  157. */
  158. public function getFromCache($key) {
  159. if ($this->cache === null) {
  160. return null;
  161. }
  162. $key = $this->getCacheKey($key);
  163. $value = $this->cache->get($key);
  164. if ($value === null) {
  165. return null;
  166. }
  167. return json_decode(base64_decode($value));
  168. }
  169. /**
  170. * @param string $key
  171. * @param mixed $value
  172. */
  173. public function writeToCache($key, $value) {
  174. if ($this->cache === null) {
  175. return;
  176. }
  177. $key = $this->getCacheKey($key);
  178. $value = base64_encode(json_encode($value));
  179. $this->cache->set($key, $value, 2592000);
  180. }
  181. public function clearCache() {
  182. if ($this->cache === null) {
  183. return;
  184. }
  185. $this->cache->clear($this->getCacheKey(null));
  186. }
  187. }