Skip to content

Commit

Permalink
feat: welcome page for external users with terms and conditions VOL-5664
Browse files Browse the repository at this point in the history
  • Loading branch information
ilindsay committed Nov 13, 2024
1 parent 14665d4 commit 019720e
Show file tree
Hide file tree
Showing 21 changed files with 633 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/api/module/Api/config/command-map.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@
TransferCommand\User\DeletePartner::class => CommandHandler\User\DeletePartner::class,
TransferCommand\User\UpdateUserLastLoginAt::class => CommandHandler\User\UpdateUserLastLoginAt::class,
TransferCommand\User\RegisterConsultantAndOperator::class => CommandHandler\User\RegisterConsultantAndOperator::class,
TransferCommand\User\AgreeTerms::class => CommandHandler\User\AgreeTerms::class,

// Transfer - Team
TransferCommand\Team\CreateTeam::class => CommandHandler\Team\CreateTeam::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 @@ -41,4 +41,5 @@
CommandHandler\User\UpdateUserSelfserve::class => CanManageUser::class,
CommandHandler\User\UpdateUserSelfserveFactory::class => CanManageUser::class,
CommandHandler\User\UpdateUserLastLoginAt::class => NotIsAnonymousUser::class,
CommandHandler\User\AgreeTerms::class => IsExternalUser::class,
];
46 changes: 46 additions & 0 deletions app/api/module/Api/src/Domain/CommandHandler/User/AgreeTerms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

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

use Dvsa\Olcs\Api\Domain\AuthAwareInterface;
use Dvsa\Olcs\Api\Domain\AuthAwareTrait;
use Dvsa\Olcs\Api\Domain\CacheAwareInterface;
use Dvsa\Olcs\Api\Domain\CacheAwareTrait;
use Dvsa\Olcs\Api\Domain\CommandHandler\AbstractCommandHandler;
use Dvsa\Olcs\Api\Domain\Repository\User as UserRepository;
use Dvsa\Olcs\Api\Entity\User\User;
use Dvsa\Olcs\Transfer\Command\CommandInterface;

