AccountManager.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Björn Schießle
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Joas Schilling <coding@schilljs.com>
  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 OC\Accounts;
  26. use OCP\Accounts\IAccount;
  27. use OCP\Accounts\IAccountManager;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\IDBConnection;
  30. use OCP\IUser;
  31. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  32. use Symfony\Component\EventDispatcher\GenericEvent;
  33. use OC\Settings\BackgroundJobs\VerifyUserData;
  34. /**
  35. * Class AccountManager
  36. *
  37. * Manage system accounts table
  38. *
  39. * @group DB
  40. * @package OC\Accounts
  41. */
  42. class AccountManager implements IAccountManager {
  43. /** @var IDBConnection database connection */
  44. private $connection;
  45. /** @var string table name */
  46. private $table = 'accounts';
  47. /** @var EventDispatcherInterface */
  48. private $eventDispatcher;
  49. /** @var IJobList */
  50. private $jobList;
  51. /**
  52. * AccountManager constructor.
  53. *
  54. * @param IDBConnection $connection
  55. * @param EventDispatcherInterface $eventDispatcher
  56. * @param IJobList $jobList
  57. */
  58. public function __construct(IDBConnection $connection,
  59. EventDispatcherInterface $eventDispatcher,
  60. IJobList $jobList) {
  61. $this->connection = $connection;
  62. $this->eventDispatcher = $eventDispatcher;
  63. $this->jobList = $jobList;
  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. $data = $this->checkEmailVerification($userData, $data, $user);
  78. $data = $this->updateVerifyStatus($userData, $data);
  79. $this->updateExistingUser($user, $data);
  80. } else {
  81. // nothing needs to be done if new and old data set are the same
  82. $updated = false;
  83. }
  84. if ($updated) {
  85. $this->eventDispatcher->dispatch(
  86. 'OC\AccountManager::userUpdated',
  87. new GenericEvent($user, $data)
  88. );
  89. }
  90. }
  91. /**
  92. * delete user from accounts table
  93. *
  94. * @param IUser $user
  95. */
  96. public function deleteUser(IUser $user) {
  97. $uid = $user->getUID();
  98. $query = $this->connection->getQueryBuilder();
  99. $query->delete($this->table)
  100. ->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
  101. ->execute();
  102. }
  103. /**
  104. * get stored data from a given user
  105. *
  106. * @param IUser $user
  107. * @return array
  108. */
  109. public function getUser(IUser $user) {
  110. $uid = $user->getUID();
  111. $query = $this->connection->getQueryBuilder();
  112. $query->select('data')->from($this->table)
  113. ->where($query->expr()->eq('uid', $query->createParameter('uid')))
  114. ->setParameter('uid', $uid);
  115. $query->execute();
  116. $result = $query->execute()->fetchAll();
  117. if (empty($result)) {
  118. $userData = $this->buildDefaultUserRecord($user);
  119. $this->insertNewUser($user, $userData);
  120. return $userData;
  121. }
  122. $userDataArray = json_decode($result[0]['data'], true);
  123. $userDataArray = $this->addMissingDefaultValues($userDataArray);
  124. return $userDataArray;
  125. }
  126. /**
  127. * check if we need to ask the server for email verification, if yes we create a cronjob
  128. *
  129. * @param $oldData
  130. * @param $newData
  131. * @param IUser $user
  132. * @return array
  133. */
  134. protected function checkEmailVerification($oldData, $newData, IUser $user) {
  135. if ($oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']) {
  136. $this->jobList->add(VerifyUserData::class,
  137. [
  138. 'verificationCode' => '',
  139. 'data' => $newData[self::PROPERTY_EMAIL]['value'],
  140. 'type' => self::PROPERTY_EMAIL,
  141. 'uid' => $user->getUID(),
  142. 'try' => 0,
  143. 'lastRun' => time()
  144. ]
  145. );
  146. $newData[AccountManager::PROPERTY_EMAIL]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
  147. }
  148. return $newData;
  149. }
  150. /**
  151. * make sure that all expected data are set
  152. *
  153. * @param array $userData
  154. * @return array
  155. */
  156. protected function addMissingDefaultValues(array $userData) {
  157. foreach ($userData as $key => $value) {
  158. if (!isset($userData[$key]['verified'])) {
  159. $userData[$key]['verified'] = self::NOT_VERIFIED;
  160. }
  161. }
  162. return $userData;
  163. }
  164. /**
  165. * reset verification status if personal data changed
  166. *
  167. * @param array $oldData
  168. * @param array $newData
  169. * @return array
  170. */
  171. protected function updateVerifyStatus($oldData, $newData) {
  172. // which account was already verified successfully?
  173. $twitterVerified = isset($oldData[self::PROPERTY_TWITTER]['verified']) && $oldData[self::PROPERTY_TWITTER]['verified'] === self::VERIFIED;
  174. $websiteVerified = isset($oldData[self::PROPERTY_WEBSITE]['verified']) && $oldData[self::PROPERTY_WEBSITE]['verified'] === self::VERIFIED;
  175. $emailVerified = isset($oldData[self::PROPERTY_EMAIL]['verified']) && $oldData[self::PROPERTY_EMAIL]['verified'] === self::VERIFIED;
  176. // keep old verification status if we don't have a new one
  177. if(!isset($newData[self::PROPERTY_TWITTER]['verified'])) {
  178. // keep old verification status if value didn't changed and an old value exists
  179. $keepOldStatus = $newData[self::PROPERTY_TWITTER]['value'] === $oldData[self::PROPERTY_TWITTER]['value'] && isset($oldData[self::PROPERTY_TWITTER]['verified']);
  180. $newData[self::PROPERTY_TWITTER]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_TWITTER]['verified'] : self::NOT_VERIFIED;
  181. }
  182. if(!isset($newData[self::PROPERTY_WEBSITE]['verified'])) {
  183. // keep old verification status if value didn't changed and an old value exists
  184. $keepOldStatus = $newData[self::PROPERTY_WEBSITE]['value'] === $oldData[self::PROPERTY_WEBSITE]['value'] && isset($oldData[self::PROPERTY_WEBSITE]['verified']);
  185. $newData[self::PROPERTY_WEBSITE]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_WEBSITE]['verified'] : self::NOT_VERIFIED;
  186. }
  187. if(!isset($newData[self::PROPERTY_EMAIL]['verified'])) {
  188. // keep old verification status if value didn't changed and an old value exists
  189. $keepOldStatus = $newData[self::PROPERTY_EMAIL]['value'] === $oldData[self::PROPERTY_EMAIL]['value'] && isset($oldData[self::PROPERTY_EMAIL]['verified']);
  190. $newData[self::PROPERTY_EMAIL]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_EMAIL]['verified'] : self::VERIFICATION_IN_PROGRESS;
  191. }
  192. // reset verification status if a value from a previously verified data was changed
  193. if($twitterVerified &&
  194. $oldData[self::PROPERTY_TWITTER]['value'] !== $newData[self::PROPERTY_TWITTER]['value']
  195. ) {
  196. $newData[self::PROPERTY_TWITTER]['verified'] = self::NOT_VERIFIED;
  197. }
  198. if($websiteVerified &&
  199. $oldData[self::PROPERTY_WEBSITE]['value'] !== $newData[self::PROPERTY_WEBSITE]['value']
  200. ) {
  201. $newData[self::PROPERTY_WEBSITE]['verified'] = self::NOT_VERIFIED;
  202. }
  203. if($emailVerified &&
  204. $oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']
  205. ) {
  206. $newData[self::PROPERTY_EMAIL]['verified'] = self::NOT_VERIFIED;
  207. }
  208. return $newData;
  209. }
  210. /**
  211. * add new user to accounts table
  212. *
  213. * @param IUser $user
  214. * @param array $data
  215. */
  216. protected function insertNewUser(IUser $user, $data) {
  217. $uid = $user->getUID();
  218. $jsonEncodedData = json_encode($data);
  219. $query = $this->connection->getQueryBuilder();
  220. $query->insert($this->table)
  221. ->values(
  222. [
  223. 'uid' => $query->createNamedParameter($uid),
  224. 'data' => $query->createNamedParameter($jsonEncodedData),
  225. ]
  226. )
  227. ->execute();
  228. }
  229. /**
  230. * update existing user in accounts table
  231. *
  232. * @param IUser $user
  233. * @param array $data
  234. */
  235. protected function updateExistingUser(IUser $user, $data) {
  236. $uid = $user->getUID();
  237. $jsonEncodedData = json_encode($data);
  238. $query = $this->connection->getQueryBuilder();
  239. $query->update($this->table)
  240. ->set('data', $query->createNamedParameter($jsonEncodedData))
  241. ->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
  242. ->execute();
  243. }
  244. /**
  245. * build default user record in case not data set exists yet
  246. *
  247. * @param IUser $user
  248. * @return array
  249. */
  250. protected function buildDefaultUserRecord(IUser $user) {
  251. return [
  252. self::PROPERTY_DISPLAYNAME =>
  253. [
  254. 'value' => $user->getDisplayName(),
  255. 'scope' => self::VISIBILITY_CONTACTS_ONLY,
  256. 'verified' => self::NOT_VERIFIED,
  257. ],
  258. self::PROPERTY_ADDRESS =>
  259. [
  260. 'value' => '',
  261. 'scope' => self::VISIBILITY_PRIVATE,
  262. 'verified' => self::NOT_VERIFIED,
  263. ],
  264. self::PROPERTY_WEBSITE =>
  265. [
  266. 'value' => '',
  267. 'scope' => self::VISIBILITY_PRIVATE,
  268. 'verified' => self::NOT_VERIFIED,
  269. ],
  270. self::PROPERTY_EMAIL =>
  271. [
  272. 'value' => $user->getEMailAddress(),
  273. 'scope' => self::VISIBILITY_CONTACTS_ONLY,
  274. 'verified' => self::NOT_VERIFIED,
  275. ],
  276. self::PROPERTY_AVATAR =>
  277. [
  278. 'scope' => self::VISIBILITY_CONTACTS_ONLY
  279. ],
  280. self::PROPERTY_PHONE =>
  281. [
  282. 'value' => '',
  283. 'scope' => self::VISIBILITY_PRIVATE,
  284. 'verified' => self::NOT_VERIFIED,
  285. ],
  286. self::PROPERTY_TWITTER =>
  287. [
  288. 'value' => '',
  289. 'scope' => self::VISIBILITY_PRIVATE,
  290. 'verified' => self::NOT_VERIFIED,
  291. ],
  292. ];
  293. }
  294. private function parseAccountData(IUser $user, $data): Account {
  295. $account = new Account($user);
  296. foreach($data as $property => $accountData) {
  297. $account->setProperty($property, $accountData['value'] ?? '', $accountData['scope'] ?? self::VISIBILITY_PRIVATE, $accountData['verified'] ?? self::NOT_VERIFIED);
  298. }
  299. return $account;
  300. }
  301. public function getAccount(IUser $user): IAccount {
  302. return $this->parseAccountData($user, $this->getUser($user));
  303. }
  304. }