Proxy.php 5.2 KB

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