proxy.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Christopher Schäpers <kondou@ts.unde.re>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Lukas Reschke <lukas@owncloud.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  10. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\user_ldap\lib;
  30. use OCA\user_ldap\lib\Access;
  31. use OCA\User_LDAP\Mapping\UserMapping;
  32. use OCA\User_LDAP\Mapping\GroupMapping;
  33. abstract class Proxy {
  34. static private $accesses = array();
  35. private $ldap = null;
  36. /**
  37. * @param ILDAPWrapper $ldap
  38. */
  39. public function __construct(ILDAPWrapper $ldap) {
  40. $this->ldap = $ldap;
  41. $this->cache = \OC\Cache::getGlobalCache();
  42. }
  43. /**
  44. * @param string $configPrefix
  45. */
  46. private function addAccess($configPrefix) {
  47. static $ocConfig;
  48. static $fs;
  49. static $log;
  50. static $avatarM;
  51. static $userMap;
  52. static $groupMap;
  53. static $db;
  54. if(is_null($fs)) {
  55. $ocConfig = \OC::$server->getConfig();
  56. $fs = new FilesystemHelper();
  57. $log = new LogWrapper();
  58. $avatarM = \OC::$server->getAvatarManager();
  59. $db = \OC::$server->getDatabaseConnection();
  60. $userMap = new UserMapping($db);
  61. $groupMap = new GroupMapping($db);
  62. }
  63. $userManager =
  64. new user\Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db);
  65. $connector = new Connection($this->ldap, $configPrefix);
  66. $access = new Access($connector, $this->ldap, $userManager);
  67. $access->setUserMapper($userMap);
  68. $access->setGroupMapper($groupMap);
  69. self::$accesses[$configPrefix] = $access;
  70. }
  71. /**
  72. * @param string $configPrefix
  73. * @return mixed
  74. */
  75. protected function getAccess($configPrefix) {
  76. if(!isset(self::$accesses[$configPrefix])) {
  77. $this->addAccess($configPrefix);
  78. }
  79. return self::$accesses[$configPrefix];
  80. }
  81. /**
  82. * @param string $uid
  83. * @return string
  84. */
  85. protected function getUserCacheKey($uid) {
  86. return 'user-'.$uid.'-lastSeenOn';
  87. }
  88. /**
  89. * @param string $gid
  90. * @return string
  91. */
  92. protected function getGroupCacheKey($gid) {
  93. return 'group-'.$gid.'-lastSeenOn';
  94. }
  95. /**
  96. * @param string $id
  97. * @param string $method
  98. * @param array $parameters
  99. * @param bool $passOnWhen
  100. * @return mixed
  101. */
  102. abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  103. /**
  104. * @param string $id
  105. * @param string $method
  106. * @param array $parameters
  107. * @return mixed
  108. */
  109. abstract protected function walkBackends($id, $method, $parameters);
  110. /**
  111. * Takes care of the request to the User backend
  112. * @param string $id
  113. * @param string $method string, the method of the user backend that shall be called
  114. * @param array $parameters an array of parameters to be passed
  115. * @param bool $passOnWhen
  116. * @return mixed, the result of the specified method
  117. */
  118. protected function handleRequest($id, $method, $parameters, $passOnWhen = false) {
  119. $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  120. if($result === $passOnWhen) {
  121. $result = $this->walkBackends($id, $method, $parameters);
  122. }
  123. return $result;
  124. }
  125. /**
  126. * @param string|null $key
  127. * @return string
  128. */
  129. private function getCacheKey($key) {
  130. $prefix = 'LDAP-Proxy-';
  131. if(is_null($key)) {
  132. return $prefix;
  133. }
  134. return $prefix.md5($key);
  135. }
  136. /**
  137. * @param string $key
  138. * @return mixed|null
  139. */
  140. public function getFromCache($key) {
  141. if(!$this->isCached($key)) {
  142. return null;
  143. }
  144. $key = $this->getCacheKey($key);
  145. return unserialize(base64_decode($this->cache->get($key)));
  146. }
  147. /**
  148. * @param string $key
  149. * @return bool
  150. */
  151. public function isCached($key) {
  152. $key = $this->getCacheKey($key);
  153. return $this->cache->hasKey($key);
  154. }
  155. /**
  156. * @param string $key
  157. * @param mixed $value
  158. */
  159. public function writeToCache($key, $value) {
  160. $key = $this->getCacheKey($key);
  161. $value = base64_encode(serialize($value));
  162. $this->cache->set($key, $value, '2592000');
  163. }
  164. public function clearCache() {
  165. $this->cache->clear($this->getCacheKey(null));
  166. }
  167. }