You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Proxy.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. static $coreNotificationManager;
  62. if(is_null($fs)) {
  63. $ocConfig = \OC::$server->getConfig();
  64. $fs = new FilesystemHelper();
  65. $log = new LogWrapper();
  66. $avatarM = \OC::$server->getAvatarManager();
  67. $db = \OC::$server->getDatabaseConnection();
  68. $userMap = new UserMapping($db);
  69. $groupMap = new GroupMapping($db);
  70. $coreUserManager = \OC::$server->getUserManager();
  71. $coreNotificationManager = \OC::$server->getNotificationManager();
  72. }
  73. $userManager =
  74. new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db,
  75. $coreUserManager, $coreNotificationManager);
  76. $connector = new Connection($this->ldap, $configPrefix);
  77. $access = new Access($connector, $this->ldap, $userManager, new Helper(\OC::$server->getConfig()));
  78. $access->setUserMapper($userMap);
  79. $access->setGroupMapper($groupMap);
  80. self::$accesses[$configPrefix] = $access;
  81. }
  82. /**
  83. * @param string $configPrefix
  84. * @return mixed
  85. */
  86. protected function getAccess($configPrefix) {
  87. if(!isset(self::$accesses[$configPrefix])) {
  88. $this->addAccess($configPrefix);
  89. }
  90. return self::$accesses[$configPrefix];
  91. }
  92. /**
  93. * @param string $uid
  94. * @return string
  95. */
  96. protected function getUserCacheKey($uid) {
  97. return 'user-'.$uid.'-lastSeenOn';
  98. }
  99. /**
  100. * @param string $gid
  101. * @return string
  102. */
  103. protected function getGroupCacheKey($gid) {
  104. return 'group-'.$gid.'-lastSeenOn';
  105. }
  106. /**
  107. * @param string $id
  108. * @param string $method
  109. * @param array $parameters
  110. * @param bool $passOnWhen
  111. * @return mixed
  112. */
  113. abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  114. /**
  115. * @param string $id
  116. * @param string $method
  117. * @param array $parameters
  118. * @return mixed
  119. */
  120. abstract protected function walkBackends($id, $method, $parameters);
  121. /**
  122. * @param string $id
  123. * @return Access
  124. */
  125. abstract public function getLDAPAccess($id);
  126. /**
  127. * Takes care of the request to the User backend
  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. $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  136. if($result === $passOnWhen) {
  137. $result = $this->walkBackends($id, $method, $parameters);
  138. }
  139. return $result;
  140. }
  141. /**
  142. * @param string|null $key
  143. * @return string
  144. */
  145. private function getCacheKey($key) {
  146. $prefix = 'LDAP-Proxy-';
  147. if(is_null($key)) {
  148. return $prefix;
  149. }
  150. return $prefix.md5($key);
  151. }
  152. /**
  153. * @param string $key
  154. * @return mixed|null
  155. */
  156. public function getFromCache($key) {
  157. if(is_null($this->cache) || !$this->isCached($key)) {
  158. return null;
  159. }
  160. $key = $this->getCacheKey($key);
  161. return json_decode(base64_decode($this->cache->get($key)));
  162. }
  163. /**
  164. * @param string $key
  165. * @return bool
  166. */
  167. public function isCached($key) {
  168. if(is_null($this->cache)) {
  169. return false;
  170. }
  171. $key = $this->getCacheKey($key);
  172. return $this->cache->hasKey($key);
  173. }
  174. /**
  175. * @param string $key
  176. * @param mixed $value
  177. */
  178. public function writeToCache($key, $value) {
  179. if(is_null($this->cache)) {
  180. return;
  181. }
  182. $key = $this->getCacheKey($key);
  183. $value = base64_encode(json_encode($value));
  184. $this->cache->set($key, $value, '2592000');
  185. }
  186. public function clearCache() {
  187. if(is_null($this->cache)) {
  188. return;
  189. }
  190. $this->cache->clear($this->getCacheKey(null));
  191. }
  192. }