You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccountManager.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @author Björn Schießle <bjoern@schiessle.org>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @copyright Copyright (c) 2016, Björn Schießle
  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 OC\Accounts;
  23. use OCP\IDBConnection;
  24. use OCP\IUser;
  25. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  26. use Symfony\Component\EventDispatcher\GenericEvent;
  27. /**
  28. * Class AccountManager
  29. *
  30. * Manage system accounts table
  31. *
  32. * @group DB
  33. * @package OC\Accounts
  34. */
  35. class AccountManager {
  36. /** nobody can see my account details */
  37. const VISIBILITY_PRIVATE = 'private';
  38. /** only contacts, especially trusted servers can see my contact details */
  39. const VISIBILITY_CONTACTS_ONLY = 'contacts';
  40. /** every body ca see my contact detail, will be published to the lookup server */
  41. const VISIBILITY_PUBLIC = 'public';
  42. const PROPERTY_AVATAR = 'avatar';
  43. const PROPERTY_DISPLAYNAME = 'displayname';
  44. const PROPERTY_PHONE = 'phone';
  45. const PROPERTY_EMAIL = 'email';
  46. const PROPERTY_WEBSITE = 'website';
  47. const PROPERTY_ADDRESS = 'address';
  48. const PROPERTY_TWITTER = 'twitter';
  49. /** @var IDBConnection database connection */
  50. private $connection;
  51. /** @var string table name */
  52. private $table = 'accounts';
  53. /** @var EventDispatcherInterface */
  54. private $eventDispatcher;
  55. /**
  56. * AccountManager constructor.
  57. *
  58. * @param IDBConnection $connection
  59. * @param EventDispatcherInterface $eventDispatcher
  60. */
  61. public function __construct(IDBConnection $connection, EventDispatcherInterface $eventDispatcher) {
  62. $this->connection = $connection;
  63. $this->eventDispatcher = $eventDispatcher;
  64. }
  65. /**
  66. * update user record
  67. *
  68. * @param IUser $user
  69. * @param $data
  70. */
  71. public function updateUser(IUser $user, $data) {
  72. $userData = $this->getUser($user);
  73. $updated = true;
  74. if (empty($userData)) {
  75. $this->insertNewUser($user, $data);
  76. } elseif ($userData !== $data) {
  77. $this->updateExistingUser($user, $data);
  78. } else {
  79. // nothing needs to be done if new and old data set are the same
  80. $updated = false;
  81. }
  82. if ($updated) {
  83. $this->eventDispatcher->dispatch(
  84. 'OC\AccountManager::userUpdated',
  85. new GenericEvent($user)
  86. );
  87. }
  88. }
  89. /**
  90. * get stored data from a given user
  91. *
  92. * @param IUser $user
  93. * @return array
  94. */
  95. public function getUser(IUser $user) {
  96. $uid = $user->getUID();
  97. $query = $this->connection->getQueryBuilder();
  98. $query->select('data')->from($this->table)
  99. ->where($query->expr()->eq('uid', $query->createParameter('uid')))
  100. ->setParameter('uid', $uid);
  101. $query->execute();
  102. $result = $query->execute()->fetchAll();
  103. if (empty($result)) {
  104. $userData = $this->buildDefaultUserRecord($user);
  105. $this->insertNewUser($user, $userData);
  106. return $userData;
  107. }
  108. return json_decode($result[0]['data'], true);
  109. }
  110. /**
  111. * add new user to accounts table
  112. *
  113. * @param IUser $user
  114. * @param array $data
  115. */
  116. protected function insertNewUser(IUser $user, $data) {
  117. $uid = $user->getUID();
  118. $jsonEncodedData = json_encode($data);
  119. $query = $this->connection->getQueryBuilder();
  120. $query->insert($this->table)
  121. ->values(
  122. [
  123. 'uid' => $query->createNamedParameter($uid),
  124. 'data' => $query->createNamedParameter($jsonEncodedData),
  125. ]
  126. )
  127. ->execute();
  128. }
  129. /**
  130. * update existing user in accounts table
  131. *
  132. * @param IUser $user
  133. * @param array $data
  134. */
  135. protected function updateExistingUser(IUser $user, $data) {
  136. $uid = $user->getUID();
  137. $jsonEncodedData = json_encode($data);
  138. $query = $this->connection->getQueryBuilder();
  139. $query->update($this->table)
  140. ->set('data', $query->createNamedParameter($jsonEncodedData))
  141. ->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
  142. ->execute();
  143. }
  144. /**
  145. * build default user record in case not data set exists yet
  146. *
  147. * @param IUser $user
  148. * @return array
  149. */
  150. protected function buildDefaultUserRecord(IUser $user) {
  151. return [
  152. self::PROPERTY_DISPLAYNAME =>
  153. [
  154. 'value' => $user->getDisplayName(),
  155. 'scope' => self::VISIBILITY_CONTACTS_ONLY,
  156. ],
  157. self::PROPERTY_ADDRESS =>
  158. [
  159. 'value' => '',
  160. 'scope' => self::VISIBILITY_PRIVATE,
  161. ],
  162. self::PROPERTY_WEBSITE =>
  163. [
  164. 'value' => '',
  165. 'scope' => self::VISIBILITY_PRIVATE,
  166. ],
  167. self::PROPERTY_EMAIL =>
  168. [
  169. 'value' => $user->getEMailAddress(),
  170. 'scope' => self::VISIBILITY_CONTACTS_ONLY,
  171. ],
  172. self::PROPERTY_AVATAR =>
  173. [
  174. 'scope' => self::VISIBILITY_CONTACTS_ONLY
  175. ],
  176. self::PROPERTY_PHONE =>
  177. [
  178. 'value' => '',
  179. 'scope' => self::VISIBILITY_PRIVATE,
  180. ],
  181. self::PROPERTY_TWITTER =>
  182. [
  183. 'value' => '',
  184. 'scope' => self::VISIBILITY_PRIVATE,
  185. ],
  186. ];
  187. }
  188. }