AccountManager.php 10 KB

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