proxy.php 4.4 KB

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