final class AgreeTerms extends AbstractCommandHandler implements AuthAwareInterface, CacheAwareInterface
{
use AuthAwareTrait;
use CacheAwareTrait;

public const SUCCESS_MSG = 'Terms and conditions accepted';

protected $repoServiceName = 'User';

public function handleCommand(CommandInterface $command)
{
$userId = $this->getCurrentUser()->getId();

/**
* @var UserRepository $repo
* @var User $user
*/
$repo = $this->getRepo();

$user = $repo->fetchById($userId);
$user->agreeTermsAndConditions();
$repo->save($user);

$this->clearUserCaches([$userId]);

$this->result->addId('User', $userId);
$this->result->addMessage(self::SUCCESS_MSG);

return $this->result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Dvsa\Olcs\Api\Domain\CommandHandler\AbstractUserCommandHandler;
use Dvsa\Olcs\Api\Domain\CommandHandler\TransactionedInterface;
use Dvsa\Olcs\Api\Entity\User\Role as RoleEntity;
use Dvsa\Olcs\Api\Entity\User\User as UserEntity;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Transfer\Command\User\RegisterUserSelfserve as RegisterUserSelfServeCommand;

Expand All @@ -32,10 +33,19 @@ public function handleCommand(CommandInterface $command)

$this->result->merge($this->handleSideEffect(RegisterUserSelfServeCommand::create($consultantDetails)));

// Get the new consultant user entity and set the correct role.
/**
* Get the new consultant user entity and set the correct role
*
* @var UserEntity $consultantUser
* @var RoleEntity $operatorTcRole
*/
$consultantUser = $this->getRepo()->fetchById($this->result->getId('user'));
$operatorTcRole = $this->getRepo('Role')->fetchByRole(RoleEntity::ROLE_OPERATOR_TC);
$consultantUser->setRoles(new ArrayCollection([$operatorTcRole]));

//the consultant has already accepted terms and conditions
$consultantUser->agreeTermsAndConditions();

$this->getRepo()->save($consultantUser);
return $this->result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function handleQuery(QueryInterface $query)
$result = $this->result(
$user,
[
'termsAgreed',
'team',
'transportManager',
'partnerContactDetails',
Expand Down
33 changes: 33 additions & 0 deletions app/api/module/Api/src/Entity/User/AbstractUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ abstract class AbstractUser implements BundleSerializableInterface, JsonSerializ
*/
protected $translateToWelsh = 0;

/**
* Terms agreed
*
* @var boolean
*
* @ORM\Column(type="boolean", name="terms_agreed", nullable=false, options={"default": 0})
*/
protected $termsAgreed = 0;

/**
* Transport manager
*
Expand Down Expand Up @@ -303,6 +312,30 @@ public function getAccountDisabled()
return $this->accountDisabled;
}

/**
* Set the terms agreed
*
* @param boolean $termsAgreed new value being set
*
* @return User
*/
public function setTermsAgreed($termsAgreed)
{
$this->termsAgreed = $termsAgreed;

return $this;
}

/**
* Get the terms agreed
*
* @return boolean
*/
public function getTermsAgreed()
{
return $this->termsAgreed;
}

/**
* Set the contact details
*
Expand Down
12 changes: 12 additions & 0 deletions app/api/module/Api/src/Entity/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,16 @@ public function canResetPassword(): bool
//disabled users can't reset
return !$this->isDisabled();
}

public function agreeTermsAndConditions(): User
{
$this->termsAgreed = true;

return $this;
}

public function hasAgreedTermsAndConditions(): bool
{
return $this->termsAgreed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Dvsa\OlcsTest\Api\Domain\CommandHandler\User;

use Dvsa\Olcs\Api\Domain\Repository\User as UserRepository;
use Dvsa\Olcs\Api\Entity\User\User as UserEntity;
use Dvsa\Olcs\Transfer\Command\User\AgreeTerms as Cmd;
use Dvsa\Olcs\Transfer\Service\CacheEncryption;
use Dvsa\OlcsTest\Api\Domain\CommandHandler\AbstractCommandHandlerTestCase;
use Dvsa\Olcs\Api\Domain\CommandHandler\User\AgreeTerms;
use LmcRbacMvc\Service\AuthorizationService;
use Mockery as m;

class AgreeTermsTest extends AbstractCommandHandlerTestCase
{
public function setUp(): void
{
$this->sut = new AgreeTerms();
$this->mockRepo('User', UserRepository::class);

$this->mockedSmServices = [
AuthorizationService::class => m::mock(AuthorizationService::class),
CacheEncryption::class => m::mock(CacheEncryption::class),
];

parent::setUp();
}

public function testHandleCommand()
{
$userId = 999;

$command = Cmd::create([]);

$loggedInUser = m::mock(UserEntity::class);
$loggedInUser->expects('getId')->andReturn($userId);

$this->mockedSmServices[AuthorizationService::class]->expects('getIdentity->getUser')
->andReturn($loggedInUser);

$this->mockedSmServices[CacheEncryption::class]->expects('removeCustomItems')
->with(CacheEncryption::USER_ACCOUNT_IDENTIFIER, [$userId]);

$userFromRepo = m::mock(UserEntity::class);
$userFromRepo->expects('agreeTermsAndConditions')->withNoArgs();

$this->repoMap['User']->expects('fetchById')->with($userId)->andReturn($userFromRepo);
$this->repoMap['User']->expects('save')->with($userFromRepo);

$expectedResult = [
'id' => [
'User' => $userId,
],
'messages' => [
0 => AgreeTerms::SUCCESS_MSG
],
];

$this->assertEquals($expectedResult, $this->sut->handleCommand($command)->toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function testHandleCommand()
->once()
->andReturnSelf();

$consultant->expects('agreeTermsAndConditions')->withNoArgs();

$this->repoMap['User']->shouldReceive('save')->with($consultant);

$mockRole = m::mock(Role::class);
Expand Down
8 changes: 8 additions & 0 deletions app/api/test/module/Api/src/Entity/User/UserEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,4 +1409,12 @@ public function dpOperatorAdminRoles(): array
[RoleEntity::ROLE_OPERATOR_TC],
];
}

public function testAgreeTermsAndConditions(): void
{
$user = new Entity('pid', Entity::USER_TYPE_OPERATOR);
$this->assertFalse($user->hasAgreedTermsAndConditions());
$user->agreeTermsAndConditions();
$this->assertTrue($user->hasAgreedTermsAndConditions());
}
}
4 changes: 4 additions & 0 deletions app/selfserve/module/Olcs/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laminas\Session\Container;
use Laminas\Session\SessionManager;
use Laminas\Validator\AbstractValidator;
use Olcs\Mvc\TermsAgreedListener;

/**
* Module.php
Expand Down Expand Up @@ -64,6 +65,9 @@ public function onBootstrap(MvcEvent $e)
$cookieListener = $sm->get('CookieListener');
$cookieListener->attach($eventManager, 2);

$termsAgreedListener = $sm->get(TermsAgreedListener::class);
$termsAgreedListener->attach($eventManager, 3);

$this->initSession(
[
'remember_me_seconds' => 86400,
Expand Down
15 changes: 15 additions & 0 deletions app/selfserve/module/Olcs/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Olcs\Controller\SessionTimeoutController;
use Olcs\Controller\UserForgotUsernameController;
use Olcs\Controller\UserRegistrationController;
use Olcs\Controller\WelcomeController;
use Olcs\Form\Element\SearchDateRangeFieldset;
use Olcs\Form\Element\SearchDateRangeFieldsetFactory;
use Olcs\Form\Element\SearchFilterFieldset;
Expand All @@ -54,6 +55,8 @@
use Olcs\FormService\Form\Lva as LvaFormService;
use Olcs\Logging\Log\Processor\CorrelationId;
use Olcs\Logging\Log\Processor\CorrelationIdFactory;
use Olcs\Mvc\TermsAgreedListener;
use Olcs\Mvc\TermsAgreedListenerFactory;
use Olcs\Service\Cookie as CookieService;
use Olcs\Service\Data as DataService;
use Olcs\Service\Processing as ProcessingService;
Expand Down Expand Up @@ -108,6 +111,16 @@
]
]
],
'welcome' => [
'type' => Segment::class,
'options' => [
'route' => '/welcome[/]',
'defaults' => [
'controller' => WelcomeController::class,
'action' => 'generic',
],
],
],
'cookies' => [
'type' => 'segment',
'options' => [
Expand Down Expand Up @@ -1412,6 +1425,7 @@
\Olcs\Controller\Licence\Vehicle\Reprint\ReprintLicenceVehicleDiscConfirmationController::class => \Olcs\Controller\Licence\Vehicle\Reprint\ReprintLicenceVehicleDiscConfirmationControllerFactory::class,
Olcs\Controller\ConversationsController::class => Olcs\Controller\Factory\ConversationsControllerFactory::class,
PromptController::class => \Olcs\Controller\PromptControllerFactory::class,
WelcomeController::class => \Olcs\Controller\WelcomeControllerFactory::class,
// Process Signature from GOV.UK Account
\Olcs\Controller\SignatureVerificationController::class => \Olcs\Controller\SignatureVerificationControllerFactory::class,
// LVA Controller Factories
Expand Down Expand Up @@ -1498,6 +1512,7 @@
\Laminas\Cache\Service\StorageCacheAbstractServiceFactory::class,
],
'factories' => [
TermsAgreedListener::class => TermsAgreedListenerFactory::class,
'CookieListener' => \Olcs\Mvc\CookieListenerFactory::class,
'CookieBannerListener' => \Olcs\Mvc\CookieBannerListenerFactory::class,
'CookieAcceptAllSetCookieGenerator' => CookieService\AcceptAllSetCookieGeneratorFactory::class,
Expand Down
59 changes: 59 additions & 0 deletions app/selfserve/module/Olcs/src/Controller/WelcomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Olcs\Controller;

use Common\Service\Helper\FormHelperService;
use Common\Service\Helper\TranslationHelperService;
use Common\Service\Table\TableFactory;
use Dvsa\Olcs\Transfer\Command\User\AgreeTerms as AgreeTermsCmd;
use Laminas\Http\Response;
use Olcs\Form\Model\Form\AgreeTerms as AgreeTermsForm;
use Permits\Data\Mapper\MapperManager;

class WelcomeController extends AbstractSelfserveController
{
protected $templateConfig = [
'generic' => 'pages/welcome',
];

protected $formConfig = [
'generic' => [
'confirmationForm' => [
'formClass' => AgreeTermsForm::class
]
]
];

protected $postConfig = [
'generic' => [
'command' => AgreeTermsCmd::class,
'step' => 'index',
'saveAndReturnStep' => 'index',
],
];

public function __construct(
TranslationHelperService $translationHelper,
FormHelperService $formHelper,
TableFactory $tableBuilder,
MapperManager $mapperManager
) {
parent::__construct($translationHelper, $formHelper, $tableBuilder, $mapperManager);
}

/**
* @return Response|void
*/
public function checkConditionalDisplay()
{
if (isset($this->postParams['form-actions']['signOut'])) {
return $this->conditionalDisplayNotMet('auth/logout');
}

if ($this->currentUser()->getIdentity()->hasAgreedTerms()) {
return $this->conditionalDisplayNotMet('index');
}
}
}
Loading

0 comments on commit 019720e

Please sign in to comment.