Skip to content

Commit

Permalink
# This is a combination of 2 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

fix: Operator Admins no longer get operator-tc Role assigned when being created.

# This is the commit message #2:

chore: phpunit test config
  • Loading branch information
fibble committed Sep 9, 2024
1 parent ac10eda commit 0d15888
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
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.

30 changes: 24 additions & 6 deletions app/api/module/Api/src/Entity/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
namespace Dvsa\Olcs\Api\Entity\User;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Dvsa\Olcs\Api\Domain\Exception\ValidationException;
use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity;
use Dvsa\Olcs\Api\Entity\Licence\Licence as LicenceEntity;
use Dvsa\Olcs\Api\Entity\Organisation\Organisation;
use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser as OrganisationUserEntity;
use Dvsa\Olcs\Api\Entity\OrganisationProviderInterface;
use Dvsa\Olcs\Api\Entity\Licence\Licence as LicenceEntity;
use Dvsa\Olcs\Api\Entity\User\Role as RoleEntity;
use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity;
use Dvsa\Olcs\Api\Rbac\IdentityProviderInterface;

/**
Expand Down Expand Up @@ -39,6 +38,7 @@ class User extends AbstractUser implements OrganisationProviderInterface
public const USER_TYPE_ANON = 'anon';
public const USER_TYPE_LOCAL_AUTHORITY = 'local-authority';
public const USER_TYPE_OPERATOR = 'operator';
public const USER_TYPE_TC = 'operator-tc';
public const USER_TYPE_PARTNER = 'partner';
public const USER_TYPE_TRANSPORT_MANAGER = 'transport-manager';

Expand All @@ -57,14 +57,15 @@ class User extends AbstractUser implements OrganisationProviderInterface
RoleEntity::ROLE_LOCAL_AUTHORITY_ADMIN,
RoleEntity::ROLE_LOCAL_AUTHORITY_USER,
],
self::USER_TYPE_OPERATOR => [
self::USER_TYPE_TC => [
RoleEntity::ROLE_OPERATOR_TC,
],
self::USER_TYPE_OPERATOR => [
RoleEntity::ROLE_OPERATOR_ADMIN,
RoleEntity::ROLE_OPERATOR_USER,
RoleEntity::ROLE_OPERATOR_TM,
],
self::USER_TYPE_TRANSPORT_MANAGER => [
RoleEntity::ROLE_OPERATOR_TC,
RoleEntity::ROLE_OPERATOR_ADMIN,
RoleEntity::ROLE_OPERATOR_USER,
RoleEntity::ROLE_OPERATOR_TM,
Expand Down Expand Up @@ -96,8 +97,11 @@ class User extends AbstractUser implements OrganisationProviderInterface
self::PERMISSION_ADMIN => [RoleEntity::ROLE_LOCAL_AUTHORITY_ADMIN],
self::PERMISSION_USER => [RoleEntity::ROLE_LOCAL_AUTHORITY_USER],
],
self::USER_TYPE_TC => [
self::PERMISSION_ADMIN => [RoleEntity::ROLE_OPERATOR_TC],
],
self::USER_TYPE_OPERATOR => [
self::PERMISSION_ADMIN => [RoleEntity::ROLE_OPERATOR_ADMIN, RoleEntity::ROLE_OPERATOR_TC],
self::PERMISSION_ADMIN => [RoleEntity::ROLE_OPERATOR_ADMIN],
self::PERMISSION_USER => [RoleEntity::ROLE_OPERATOR_USER],
self::PERMISSION_TM => [RoleEntity::ROLE_OPERATOR_TM],
],
Expand Down Expand Up @@ -415,6 +419,8 @@ public function getUserType()
$this->userType = self::USER_TYPE_TRANSPORT_MANAGER;
} elseif (isset($this->partnerContactDetails)) {
$this->userType = self::USER_TYPE_PARTNER;
} elseif ($this->isTransportConsultant()) {
$this->userType = self::USER_TYPE_TC;
} else {
$this->userType = self::USER_TYPE_OPERATOR;
}
Expand Down Expand Up @@ -515,6 +521,18 @@ public function isInternal()
return (self::USER_TYPE_INTERNAL === $this->getUserType());
}

/**
* Checks whether the user is of internal type
*
* @return bool
*/
public function isTransportConsultant()
{
return $this->hasRoles([RoleEntity::ROLE_OPERATOR_TC]);
}



