allow using saved login credentials for notify
when only the user is provided, we use to provided user to try and get saved login credentials Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
09b4f9b13e
commit
b750503f4a
|
@ -41,6 +41,7 @@ use OCP\Files\Storage\IStorage;
|
||||||
use OCP\Files\StorageNotAvailableException;
|
use OCP\Files\StorageNotAvailableException;
|
||||||
use OCP\IDBConnection;
|
use OCP\IDBConnection;
|
||||||
use OCP\ILogger;
|
use OCP\ILogger;
|
||||||
|
use OCP\IUserManager;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
@ -53,12 +54,20 @@ class Notify extends Base {
|
||||||
private $connection;
|
private $connection;
|
||||||
/** @var ILogger */
|
/** @var ILogger */
|
||||||
private $logger;
|
private $logger;
|
||||||
|
/** @var IUserManager */
|
||||||
|
private $userManager;
|
||||||
|
|
||||||
public function __construct(GlobalStoragesService $globalService, IDBConnection $connection, ILogger $logger) {
|
public function __construct(
|
||||||
|
GlobalStoragesService $globalService,
|
||||||
|
IDBConnection $connection,
|
||||||
|
ILogger $logger,
|
||||||
|
IUserManager $userManager
|
||||||
|
) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->globalService = $globalService;
|
$this->globalService = $globalService;
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->userManager = $userManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure() {
|
protected function configure() {
|
||||||
|
@ -89,6 +98,30 @@ class Notify extends Base {
|
||||||
parent::configure();
|
parent::configure();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getUserOption(InputInterface $input): ?string {
|
||||||
|
if ($input->getOption('user')) {
|
||||||
|
return (string)$input->getOption('user');
|
||||||
|
} elseif (isset($_ENV['NOTIFY_USER'])) {
|
||||||
|
return (string)$_ENV['NOTIFY_USER'];
|
||||||
|
} elseif (isset($_SERVER['NOTIFY_USER'])) {
|
||||||
|
return (string)$_SERVER['NOTIFY_USER'];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getPasswordOption(InputInterface $input): ?string {
|
||||||
|
if ($input->getOption('password')) {
|
||||||
|
return (string)$input->getOption('password');
|
||||||
|
} elseif (isset($_ENV['NOTIFY_PASSWORD'])) {
|
||||||
|
return (string)$_ENV['NOTIFY_PASSWORD'];
|
||||||
|
} elseif (isset($_SERVER['NOTIFY_PASSWORD'])) {
|
||||||
|
return (string)$_SERVER['NOTIFY_PASSWORD'];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||||
$mount = $this->globalService->getStorage($input->getArgument('mount_id'));
|
$mount = $this->globalService->getStorage($input->getArgument('mount_id'));
|
||||||
if (is_null($mount)) {
|
if (is_null($mount)) {
|
||||||
|
@ -96,28 +129,28 @@ class Notify extends Base {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
$noAuth = false;
|
$noAuth = false;
|
||||||
|
|
||||||
|
$userOption = $this->getUserOption($input);
|
||||||
|
$passwordOption = $this->getPasswordOption($input);
|
||||||
|
|
||||||
|
// if only the user is provided, we get the user object to pass along to the auth backend
|
||||||
|
// this allows using saved user credentials
|
||||||
|
$user = ($userOption && !$passwordOption) ? $this->userManager->get($userOption) : null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$authBackend = $mount->getAuthMechanism();
|
$authBackend = $mount->getAuthMechanism();
|
||||||
$authBackend->manipulateStorageConfig($mount);
|
$authBackend->manipulateStorageConfig($mount, $user);
|
||||||
} catch (InsufficientDataForMeaningfulAnswerException $e) {
|
} catch (InsufficientDataForMeaningfulAnswerException $e) {
|
||||||
$noAuth = true;
|
$noAuth = true;
|
||||||
} catch (StorageNotAvailableException $e) {
|
} catch (StorageNotAvailableException $e) {
|
||||||
$noAuth = true;
|
$noAuth = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($input->getOption('user')) {
|
if ($userOption) {
|
||||||
$mount->setBackendOption('user', $input->getOption('user'));
|
$mount->setBackendOption('user', $userOption);
|
||||||
} elseif (isset($_ENV['NOTIFY_USER'])) {
|
|
||||||
$mount->setBackendOption('user', $_ENV['NOTIFY_USER']);
|
|
||||||
} elseif (isset($_SERVER['NOTIFY_USER'])) {
|
|
||||||
$mount->setBackendOption('user', $_SERVER['NOTIFY_USER']);
|
|
||||||
}
|
}
|
||||||
if ($input->getOption('password')) {
|
if ($passwordOption) {
|
||||||
$mount->setBackendOption('password', $input->getOption('password'));
|
$mount->setBackendOption('password', $passwordOption);
|
||||||
} elseif (isset($_ENV['NOTIFY_PASSWORD'])) {
|
|
||||||
$mount->setBackendOption('password', $_ENV['NOTIFY_PASSWORD']);
|
|
||||||
} elseif (isset($_SERVER['NOTIFY_PASSWORD'])) {
|
|
||||||
$mount->setBackendOption('password', $_SERVER['NOTIFY_PASSWORD']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue