Manager.php 7.7 KB

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