OfflineUser.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\User;
  8. use OCA\User_LDAP\Mapping\UserMapping;
  9. use OCP\IConfig;
  10. use OCP\IDBConnection;
  11. use OCP\Share\IManager;
  12. use OCP\Share\IShare;
  13. class OfflineUser {
  14. /**
  15. * @var string $ocName
  16. */
  17. protected $ocName;
  18. /**
  19. * @var string $dn
  20. */
  21. protected $dn;
  22. /**
  23. * @var string $uid the UID as provided by LDAP
  24. */
  25. protected $uid;
  26. /**
  27. * @var string $displayName
  28. */
  29. protected $displayName;
  30. /**
  31. * @var string $homePath
  32. */
  33. protected $homePath;
  34. /**
  35. * @var string $lastLogin the timestamp of the last login
  36. */
  37. protected $lastLogin;
  38. /**
  39. * @var string $foundDeleted the timestamp when the user was detected as unavailable
  40. */
  41. protected $foundDeleted;
  42. protected ?string $extStorageHome = null;
  43. /**
  44. * @var string $email
  45. */
  46. protected $email;
  47. /**
  48. * @var bool $hasActiveShares
  49. */
  50. protected $hasActiveShares;
  51. /**
  52. * @var IConfig $config
  53. */
  54. protected $config;
  55. /**
  56. * @var IDBConnection $db
  57. */
  58. protected $db;
  59. /**
  60. * @var \OCA\User_LDAP\Mapping\UserMapping
  61. */
  62. protected $mapping;
  63. /** @var IManager */
  64. private $shareManager;
  65. public function __construct(
  66. $ocName,
  67. IConfig $config,
  68. UserMapping $mapping,
  69. IManager $shareManager
  70. ) {
  71. $this->ocName = $ocName;
  72. $this->config = $config;
  73. $this->mapping = $mapping;
  74. $this->shareManager = $shareManager;
  75. }
  76. /**
  77. * remove the Delete-flag from the user.
  78. */
  79. public function unmark() {
  80. $this->config->deleteUserValue($this->ocName, 'user_ldap', 'isDeleted');
  81. $this->config->deleteUserValue($this->ocName, 'user_ldap', 'foundDeleted');
  82. }
  83. /**
  84. * exports the user details in an assoc array
  85. * @return array
  86. */
  87. public function export() {
  88. $data = [];
  89. $data['ocName'] = $this->getOCName();
  90. $data['dn'] = $this->getDN();
  91. $data['uid'] = $this->getUID();
  92. $data['displayName'] = $this->getDisplayName();
  93. $data['homePath'] = $this->getHomePath();
  94. $data['lastLogin'] = $this->getLastLogin();
  95. $data['email'] = $this->getEmail();
  96. $data['hasActiveShares'] = $this->getHasActiveShares();
  97. return $data;
  98. }
  99. /**
  100. * getter for Nextcloud internal name
  101. * @return string
  102. */
  103. public function getOCName() {
  104. return $this->ocName;
  105. }
  106. /**
  107. * getter for LDAP uid
  108. * @return string
  109. */
  110. public function getUID() {
  111. if ($this->uid === null) {
  112. $this->fetchDetails();
  113. }
  114. return $this->uid;
  115. }
  116. /**
  117. * getter for LDAP DN
  118. * @return string
  119. */
  120. public function getDN() {
  121. if ($this->dn === null) {
  122. $dn = $this->mapping->getDNByName($this->ocName);
  123. $this->dn = ($dn !== false) ? $dn : '';
  124. }
  125. return $this->dn;
  126. }
  127. /**
  128. * getter for display name
  129. * @return string
  130. */
  131. public function getDisplayName() {
  132. if ($this->displayName === null) {
  133. $this->fetchDetails();
  134. }
  135. return $this->displayName;
  136. }
  137. /**
  138. * getter for email
  139. * @return string
  140. */
  141. public function getEmail() {
  142. if ($this->email === null) {
  143. $this->fetchDetails();
  144. }
  145. return $this->email;
  146. }
  147. /**
  148. * getter for home directory path
  149. * @return string
  150. */
  151. public function getHomePath() {
  152. if ($this->homePath === null) {
  153. $this->fetchDetails();
  154. }
  155. return $this->homePath;
  156. }
  157. /**
  158. * getter for the last login timestamp
  159. * @return int
  160. */
  161. public function getLastLogin() {
  162. if ($this->lastLogin === null) {
  163. $this->fetchDetails();
  164. }
  165. return (int)$this->lastLogin;
  166. }
  167. /**
  168. * getter for the detection timestamp
  169. * @return int
  170. */
  171. public function getDetectedOn() {
  172. if ($this->foundDeleted === null) {
  173. $this->fetchDetails();
  174. }
  175. return (int)$this->foundDeleted;
  176. }
  177. public function getExtStorageHome(): string {
  178. if ($this->extStorageHome === null) {
  179. $this->fetchDetails();
  180. }
  181. return (string)$this->extStorageHome;
  182. }
  183. /**
  184. * getter for having active shares
  185. * @return bool
  186. */
  187. public function getHasActiveShares() {
  188. if ($this->hasActiveShares === null) {
  189. $this->determineShares();
  190. }
  191. return $this->hasActiveShares;
  192. }
  193. /**
  194. * reads the user details
  195. */
  196. protected function fetchDetails() {
  197. $properties = [
  198. 'displayName' => 'user_ldap',
  199. 'uid' => 'user_ldap',
  200. 'homePath' => 'user_ldap',
  201. 'foundDeleted' => 'user_ldap',
  202. 'extStorageHome' => 'user_ldap',
  203. 'email' => 'settings',
  204. 'lastLogin' => 'login',
  205. ];
  206. foreach ($properties as $property => $app) {
  207. $this->$property = $this->config->getUserValue($this->ocName, $app, $property, '');
  208. }
  209. }
  210. /**
  211. * finds out whether the user has active shares. The result is stored in
  212. * $this->hasActiveShares
  213. */
  214. protected function determineShares() {
  215. $shareInterface = new \ReflectionClass(IShare::class);
  216. $shareConstants = $shareInterface->getConstants();
  217. foreach ($shareConstants as $constantName => $constantValue) {
  218. if (!str_starts_with($constantName, 'TYPE_')
  219. || $constantValue === IShare::TYPE_USERGROUP
  220. ) {
  221. continue;
  222. }
  223. $shares = $this->shareManager->getSharesBy(
  224. $this->ocName,
  225. $constantValue,
  226. null,
  227. false,
  228. 1
  229. );
  230. if (!empty($shares)) {
  231. $this->hasActiveShares = true;
  232. return;
  233. }
  234. }
  235. $this->hasActiveShares = false;
  236. }
  237. }