Skip to content

Commit

Permalink
move share structure
Browse files Browse the repository at this point in the history
implement new functions
  • Loading branch information
doganoo committed Aug 3, 2024
1 parent cb30a90 commit 7a31094
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace KSA\PasswordManager\Api\Node\Share;
namespace KSA\PasswordManager\Api\Node\Share\Public;

use Doctrine\DBAL\Exception;
use Keestash\Exception\User\UserNotFoundException;
use KSA\PasswordManager\Entity\IResponseCodes;
use KSA\PasswordManager\Entity\Share\NullShare;
use KSA\PasswordManager\Exception\PasswordManagerException;
use KSA\PasswordManager\Repository\Node\NodeRepository;
use KSA\PasswordManager\Repository\PublicShareRepository;
use KSA\PasswordManager\Service\AccessService;
use KSA\PasswordManager\Service\Node\Share\ShareService;
use KSP\Api\IResponse;
use KSP\Api\IVerb;
use KSP\Core\DTO\Token\IToken;
use KSP\Core\Service\HTTP\IResponseService;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -47,10 +52,52 @@ public function __construct(
, private PublicShareRepository $shareRepository
, private LoggerInterface $logger
, private IResponseService $responseService
, private AccessService $accessService
) {
}

public function handle(ServerRequestInterface $request): ResponseInterface {
return match (strtolower($request->getMethod())) {
IVerb::POST => $this->handlePost($request),
IVerb::DELETE => $this->handleDelete($request),
default => new JsonResponse([], IResponse::BAD_REQUEST),
};
}

private function handleDelete(ServerRequestInterface $request): ResponseInterface {
try {
$parameters = (array) $request->getParsedBody();
$shareId = $parameters["shareId"] ?? null;
/** @var IToken $token */
$token = $request->getAttribute(IToken::class);

if ($shareId === null) {
return new JsonResponse([], IResponse::BAD_REQUEST);
}

$share = $this->shareRepository->getShareById($shareId);

if (
$share instanceof NullShare
|| $this->shareService->isExpired($share)
) {
return new JsonResponse([], IResponse::NOT_FOUND);
}

$node = $this->nodeRepository->getNode($share->getNodeId(), 0, 0);
if (false === $this->accessService->hasAccess($node, $token->getUser())) {
return new JsonResponse([], IResponse::FORBIDDEN);
}

$this->shareRepository->remove($share);
return new JsonResponse([], IResponse::OK);
} catch (Exception|PasswordManagerException|UserNotFoundException $e) {
$this->logger->error('error deleting public share', ['e' => $e]);
return new JsonResponse([], IResponse::NOT_IMPLEMENTED);
}
}

