Manager.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\User_LDAP\User;
  27. use OC\Cache\CappedMemoryCache;
  28. use OCA\User_LDAP\LogWrapper;
  29. use OCA\User_LDAP\FilesystemHelper;
  30. use OCP\IAvatarManager;
  31. use OCP\IConfig;
  32. use OCP\IDBConnection;
  33. use OCP\Image;
  34. use OCP\IUserManager;
  35. /**
  36. * Manager
  37. *
  38. * upon request, returns an LDAP user object either by creating or from run-time
  39. * cache
  40. */
  41. class Manager {
  42. /** @var IUserTools */
  43. protected $access;
  44. /** @var IConfig */
  45. protected $ocConfig;
  46. /** @var IDBConnection */
  47. protected $db;
  48. /** @var FilesystemHelper */
  49. protected $ocFilesystem;
  50. /** @var LogWrapper */
  51. protected $ocLog;
  52. /** @var Image */
  53. protected $image;
  54. /** @param \OCP\IAvatarManager */
  55. protected $avatarManager;
  56. /**
  57. * @var CappedMemoryCache $usersByDN
  58. */
  59. protected $usersByDN;
  60. /**
  61. * @var CappedMemoryCache $usersByUid
  62. */
  63. protected $usersByUid;
  64. /**
  65. * @param IConfig $ocConfig
  66. * @param \OCA\User_LDAP\FilesystemHelper $ocFilesystem object that
  67. * gives access to necessary functions from the OC filesystem
  68. * @param \OCA\User_LDAP\LogWrapper $ocLog
  69. * @param IAvatarManager $avatarManager
  70. * @param Image $image an empty image instance
  71. * @param IDBConnection $db
  72. * @throws \Exception when the methods mentioned above do not exist
  73. */
  74. public function __construct(IConfig $ocConfig,
  75. FilesystemHelper $ocFilesystem, LogWrapper $ocLog,
  76. IAvatarManager $avatarManager, Image $image,
  77. IDBConnection $db, IUserManager $userManager) {
  78. $this->ocConfig = $ocConfig;
  79. $this->ocFilesystem = $ocFilesystem;
  80. $this->ocLog = $ocLog;
  81. $this->avatarManager = $avatarManager;
  82. $this->image = $image;
  83. $this->db = $db;
  84. $this->userManager = $userManager;
  85. $this->usersByDN = new CappedMemoryCache();
  86. $this->usersByUid = new CappedMemoryCache();
  87. }
  88. /**
  89. * @brief binds manager to an instance of IUserTools (implemented by
  90. * Access). It needs to be assigned first before the manager can be used.
  91. * @param IUserTools
  92. */
  93. public function setLdapAccess(IUserTools $access) {
  94. $this->access = $access;
  95. }
  96. /**
  97. * @brief creates an instance of User and caches (just runtime) it in the
  98. * property array
  99. * @param string $dn the DN of the user
  100. * @param string $uid the internal (owncloud) username
  101. * @return \OCA\User_LDAP\User\User
  102. */
  103. private function createAndCache($dn, $uid) {
  104. $this->checkAccess();
  105. $user = new User($uid, $dn, $this->access, $this->ocConfig,
  106. $this->ocFilesystem, clone $this->image, $this->ocLog,
  107. $this->avatarManager, $this->userManager);
  108. $this->usersByDN[$dn] = $user;
  109. $this->usersByUid[$uid] = $user;
  110. return $user;
  111. }
  112. /**
  113. * @brief checks whether the Access instance has been set
  114. * @throws \Exception if Access has not been set
  115. * @return null
  116. */
  117. private function checkAccess() {
  118. if(is_null($this->access)) {
  119. throw new \Exception('LDAP Access instance must be set first');
  120. }
  121. }
  122. /**
  123. * returns a list of attributes that will be processed further, e.g. quota,
  124. * email, displayname, or others.
  125. * @param bool $minimal - optional, set to true to skip attributes with big
  126. * payload
  127. * @return string[]
  128. */
  129. public function getAttributes($minimal = false) {
  130. $attributes = array('dn', 'uid', 'samaccountname', 'memberof');
  131. $possible = array(
  132. $this->access->getConnection()->ldapQuotaAttribute,
  133. $this->access->getConnection()->ldapEmailAttribute,
  134. $this->access->getConnection()->ldapUserDisplayName,
  135. $this->access->getConnection()->ldapUserDisplayName2,
  136. );
  137. foreach($possible as $attr) {
  138. if(!is_null($attr)) {
  139. $attributes[] = $attr;
  140. }
  141. }
  142. $homeRule = $this->access->getConnection()->homeFolderNamingRule;
  143. if(strpos($homeRule, 'attr:') === 0) {
  144. $attributes[] = substr($homeRule, strlen('attr:'));
  145. }
  146. if(!$minimal) {
  147. // attributes that are not really important but may come with big
  148. // payload.
  149. $attributes = array_merge($attributes, array(
  150. 'jpegphoto',
  151. 'thumbnailphoto'
  152. ));
  153. }
  154. return $attributes;
  155. }
  156. /**
  157. * Checks whether the specified user is marked as deleted
  158. * @param string $id the ownCloud user name
  159. * @return bool
  160. */
  161. public function isDeletedUser($id) {
  162. $isDeleted = $this->ocConfig->getUserValue(
  163. $id, 'user_ldap', 'isDeleted', 0);
  164. return intval($isDeleted) === 1;
  165. }
  166. /**
  167. * creates and returns an instance of OfflineUser for the specified user
  168. * @param string $id
  169. * @return \OCA\User_LDAP\User\OfflineUser
  170. */
  171. public function getDeletedUser($id) {
  172. return new OfflineUser(
  173. $id,
  174. $this->ocConfig,
  175. $this->db,
  176. $this->access->getUserMapper());
  177. }
  178. /**
  179. * @brief returns a User object by it's ownCloud username
  180. * @param string $id the DN or username of the user
  181. * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null
  182. */
  183. protected function createInstancyByUserName($id) {
  184. //most likely a uid. Check whether it is a deleted user
  185. if($this->isDeletedUser($id)) {
  186. return $this->getDeletedUser($id);
  187. }
  188. $dn = $this->access->username2dn($id);
  189. if($dn !== false) {
  190. return $this->createAndCache($dn, $id);
  191. }
  192. return null;
  193. }
  194. /**
  195. * @brief returns a User object by it's DN or ownCloud username
  196. * @param string $id the DN or username of the user
  197. * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null
  198. * @throws \Exception when connection could not be established
  199. */
  200. public function get($id) {
  201. $this->checkAccess();
  202. if(isset($this->usersByDN[$id])) {
  203. return $this->usersByDN[$id];
  204. } else if(isset($this->usersByUid[$id])) {
  205. return $this->usersByUid[$id];
  206. }
  207. if($this->access->stringResemblesDN($id) ) {
  208. $uid = $this->access->dn2username($id);
  209. if($uid !== false) {
  210. return $this->createAndCache($id, $uid);
  211. }
  212. }
  213. return $this->createInstancyByUserName($id);
  214. }
  215. }