OfflineUser.php 5.2 KB

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