Search in sources :

Example 16 with ServiceRoleEntity

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

the class ServiceRoleDaoIT method shouldRemoveAServiceRoleOfAUserSuccessfully.

@Test
public void shouldRemoveAServiceRoleOfAUserSuccessfully() {
    Service service = ServiceDbFixture.serviceDbFixture(databaseHelper).insertService();
    int roleId = RoleDbFixture.roleDbFixture(databaseHelper).insertRole().getId();
    String username = randomUuid();
    String email = username + "@example.com";
    User user = UserDbFixture.userDbFixture(databaseHelper).withServiceRole(service, roleId).withUsername(username).withEmail(email).insertUser();
    UserServiceId userServiceId = new UserServiceId();
    userServiceId.setServiceId(user.getServiceRoles().get(0).getService().getId());
    userServiceId.setUserId(user.getId());
    List<Map<String, Object>> serviceRoles = databaseHelper.findServiceRoleForUser(user.getId());
    assertThat(serviceRoles.size(), is(1));
    ServiceRoleEntity serviceRoleOfUser = serviceRoleDao.findById(userServiceId).get();
    serviceRoleDao.remove(serviceRoleOfUser);
    List<Map<String, Object>> serviceRolesAfterRemove = databaseHelper.findServiceRoleForUser(user.getId());
    assertThat(serviceRolesAfterRemove.size(), is(0));
}
Also used : User(uk.gov.pay.adminusers.model.User) Service(uk.gov.pay.adminusers.model.Service) UserServiceId(uk.gov.pay.adminusers.persistence.entity.UserServiceId) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 17 with ServiceRoleEntity

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

the class ServiceUserRemover method removeWithoutAdminCheck.

public void removeWithoutAdminCheck(String userExternalId, String serviceExternalId) {
    LOGGER.info("User remove from toolbox requested - serviceId={}, userId={}", serviceExternalId, userExternalId);
    ServiceRoleEntity userServiceRoleToRemove = getServiceRoleEntityOf(userExternalId, serviceExternalId);
    serviceRoleDao.remove(userServiceRoleToRemove);
}
Also used : ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)

Example 18 with ServiceRoleEntity

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

the class UserCreator method addServiceRoleToUser.

private void addServiceRoleToUser(UserEntity user, RoleEntity role, List<String> serviceExternalIds) {
    serviceExternalIds.forEach(serviceExternalId -> serviceDao.findByExternalId(serviceExternalId).map(serviceEntity -> {
        ServiceRoleEntity serviceRole = new ServiceRoleEntity(serviceEntity, role);
        serviceRole.setUser(user);
        user.addServiceRole(serviceRole);
        return null;
    }).orElseGet(() -> {
        LOGGER.error("Unable to assign service with external id {} to user, as it does not exist", serviceExternalId);
        return null;
    }));
}
Also used : ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)

Example 19 with ServiceRoleEntity

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

the class UserCreator method addServiceFromGatewayAccountsToUser.

private void addServiceFromGatewayAccountsToUser(UserEntity user, RoleEntity role, List<String> gatewayAccountIds) {
    ServiceRoleEntity serviceRole = getServiceAssignedTo(gatewayAccountIds).map(serviceEntity -> new ServiceRoleEntity(serviceEntity, role)).orElseGet(() -> {
        ServiceEntity service = new ServiceEntity(gatewayAccountIds);
        service.addOrUpdateServiceName(ServiceNameEntity.from(SupportedLanguage.ENGLISH, Service.DEFAULT_NAME_VALUE));
        serviceDao.persist(service);
        return new ServiceRoleEntity(service, role);
    });
    serviceRole.setUser(user);
    user.addServiceRole(serviceRole);
}
Also used : Logger(org.slf4j.Logger) AdminUsersExceptions.undefinedRoleException(uk.gov.pay.adminusers.service.AdminUsersExceptions.undefinedRoleException) ServiceDao(uk.gov.pay.adminusers.persistence.dao.ServiceDao) CreateUserRequest(uk.gov.pay.adminusers.model.CreateUserRequest) User(uk.gov.pay.adminusers.model.User) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) AdminUsersExceptions.conflictingServiceGatewayAccountsForUser(uk.gov.pay.adminusers.service.AdminUsersExceptions.conflictingServiceGatewayAccountsForUser) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Transactional(com.google.inject.persist.Transactional) UserDao(uk.gov.pay.adminusers.persistence.dao.UserDao) List(java.util.List) ServiceNameEntity(uk.gov.pay.adminusers.persistence.entity.service.ServiceNameEntity) RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) Service(uk.gov.pay.adminusers.model.Service) Optional(java.util.Optional) RoleDao(uk.gov.pay.adminusers.persistence.dao.RoleDao) SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)

Example 20 with ServiceRoleEntity

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

the class UserEntityTest method creatingAUser_shouldSetGatewayAccountAndRole_whenServiceRoleIsSet.

@Test
void creatingAUser_shouldSetGatewayAccountAndRole_whenServiceRoleIsSet() {
    UserEntity userEntity = new UserEntity();
    String gatewayAccountId = "1";
    ServiceEntity service = new ServiceEntity(List.of(gatewayAccountId));
    Role role = role(1, "role", "hey");
    role.setPermissions(Set.of(permission(1, "perm1", "perm1 desc"), permission(2, "perm2", "perm2 desc")));
    RoleEntity roleEntity = new RoleEntity(role);
    ServiceRoleEntity serviceRole = new ServiceRoleEntity(service, roleEntity);
    userEntity.addServiceRole(serviceRole);
    assertThat(userEntity.getRoles().size(), is(1));
    assertThat(userEntity.getRoles().get(0).getId(), is(1));
    assertThat(userEntity.getGatewayAccountId(), is(gatewayAccountId));
}
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) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceRoleEntity (uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)28 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)22 RoleEntity (uk.gov.pay.adminusers.persistence.entity.RoleEntity)20 ServiceEntity (uk.gov.pay.adminusers.persistence.entity.ServiceEntity)20 Test (org.junit.jupiter.api.Test)12 Transactional (com.google.inject.persist.Transactional)5 WebApplicationException (javax.ws.rs.WebApplicationException)4 Role (uk.gov.pay.adminusers.model.Role)4 User (uk.gov.pay.adminusers.model.User)4 InviteEntity (uk.gov.pay.adminusers.persistence.entity.InviteEntity)4 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