UsersController.php 20 KB

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