Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create operator administrator account yes path #259

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -374,6 +374,7 @@
TransferCommand\User\UpdatePartner::class => CommandHandler\User\UpdatePartner::class,
TransferCommand\User\DeletePartner::class => CommandHandler\User\DeletePartner::class,
TransferCommand\User\UpdateUserLastLoginAt::class => CommandHandler\User\UpdateUserLastLoginAt::class,
TransferCommand\User\RegisterConsultantAndOperator::class => CommandHandler\User\RegisterConsultantAndOperator::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 @@ -31,6 +31,7 @@
CommandHandler\User\RegisterUserSelfserve::class => NoValidationRequired::class,
CommandHandler\User\RegisterUserSelfserveFactory::class => NoValidationRequired::class,
CommandHandler\User\RemindUsernameSelfserve::class => NoValidationRequired::class,
CommandHandler\User\RegisterConsultantAndOperator::class => NoValidationRequired::class,
CommandHandler\User\UpdatePartner::class => IsInternalUser::class,
CommandHandler\User\CreateUserSelfserve::class => CanManageUser::class,
CommandHandler\User\CreateUserSelfServeFactory::class => CanManageUser::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
namespace Dvsa\Olcs\Api\Domain\CommandHandler\Email;

use Dvsa\Olcs\Api\Domain\Command\Result;
use Dvsa\Olcs\Api\Domain\ToggleAwareInterface;
use Dvsa\Olcs\Api\Domain\ToggleAwareTrait;
use Dvsa\Olcs\Api\Entity\System\FeatureToggle;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Api\Domain\CommandHandler\AbstractCommandHandler;

