Skip to content

Commit

Permalink
feat: cannot sign declaration unless op-adm has logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
jerotire committed Oct 2, 2024
1 parent 8918d18 commit ace7e98
Show file tree
Hide file tree
Showing 8 changed files with 819 additions and 2,300 deletions.
1,742 changes: 386 additions & 1,356 deletions app/api/composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/api/module/Api/config/query-map.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
TransferQuery\User\RoleList::class => QueryHandler\User\RoleList::class,
TransferQuery\User\UserListInternal::class => QueryHandler\User\UserListInternal::class,
Query\User\UserListInternalByTrafficArea::class => QueryHandler\User\UserListInternalByTrafficArea::class,
TransferQuery\User\OperatorAdminForOrganisationHasLoggedIn::class => QueryHandler\User\OperatorAdminForOrganisationHasLoggedIn::class,

// User
TransferQuery\Team\Team::class => QueryHandler\Team\Team::class,
Expand Down
1 change: 1 addition & 0 deletions app/api/module/Api/config/validation-map/user.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
QueryHandler\User\UserListInternalByTrafficArea::class => IsInternalUser::class,
QueryHandler\User\UserListSelfserve::class => CanManageUser::class,
QueryHandler\User\UserSelfserve::class => CanReadUser::class,
QueryHandler\User\OperatorAdminForOrganisationHasLoggedIn::class => NoValidationRequired::class,

// Commands
CommandHandler\MyAccount\UpdateMyAccountInternal::class => IsInternalUser::class,
Expand Down
22 changes: 6 additions & 16 deletions app/api/module/Api/src/Domain/Query/User/UserListSelfserve.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Dvsa\Olcs\Api\Domain\Query\User;

use Dvsa\Olcs\Transfer\FieldType\Traits\LastLoggedInFromOptional;
use Dvsa\Olcs\Transfer\FieldType\Traits\OrganisationOptional;
use Dvsa\Olcs\Transfer\FieldType\Traits\RolesOptional;
use Dvsa\Olcs\Transfer\Query\AbstractQuery;
use Dvsa\Olcs\Transfer\Query\OrderedQueryInterface;
use Dvsa\Olcs\Transfer\Query\OrderedTrait;
Expand All @@ -19,6 +22,9 @@ final class UserListSelfserve extends AbstractQuery implements PagedQueryInterfa
{
use PagedTrait;
use OrderedTrait;
use RolesOptional;
use OrganisationOptional;
use LastLoggedInFromOptional;

/**
* @Transfer\Filter("Laminas\Filter\Digits")
Expand All @@ -36,14 +42,6 @@ final class UserListSelfserve extends AbstractQuery implements PagedQueryInterfa
*/
protected $partnerContactDetails = null;

/**
* @Transfer\Filter("Laminas\Filter\Digits")
* @Transfer\Validator("Laminas\Validator\Digits")
* @Transfer\Validator("Laminas\Validator\GreaterThan", options={"min": 0})
* @Transfer\Optional
*/
protected $organisation = null;

/**
* @return int
*/
Expand All @@ -59,12 +57,4 @@ public function getPartnerContactDetails()
{
return $this->partnerContactDetails;
}

/**
* @return int
*/
public function getOrganisation()
{
return $this->organisation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types=1);

namespace Dvsa\Olcs\Api\Domain\QueryHandler\User;

use DateTimeImmutable;
use Doctrine\ORM\AbstractQuery;
use Dvsa\Olcs\Api\Domain\Exception\BadRequestException;
use Dvsa\Olcs\Api\Domain\Exception\RuntimeException;
use Dvsa\Olcs\Api\Domain\Query\User\UserListSelfserve as ListDto;
use Dvsa\Olcs\Api\Domain\QueryHandler\AbstractQueryHandler;
use Dvsa\Olcs\Api\Domain\QueryHandler\Result;
use Dvsa\Olcs\Api\Domain\Repository;
use Dvsa\Olcs\Api\Entity;
use Dvsa\Olcs\Transfer\Query\QueryInterface;
use Dvsa\Olcs\Transfer\Query\User\OperatorAdminForOrganisationHasLoggedIn as Qry;

/**
* Returns true if an operator admin for the organisation has logged in
*/
class OperatorAdminForOrganisationHasLoggedIn extends AbstractQueryHandler
{
protected $repoServiceName = Repository\User::class;

/**
* Handle query
*
* @param QueryInterface $query query
*
* @throws BadRequestException|RuntimeException
*/
public function handleQuery(QueryInterface $query): array
{
if (!$query instanceof Qry) {
throw new BadRequestException('Expected instance of: ' . Qry::class);
}

if (empty($query->getOrganisation())) {
throw new BadRequestException('Organisation ID is required');
}

$repo = $this->getRepo(Repository\User::class);

$params = [
'organisation' => $query->getOrganisation(),
'roles' => [Entity\User\Role::ROLE_OPERATOR_ADMIN],
'page' => 1,
'limit' => 100,
'sort' => 'id',
'order' => 'DESC',
];

$lastLoginDate = DateTimeImmutable::createFromFormat("Y-m-d", '1970-01-01');
if (!empty($query->getLastLoggedInFrom())) {
$lastLoginDate = DateTimeImmutable::createFromFormat("Y-m-d", $query->getLastLoggedInFrom());
}

$params['lastLoggedInFrom'] = $lastLoginDate;

$userListDto = ListDto::create($params);

$result = [
'organisation' => (int) $query->getOrganisation(),
'lastLoggedInFrom' => $lastLoginDate->format('Y-m-d'),
'operatorAdminHasLoggedIn' => false,
];

if ($repo->fetchCount($userListDto) > 0) {
$result['operatorAdminHasLoggedIn'] = true;
}

return $result;
}
}
6 changes: 6 additions & 0 deletions app/api/module/Api/src/Domain/Repository/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ protected function applyListFilters(QueryBuilder $qb, QueryInterface $query)
$qb->setParameter('roles', $query->getRoles());
}

// filter by lastLoggedInFrom if it has been specified
if (method_exists($query, 'getLastLoggedInFrom') && !empty($query->getLastLoggedInFrom())) {
$qb->andWhere($qb->expr()->gte($this->alias . '.lastLoginAt', ':lastLoggedInFrom'))
->setParameter('lastLoggedInFrom', $query->getLastLoggedInFrom());
}

// exclude system user from all lists
$qb->andWhere($qb->expr()->neq($this->alias . '.id', ':systemUser'))
->setParameter('systemUser', IdentityProviderInterface::SYSTEM_USER);
Expand Down
Loading

0 comments on commit ace7e98

Please sign in to comment.