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()));
}
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()));
}
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()));
}
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);
}
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);
}
Aggregations