Search in sources :

Example 1 with ServiceRoleEntity

use of uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity in project pay-adminusers by alphagov.

the class UserDaoIT method shouldCreateAUserSuccessfully.

@Test
public void shouldCreateAUserSuccessfully() {
    Role role = roleDbFixture(databaseHelper).insertRole();
    String gatewayAccountId = randomInt().toString();
    int serviceId = serviceDbFixture(databaseHelper).withGatewayAccountIds(gatewayAccountId).insertService().getId();
    String username = valueOf(nextInt());
    UserEntity userEntity = new UserEntity();
    userEntity.setExternalId(randomUuid());
    userEntity.setUsername(username);
    userEntity.setPassword("password-" + username);
    userEntity.setDisabled(false);
    userEntity.setEmail(username + "@example.com");
    userEntity.setOtpKey(randomInt().toString());
    userEntity.setTelephoneNumber("+447700900000");
    userEntity.setSecondFactor(SecondFactorMethod.SMS);
    userEntity.setSessionVersion(0);
    ZonedDateTime timeNow = ZonedDateTime.now(ZoneId.of("UTC"));
    userEntity.setCreatedAt(timeNow);
    userEntity.setUpdatedAt(timeNow);
    ServiceEntity serviceEntity = serviceDao.findByGatewayAccountId(gatewayAccountId).get();
    RoleEntity roleEntity = roleDao.findByRoleName(role.getName()).get();
    ServiceRoleEntity serviceRoleEntity = new ServiceRoleEntity(serviceEntity, roleEntity);
    serviceRoleEntity.setUser(userEntity);
    userEntity.addServiceRole(serviceRoleEntity);
    userDao.persist(userEntity);
    assertThat(userEntity.getId(), is(notNullValue()));
    List<Map<String, Object>> savedUserData = databaseHelper.findUser(userEntity.getId());
    assertThat(savedUserData.size(), is(1));
    assertThat((String) savedUserData.get(0).get("external_id"), not(emptyOrNullString()));
    assertThat(((String) savedUserData.get(0).get("external_id")).length(), equalTo(32));
    assertThat(savedUserData.get(0).get("username"), is(userEntity.getUsername()));
    assertThat(savedUserData.get(0).get("password"), is(userEntity.getPassword()));
    assertThat(savedUserData.get(0).get("email"), is(userEntity.getEmail()));
    assertThat(savedUserData.get(0).get("otp_key"), is(userEntity.getOtpKey()));
    assertThat(savedUserData.get(0).get("telephone_number"), is(userEntity.getTelephoneNumber()));
    assertThat(savedUserData.get(0).get("disabled"), is(Boolean.FALSE));
    assertThat(savedUserData.get(0).get("session_version"), is(0));
    assertThat(savedUserData.get(0).get("createdat"), is(java.sql.Timestamp.from(timeNow.toInstant())));
    assertThat(savedUserData.get(0).get("updatedat"), is(java.sql.Timestamp.from(timeNow.toInstant())));
    List<Map<String, Object>> serviceRolesForUser = databaseHelper.findServiceRoleForUser(userEntity.getId());
    assertThat(serviceRolesForUser.size(), is(1));
    assertThat(serviceRolesForUser.get(0).get("id"), is(role.getId()));
    assertThat(serviceRolesForUser.get(0).get("service_id"), is(serviceId));
    assertThat(serviceRolesForUser.get(0).get("name"), is(role.getName()));
    assertThat(serviceRolesForUser.get(0).get("description"), is(role.getDescription()));
}
Also used : Role(uk.gov.pay.adminusers.model.Role) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) ZonedDateTime(java.time.ZonedDateTime) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) Map(java.util.Map) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 2 with ServiceRoleEntity

use of uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity in project pay-adminusers by alphagov.

the class UserDaoIT method shouldAddServiceRoleOfAnExistingUser_whenSettingANewServiceRole.

