manager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * ownCloud – LDAP User
  4. *
  5. * @author Arthur Schiwon
  6. * @copyright 2014 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\user;
  23. use OCA\user_ldap\lib\user\IUserTools;
  24. use OCA\user_ldap\lib\user\User;
  25. use OCA\user_ldap\lib\LogWrapper;
  26. use OCA\user_ldap\lib\FilesystemHelper;
  27. use OCA\user_ldap\lib\user\OfflineUser;
  28. /**
  29. * Manager
  30. *
  31. * upon request, returns an LDAP user object either by creating or from run-time
  32. * cache
  33. */
  34. class Manager {
  35. /** @var IUserTools */
  36. protected $access;
  37. /** @var \OCP\IConfig */
  38. protected $ocConfig;
  39. /** @var \OCP\IDBConnection */
  40. protected $db;
  41. /** @var FilesystemHelper */
  42. protected $ocFilesystem;
  43. /** @var LogWrapper */
  44. protected $ocLog;
  45. /** @var \OCP\Image */
  46. protected $image;
  47. /** @param \OCP\IAvatarManager */
  48. protected $avatarManager;
  49. /**
  50. * array['byDN'] \OCA\user_ldap\lib\User[]
  51. * ['byUid'] \OCA\user_ldap\lib\User[]
  52. * @var array $users
  53. */
  54. protected $users = array(
  55. 'byDN' => array(),
  56. 'byUid' => array(),
  57. );
  58. /**
  59. * @param \OCP\IConfig $ocConfig
  60. * @param \OCA\user_ldap\lib\FilesystemHelper $ocFilesystem object that
  61. * gives access to necessary functions from the OC filesystem
  62. * @param \OCA\user_ldap\lib\LogWrapper $ocLog
  63. * @param \OCP\IAvatarManager $avatarManager
  64. * @param \OCP\Image $image an empty image instance
  65. * @param \OCP\IDBConnection $db
  66. * @throws Exception when the methods mentioned above do not exist
  67. */
  68. public function __construct(\OCP\IConfig $ocConfig,
  69. FilesystemHelper $ocFilesystem, LogWrapper $ocLog,
  70. \OCP\IAvatarManager $avatarManager, \OCP\Image $image, \OCP\IDBConnection $db) {
  71. $this->ocConfig = $ocConfig;
  72. $this->ocFilesystem = $ocFilesystem;
  73. $this->ocLog = $ocLog;
  74. $this->avatarManager = $avatarManager;
  75. $this->image = $image;
  76. $this->db = $db;
  77. }
  78. /**
  79. * @brief binds manager to an instance of IUserTools (implemented by
  80. * Access). It needs to be assigned first before the manager can be used.
  81. * @param IUserTools
  82. */
  83. public function setLdapAccess(IUserTools $access) {
  84. $this->access = $access;
  85. }
  86. /**
  87. * @brief creates an instance of User and caches (just runtime) it in the
  88. * property array
  89. * @param string the DN of the user
  90. * @param string the internal (owncloud) username
  91. * @return \OCA\user_ldap\lib\User
  92. */
  93. private function createAndCache($dn, $uid) {
  94. $this->checkAccess();
  95. $user = new User($uid, $dn, $this->access, $this->ocConfig,
  96. $this->ocFilesystem, clone $this->image, $this->ocLog,
  97. $this->avatarManager);
  98. $users['byDN'][$dn] = $user;
  99. $users['byUid'][$uid] = $user;
  100. return $user;
  101. }
  102. /**
  103. * @brief checks whether the Access instance has been set
  104. * @throws Exception if Access has not been set
  105. * @return null
  106. */
  107. private function checkAccess() {
  108. if(is_null($this->access)) {
  109. throw new \Exception('LDAP Access instance must be set first');
  110. }
  111. }
  112. /**
  113. * Checks whether the specified user is marked as deleted
  114. * @param string $id the ownCloud user name
  115. * @return bool
  116. */
  117. public function isDeletedUser($id) {
  118. $isDeleted = $this->ocConfig->getUserValue(
  119. $id, 'user_ldap', 'isDeleted', 0);
  120. return intval($isDeleted) === 1;
  121. }
  122. /**
  123. * creates and returns an instance of OfflineUser for the specified user
  124. * @param string $id
  125. * @return \OCA\user_ldap\lib\user\OfflineUser
  126. */
  127. public function getDeletedUser($id) {
  128. return new OfflineUser(
  129. $id,
  130. $this->ocConfig,
  131. $this->db,
  132. $this->access->getUserMapper());
  133. }
  134. protected function createInstancyByUserName($id) {
  135. //most likely a uid. Check whether it is a deleted user
  136. if($this->isDeletedUser($id)) {
  137. return $this->getDeletedUser($id);
  138. }
  139. $dn = $this->access->username2dn($id);
  140. if($dn !== false) {
  141. return $this->createAndCache($dn, $id);
  142. }
  143. throw new \Exception('Could not create User instance');
  144. }
  145. /**
  146. * @brief returns a User object by it's DN or ownCloud username
  147. * @param string the DN or username of the user
  148. * @return \OCA\user_ldap\lib\user\User|\OCA\user_ldap\lib\user\OfflineUser|null
  149. */
  150. public function get($id) {
  151. $this->checkAccess();
  152. if(isset($this->users['byDN'][$id])) {
  153. return $this->users['byDN'][$id];
  154. } else if(isset($this->users['byUid'][$id])) {
  155. return $this->users['byUid'][$id];
  156. }
  157. if($this->access->stringResemblesDN($id) ) {
  158. $uid = $this->access->dn2username($id);
  159. if($uid !== false) {
  160. return $this->createAndCache($id, $uid);
  161. }
  162. }
  163. try {
  164. $user = $this->createInstancyByUserName($id);
  165. return $user;
  166. } catch (\Exception $e) {
  167. return null;
  168. }
  169. }
  170. }