Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Manager.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Roger Szabo <roger.szabo@web.de>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\User_LDAP\User;
  30. use OC\Cache\CappedMemoryCache;
  31. use OCA\User_LDAP\Access;
  32. use OCA\User_LDAP\FilesystemHelper;
  33. use OCP\IAvatarManager;
  34. use OCP\IConfig;
  35. use OCP\IDBConnection;
  36. use OCP\Image;
  37. use OCP\IUserManager;
  38. use OCP\Notification\IManager as INotificationManager;
  39. use OCP\Share\IManager;
  40. use Psr\Log\LoggerInterface;
  41. /**
  42. * Manager
  43. *
  44. * upon request, returns an LDAP user object either by creating or from run-time
  45. * cache
  46. */
  47. class Manager {
  48. /** @var Access */
  49. protected $access;
  50. /** @var IConfig */
  51. protected $ocConfig;
  52. /** @var IDBConnection */
  53. protected $db;
  54. /** @var IUserManager */
  55. protected $userManager;
  56. /** @var INotificationManager */
  57. protected $notificationManager;
  58. /** @var FilesystemHelper */
  59. protected $ocFilesystem;
  60. /** @var LoggerInterface */
  61. protected $logger;
  62. /** @var Image */
  63. protected $image;
  64. /** @param \OCP\IAvatarManager */
  65. protected $avatarManager;
  66. /**
  67. * @var CappedMemoryCache $usersByDN
  68. */
  69. protected $usersByDN;
  70. /**
  71. * @var CappedMemoryCache $usersByUid
  72. */
  73. protected $usersByUid;
  74. /** @var IManager */
  75. private $shareManager;
  76. public function __construct(
  77. IConfig $ocConfig,
  78. FilesystemHelper $ocFilesystem,
  79. LoggerInterface $logger,
  80. IAvatarManager $avatarManager,
  81. Image $image,
  82. IUserManager $userManager,
  83. INotificationManager $notificationManager,
  84. IManager $shareManager
  85. ) {
  86. $this->ocConfig = $ocConfig;
  87. $this->ocFilesystem = $ocFilesystem;
  88. $this->logger = $logger;
  89. $this->avatarManager = $avatarManager;
  90. $this->image = $image;
  91. $this->userManager = $userManager;
  92. $this->notificationManager = $notificationManager;
  93. $this->usersByDN = new CappedMemoryCache();
  94. $this->usersByUid = new CappedMemoryCache();
  95. $this->shareManager = $shareManager;
  96. }
  97. /**
  98. * Binds manager to an instance of Access.
  99. * It needs to be assigned first before the manager can be used.
  100. * @param Access
  101. */
  102. public function setLdapAccess(Access $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->logger,
  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. *
  148. * @param bool $minimal - optional, set to true to skip attributes with big
  149. * payload
  150. * @return string[]
  151. */
  152. public function getAttributes($minimal = false) {
  153. $baseAttributes = array_merge(Access::UUID_ATTRIBUTES, ['dn', 'uid', 'samaccountname', 'memberof']);
  154. $attributes = [
  155. $this->access->getConnection()->ldapExpertUUIDUserAttr,
  156. $this->access->getConnection()->ldapQuotaAttribute,
  157. $this->access->getConnection()->ldapEmailAttribute,
  158. $this->access->getConnection()->ldapUserDisplayName,
  159. $this->access->getConnection()->ldapUserDisplayName2,
  160. $this->access->getConnection()->ldapExtStorageHomeAttribute,
  161. ];
  162. $homeRule = (string)$this->access->getConnection()->homeFolderNamingRule;
  163. if (strpos($homeRule, 'attr:') === 0) {
  164. $attributes[] = substr($homeRule, strlen('attr:'));
  165. }
  166. if (!$minimal) {
  167. // attributes that are not really important but may come with big
  168. // payload.
  169. $attributes = array_merge(
  170. $attributes,
  171. $this->access->getConnection()->resolveRule('avatar')
  172. );
  173. }
  174. $attributes = array_reduce($attributes,
  175. function ($list, $attribute) {
  176. $attribute = strtolower(trim((string)$attribute));
  177. if (!empty($attribute) && !in_array($attribute, $list)) {
  178. $list[] = $attribute;
  179. }
  180. return $list;
  181. },
  182. $baseAttributes // hard-coded, lower-case, non-empty attributes
  183. );
  184. return $attributes;
  185. }
  186. /**
  187. * Checks whether the specified user is marked as deleted
  188. * @param string $id the Nextcloud user name
  189. * @return bool
  190. */
  191. public function isDeletedUser($id) {
  192. $isDeleted = $this->ocConfig->getUserValue(
  193. $id, 'user_ldap', 'isDeleted', 0);
  194. return (int)$isDeleted === 1;
  195. }
  196. /**
  197. * creates and returns an instance of OfflineUser for the specified user
  198. * @param string $id
  199. * @return \OCA\User_LDAP\User\OfflineUser
  200. */
  201. public function getDeletedUser($id) {
  202. return new OfflineUser(
  203. $id,
  204. $this->ocConfig,
  205. $this->access->getUserMapper(),
  206. $this->shareManager
  207. );
  208. }
  209. /**
  210. * @brief returns a User object by it's Nextcloud username
  211. * @param string $id the DN or username of the user
  212. * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null
  213. */
  214. protected function createInstancyByUserName($id) {
  215. //most likely a uid. Check whether it is a deleted user
  216. if ($this->isDeletedUser($id)) {
  217. return $this->getDeletedUser($id);
  218. }
  219. $dn = $this->access->username2dn($id);
  220. if ($dn !== false) {
  221. return $this->createAndCache($dn, $id);
  222. }
  223. return null;
  224. }
  225. /**
  226. * @brief returns a User object by it's DN or Nextcloud username
  227. * @param string $id the DN or username of the user
  228. * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null
  229. * @throws \Exception when connection could not be established
  230. */
  231. public function get($id) {
  232. $this->checkAccess();
  233. if (isset($this->usersByDN[$id])) {
  234. return $this->usersByDN[$id];
  235. } elseif (isset($this->usersByUid[$id])) {
  236. return $this->usersByUid[$id];
  237. }
  238. if ($this->access->stringResemblesDN($id)) {
  239. $uid = $this->access->dn2username($id);
  240. if ($uid !== false) {
  241. return $this->createAndCache($id, $uid);
  242. }
  243. }
  244. return $this->createInstancyByUserName($id);
  245. }
  246. }