@Test
public void shouldAddServiceRoleOfAnExistingUser_whenSettingANewServiceRole() {
    Role role1 = roleDbFixture(databaseHelper).insertRole();
    Role role2 = roleDbFixture(databaseHelper).insertRole();
    String gatewayAccountId1 = randomInt().toString();
    String gatewayAccountId2 = randomInt().toString();
    Service service1 = serviceDbFixture(databaseHelper).withGatewayAccountIds(gatewayAccountId1).insertService();
    serviceDbFixture(databaseHelper).withGatewayAccountIds(gatewayAccountId2).insertService();
    String username = randomUuid();
    String email = username + "@example.com";
    userDbFixture(databaseHelper).withServiceRole(service1, role1.getId()).withUsername(username).withEmail(email).insertUser();
    UserEntity existingUser = userDao.findByUsername(username).get();
    assertThat(existingUser.getGatewayAccountId(), is(gatewayAccountId1));
    assertThat(existingUser.getRoles().size(), is(1));
    assertThat(existingUser.getRoles().get(0).getId(), is(role1.getId()));
    ServiceEntity serviceEntity2 = serviceDao.findByGatewayAccountId(gatewayAccountId2).get();
    RoleEntity roleEntity2 = roleDao.findByRoleName(role2.getName()).get();
    ServiceRoleEntity serviceRole = new ServiceRoleEntity(serviceEntity2, roleEntity2);
    serviceRole.setUser(existingUser);
    existingUser.addServiceRole(serviceRole);
    userDao.merge(existingUser);
    UserEntity changedUser = userDao.findByUsername(username).get();
    List<ServiceRoleEntity> servicesRoles = changedUser.getServicesRoles();
    assertThat(servicesRoles.size(), is(2));
    assertThat(servicesRoles.stream().map(sr -> sr.getService().getExternalId()).collect(toUnmodifiableList()), hasItems(service1.getExternalId(), serviceEntity2.getExternalId()));
    assertThat(servicesRoles.stream().map(sr -> sr.getRole().getName()).collect(toUnmodifiableList()), hasItems(role1.getName(), role2.getName()));
}
Also used : Role(uk.gov.pay.adminusers.model.Role) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) Service(uk.gov.pay.adminusers.model.Service) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 3 with ServiceRoleEntity

use of uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity in project pay-adminusers by alphagov.

the class ServiceRoleCreator method doCreate.

@Transactional
public Optional<User> doCreate(String userExternalId, String serviceExternalId, String roleName) {
    Optional<UserEntity> userMaybe = userDao.findByExternalId(userExternalId);
    if (!userMaybe.isPresent()) {
        return Optional.empty();
    }
    Optional<ServiceEntity> serviceMaybe = serviceDao.findByExternalId(serviceExternalId);
    if (!serviceMaybe.isPresent()) {
        throw serviceDoesNotExistError(serviceExternalId);
    }
    Optional<RoleEntity> roleMaybe = roleDao.findByRoleName(roleName);
    if (!roleMaybe.isPresent()) {
        throw undefinedRoleException(roleName);
    }
    UserEntity userEntity = userMaybe.get();
    userEntity.getServicesRole(serviceExternalId).ifPresent(serviceRoleEntity -> {
        throw conflictingServiceRoleForUser(userExternalId, serviceExternalId);
    });
    userEntity.addServiceRole(new ServiceRoleEntity(serviceMaybe.get(), roleMaybe.get()));
    userDao.merge(userEntity);
    return Optional.of(linksBuilder.decorate(userEntity.toUser()));
}
Also used : ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Transactional(com.google.inject.persist.Transactional)

Example 4 with ServiceRoleEntity

use of uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity in project pay-adminusers by alphagov.

the class ServiceUserRemover method remove.

