PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/user_ldap/lib/proxy.php

https://github.com/sezuan/core
PHP | 104 lines | 60 code | 17 blank | 27 comment | 4 complexity | 36516f8ce7841da6293acca551331be1 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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. abstract class Proxy {
  24. static private $connectors = array();
  25. public function __construct() {
  26. $this->cache = \OC_Cache::getGlobalCache();
  27. }
  28. private function addConnector($configPrefix) {
  29. self::$connectors[$configPrefix] = new \OCA\user_ldap\lib\Connection($configPrefix);
  30. }
  31. protected function getConnector($configPrefix) {
  32. if(!isset(self::$connectors[$configPrefix])) {
  33. $this->addConnector($configPrefix);
  34. }
  35. return self::$connectors[$configPrefix];
  36. }
  37. protected function getConnectors() {
  38. return self::$connectors;
  39. }
  40. protected function getUserCacheKey($uid) {
  41. return 'user-'.$uid.'-lastSeenOn';
  42. }
  43. protected function getGroupCacheKey($gid) {
  44. return 'group-'.$gid.'-lastSeenOn';
  45. }
  46. abstract protected function callOnLastSeenOn($id, $method, $parameters);
  47. abstract protected function walkBackends($id, $method, $parameters);
  48. /**
  49. * @brief Takes care of the request to the User backend
  50. * @param $uid string, the uid connected to the request
  51. * @param $method string, the method of the user backend that shall be called
  52. * @param $parameters an array of parameters to be passed
  53. * @return mixed, the result of the specified method
  54. */
  55. protected function handleRequest($id, $method, $parameters) {
  56. if(!$result = $this->callOnLastSeenOn($id, $method, $parameters)) {
  57. $result = $this->walkBackends($id, $method, $parameters);
  58. }
  59. return $result;
  60. }
  61. private function getCacheKey($key) {
  62. $prefix = 'LDAP-Proxy-';
  63. if(is_null($key)) {
  64. return $prefix;
  65. }
  66. return $prefix.md5($key);
  67. }
  68. public function getFromCache($key) {
  69. if(!$this->isCached($key)) {
  70. return null;
  71. }
  72. $key = $this->getCacheKey($key);
  73. return unserialize(base64_decode($this->cache->get($key)));
  74. }
  75. public function isCached($key) {
  76. $key = $this->getCacheKey($key);
  77. return $this->cache->hasKey($key);
  78. }
  79. public function writeToCache($key, $value) {
  80. $key = $this->getCacheKey($key);
  81. $value = base64_encode(serialize($value));
  82. $this->cache->set($key, $value, '2592000');
  83. }
  84. public function clearCache() {
  85. $this->cache->clear($this->getCacheKey(null));
  86. }
  87. }