OfflineUser.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /**
  62. * @var string $email
  63. */
  64. protected $email;
  65. /**
  66. * @var bool $hasActiveShares
  67. */
  68. protected $hasActiveShares;
  69. /**
  70. * @var IConfig $config
  71. */
  72. protected $config;
  73. /**
  74. * @var IDBConnection $db
  75. */
  76. protected $db;
  77. /**
  78. * @var \OCA\User_LDAP\Mapping\UserMapping
  79. */
  80. protected $mapping;
  81. /** @var IManager */
  82. private $shareManager;
  83. public function __construct(
  84. $ocName,
  85. IConfig $config,
  86. UserMapping $mapping,
  87. IManager $shareManager
  88. ) {
  89. $this->ocName = $ocName;
  90. $this->config = $config;
  91. $this->mapping = $mapping;
  92. $this->shareManager = $shareManager;
  93. }
  94. /**
  95. * remove the Delete-flag from the user.
  96. */
  97. public function unmark() {
  98. $this->config->deleteUserValue($this->ocName, 'user_ldap', 'isDeleted');
  99. $this->config->deleteUserValue($this->ocName, 'user_ldap', 'foundDeleted');
  100. }
  101. /**
  102. * exports the user details in an assoc array
  103. * @return array
  104. */
  105. public function export() {
  106. $data = [];
  107. $data['ocName'] = $this->getOCName();
  108. $data['dn'] = $this->getDN();
  109. $data['uid'] = $this->getUID();
  110. $data['displayName'] = $this->getDisplayName();
  111. $data['homePath'] = $this->getHomePath();
  112. $data['lastLogin'] = $this->getLastLogin();
  113. $data['email'] = $this->getEmail();
  114. $data['hasActiveShares'] = $this->getHasActiveShares();
  115. return $data;
  116. }
  117. /**
  118. * getter for Nextcloud internal name
  119. * @return string
  120. */
  121. public function getOCName() {
  122. return $this->ocName;
  123. }
  124. /**
  125. * getter for LDAP uid
  126. * @return string
  127. */
  128. public function getUID() {
  129. if ($this->uid === null) {
  130. $this->fetchDetails();
  131. }
  132. return $this->uid;
  133. }
  134. /**
  135. * getter for LDAP DN
  136. * @return string
  137. */
  138. public function getDN() {
  139. if ($this->dn === null) {
  140. $dn = $this->mapping->getDNByName($this->ocName);
  141. $this->dn = ($dn !== false) ? $dn : '';
  142. }
  143. return $this->dn;
  144. }
  145. /**
  146. * getter for display name
  147. * @return string
  148. */
  149. public function getDisplayName() {
  150. if ($this->displayName === null) {
  151. $this->fetchDetails();
  152. }
  153. return $this->displayName;
  154. }
  155. /**
  156. * getter for email
  157. * @return string
  158. */
  159. public function getEmail() {
  160. if ($this->email === null) {
  161. $this->fetchDetails();
  162. }
  163. return $this->email;
  164. }
  165. /**
  166. * getter for home directory path
  167. * @return string
  168. */
  169. public function getHomePath() {
  170. if ($this->homePath === null) {
  171. $this->fetchDetails();
  172. }
  173. return $this->homePath;
  174. }
  175. /**
  176. * getter for the last login timestamp
  177. * @return int
  178. */
  179. public function getLastLogin() {
  180. if ($this->lastLogin === null) {
  181. $this->fetchDetails();
  182. }
  183. return (int)$this->lastLogin;
  184. }
  185. /**
  186. * getter for the detection timestamp
  187. * @return int
  188. */
  189. public function getDetectedOn() {
  190. if ($this->foundDeleted === null) {
  191. $this->fetchDetails();
  192. }
  193. return (int)$this->foundDeleted;
  194. }
  195. /**
  196. * getter for having active shares
  197. * @return bool
  198. */
  199. public function getHasActiveShares() {
  200. if ($this->hasActiveShares === null) {
  201. $this->determineShares();
  202. }
  203. return $this->hasActiveShares;
  204. }
  205. /**
  206. * reads the user details
  207. */
  208. protected function fetchDetails() {
  209. $properties = [
  210. 'displayName' => 'user_ldap',
  211. 'uid' => 'user_ldap',
  212. 'homePath' => 'user_ldap',
  213. 'foundDeleted' => 'user_ldap',
  214. 'email' => 'settings',
  215. 'lastLogin' => 'login',
  216. ];
  217. foreach ($properties as $property => $app) {
  218. $this->$property = $this->config->getUserValue($this->ocName, $app, $property, '');
  219. }
  220. }
  221. /**
  222. * finds out whether the user has active shares. The result is stored in
  223. * $this->hasActiveShares
  224. */
  225. protected function determineShares() {
  226. $shareInterface = new \ReflectionClass(IShare::class);
  227. $shareConstants = $shareInterface->getConstants();
  228. foreach ($shareConstants as $constantName => $constantValue) {
  229. if (strpos($constantName, 'TYPE_') !== 0
  230. || $constantValue === IShare::TYPE_USERGROUP
  231. ) {
  232. continue;
  233. }
  234. $shares = $this->shareManager->getSharesBy(
  235. $this->ocName,
  236. $constantValue,
  237. null,
  238. false,
  239. 1
  240. );
  241. if (!empty($shares)) {
  242. $this->hasActiveShares = true;
  243. return;
  244. }
  245. }
  246. $this->hasActiveShares = false;
  247. }
  248. }