/**
* Get permission
*
Expand Down
50 changes: 41 additions & 9 deletions app/api/test/module/Api/src/Entity/User/UserEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
namespace Dvsa\OlcsTest\Api\Entity\User;

use Doctrine\Common\Collections\ArrayCollection;
use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser;
use Dvsa\Olcs\Api\Entity\User\User as UserEntity;
use Dvsa\Olcs\Api\Rbac\IdentityProviderInterface;
use Dvsa\OlcsTest\Api\Entity\Abstracts\EntityTester;
use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity;
use Dvsa\Olcs\Api\Entity\Bus\LocalAuthority as LocalAuthorityEntity;
use Dvsa\Olcs\Api\Entity\ContactDetails\ContactDetails as ContactDetailsEntity;
use Dvsa\Olcs\Api\Entity\Licence\Licence as LicenceEntity;
use Dvsa\Olcs\Api\Entity\Organisation\Organisation as OrganisationEntity;
use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser;
use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser as OrganisationUserEntity;
use Dvsa\Olcs\Api\Entity\Licence\Licence as LicenceEntity;
use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity;
use Dvsa\Olcs\Api\Entity\Tm\TransportManager as TransportManagerEntity;
use Dvsa\Olcs\Api\Entity\User\Role as RoleEntity;
use Dvsa\Olcs\Api\Entity\User\User as Entity;
use Dvsa\Olcs\Api\Entity\User\Team as TeamEntity;
use Dvsa\Olcs\Api\Entity\User\User as Entity;
use Dvsa\Olcs\Api\Entity\User\User as UserEntity;
use Dvsa\Olcs\Api\Rbac\IdentityProviderInterface;
use Dvsa\OlcsTest\Api\Entity\Abstracts\EntityTester;
use Mockery as m;
use ReflectionClass;

Expand Down Expand Up @@ -1405,8 +1405,40 @@ public function dpCanResetPassword(): array
public function dpOperatorAdminRoles(): array
{
return [
[RoleEntity::ROLE_OPERATOR_ADMIN],
[RoleEntity::ROLE_OPERATOR_TC],
[RoleEntity::ROLE_OPERATOR_ADMIN]
];
}

public function testIsTransportConsultant()
{
$roleTC = m::mock(RoleEntity::class)->makePartial();
$roleTC->setRole(RoleEntity::ROLE_OPERATOR_TC);

$roleOther = m::mock(RoleEntity::class)->makePartial();
$roleOther->setRole('some_other_role');

$userTC = new UserEntity('pid', UserEntity::USER_TYPE_OPERATOR);
$userTC->setRoles(new ArrayCollection([$roleTC]));
$this->assertTrue($userTC->isTransportConsultant(), 'User should be identified as a transport consultant');

$userNonTC = new UserEntity('pid', UserEntity::USER_TYPE_OPERATOR);
$userNonTC->setRoles(new ArrayCollection([$roleOther]));
$this->assertFalse($userNonTC->isTransportConsultant(), 'User should not be identified as a transport consultant');
}

public function testGetUserTypeForTransportConsultant()
{
$roleTC = m::mock(RoleEntity::class)->makePartial();
$roleTC->setRole(RoleEntity::ROLE_OPERATOR_TC);

$user = new UserEntity('pid', UserEntity::USER_TYPE_OPERATOR);
$user->setRoles(new ArrayCollection([$roleTC]));

$reflectionClass = new ReflectionClass(UserEntity::class);
$property = $reflectionClass->getProperty('userType');
$property->setAccessible(true);
$property->setValue($user, null);

$this->assertEquals(UserEntity::USER_TYPE_TC, $user->getUserType(), 'User type should be set to transport consultant');
}
}

0 comments on commit 0d15888

Please sign in to comment.