user.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\User;
  9. use OC\Hooks\Emitter;
  10. class User {
  11. /**
  12. * @var string $uid
  13. */
  14. private $uid;
  15. /**
  16. * @var string $displayName
  17. */
  18. private $displayName;
  19. /**
  20. * @var \OC_User_Backend $backend
  21. */
  22. private $backend;
  23. /**
  24. * @var bool $enabled
  25. */
  26. private $enabled;
  27. /**
  28. * @var Emitter | Manager $emitter
  29. */
  30. private $emitter;
  31. /**
  32. * @param string $uid
  33. * @param \OC_User_Backend $backend
  34. * @param Emitter $emitter
  35. */
  36. public function __construct($uid, $backend, $emitter = null) {
  37. $this->uid = $uid;
  38. if ($backend and $backend->implementsActions(OC_USER_BACKEND_GET_DISPLAYNAME)) {
  39. $this->displayName = $backend->getDisplayName($uid);
  40. } else {
  41. $this->displayName = $uid;
  42. }
  43. $this->backend = $backend;
  44. $this->emitter = $emitter;
  45. $enabled = \OC_Preferences::getValue($uid, 'core', 'enabled', 'true'); //TODO: DI for OC_Preferences
  46. $this->enabled = ($enabled === 'true');
  47. }
  48. /**
  49. * get the user id
  50. *
  51. * @return string
  52. */
  53. public function getUID() {
  54. return $this->uid;
  55. }
  56. /**
  57. * get the displayname for the user, if no specific displayname is set it will fallback to the user id
  58. *
  59. * @return string
  60. */
  61. public function getDisplayName() {
  62. return $this->displayName;
  63. }
  64. /**
  65. * set the displayname for the user
  66. *
  67. * @param string $displayName
  68. * @return bool
  69. */
  70. public function setDisplayName($displayName) {
  71. if ($this->canChangeDisplayName()) {
  72. $this->displayName = $displayName;
  73. $result = $this->backend->setDisplayName($this->uid, $displayName);
  74. return $result !== false;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * Delete the user
  81. *
  82. * @return bool
  83. */
  84. public function delete() {
  85. if ($this->emitter) {
  86. $this->emitter->emit('\OC\User', 'preDelete', array($this));
  87. }
  88. $result = $this->backend->deleteUser($this->uid);
  89. if ($this->emitter) {
  90. $this->emitter->emit('\OC\User', 'postDelete', array($this));
  91. }
  92. return !($result === false);
  93. }
  94. /**
  95. * Check if the password is valid for the user
  96. *
  97. * @param $password
  98. * @return bool
  99. */
  100. public function checkPassword($password) {
  101. if ($this->backend->implementsActions(\OC_USER_BACKEND_CHECK_PASSWORD)) {
  102. $result = $this->backend->checkPassword($this->uid, $password);
  103. if ($result !== false) {
  104. $this->uid = $result;
  105. }
  106. return !($result === false);
  107. } else {
  108. return false;
  109. }
  110. }
  111. /**
  112. * Set the password of the user
  113. *
  114. * @param string $password
  115. * @param string $recoveryPassword for the encryption app to reset encryption keys
  116. * @return bool
  117. */
  118. public function setPassword($password, $recoveryPassword) {
  119. if ($this->emitter) {
  120. $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword));
  121. }
  122. if ($this->backend->implementsActions(\OC_USER_BACKEND_SET_PASSWORD)) {
  123. $result = $this->backend->setPassword($this->uid, $password);
  124. if ($this->emitter) {
  125. $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword));
  126. }
  127. return !($result === false);
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * get the users home folder to mount
  134. *
  135. * @return string
  136. */
  137. public function getHome() {
  138. if ($this->backend->implementsActions(\OC_USER_BACKEND_GET_HOME) and $home = $this->backend->getHome($this->uid)) {
  139. return $home;
  140. }
  141. return \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data") . '/' . $this->uid; //TODO switch to Config object once implemented
  142. }
  143. /**
  144. * check if the backend supports changing passwords
  145. *
  146. * @return bool
  147. */
  148. public function canChangePassword() {
  149. return $this->backend->implementsActions(\OC_USER_BACKEND_SET_PASSWORD);
  150. }
  151. /**
  152. * check if the backend supports changing display names
  153. *
  154. * @return bool
  155. */
  156. public function canChangeDisplayName() {
  157. return $this->backend->implementsActions(\OC_USER_BACKEND_SET_DISPLAYNAME);
  158. }
  159. /**
  160. * check if the user is enabled
  161. *
  162. * @return bool
  163. */
  164. public function isEnabled() {
  165. return $this->enabled;
  166. }
  167. /**
  168. * set the enabled status for the user
  169. *
  170. * @param bool $enabled
  171. */
  172. public function setEnabled($enabled) {
  173. $this->enabled = $enabled;
  174. $enabled = ($enabled) ? 'true' : 'false';
  175. \OC_Preferences::setValue($this->uid, 'core', 'enabled', $enabled);
  176. }
  177. }