1
0

OfflineUser.php 4.8 KB

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