OfflineUser.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\User_LDAP\User;
  27. use OCA\User_LDAP\Mapping\UserMapping;
  28. use OCP\IConfig;
  29. use OCP\IDBConnection;
  30. use OCP\Share\IManager;
  31. use OCP\Share\IShare;
  32. class OfflineUser {
  33. /**
  34. * @var string $ocName
  35. */
  36. protected $ocName;
  37. /**
  38. * @var string $dn
  39. */
  40. protected $dn;
  41. /**
  42. * @var string $uid the UID as provided by LDAP
  43. */
  44. protected $uid;
  45. /**
  46. * @var string $displayName
  47. */
  48. protected $displayName;
  49. /**
  50. * @var string $homePath
  51. */
  52. protected $homePath;
  53. /**
  54. * @var string $lastLogin the timestamp of the last login
  55. */
  56. protected $lastLogin;
  57. /**
  58. * @var string $foundDeleted the timestamp when the user was detected as unavailable
  59. */
  60. protected $foundDeleted;
  61. protected ?string $extStorageHome = null;
  62. /**
  63. * @var string $email
  64. */
  65. protected $email;
  66. /**
  67. * @var bool $hasActiveShares
  68. */
  69. protected $hasActiveShares;
  70. /**
  71. * @var IConfig $config
  72. */
  73. protected $config;
  74. /**
  75. * @var IDBConnection $db
  76. */
  77. protected $db;
  78. /**
  79. * @var \OCA\User_LDAP\Mapping\UserMapping
  80. */
  81. protected $mapping;
  82. /** @var IManager */
  83. private $shareManager;
  84. public function __construct(
  85. $ocName,
  86. IConfig $config,
  87. UserMapping $mapping,
  88. IManager $shareManager
  89. ) {
  90. $this->ocName = $ocName;
  91. $this->config = $config;
  92. $this->mapping = $mapping;
  93. $this->shareManager = $shareManager;
  94. }
  95. /**
  96. * remove the Delete-flag from the user.
  97. */
  98. public function unmark() {
  99. $this->config->deleteUserValue($this->ocName, 'user_ldap', 'isDeleted');
  100. $this->config->deleteUserValue($this->ocName, 'user_ldap', 'foundDeleted');
  101. }
  102. /**
  103. * exports the user details in an assoc array
  104. * @return array
  105. */
  106. public function export() {
  107. $data = [];
  108. $data['ocName'] = $this->getOCName();
  109. $data['dn'] = $this->getDN();
  110. $data['uid'] = $this->getUID();
  111. $data['displayName'] = $this->getDisplayName();
  112. $data['homePath'] = $this->getHomePath();
  113. $data['lastLogin'] = $this->getLastLogin();
  114. $data['email'] = $this->getEmail();
  115. $data['hasActiveShares'] = $this->getHasActiveShares();
  116. return $data;
  117. }
  118. /**
  119. * getter for Nextcloud internal name
  120. * @return string
  121. */
  122. public function getOCName() {
  123. return $this->ocName;
  124. }
  125. /**
  126. * getter for LDAP uid
  127. * @return string
  128. */
  129. public function getUID() {
  130. if ($this->uid === null) {
  131. $this->fetchDetails();
  132. }
  133. return $this->uid;
  134. }
  135. /**
  136. * getter for LDAP DN
  137. * @return string
  138. */
  139. public function getDN() {
  140. if ($this->dn === null) {
  141. $dn = $this->mapping->getDNByName($this->ocName);
  142. $this->dn = ($dn !== false) ? $dn : '';
  143. }
  144. return $this->dn;
  145. }
  146. /**
  147. * getter for display name
  148. * @return string
  149. */
  150. public function getDisplayName() {
  151. if ($this->displayName === null) {
  152. $this->fetchDetails();
  153. }
  154. return $this->displayName;
  155. }
  156. /**
  157. * getter for email
  158. * @return string
  159. */
  160. public function getEmail() {
  161. if ($this->email === null) {
  162. $this->fetchDetails();
  163. }
  164. return $this->email;
  165. }
  166. /**
  167. * getter for home directory path
  168. * @return string
  169. */
  170. public function getHomePath() {
  171. if ($this->homePath === null) {
  172. $this->fetchDetails();
  173. }
  174. return $this->homePath;
  175. }
  176. /**
  177. * getter for the last login timestamp
  178. * @return int
  179. */
  180. public function getLastLogin() {
  181. if ($this->lastLogin === null) {
  182. $this->fetchDetails();
  183. }
  184. return (int)$this->lastLogin;
  185. }
  186. /**
  187. * getter for the detection timestamp
  188. * @return int
  189. */
  190. public function getDetectedOn() {
  191. if ($this->foundDeleted === null) {
  192. $this->fetchDetails();
  193. }
  194. return (int)$this->foundDeleted;
  195. }
  196. public function getExtStorageHome(): string {
  197. if ($this->extStorageHome === null) {
  198. $this->fetchDetails();
  199. }
  200. return (string)$this->extStorageHome;
  201. }
  202. /**
  203. * getter for having active shares
  204. * @return bool
  205. */
  206. public function getHasActiveShares() {
  207. if ($this->hasActiveShares === null) {
  208. $this->determineShares();
  209. }
  210. return $this->hasActiveShares;
  211. }
  212. /**
  213. * reads the user details
  214. */
  215. protected function fetchDetails() {
  216. $properties = [
  217. 'displayName' => 'user_ldap',
  218. 'uid' => 'user_ldap',
  219. 'homePath' => 'user_ldap',
  220. 'foundDeleted' => 'user_ldap',
  221. 'extStorageHome' => 'user_ldap',
  222. 'email' => 'settings',
  223. 'lastLogin' => 'login',
  224. ];
  225. foreach ($properties as $property => $app) {
  226. $this->$property = $this->config->getUserValue($this->ocName, $app, $property, '');
  227. }
  228. }
  229. /**
  230. * finds out whether the user has active shares. The result is stored in
  231. * $this->hasActiveShares
  232. */
  233. protected function determineShares() {
  234. $shareInterface = new \ReflectionClass(IShare::class);
  235. $shareConstants = $shareInterface->getConstants();
  236. foreach ($shareConstants as $constantName => $constantValue) {
  237. if (!str_starts_with($constantName, 'TYPE_')
  238. || $constantValue === IShare::TYPE_USERGROUP
  239. ) {
  240. continue;
  241. }
  242. $shares = $this->shareManager->getSharesBy(
  243. $this->ocName,
  244. $constantValue,
  245. null,
  246. false,
  247. 1
  248. );
  249. if (!empty($shares)) {
  250. $this->hasActiveShares = true;
  251. return;
  252. }
  253. }
  254. $this->hasActiveShares = false;
  255. }
  256. }