UsersController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  10. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  11. * @author GretaD <gretadoci@gmail.com>
  12. * @author Joas Schilling <coding@schilljs.com>
  13. * @author John Molakvoæ <skjnldsv@protonmail.com>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. * @author Kate Döen <kate.doeen@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. // FIXME: disabled for now to be able to inject IGroupManager and also use
  35. // getSubAdmin()
  36. namespace OCA\Settings\Controller;
  37. use InvalidArgumentException;
  38. use OC\AppFramework\Http;
  39. use OC\Encryption\Exceptions\ModuleDoesNotExistsException;
  40. use OC\ForbiddenException;
  41. use OC\Group\Manager as GroupManager;
  42. use OC\KnownUser\KnownUserService;
  43. use OC\L10N\Factory;
  44. use OC\Security\IdentityProof\Manager;
  45. use OC\User\Manager as UserManager;
  46. use OCA\Settings\BackgroundJobs\VerifyUserData;
  47. use OCA\Settings\Events\BeforeTemplateRenderedEvent;
  48. use OCA\User_LDAP\User_Proxy;
  49. use OCP\Accounts\IAccount;
  50. use OCP\Accounts\IAccountManager;
  51. use OCP\Accounts\PropertyDoesNotExistException;
  52. use OCP\App\IAppManager;
  53. use OCP\AppFramework\Controller;
  54. use OCP\AppFramework\Http\Attribute\OpenAPI;
  55. use OCP\AppFramework\Http\DataResponse;
  56. use OCP\AppFramework\Http\JSONResponse;
  57. use OCP\AppFramework\Http\TemplateResponse;
  58. use OCP\BackgroundJob\IJobList;
  59. use OCP\Encryption\IManager;
  60. use OCP\EventDispatcher\IEventDispatcher;
  61. use OCP\IConfig;
  62. use OCP\IGroupManager;
  63. use OCP\IL10N;
  64. use OCP\IRequest;
  65. use OCP\IUser;
  66. use OCP\IUserManager;
  67. use OCP\IUserSession;
  68. use OCP\L10N\IFactory;
  69. use OCP\Mail\IMailer;
  70. use function in_array;
  71. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  72. class UsersController extends Controller {
  73. /** @var UserManager */
  74. private $userManager;
  75. /** @var GroupManager */
  76. private $groupManager;
  77. /** @var IUserSession */
  78. private $userSession;
  79. /** @var IConfig */
  80. private $config;
  81. /** @var IL10N */
  82. private $l10n;
  83. /** @var IMailer */
  84. private $mailer;
  85. /** @var Factory */
  86. private $l10nFactory;
  87. /** @var IAppManager */
  88. private $appManager;
  89. /** @var IAccountManager */
  90. private $accountManager;
  91. /** @var Manager */
  92. private $keyManager;
  93. /** @var IJobList */
  94. private $jobList;
  95. /** @var IManager */
  96. private $encryptionManager;
  97. /** @var KnownUserService */
  98. private $knownUserService;
  99. /** @var IEventDispatcher */
  100. private $dispatcher;
  101. public function __construct(
  102. string $appName,
  103. IRequest $request,
  104. IUserManager $userManager,
  105. IGroupManager $groupManager,
  106. IUserSession $userSession,
  107. IConfig $config,
  108. IL10N $l10n,
  109. IMailer $mailer,
  110. IFactory $l10nFactory,
  111. IAppManager $appManager,
  112. IAccountManager $accountManager,
  113. Manager $keyManager,
  114. IJobList $jobList,
  115. IManager $encryptionManager,
  116. KnownUserService $knownUserService,
  117. IEventDispatcher $dispatcher
  118. ) {
  119. parent::__construct($appName, $request);
  120. $this->userManager = $userManager;
  121. $this->groupManager = $groupManager;
  122. $this->userSession = $userSession;
  123. $this->config = $config;
  124. $this->l10n = $l10n;
  125. $this->mailer = $mailer;
  126. $this->l10nFactory = $l10nFactory;
  127. $this->appManager = $appManager;
  128. $this->accountManager = $accountManager;
  129. $this->keyManager = $keyManager;
  130. $this->jobList = $jobList;
  131. $this->encryptionManager = $encryptionManager;
  132. $this->knownUserService = $knownUserService;
  133. $this->dispatcher = $dispatcher;
  134. }
  135. /**
  136. * @NoCSRFRequired
  137. * @NoAdminRequired
  138. *
  139. * Display users list template
  140. *
  141. * @return TemplateResponse
  142. */
  143. public function usersListByGroup(): TemplateResponse {
  144. return $this->usersList();
  145. }
  146. /**
  147. * @NoCSRFRequired
  148. * @NoAdminRequired
  149. *
  150. * Display users list template
  151. *
  152. * @return TemplateResponse
  153. */
  154. public function usersList(): TemplateResponse {
  155. $user = $this->userSession->getUser();
  156. $uid = $user->getUID();
  157. $isAdmin = $this->groupManager->isAdmin($uid);
  158. \OC::$server->getNavigationManager()->setActiveEntry('core_users');
  159. /* SORT OPTION: SORT_USERCOUNT or SORT_GROUPNAME */
  160. $sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT;
  161. $isLDAPUsed = false;
  162. if ($this->config->getSystemValue('sort_groups_by_name', false)) {
  163. $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
  164. } else {
  165. if ($this->appManager->isEnabledForUser('user_ldap')) {
  166. $isLDAPUsed =
  167. $this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy');
  168. if ($isLDAPUsed) {
  169. // LDAP user count can be slow, so we sort by group name here
  170. $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME;
  171. }
  172. }
  173. }
  174. $canChangePassword = $this->canAdminChangeUserPasswords();
  175. /* GROUPS */
  176. $groupsInfo = new \OC\Group\MetaData(
  177. $uid,
  178. $isAdmin,
  179. $this->groupManager,
  180. $this->userSession
  181. );
  182. $groupsInfo->setSorting($sortGroupsBy);
  183. [$adminGroup, $groups] = $groupsInfo->get();
  184. if (!$isLDAPUsed && $this->appManager->isEnabledForUser('user_ldap')) {
  185. $isLDAPUsed = (bool)array_reduce($this->userManager->getBackends(), function ($ldapFound, $backend) {
  186. return $ldapFound || $backend instanceof User_Proxy;
  187. });
  188. }
  189. $disabledUsers = -1;
  190. $userCount = 0;
  191. if (!$isLDAPUsed) {
  192. if ($isAdmin) {
  193. $disabledUsers = $this->userManager->countDisabledUsers();
  194. $userCount = array_reduce($this->userManager->countUsers(), function ($v, $w) {
  195. return $v + (int)$w;
  196. }, 0);
  197. } else {
  198. // User is subadmin !
  199. // Map group list to ids to retrieve the countDisabledUsersOfGroups
  200. $userGroups = $this->groupManager->getUserGroups($user);
  201. $groupsIds = [];
  202. foreach ($groups as $key => $group) {
  203. // $userCount += (int)$group['usercount'];
  204. $groupsIds[] = $group['id'];
  205. }
  206. $userCount += $this->userManager->countUsersOfGroups($groupsInfo->getGroups());
  207. $disabledUsers = $this->userManager->countDisabledUsersOfGroups($groupsIds);
  208. }
  209. $userCount -= $disabledUsers;
  210. }
  211. $disabledUsersGroup = [
  212. 'id' => 'disabled',
  213. 'name' => 'Disabled users',
  214. 'usercount' => $disabledUsers
  215. ];
  216. /* QUOTAS PRESETS */
  217. $quotaPreset = $this->parseQuotaPreset($this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'));
  218. $allowUnlimitedQuota = $this->config->getAppValue('files', 'allow_unlimited_quota', '1') === '1';
  219. if (!$allowUnlimitedQuota && count($quotaPreset) > 0) {
  220. $defaultQuota = $this->config->getAppValue('files', 'default_quota', $quotaPreset[0]);
  221. } else {
  222. $defaultQuota = $this->config->getAppValue('files', 'default_quota', 'none');
  223. }
  224. $event = new BeforeTemplateRenderedEvent();
  225. $this->dispatcher->dispatch('OC\Settings\Users::loadAdditionalScripts', $event);
  226. $this->dispatcher->dispatchTyped($event);
  227. /* LANGUAGES */
  228. $languages = $this->l10nFactory->getLanguages();
  229. /* FINAL DATA */
  230. $serverData = [];
  231. // groups
  232. $serverData['groups'] = array_merge_recursive($adminGroup, [$disabledUsersGroup], $groups);
  233. // Various data
  234. $serverData['isAdmin'] = $isAdmin;
  235. $serverData['sortGroups'] = $sortGroupsBy;
  236. $serverData['quotaPreset'] = $quotaPreset;
  237. $serverData['allowUnlimitedQuota'] = $allowUnlimitedQuota;
  238. $serverData['userCount'] = $userCount;
  239. $serverData['languages'] = $languages;
  240. $serverData['defaultLanguage'] = $this->config->getSystemValue('default_language', 'en');
  241. $serverData['forceLanguage'] = $this->config->getSystemValue('force_language', false);
  242. // Settings
  243. $serverData['defaultQuota'] = $defaultQuota;
  244. $serverData['canChangePassword'] = $canChangePassword;
  245. $serverData['newUserGenerateUserID'] = $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes';
  246. $serverData['newUserRequireEmail'] = $this->config->getAppValue('core', 'newUser.requireEmail', 'no') === 'yes';
  247. $serverData['newUserSendEmail'] = $this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes';
  248. return new TemplateResponse('settings', 'settings-vue', ['serverData' => $serverData, 'pageTitle' => $this->l10n->t('Users')]);
  249. }
  250. /**
  251. * @param string $key
  252. * @param string $value
  253. *
  254. * @return JSONResponse
  255. */
  256. public function setPreference(string $key, string $value): JSONResponse {
  257. $allowed = ['newUser.sendEmail'];
  258. if (!in_array($key, $allowed, true)) {
  259. return new JSONResponse([], Http::STATUS_FORBIDDEN);
  260. }
  261. $this->config->setAppValue('core', $key, $value);
  262. return new JSONResponse([]);
  263. }
  264. /**
  265. * Parse the app value for quota_present
  266. *
  267. * @param string $quotaPreset
  268. * @return array
  269. */
  270. protected function parseQuotaPreset(string $quotaPreset): array {
  271. // 1 GB, 5 GB, 10 GB => [1 GB, 5 GB, 10 GB]
  272. $presets = array_filter(array_map('trim', explode(',', $quotaPreset)));
  273. // Drop default and none, Make array indexes numerically
  274. return array_values(array_diff($presets, ['default', 'none']));
  275. }
  276. /**
  277. * check if the admin can change the users password
  278. *
  279. * The admin can change the passwords if:
  280. *
  281. * - no encryption module is loaded and encryption is disabled
  282. * - encryption module is loaded but it doesn't require per user keys
  283. *
  284. * The admin can not change the passwords if:
  285. *
  286. * - an encryption module is loaded and it uses per-user keys
  287. * - encryption is enabled but no encryption modules are loaded
  288. *
  289. * @return bool
  290. */
  291. protected function canAdminChangeUserPasswords(): bool {
  292. $isEncryptionEnabled = $this->encryptionManager->isEnabled();
  293. try {
  294. $noUserSpecificEncryptionKeys = !$this->encryptionManager->getEncryptionModule()->needDetailedAccessList();
  295. $isEncryptionModuleLoaded = true;
  296. } catch (ModuleDoesNotExistsException $e) {
  297. $noUserSpecificEncryptionKeys = true;
  298. $isEncryptionModuleLoaded = false;
  299. }
  300. $canChangePassword = ($isEncryptionModuleLoaded && $noUserSpecificEncryptionKeys)
  301. || (!$isEncryptionModuleLoaded && !$isEncryptionEnabled);
  302. return $canChangePassword;
  303. }
  304. /**
  305. * @NoAdminRequired
  306. * @NoSubAdminRequired
  307. * @PasswordConfirmationRequired
  308. *
  309. * @param string|null $avatarScope
  310. * @param string|null $displayname
  311. * @param string|null $displaynameScope
  312. * @param string|null $phone
  313. * @param string|null $phoneScope
  314. * @param string|null $email
  315. * @param string|null $emailScope
  316. * @param string|null $website
  317. * @param string|null $websiteScope
  318. * @param string|null $address
  319. * @param string|null $addressScope
  320. * @param string|null $twitter
  321. * @param string|null $twitterScope
  322. * @param string|null $fediverse
  323. * @param string|null $fediverseScope
  324. *
  325. * @return DataResponse
  326. */
  327. public function setUserSettings(?string $avatarScope = null,
  328. ?string $displayname = null,
  329. ?string $displaynameScope = null,
  330. ?string $phone = null,
  331. ?string $phoneScope = null,
  332. ?string $email = null,
  333. ?string $emailScope = null,
  334. ?string $website = null,
  335. ?string $websiteScope = null,
  336. ?string $address = null,
  337. ?string $addressScope = null,
  338. ?string $twitter = null,
  339. ?string $twitterScope = null,
  340. ?string $fediverse = null,
  341. ?string $fediverseScope = null
  342. ) {
  343. $user = $this->userSession->getUser();
  344. if (!$user instanceof IUser) {
  345. return new DataResponse(
  346. [
  347. 'status' => 'error',
  348. 'data' => [
  349. 'message' => $this->l10n->t('Invalid user')
  350. ]
  351. ],
  352. Http::STATUS_UNAUTHORIZED
  353. );
  354. }
  355. $email = !is_null($email) ? strtolower($email) : $email;
  356. if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
  357. return new DataResponse(
  358. [
  359. 'status' => 'error',
  360. 'data' => [
  361. 'message' => $this->l10n->t('Invalid mail address')
  362. ]
  363. ],
  364. Http::STATUS_UNPROCESSABLE_ENTITY
  365. );
  366. }
  367. $userAccount = $this->accountManager->getAccount($user);
  368. $oldPhoneValue = $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getValue();
  369. $updatable = [
  370. IAccountManager::PROPERTY_AVATAR => ['value' => null, 'scope' => $avatarScope],
  371. IAccountManager::PROPERTY_DISPLAYNAME => ['value' => $displayname, 'scope' => $displaynameScope],
  372. IAccountManager::PROPERTY_EMAIL => ['value' => $email, 'scope' => $emailScope],
  373. IAccountManager::PROPERTY_WEBSITE => ['value' => $website, 'scope' => $websiteScope],
  374. IAccountManager::PROPERTY_ADDRESS => ['value' => $address, 'scope' => $addressScope],
  375. IAccountManager::PROPERTY_PHONE => ['value' => $phone, 'scope' => $phoneScope],
  376. IAccountManager::PROPERTY_TWITTER => ['value' => $twitter, 'scope' => $twitterScope],
  377. IAccountManager::PROPERTY_FEDIVERSE => ['value' => $fediverse, 'scope' => $fediverseScope],
  378. ];
  379. $allowUserToChangeDisplayName = $this->config->getSystemValueBool('allow_user_to_change_display_name', true);
  380. foreach ($updatable as $property => $data) {
  381. if ($allowUserToChangeDisplayName === false
  382. && in_array($property, [IAccountManager::PROPERTY_DISPLAYNAME, IAccountManager::PROPERTY_EMAIL], true)) {
  383. continue;
  384. }
  385. $property = $userAccount->getProperty($property);
  386. if (null !== $data['value']) {
  387. $property->setValue($data['value']);
  388. }
  389. if (null !== $data['scope']) {
  390. $property->setScope($data['scope']);
  391. }
  392. }
  393. try {
  394. $this->saveUserSettings($userAccount);
  395. if ($oldPhoneValue !== $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getValue()) {
  396. $this->knownUserService->deleteByContactUserId($user->getUID());
  397. }
  398. return new DataResponse(
  399. [
  400. 'status' => 'success',
  401. 'data' => [
  402. 'userId' => $user->getUID(),
  403. 'avatarScope' => $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope(),
  404. 'displayname' => $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getValue(),
  405. 'displaynameScope' => $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope(),
  406. 'phone' => $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getValue(),
  407. 'phoneScope' => $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getScope(),
  408. 'email' => $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue(),
  409. 'emailScope' => $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(),
  410. 'website' => $userAccount->getProperty(IAccountManager::PROPERTY_WEBSITE)->getValue(),
  411. 'websiteScope' => $userAccount->getProperty(IAccountManager::PROPERTY_WEBSITE)->getScope(),
  412. 'address' => $userAccount->getProperty(IAccountManager::PROPERTY_ADDRESS)->getValue(),
  413. 'addressScope' => $userAccount->getProperty(IAccountManager::PROPERTY_ADDRESS)->getScope(),
  414. 'twitter' => $userAccount->getProperty(IAccountManager::PROPERTY_TWITTER)->getValue(),
  415. 'twitterScope' => $userAccount->getProperty(IAccountManager::PROPERTY_TWITTER)->getScope(),
  416. 'fediverse' => $userAccount->getProperty(IAccountManager::PROPERTY_FEDIVERSE)->getValue(),
  417. 'fediverseScope' => $userAccount->getProperty(IAccountManager::PROPERTY_FEDIVERSE)->getScope(),
  418. 'message' => $this->l10n->t('Settings saved'),
  419. ],
  420. ],
  421. Http::STATUS_OK
  422. );
  423. } catch (ForbiddenException | InvalidArgumentException | PropertyDoesNotExistException $e) {
  424. return new DataResponse([
  425. 'status' => 'error',
  426. 'data' => [
  427. 'message' => $e->getMessage()
  428. ],
  429. ]);
  430. }
  431. }
  432. /**
  433. * update account manager with new user data
  434. *
  435. * @throws ForbiddenException
  436. * @throws InvalidArgumentException
  437. */
  438. protected function saveUserSettings(IAccount $userAccount): void {
  439. // keep the user back-end up-to-date with the latest display name and email
  440. // address
  441. $oldDisplayName = $userAccount->getUser()->getDisplayName();
  442. if ($oldDisplayName !== $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getValue()) {
  443. $result = $userAccount->getUser()->setDisplayName($userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getValue());
  444. if ($result === false) {
  445. throw new ForbiddenException($this->l10n->t('Unable to change full name'));
  446. }
  447. }
  448. $oldEmailAddress = $userAccount->getUser()->getSystemEMailAddress();
  449. $oldEmailAddress = strtolower((string)$oldEmailAddress);
  450. if ($oldEmailAddress !== strtolower($userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue())) {
  451. // this is the only permission a backend provides and is also used
  452. // for the permission of setting a email address
  453. if (!$userAccount->getUser()->canChangeDisplayName()) {
  454. throw new ForbiddenException($this->l10n->t('Unable to change email address'));
  455. }
  456. $userAccount->getUser()->setSystemEMailAddress($userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue());
  457. }
  458. try {
  459. $this->accountManager->updateAccount($userAccount);
  460. } catch (InvalidArgumentException $e) {
  461. if ($e->getMessage() === IAccountManager::PROPERTY_PHONE) {
  462. throw new InvalidArgumentException($this->l10n->t('Unable to set invalid phone number'));
  463. }
  464. if ($e->getMessage() === IAccountManager::PROPERTY_WEBSITE) {
  465. throw new InvalidArgumentException($this->l10n->t('Unable to set invalid website'));
  466. }
  467. throw new InvalidArgumentException($this->l10n->t('Some account data was invalid'));
  468. }
  469. }
  470. /**
  471. * Set the mail address of a user
  472. *
  473. * @NoAdminRequired
  474. * @NoSubAdminRequired
  475. * @PasswordConfirmationRequired
  476. *
  477. * @param string $account
  478. * @param bool $onlyVerificationCode only return verification code without updating the data
  479. * @return DataResponse
  480. */
  481. public function getVerificationCode(string $account, bool $onlyVerificationCode): DataResponse {
  482. $user = $this->userSession->getUser();
  483. if ($user === null) {
  484. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  485. }
  486. $userAccount = $this->accountManager->getAccount($user);
  487. $cloudId = $user->getCloudId();
  488. $message = 'Use my Federated Cloud ID to share with me: ' . $cloudId;
  489. $signature = $this->signMessage($user, $message);
  490. $code = $message . ' ' . $signature;
  491. $codeMd5 = $message . ' ' . md5($signature);
  492. switch ($account) {
  493. case 'verify-twitter':
  494. $msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
  495. $code = $codeMd5;
  496. $type = IAccountManager::PROPERTY_TWITTER;
  497. break;
  498. case 'verify-website':
  499. $msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
  500. $type = IAccountManager::PROPERTY_WEBSITE;
  501. break;
  502. default:
  503. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  504. }
  505. $userProperty = $userAccount->getProperty($type);
  506. $userProperty
  507. ->setVerified(IAccountManager::VERIFICATION_IN_PROGRESS)
  508. ->setVerificationData($signature);
  509. if ($onlyVerificationCode === false) {
  510. $this->accountManager->updateAccount($userAccount);
  511. $this->jobList->add(VerifyUserData::class,
  512. [
  513. 'verificationCode' => $code,
  514. 'data' => $userProperty->getValue(),
  515. 'type' => $type,
  516. 'uid' => $user->getUID(),
  517. 'try' => 0,
  518. 'lastRun' => $this->getCurrentTime()
  519. ]
  520. );
  521. }
  522. return new DataResponse(['msg' => $msg, 'code' => $code]);
  523. }
  524. /**
  525. * get current timestamp
  526. *
  527. * @return int
  528. */
  529. protected function getCurrentTime(): int {
  530. return time();
  531. }
  532. /**
  533. * sign message with users private key
  534. *
  535. * @param IUser $user
  536. * @param string $message
  537. *
  538. * @return string base64 encoded signature
  539. */
  540. protected function signMessage(IUser $user, string $message): string {
  541. $privateKey = $this->keyManager->getKey($user)->getPrivate();
  542. openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
  543. return base64_encode($signature);
  544. }
  545. }