public void remove(String userExternalId, String removerExternalId, String serviceExternalId) {
    LOGGER.info("User remove from service requested - serviceId={}, removerId={}, userId={}", serviceExternalId, removerExternalId, userExternalId);
    ServiceRoleEntity userServiceRoleToRemove = getServiceRoleEntityOf(userExternalId, serviceExternalId);
    checkRemoverIsAdmin(removerExternalId, serviceExternalId).orElseThrow(() -> forbiddenOperationException(userExternalId, OPERATION, serviceExternalId));
    serviceRoleDao.remove(userServiceRoleToRemove);
}
Also used : ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)

Example 5 with ServiceRoleEntity

use of uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity in project pay-adminusers by alphagov.

the class UserInviteCompleter method complete.

@Override
@Transactional
public Optional<InviteCompleteResponse> complete(String inviteCode) {
    return inviteDao.findByCode(inviteCode).map(inviteEntity -> {
        if (inviteEntity.isExpired() || inviteEntity.isDisabled()) {
            throw inviteLockedException(inviteEntity.getCode());
        }
        return userDao.findByEmail(inviteEntity.getEmail()).map(userEntity -> {
            if (inviteEntity.getService() != null && inviteEntity.isUserType()) {
                ServiceRoleEntity serviceRole = new ServiceRoleEntity(inviteEntity.getService(), inviteEntity.getRole());
                userEntity.addServiceRole(serviceRole);
                userDao.merge(userEntity);
                inviteEntity.setDisabled(true);
                inviteDao.merge(inviteEntity);
                InviteCompleteResponse response = new InviteCompleteResponse(inviteEntity.toInvite());
                response.setUserExternalId(userEntity.getExternalId());
                response.setServiceExternalId(inviteEntity.getService().getExternalId());
                return Optional.of(response);
            } else {
                throw internalServerError(format("Attempting to complete user subscription to a service for a non existent service. invite-code = %s", inviteEntity.getCode()));
            }
        }).orElseGet(() -> {
            throw internalServerError(format("Attempting to complete user subscription to a service for a non existent user. invite-code = %s", inviteEntity.getCode()));
        });
    }).orElseGet(Optional::empty);
}
Also used : AdminUsersExceptions.inviteLockedException(uk.gov.pay.adminusers.service.AdminUsersExceptions.inviteLockedException) InviteDao(uk.gov.pay.adminusers.persistence.dao.InviteDao) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) Inject(com.google.inject.Inject) AdminUsersExceptions.internalServerError(uk.gov.pay.adminusers.service.AdminUsersExceptions.internalServerError) Optional(java.util.Optional) InviteCompleteResponse(uk.gov.pay.adminusers.model.InviteCompleteResponse) String.format(java.lang.String.format) Transactional(com.google.inject.persist.Transactional) UserDao(uk.gov.pay.adminusers.persistence.dao.UserDao) Optional(java.util.Optional) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) InviteCompleteResponse(uk.gov.pay.adminusers.model.InviteCompleteResponse) Transactional(com.google.inject.persist.Transactional)

Aggregations

ServiceRoleEntity (uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)25 ServiceEntity (uk.gov.pay.adminusers.persistence.entity.ServiceEntity)20 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)20 RoleEntity (uk.gov.pay.adminusers.persistence.entity.RoleEntity)19 Test (org.junit.jupiter.api.Test)11 Transactional (com.google.inject.persist.Transactional)5 WebApplicationException (javax.ws.rs.WebApplicationException)4 User (uk.gov.pay.adminusers.model.User)4 InviteEntity (uk.gov.pay.adminusers.persistence.entity.InviteEntity)4 Role (uk.gov.pay.adminusers.model.Role)3 Service (uk.gov.pay.adminusers.model.Service)3 Inject (com.google.inject.Inject)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)2 InviteCompleteResponse (uk.gov.pay.adminusers.model.InviteCompleteResponse)2 InviteUserRequest (uk.gov.pay.adminusers.model.InviteUserRequest)2 UserDao (uk.gov.pay.adminusers.persistence.dao.UserDao)2 String.format (java.lang.String.format)1 ZonedDateTime (java.time.ZonedDateTime)1