offlineuser.php 4.6 KB

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