private function handlePost(ServerRequestInterface $request): ResponseInterface {
try {

$parameters = (array) $request->getParsedBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace KSA\PasswordManager\Api\Node\Share;
namespace KSA\PasswordManager\Api\Node\Share\Public;

use DateTimeImmutable;
use Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace KSA\PasswordManager\Api\Node\Share;
namespace KSA\PasswordManager\Api\Node\Share\Regular;

use Keestash\Api\Response\JsonResponse;
use KSA\PasswordManager\Repository\Node\NodeRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace KSA\PasswordManager\Api\Node\Share;
namespace KSA\PasswordManager\Api\Node\Share\Regular;

use Exception;
use Keestash\Api\Response\JsonResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* the Keestash software and contains warranty information and liability disclaimers.
*/

namespace KSA\PasswordManager\Api\Node\Share;
namespace KSA\PasswordManager\Api\Node\Share\Regular;

use doganoo\PHPAlgorithms\Datastructure\Lists\ArrayList\ArrayList;
use Keestash\Core\DTO\Http\JWT\Audience;
Expand Down
28 changes: 28 additions & 0 deletions apps/PasswordManager/Exception/Node/Share/ShareException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/**
* Keestash
*
* Copyright (C) <2021> <Dogan Ucar>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace KSA\PasswordManager\Exception\Node\Share;

use KSA\PasswordManager\Exception\PasswordManagerException;

class ShareException extends PasswordManagerException {

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

namespace KSA\PasswordManager\Factory\Api\Node\Share;

use KSA\PasswordManager\Api\Node\Share\PublicShare;
use KSA\PasswordManager\Api\Node\Share\Public\PublicShare;
use KSA\PasswordManager\Repository\Node\NodeRepository;
use KSA\PasswordManager\Repository\PublicShareRepository;
use KSA\PasswordManager\Service\AccessService;
use KSA\PasswordManager\Service\Node\Share\ShareService;
use KSP\Core\Service\HTTP\IResponseService;
use Psr\Container\ContainerInterface;
Expand All @@ -38,6 +39,7 @@ public function __invoke(ContainerInterface $container): PublicShare {
, $container->get(PublicShareRepository::class)
, $container->get(LoggerInterface::class)
, $container->get(IResponseService::class)
, $container->get(AccessService::class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace KSA\PasswordManager\Factory\Api\Node\Share;

use KSA\PasswordManager\Api\Node\Share\PublicShareSingle;
use KSA\PasswordManager\Api\Node\Share\Public\PublicShareSingle;
use KSA\PasswordManager\Repository\Node\NodeRepository;
use KSA\PasswordManager\Repository\PublicShareRepository;
use KSA\PasswordManager\Service\Node\Credential\CredentialService;
Expand Down
4 changes: 2 additions & 2 deletions apps/PasswordManager/Factory/Api/Node/Share/RemoveFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace KSA\PasswordManager\Factory\Api\Node\Share;

use KSA\PasswordManager\Api\Node\Share\Remove;
use KSA\PasswordManager\Api\Node\Share\Regular\Remove;
use KSA\PasswordManager\Repository\Node\NodeRepository;
use Psr\Container\ContainerInterface;

Expand All @@ -33,4 +33,4 @@ public function __invoke(ContainerInterface $container): Remove {
);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace KSA\PasswordManager\Factory\Api\Node\Share;

use KSA\PasswordManager\Api\Node\Share\Share;
use KSA\PasswordManager\Api\Node\Share\Regular\Share;
use KSA\PasswordManager\Repository\Node\NodeRepository;
use KSA\PasswordManager\Service\Node\NodeService;
use KSA\PasswordManager\Service\Node\Share\ShareService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace KSA\PasswordManager\Factory\Api\Node;

use KSA\PasswordManager\Api\Node\Share\ShareableUsers;
use KSA\PasswordManager\Api\Node\Share\Regular\ShareableUsers;
use KSA\PasswordManager\Repository\Node\NodeRepository;
use KSP\Core\Repository\User\IUserRepository;
use KSP\Core\Service\HTTP\IJWTService;
Expand All @@ -39,4 +39,4 @@ public function __invoke(ContainerInterface $container): ShareableUsers {
);
}

}
}
51 changes: 51 additions & 0 deletions apps/PasswordManager/Repository/PublicShareRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use KSA\PasswordManager\Entity\Node\Node;
use KSA\PasswordManager\Entity\Share\NullShare;
use KSA\PasswordManager\Entity\Share\PublicShare;
use KSA\PasswordManager\Exception\Node\Share\ShareException;
use KSA\PasswordManager\Exception\PasswordManagerException;
use KSP\Core\Backend\IBackend;
use KSP\Core\DTO\User\IUser;
Expand Down Expand Up @@ -115,6 +116,42 @@ public function getShare(string $hash): PublicShare {

}

public function getShareById(int $id): PublicShare {
$queryBuilder = $this->backend->getConnection()->createQueryBuilder();
$queryBuilder->select(
[
's.id'
, 's.hash'
, 's.expire_ts'
, 's.node_id'
]
)
->from('pwm_public_share', 's')
->where('s.`id` = ?')
->setParameter(0, $id);

$result = $queryBuilder->executeQuery();
$rows = $result->fetchAllNumeric();

if (0 === count($rows)) {
return new NullShare();
}

$row = $rows[0];
$shareId = $row[0];
$shareHash = $row[1];
$expireTs = $row[2];
$nodeId = $row[3];

return new PublicShare(
(int) $shareId,
(int) $nodeId,
(string) $shareHash,
$this->dateTimeService->fromFormat($expireTs)
);

}

public function getShareByNode(Node $node): PublicShare {
$queryBuilder = $this->backend->getConnection()->createQueryBuilder();
$queryBuilder->select(
Expand Down Expand Up @@ -209,6 +246,20 @@ public function removeByUser(IUser $user): bool {
}
}

public function remove(PublicShare $share): PublicShare {
try {
$queryBuilder = $this->backend->getConnection()->createQueryBuilder();
$queryBuilder->delete('pwm_public_share', 'pps')
->where('id = ?')
->setParameter(0, $share->getId())
->executeStatement();
return $share;
} catch (Exception $e) {
$this->logger->warning('can not remove users public share', ['share' => $share, 'exception' => $e]);
throw new ShareException();
}
}

public function removeOutdated(): bool {
$now = $this->dateTimeService->toYMDHIS(new DateTimeImmutable());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Integration\Api\Node\Share;
namespace Integration\Api\Node\Share\Public;

use KSA\PasswordManager\Api\Node\Share\PublicShareSingle;
use KSA\PasswordManager\Api\Node\Share\Public\PublicShareSingle;
use KSA\PasswordManager\Entity\Share\PublicShare;
use KSA\PasswordManager\Repository\PublicShareRepository;
use KSA\PasswordManager\Service\Node\Credential\CredentialService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Integration\Api\Node\Share;
namespace Integration\Api\Node\Share\Public;

use DateTime;
use KSA\PasswordManager\Api\Node\Share\PublicShare;
use KSA\PasswordManager\Api\Node\Share\Public\PublicShare;
use KSA\PasswordManager\ConfigProvider;
use KSA\PasswordManager\Entity\IResponseCodes;
use KSA\PasswordManager\Service\Node\Credential\CredentialService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Integration\Api\Node\Share;
namespace Integration\Api\Node\Share\Regular;

use KSA\PasswordManager\Api\Node\Share\Remove;
use KSA\PasswordManager\Api\Node\Share\Regular\Remove;
use KSA\PasswordManager\Repository\PublicShareRepository;
use KSA\PasswordManager\Service\Node\Share\ShareService;
use KSA\PasswordManager\Test\Integration\TestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Integration\Api\Node\Share;
namespace Integration\Api\Node\Share\Regular;

use KSA\PasswordManager\Api\Node\Share\Share;
use KSA\PasswordManager\Api\Node\Share\Regular\Share;
use KSA\PasswordManager\Test\Integration\TestCase;
use Ramsey\Uuid\Uuid;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace KSA\PasswordManager\Test\Integration\Api\Node;
namespace Integration\Api\Node\Share\Regular;

use KSA\PasswordManager\Api\Node\Share\ShareableUsers;
use KSA\PasswordManager\Api\Node\Share\Regular\ShareableUsers;
use KSA\PasswordManager\Entity\Node\Credential\Credential;
use KSA\PasswordManager\Repository\Node\NodeRepository;
use KSA\PasswordManager\Test\Integration\TestCase;
Expand All @@ -44,4 +44,4 @@ public function testShareableUsers(): void {
$this->assertTrue(true === $this->getResponseService()->isValidResponse($response));
}

}
}
16 changes: 11 additions & 5 deletions apps/PasswordManager/config/api_router.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
use KSA\PasswordManager\Api\Node\Pwned\ChartData;
use KSA\PasswordManager\Api\Node\Pwned\IsActive;
use KSA\PasswordManager\Api\Node\Search;
use KSA\PasswordManager\Api\Node\Share\PublicShare;
use KSA\PasswordManager\Api\Node\Share\PublicShareSingle;
use KSA\PasswordManager\Api\Node\Share\Remove as RemoveShare;
use KSA\PasswordManager\Api\Node\Share\Share;
use KSA\PasswordManager\Api\Node\Share\ShareableUsers;
use KSA\PasswordManager\Api\Node\Share\Public\PublicShare;
use KSA\PasswordManager\Api\Node\Share\Public\PublicShareSingle;
use KSA\PasswordManager\Api\Node\Share\Regular\Remove as RemoveShare;
use KSA\PasswordManager\Api\Node\Share\Regular\Share;
use KSA\PasswordManager\Api\Node\Share\Regular\ShareableUsers;
use KSA\PasswordManager\ConfigProvider;
use KSA\PasswordManager\Middleware\NodeAccessMiddleware;
use KSP\Api\IRoute;
Expand Down Expand Up @@ -95,6 +95,12 @@
, IRoute::METHOD => IVerb::POST
, IRoute::NAME => PublicShare::class
],
[
IRoute::PATH => ConfigProvider::PASSWORD_MANAGER_PUBLIC_SHARE_PUBLIC
, IRoute::MIDDLEWARE => [NodeAccessMiddleware::class, PublicShare::class]
, IRoute::METHOD => IVerb::DELETE
, IRoute::NAME => PublicShare::class . '@' . IVerb::DELETE
],
[
IRoute::PATH => ConfigProvider::PASSWORD_MANAGER_PUBLIC_SHARE_REMOVE
, IRoute::MIDDLEWARE => [NodeAccessMiddleware::class, RemoveShare::class]
Expand Down
Loading

0 comments on commit 7a31094

Please # to comment.