-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christopher Ng <chrng8@gmail.com>
- Loading branch information
Showing
3 changed files
with
184 additions
and
29 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Guests\BackgroundJob; | ||
|
||
use OCA\Guests\AppInfo\Application; | ||
use OCA\Guests\Db\Transfer; | ||
use OCA\Guests\Db\TransferMapper; | ||
use OCA\Guests\GuestManager; | ||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\BackgroundJob\QueuedJob; | ||
use OCP\IURLGenerator; | ||
use OCP\IUser; | ||
use OCP\IUserManager; | ||
use OCP\Notification\IManager as NotificationManager; | ||
use OCP\Security\ISecureRandom; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class TransferJob extends QueuedJob { | ||
public function __construct( | ||
private IUserManager $userManager, | ||
private ISecureRandom $secureRandom, | ||
private NotificationManager $notificationManager, | ||
private IURLGenerator $urlGenerator, | ||
private GuestManager $guestManager, | ||
private TransferMapper $transferMapper, | ||
private LoggerInterface $logger, | ||
) | ||
{ | ||
} | ||
|
||
private function notifyFailure(Transfer $transfer): void { | ||
$notification = $this->notificationManager->createNotification(); | ||
$notification | ||
->setApp(Application::APP_ID) | ||
->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath(Application::APP_ID, 'account-arrow-right.svg'))) | ||
->setUser($transfer->getAuthor()) | ||
->setDateTime($this->time->getDateTime()) | ||
->setObject('guest-transfer', (string)$transfer->getId()) | ||
->setSubject('guest-transfer-fail', [ | ||
'source' => $transfer->getSource(), | ||
'target' => $transfer->getTarget(), | ||
]); | ||
$this->notificationManager->notify($notification); | ||
} | ||
|
||
private function notifySuccess(Transfer $transfer): void { | ||
$notification = $this->notificationManager->createNotification(); | ||
$notification | ||
->setApp(Application::APP_ID) | ||
->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath(Application::APP_ID, 'account-arrow-right.svg'))) | ||
->setUser($transfer->getAuthor()) | ||
->setDateTime($this->time->getDateTime()) | ||
->setObject('guest-transfer', (string)$transfer->getId()) | ||
->setSubject('guest-transfer-done', [ | ||
'source' => $transfer->getSource(), | ||
'target' => $transfer->getTarget(), | ||
]); | ||
$this->notificationManager->notify($notification); | ||
} | ||
|
||
public function run($argument): void { | ||
/** @var int $id */ | ||
$id = $argument['id']; | ||
|
||
try { | ||
$transfer = $this->transferMapper->getById($id); | ||
} catch (DoesNotExistException $e) { | ||
$this->notifyFailure($transfer); | ||
return; | ||
} | ||
$transfer->setStatus(Transfer::STATUS_STARTED); | ||
$this->transferMapper->update($transfer); | ||
|
||
$source = $transfer->getSource(); | ||
$target = $transfer->getTarget(); | ||
|
||
$sourceUser = $this->userManager->get($source); | ||
if (!($sourceUser instanceof IUser)) { | ||
$this->logger->error('Failed to transfer missing guest user: ' . $source); | ||
$this->notifyFailure($transfer); | ||
$this->transferMapper->delete($transfer); | ||
return; | ||
} | ||
|
||
if ($this->userManager->userExists($target)) { | ||
$this->logger->error("Cannot transfer guest user \"$source\", target user \"$target\" already exists"); | ||
$this->notifyFailure($transfer); | ||
$this->transferMapper->delete($transfer); | ||
return; | ||
} | ||
|
||
$targetUser = $this->userManager->createUser( | ||
$target, | ||
$this->secureRandom->generate(20), // Password hash will be copied to target user from source user | ||
); | ||
|
||
if (!($targetUser instanceof IUser)) { | ||
$this->logger->error('Failed to create new user: ' . $target); | ||
$this->notifyFailure($transfer); | ||
$this->transferMapper->delete($transfer); | ||
return; | ||
} | ||
|
||
$targetUser->setSystemEMailAddress($sourceUser->getUID()); // Guest user id is an email | ||
|
||
// TODO copy password hash to target user | ||
|
||
try { | ||
$this->guestManager->transfer($sourceUser, $targetUser); | ||
$result = $sourceUser->delete(); | ||
if (!$result) { | ||
$this->logger->error('Failed to delete user', ['userId' => $sourceUser->getUID()]); | ||
} | ||
$this->notifySuccess($transfer); | ||
} catch (\Throwable $th) { | ||
$this->logger->error($th->getMessage(), ['exception' => $th]); | ||
$result = $targetUser->delete(); // Rollback created user | ||
if (!$result) { | ||
$this->logger->error('Failed to delete user', ['userId' => $targetUser->getUID()]); | ||
} | ||
$this->notifyFailure($transfer); | ||
} | ||
$this->transferMapper->delete($transfer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters