1
0

Proxy.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Roger Szabo <roger.szabo@web.de>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCA\User_LDAP;
  33. use OCA\User_LDAP\Mapping\UserMapping;
  34. use OCA\User_LDAP\Mapping\GroupMapping;
  35. use OCA\User_LDAP\User\Manager;
  36. abstract class Proxy {
  37. static private $accesses = array();
  38. private $ldap = null;
  39. /** @var \OCP\ICache|null */
  40. private $cache;
  41. /**
  42. * @param ILDAPWrapper $ldap
  43. */
  44. public function __construct(ILDAPWrapper $ldap) {
  45. $this->ldap = $ldap;
  46. $memcache = \OC::$server->getMemCacheFactory();
  47. if($memcache->isAvailable()) {
  48. $this->cache = $memcache->createDistributed();
  49. }
  50. }
  51. /**
  52. * @param string $configPrefix
  53. */
  54. private function addAccess($configPrefix) {
  55. static $ocConfig;
  56. static $fs;
  57. static $log;
  58. static $avatarM;
  59. static $userMap;
  60. static $groupMap;
  61. static $db;
  62. static $coreUserManager;
  63. static $coreNotificationManager;
  64. if($fs === null) {
  65. $ocConfig = \OC::$server->getConfig();
  66. $fs = new FilesystemHelper();
  67. $log = new LogWrapper();
  68. $avatarM = \OC::$server->getAvatarManager();
  69. $db = \OC::$server->getDatabaseConnection();
  70. $userMap = new UserMapping($db);
  71. $groupMap = new GroupMapping($db);
  72. $coreUserManager = \OC::$server->getUserManager();
  73. $coreNotificationManager = \OC::$server->getNotificationManager();
  74. }
  75. $userManager =
  76. new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db,
  77. $coreUserManager, $coreNotificationManager);
  78. $connector = new Connection($this->ldap, $configPrefix);
  79. $access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig, $coreUserManager);
  80. $access->setUserMapper($userMap);
  81. $access->setGroupMapper($groupMap);
  82. self::$accesses[$configPrefix] = $access;
  83. }
  84. /**
  85. * @param string $configPrefix
  86. * @return mixed
  87. */
  88. protected function getAccess($configPrefix) {
  89. if(!isset(self::$accesses[$configPrefix])) {
  90. $this->addAccess($configPrefix);
  91. }
  92. return self::$accesses[$configPrefix];
  93. }
  94. /**
  95. * @param string $uid
  96. * @return string
  97. */
  98. protected function getUserCacheKey($uid) {
  99. return 'user-'.$uid.'-lastSeenOn';
  100. }
  101. /**
  102. * @param string $gid
  103. * @return string
  104. */
  105. protected function getGroupCacheKey($gid) {
  106. return 'group-'.$gid.'-lastSeenOn';
  107. }
  108. /**
  109. * @param string $id
  110. * @param string $method
  111. * @param array $parameters
  112. * @param bool $passOnWhen
  113. * @return mixed
  114. */
  115. abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  116. /**
  117. * @param string $id
  118. * @param string $method
  119. * @param array $parameters
  120. * @return mixed
  121. */
  122. abstract protected function walkBackends($id, $method, $parameters);
  123. /**
  124. * @param string $id
  125. * @return Access
  126. */
  127. abstract public function getLDAPAccess($id);
  128. /**
  129. * Takes care of the request to the User backend
  130. * @param string $id
  131. * @param string $method string, the method of the user backend that shall be called
  132. * @param array $parameters an array of parameters to be passed
  133. * @param bool $passOnWhen
  134. * @return mixed, the result of the specified method
  135. */
  136. protected function handleRequest($id, $method, $parameters, $passOnWhen = false) {
  137. $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen);
  138. if($result === $passOnWhen) {
  139. $result = $this->walkBackends($id, $method, $parameters);
  140. }
  141. return $result;
  142. }
  143. /**
  144. * @param string|null $key
  145. * @return string
  146. */
  147. private function getCacheKey($key) {
  148. $prefix = 'LDAP-Proxy-';
  149. if($key === null) {
  150. return $prefix;
  151. }
  152. return $prefix.hash('sha256', $key);
  153. }
  154. /**
  155. * @param string $key
  156. * @return mixed|null
  157. */
  158. public function getFromCache($key) {
  159. if($this->cache === null) {
  160. return null;
  161. }
  162. $key = $this->getCacheKey($key);
  163. $value = $this->cache->get($key);
  164. if ($value === null) {
  165. return null;
  166. }
  167. return json_decode(base64_decode($value));
  168. }
  169. /**
  170. * @param string $key
  171. * @param mixed $value
  172. */
  173. public function writeToCache($key, $value) {
  174. if($this->cache === null) {
  175. return;
  176. }
  177. $key = $this->getCacheKey($key);
  178. $value = base64_encode(json_encode($value));
  179. $this->cache->set($key, $value, 2592000);
  180. }
  181. public function clearCache() {
  182. if($this->cache === null) {
  183. return;
  184. }
  185. $this->cache->clear($this->getCacheKey(null));
  186. }
  187. }