/**
* Send User Registered Email
*/
final class SendUserRegistered extends AbstractCommandHandler implements \Dvsa\Olcs\Api\Domain\EmailAwareInterface
final class SendUserRegistered extends AbstractCommandHandler implements \Dvsa\Olcs\Api\Domain\EmailAwareInterface, ToggleAwareInterface
{
use \Dvsa\Olcs\Api\Domain\EmailAwareTrait;
use ToggleAwareTrait;

protected $repoServiceName = 'User';

Expand All @@ -35,9 +39,13 @@ public function handleCommand(CommandInterface $command)

$message->setTranslateToWelsh($user->getTranslateToWelsh());

$template = $this->toggleService->isEnabled(
FeatureToggle::TRANSPORT_CONSULTANT_ROLE)
? 'user-registered-tc' : 'user-registered';

$this->sendEmailTemplate(
$message,
'user-registered',
$template,
[
'orgName' => $user->getRelatedOrganisationName(),
'loginId' => $user->getLoginId(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Register Operator and Consultant
*/

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

use Doctrine\Common\Collections\ArrayCollection;
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\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Transfer\Command\User\RegisterUserSelfserve as RegisterUserSelfServeCommand;

final class RegisterConsultantAndOperator extends AbstractUserCommandHandler implements TransactionedInterface
{
protected $repoServiceName = 'User';
protected $extraRepos = ['Role'];

public function handleCommand(CommandInterface $command)
{
// Register the operator first, a new Org will be created.
$this->result->merge($this->handleSideEffect(RegisterUserSelfServeCommand::create($command->getOperatorDetails())));

// Get the newly created user entity
$user = $this->getRepo()->fetchById($this->result->getId('user'));

// Add the org ID of the newly created user/org to the consultant details, then register the consultant
$consultantDetails = $command->getConsultantDetails();
$consultantDetails['organisation'] = $user->getOrganisationUsers()->first()->getOrganisation()->getId();

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

// Get the new consultant user entity and set the correct role.
$consultantUser = $this->getRepo()->fetchById($this->result->getId('user'));
$operatorTcRole = $this->getRepo('Role')->fetchByRole(RoleEntity::ROLE_OPERATOR_TC);
$consultantUser->setRoles(new ArrayCollection([$operatorTcRole]));
$this->getRepo()->save($consultantUser);
return $this->result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function handleCommand(CommandInterface $command)
} elseif (!empty($data['organisationName'])) {
// create organisation and link with it
$data['organisations'] = [$this->createOrganisation($data)];
} elseif (!empty($data['organisation'])) {
// link with the organisation
$data['organisations'] = [$this->getRepo('Organisation')->fetchById($data['organisation'])];
}

if (empty($data['organisations'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<p>Hello,</p>

<p>You've been added to the Vehicle Operator Licensing service for <?php echo $this->escapeHtml($this->orgName); ?>.</p>

<p>Your username is: <?php echo $this->escapeHtml($this->loginId); ?></p>

<p>You'll receive a second email with a temporary password in the next two hours. If it doesn't arrive email notification@vehicle-operator-licensing.service.gov.uk</p>

<p>
<a href="<?php echo $this->escapeHtml($this->url); ?>">Sign in</a>
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hello,

You've been added to the Vehicle Operator Licensing service for <?php echo $this->orgName; ?>.

Your username is: <?php echo $this->loginId; ?></p>

You'll receive a second email with a temporary password in the next two hours. If it doesn't arrive email notification@vehicle-operator-licensing.service.gov.uk

Sign in at <?php echo $this->url; ?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Dvsa\Olcs\Api\Entity\Organisation\Organisation as OrganisationEntity;
use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser as OrganisationUserEntity;
use Dvsa\Olcs\Api\Entity\User\User;
use Dvsa\Olcs\Api\Service\Toggle\ToggleService;
use Dvsa\Olcs\Email\Data\Message;
use Dvsa\Olcs\Email\Domain\Command\SendEmail;
use Dvsa\Olcs\Email\Service\TemplateRenderer;
Expand All @@ -32,6 +33,7 @@ public function setUp(): void

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

parent::setUp();
Expand Down Expand Up @@ -87,6 +89,10 @@ public function testHandleCommand()
'default'
);

$this->mockedSmServices[ToggleService::class]->shouldReceive('isEnabled')
->with(\Dvsa\Olcs\Api\Entity\System\FeatureToggle::TRANSPORT_CONSULTANT_ROLE)
->andReturn(false);

$this->expectedSideEffect(
SendEmail::class,
[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

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

use Dvsa\Olcs\Api\Domain\CommandHandler\User\RegisterConsultantAndOperator;
use Dvsa\Olcs\Api\Domain\Repository\User as UserRepo;
use Dvsa\Olcs\Api\Entity\User\Role;
use Dvsa\Olcs\Api\Entity\User\User as UserEntity;
use Dvsa\Olcs\Transfer\Command\User\RegisterConsultantAndOperator as RegisterConsultantAndOperatorCommand;
use Dvsa\Olcs\Transfer\Command\User\RegisterUserSelfserve as RegisterUserSelfServeCommand;
use Dvsa\OlcsTest\Api\Domain\CommandHandler\AbstractCommandHandlerTestCase;
use Dvsa\Olcs\Api\Domain\Command\Result;
use Mockery as m;

class RegisterConsultantAndOperatorTest extends AbstractCommandHandlerTestCase
{
public function setUp(): void
{
$this->sut = new RegisterConsultantAndOperator();
$this->mockRepo('User', UserRepo::class);
$this->mockRepo('Role', \Dvsa\Olcs\Api\Domain\Repository\Role::class);

$mockAuthService = m::mock(\LmcRbacMvc\Service\AuthorizationService::class);
$this->mockedSmServices['LmcRbacMvc\Service\AuthorizationService'] = $mockAuthService;

parent::setUp();
}

public function testHandleCommand()
{
$operatorDetails = ['organisationName' => 'Operator Org',];

$command = RegisterConsultantAndOperatorCommand::create(
[
'operatorDetails' => $operatorDetails,
'consultantDetails' => []
]);

$operatorResult = new Result();
$operatorResult->addId('user', 100)->addMessage('User created successfully');

$this->expectedSideEffect(
RegisterUserSelfServeCommand::class,
$operatorDetails,
$operatorResult
);

$organisationId = 200;
$organisation = m::mock();
$organisation->shouldReceive('getId')->andReturn($organisationId);

$organisationUser = m::mock();
$organisationUser->shouldReceive('getOrganisation')->andReturn($organisation);

$user = m::mock(UserEntity::class)->makePartial();
$user->shouldReceive('getOrganisationUsers->first')->andReturn($organisationUser);

$this->repoMap['User']->shouldReceive('fetchById')->with(100)->andReturn($user);

$consultantDetails['organisation'] = $organisationId;

$consultantResult = new Result();
$consultantResult->addId('user', 101)->addMessage('User created successfully');

$consultant = m::mock(UserEntity::class)->makePartial();
$this->repoMap['User']->shouldReceive('fetchById')->with(101)->andReturn($consultant);

$consultant->shouldReceive('setRoles')
->with(m::type(\Doctrine\Common\Collections\ArrayCollection::class))
->once()
->andReturnSelf();

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

$mockRole = m::mock(Role::class);
$this->repoMap['Role']->shouldReceive('fetchByRole')->with(Role::ROLE_OPERATOR_TC)->andReturn($mockRole);

$this->expectedSideEffect(
RegisterUserSelfServeCommand::class,
$consultantDetails,
$consultantResult
);

$result = $this->sut->handleCommand($command);
$this->assertEquals(['User created successfully', 'User created successfully'], $result->getMessages());
}
}
61 changes: 57 additions & 4 deletions app/selfserve/module/Olcs/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Olcs\Auth\Adapter\SelfserveCommandAdapterFactory;
use Olcs\Auth\Service\AuthenticationServiceFactory;
use Olcs\Auth\Service\AuthenticationServiceInterface;
use Olcs\Controller\ConsultantRegistrationController;
use Olcs\Controller\Cookie\DetailsController as CookieDetailsController;
use Olcs\Controller\Cookie\DetailsControllerFactory;
use Olcs\Controller\Cookie\SettingsController as CookieSettingsController;
Expand Down Expand Up @@ -502,7 +503,50 @@
'route' => '/register[/]',
'defaults' => [
'controller' => UserRegistrationController::class,
'action' => 'add'
'action' => 'start'
]
],
'may_terminate' => true,
'child_routes' => [
'operator' => [
'type' => 'segment',
'options' => [
'route' => 'operator[/]',
'defaults' => [
'controller' => UserRegistrationController::class,
'action' => 'add'
]
]
],
'operator-representation' => [
'type' => 'segment',
'options' => [
'route' => 'operator-representation[/]',
'defaults' => [
'controller' => ConsultantRegistrationController::class,
'action' => 'operatorRepresentation'
]
]
],
'register-for-operator' => [
'type' => 'segment',
'options' => [
'route' => 'register-for-operator[/]',
'defaults' => [
'controller' => ConsultantRegistrationController::class,
'action' => 'registerForOperator'
]
]
],
'register-consultant-account' => [
'type' => 'segment',
'options' => [
'route' => 'register-consultant-account[/]',
'defaults' => [
'controller' => \Olcs\Controller\ConsultantRegistrationController::class,
'action' => 'registerConsultantAccount'
]
]
]
]
],
Expand Down Expand Up @@ -1326,6 +1370,7 @@
Olcs\Controller\UserController::class => \Olcs\Controller\Factory\UserControllerFactory::class,
UserForgotUsernameController::class => \Olcs\Controller\Factory\UserForgotUsernameControllerFactory::class,
UserRegistrationController::class => \Olcs\Controller\Factory\UserRegistrationControllerFactory::class,
ConsultantRegistrationController::class => \Olcs\Controller\Factory\ConsultantRegistrationControllerFactory::class,

Olcs\Controller\Entity\ViewController::class => \Olcs\Controller\Factory\Entity\ViewControllerFactory::class,

Expand Down Expand Up @@ -1434,7 +1479,10 @@
'CookieSettingsCookieNamesProvider' => CookieService\SettingsCookieNamesProvider::class,
'QaIrhpApplicationViewGenerator' => QaService\ViewGenerator\IrhpApplicationViewGenerator::class,
'QaIrhpPermitApplicationViewGenerator' => QaService\ViewGenerator\IrhpPermitApplicationViewGenerator::class,
LicenceVehicleManagement::class => LicenceVehicleManagement::class
LicenceVehicleManagement::class => LicenceVehicleManagement::class,
\Olcs\Session\ConsultantRegistration::class => \Olcs\Session\ConsultantRegistration::class,
\Olcs\Controller\Mapper\CreateAccountMapper::class => \Olcs\Controller\Mapper\CreateAccountMapper::class,

],
'abstract_factories' => [
\Laminas\Cache\Service\StorageCacheAbstractServiceFactory::class,
Expand Down Expand Up @@ -1668,7 +1716,7 @@
'verify/process-response' => ['*'],
'search*' => ['*'],
'index' => ['*'],
'user-registration' => ['*'],
'user-registration*' => ['*'],
'user-forgot-username' => ['*'],
'cookies*' => ['*'],
'privacy-notice' => ['*'],
Expand Down Expand Up @@ -1701,5 +1749,10 @@
'options_default_plus_cancel' => \Permits\Form\Model\Fieldset\SubmitOrCancelApplication::class,
'options_bilateral' => \Permits\Form\Model\Fieldset\SubmitOnly::class,
]
]
],
'validators' => [
'factories' => [
\Olcs\Form\Validator\UniqueConsultantDetails::class => \Olcs\Form\Validator\Factory\UniqueConsultantDetailsFactory::class,
],
],
];
Loading
Loading