Manager.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 Access */
  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. * 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->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. *
  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. $attributes = array_merge(Access::UUID_ATTRIBUTES, ['dn', 'uid', 'samaccountname', 'memberof']);
  154. $possible = array(
  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. );
  161. foreach($possible as $attr) {
  162. if(!is_null($attr)) {
  163. $attributes[] = $attr;
  164. }
  165. }
  166. $homeRule = $this->access->getConnection()->homeFolderNamingRule;
  167. if(strpos($homeRule, 'attr:') === 0) {
  168. $attributes[] = substr($homeRule, strlen('attr:'));
  169. }
  170. if(!$minimal) {
  171. // attributes that are not really important but may come with big
  172. // payload.
  173. $attributes = array_merge(
  174. $attributes,
  175. $this->access->getConnection()->resolveRule('avatar')
  176. );
  177. }
  178. // remove possible empty attributes
  179. $attributes = array_values(
  180. array_filter($attributes, function ($attributeName) {
  181. return !empty($attributeName);
  182. })
  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->db,
  206. $this->access->getUserMapper());
  207. }
  208. /**
  209. * @brief returns a User object by it's Nextcloud username
  210. * @param string $id the DN or username of the user
  211. * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null
  212. */
  213. protected function createInstancyByUserName($id) {
  214. //most likely a uid. Check whether it is a deleted user
  215. if($this->isDeletedUser($id)) {
  216. return $this->getDeletedUser($id);
  217. }
  218. $dn = $this->access->username2dn($id);
  219. if($dn !== false) {
  220. return $this->createAndCache($dn, $id);
  221. }
  222. return null;
  223. }
  224. /**
  225. * @brief returns a User object by it's DN or Nextcloud username
  226. * @param string $id the DN or username of the user
  227. * @return \OCA\User_LDAP\User\User|\OCA\User_LDAP\User\OfflineUser|null
  228. * @throws \Exception when connection could not be established
  229. */
  230. public function get($id) {
  231. $this->checkAccess();
  232. if(isset($this->usersByDN[$id])) {
  233. return $this->usersByDN[$id];
  234. } else if(isset($this->usersByUid[$id])) {
  235. return $this->usersByUid[$id];
  236. }
  237. if($this->access->stringResemblesDN($id) ) {
  238. $uid = $this->access->dn2username($id);
  239. if($uid !== false) {
  240. return $this->createAndCache($id, $uid);
  241. }
  242. }
  243. return $this->createInstancyByUserName($id);
  244. }
  245. }