Proxy.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 OCP\ICache;
  37. use OCP\Server;
  38. abstract class Proxy {
  39. /** @var array<string,Access> */
  40. private static array $accesses = [];
  41. private ILDAPWrapper $ldap;
  42. private ?bool $isSingleBackend = null;
  43. private ?ICache $cache = null;
  44. private AccessFactory $accessFactory;
  45. public function __construct(
  46. ILDAPWrapper $ldap,
  47. AccessFactory $accessFactory
  48. ) {
  49. $this->ldap = $ldap;
  50. $this->accessFactory = $accessFactory;
  51. $memcache = \OC::$server->getMemCacheFactory();
  52. if ($memcache->isAvailable()) {
  53. $this->cache = $memcache->createDistributed();
  54. }
  55. }
  56. private function addAccess(string $configPrefix): void {
  57. $userMap = Server::get(UserMapping::class);
  58. $groupMap = Server::get(GroupMapping::class);
  59. $connector = new Connection($this->ldap, $configPrefix);
  60. $access = $this->accessFactory->get($connector);
  61. $access->setUserMapper($userMap);
  62. $access->setGroupMapper($groupMap);
  63. self::$accesses[$configPrefix] = $access;
  64. }
  65. protected function getAccess(string $configPrefix): Access {
  66. if (!isset(self::$accesses[$configPrefix])) {
  67. $this->addAccess($configPrefix);
  68. }
  69. return self::$accesses[$configPrefix];
  70. }
  71. /**
  72. * @param string $uid
  73. * @return string
  74. */
  75. protected function getUserCacheKey($uid) {
  76. return 'user-' . $uid . '-lastSeenOn';
  77. }
  78. /**
  79. * @param string $gid
  80. * @return string
  81. */
  82. protected function getGroupCacheKey($gid) {
  83. return 'group-' . $gid . '-lastSeenOn';
  84. }
  85. /**
  86. * @param string $id
  87. * @param string $method
  88. * @param array $parameters
  89. * @param bool $passOnWhen
  90. * @return mixed
  91. */
  92. abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  93. /**
  94. * @param string $id
  95. * @param string $method
  96. * @param array $parameters
  97. * @return mixed
  98. */
  99. abstract protected function walkBackends($id, $method, $parameters);
  100. /**
  101. * @param string $id
  102. * @return Access
  103. */
  104. abstract public function getLDAPAccess($id);
  105. abstract protected function activeBackends(): int;
  106. protected function isSingleBackend(): bool {
  107. if ($this->isSingleBackend === null) {
  108. $this->isSingleBackend = $this->activeBackends() === 1;
  109. }
  110. return $this->isSingleBackend;
  111. }
  112. /**
  113. * Takes care of the request to the User backend
  114. *
  115. * @param string $id
  116. * @param string $method string, the method of the user backend that shall be called
  117. * @param array $parameters an array of parameters to be passed
  118. * @param bool $passOnWhen
  119. * @return mixed, the result of the specified method
  120. */
  121. protected function handleRequest($id, $method, $parameters, $passOnWhen = false) {
  122. if (!$this->isSingleBackend()) {
  123. $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  124. }
  125. if (!isset($result) || $result === $passOnWhen) {
  126. $result = $this->walkBackends($id, $method, $parameters);
  127. }
  128. return $result;
  129. }
  130. /**
  131. * @param string|null $key
  132. * @return string
  133. */
  134. private function getCacheKey($key) {
  135. $prefix = 'LDAP-Proxy-';
  136. if ($key === null) {
  137. return $prefix;
  138. }
  139. return $prefix . hash('sha256', $key);
  140. }
  141. /**
  142. * @param string $key
  143. * @return mixed|null
  144. */
  145. public function getFromCache($key) {
  146. if ($this->cache === null) {
  147. return null;
  148. }
  149. $key = $this->getCacheKey($key);
  150. $value = $this->cache->get($key);
  151. if ($value === null) {
  152. return null;
  153. }
  154. return json_decode(base64_decode($value));
  155. }
  156. /**
  157. * @param string $key
  158. * @param mixed $value
  159. */
  160. public function writeToCache($key, $value) {
  161. if ($this->cache === null) {
  162. return;
  163. }
  164. $key = $this->getCacheKey($key);
  165. $value = base64_encode(json_encode($value));
  166. $this->cache->set($key, $value, 2592000);
  167. }
  168. public function clearCache() {
  169. if ($this->cache === null) {
  170. return;
  171. }
  172. $this->cache->clear($this->getCacheKey(null));
  173. }
  174. }