Proxy.php 5.4 KB

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