Manager.php 7.4 KB

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