2020-01-08 19:24:33 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-08 19:24:33 +00:00
|
|
|
namespace App\Console\Command\Internal;
|
|
|
|
|
2020-02-06 02:35:13 +00:00
|
|
|
use App\Console\Command\CommandAbstract;
|
2020-06-26 20:22:53 +00:00
|
|
|
use App\Entity\SftpUser;
|
2020-01-22 05:21:33 +00:00
|
|
|
use Brick\Math\BigInteger;
|
2020-06-26 20:22:53 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-01-08 19:24:33 +00:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2020-07-08 07:03:50 +00:00
|
|
|
use const JSON_NUMERIC_CHECK;
|
|
|
|
use const JSON_THROW_ON_ERROR;
|
|
|
|
use const JSON_UNESCAPED_SLASHES;
|
2020-01-08 19:24:33 +00:00
|
|
|
|
|
|
|
class SftpAuthCommand extends CommandAbstract
|
|
|
|
{
|
|
|
|
public function __invoke(
|
|
|
|
SymfonyStyle $io,
|
2020-06-26 20:22:53 +00:00
|
|
|
EntityManagerInterface $em
|
2020-10-14 22:19:31 +00:00
|
|
|
): int {
|
2021-07-19 05:53:45 +00:00
|
|
|
$username = getenv('SFTPGO_AUTHD_USERNAME') ?: null;
|
|
|
|
$password = getenv('SFTPGO_AUTHD_PASSWORD') ?: null;
|
|
|
|
$pubKey = getenv('SFTPGO_AUTHD_PUBLIC_KEY') ?: null;
|
2020-01-08 19:24:33 +00:00
|
|
|
|
2021-06-08 06:40:49 +00:00
|
|
|
$sftpUser = $em->getRepository(SftpUser::class)->findOneBy(['username' => $username]);
|
2020-01-08 19:24:33 +00:00
|
|
|
|
|
|
|
if ($sftpUser instanceof SftpUser && $sftpUser->authenticate($password, $pubKey)) {
|
2021-06-08 06:40:49 +00:00
|
|
|
$storageLocation = $sftpUser->getStation()->getMediaStorageLocation();
|
2020-01-22 05:21:33 +00:00
|
|
|
|
2020-11-10 03:06:48 +00:00
|
|
|
$quotaRaw = $storageLocation->getStorageQuotaBytes();
|
2020-01-22 05:21:33 +00:00
|
|
|
$quota = ($quotaRaw instanceof BigInteger)
|
|
|
|
? (string)$quotaRaw
|
|
|
|
: 0;
|
|
|
|
|
2020-01-08 19:24:33 +00:00
|
|
|
$row = [
|
|
|
|
'status' => 1,
|
|
|
|
'username' => $sftpUser->getUsername(),
|
|
|
|
'expiration_date' => 0,
|
2020-11-10 03:06:48 +00:00
|
|
|
'home_dir' => $storageLocation->getPath(),
|
2020-01-08 19:24:33 +00:00
|
|
|
'uid' => 0,
|
|
|
|
'gid' => 0,
|
2020-01-22 05:21:33 +00:00
|
|
|
'quota_size' => $quota,
|
2020-01-08 19:24:33 +00:00
|
|
|
'permissions' => [
|
|
|
|
'/' => ['*'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-07-08 07:03:50 +00:00
|
|
|
$io->write(json_encode($row, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK));
|
2020-01-08 19:24:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-08 07:03:50 +00:00
|
|
|
$io->write(json_encode(['username' => ''], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES));
|
2020-01-08 19:24:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|