Search in sources :

Example 16 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class CaseService method createUserFilter.

@SuppressWarnings("rawtypes")
public Predicate createUserFilter(CriteriaBuilder cb, CriteriaQuery cq, From<?, Case> casePath, CaseUserFilterCriteria userFilterCriteria) {
    User currentUser = getCurrentUser();
    if (currentUser == null) {
        return null;
    }
    Predicate filterResponsible = null;
    Predicate filter = null;
    final JurisdictionLevel jurisdictionLevel = currentUser.getCalculatedJurisdictionLevel();
    if (jurisdictionLevel != JurisdictionLevel.NATION && !currentUser.hasAnyUserRole(UserRole.REST_USER, UserRole.REST_EXTERNAL_VISITS_USER)) {
        // whoever created the case or is assigned to it is allowed to access it
        if (userFilterCriteria == null || (userFilterCriteria.getIncludeCasesFromOtherJurisdictions())) {
            filterResponsible = cb.equal(casePath.get(Case.REPORTING_USER).get(User.ID), currentUser.getId());
            filterResponsible = cb.or(filterResponsible, cb.equal(casePath.get(Case.SURVEILLANCE_OFFICER).get(User.ID), currentUser.getId()));
            filterResponsible = cb.or(filterResponsible, cb.equal(casePath.get(Case.CASE_OFFICER).get(User.ID), currentUser.getId()));
        }
        switch(jurisdictionLevel) {
            case REGION:
                final Region region = currentUser.getRegion();
                if (region != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.REGION).get(Region.ID), region.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_REGION).get(Region.ID), region.getId()));
                }
                break;
            case DISTRICT:
                final District district = currentUser.getDistrict();
                if (district != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.DISTRICT).get(District.ID), district.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_DISTRICT).get(District.ID), district.getId()));
                }
                break;
            case HEALTH_FACILITY:
                final Facility healthFacility = currentUser.getHealthFacility();
                if (healthFacility != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.HEALTH_FACILITY).get(Facility.ID), healthFacility.getId()));
                }
                break;
            case COMMUNITY:
                final Community community = currentUser.getCommunity();
                if (community != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.COMMUNITY).get(Community.ID), community.getId()), cb.equal(casePath.get(Case.RESPONSIBLE_COMMUNITY).get(Community.ID), community.getId()));
                }
                break;
            case POINT_OF_ENTRY:
                final PointOfEntry pointOfEntry = currentUser.getPointOfEntry();
                if (pointOfEntry != null) {
                    filter = CriteriaBuilderHelper.or(cb, filter, cb.equal(casePath.get(Case.POINT_OF_ENTRY).get(PointOfEntry.ID), pointOfEntry.getId()));
                }
                break;
            case LABORATORY:
                final Subquery<Long> sampleSubQuery = cq.subquery(Long.class);
                final Root<Sample> sampleRoot = sampleSubQuery.from(Sample.class);
                final SampleJoins joins = new SampleJoins(sampleRoot);
                final Join cazeJoin = joins.getCaze();
                sampleSubQuery.where(cb.and(cb.equal(cazeJoin, casePath), sampleService.createUserFilterWithoutAssociations(cb, joins)));
                sampleSubQuery.select(sampleRoot.get(Sample.ID));
                filter = CriteriaBuilderHelper.or(cb, filter, cb.exists(sampleSubQuery));
                break;
            default:
        }
        // get all cases based on the user's contact association
        if (userFilterCriteria == null || (!userFilterCriteria.isExcludeCasesFromContacts() && Boolean.TRUE.equals(userFilterCriteria.getIncludeCasesFromOtherJurisdictions()))) {
            filter = CriteriaBuilderHelper.or(cb, filter, contactService.createUserFilterWithoutCase(new ContactQueryContext(cb, cq, casePath.join(Case.CONTACTS, JoinType.LEFT))));
        }
        // all users (without specific restrictions) get access to cases that have been made available to the whole country
        if ((userFilterCriteria == null || userFilterCriteria.getIncludeCasesFromOtherJurisdictions()) && !featureConfigurationFacade.isFeatureDisabled(FeatureType.NATIONAL_CASE_SHARING)) {
            filter = CriteriaBuilderHelper.or(cb, filter, cb.isTrue(casePath.get(Case.SHARED_TO_COUNTRY)));
        }
    }
    // only show cases of a specific disease if a limited disease is set
    if (currentUser.getLimitedDisease() != null) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(casePath.get(Case.DISEASE), currentUser.getLimitedDisease()));
    }
    // port health users can only see port health cases
    if (UserRole.isPortHealthUser(currentUser.getUserRoles())) {
        filter = CriteriaBuilderHelper.and(cb, filter, cb.equal(casePath.get(Case.CASE_ORIGIN), CaseOrigin.POINT_OF_ENTRY));
    }
    filter = CriteriaBuilderHelper.or(cb, filter, filterResponsible);
    return filter;
}
Also used : User(de.symeda.sormas.backend.user.User) Sample(de.symeda.sormas.backend.sample.Sample) ContactQueryContext(de.symeda.sormas.backend.contact.ContactQueryContext) JurisdictionLevel(de.symeda.sormas.api.user.JurisdictionLevel) Join(javax.persistence.criteria.Join) Predicate(javax.persistence.criteria.Predicate) Region(de.symeda.sormas.backend.infrastructure.region.Region) Facility(de.symeda.sormas.backend.infrastructure.facility.Facility) PointOfEntry(de.symeda.sormas.backend.infrastructure.pointofentry.PointOfEntry) District(de.symeda.sormas.backend.infrastructure.district.District) SampleJoins(de.symeda.sormas.backend.sample.SampleJoins) Community(de.symeda.sormas.backend.infrastructure.community.Community)

Example 17 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class SurveillanceReportFacadeEjb method getIndexList.

@Override
public List<SurveillanceReportDto> getIndexList(SurveillanceReportCriteria criteria, Integer first, Integer max) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<SurveillanceReport> cq = cb.createQuery(SurveillanceReport.class);
    Root<SurveillanceReport> root = cq.from(SurveillanceReport.class);
    Predicate filter = service.buildCriteriaFilter(criteria, cb, root);
    if (filter != null) {
        cq.where(filter);
    }
    cq.orderBy(cb.desc(root.get(SurveillanceReport.CREATION_DATE)));
    List<SurveillanceReport> resultList = QueryHelper.getResultList(em, cq, first, max);
    List<SurveillanceReportDto> reports = resultList.stream().map(SurveillanceReportFacadeEjb::toDto).collect(Collectors.toList());
    User currentUser = userService.getCurrentUser();
    Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight, I18nProperties.getCaption(Captions.inaccessibleValue));
    pseudonymizer.pseudonymizeDtoCollection(SurveillanceReportDto.class, reports, reportDto -> {
        Optional<SurveillanceReport> report = resultList.stream().filter(r -> r.getUuid().equals(r.getUuid())).findFirst();
        return report.isPresent() ? caseService.inJurisdictionOrOwned(report.get().getCaze()) : false;
    }, (reportDto, inJurisdiction) -> {
        Optional<SurveillanceReport> report = resultList.stream().filter(r -> r.getUuid().equals(r.getUuid())).findFirst();
        report.ifPresent(surveillanceReport -> pseudonymizer.pseudonymizeUser(surveillanceReport.getCreatingUser(), currentUser, reportDto::setCreatingUser));
    });
    return reports;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) DtoHelper(de.symeda.sormas.backend.util.DtoHelper) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) FacilityFacadeEjb(de.symeda.sormas.backend.infrastructure.facility.FacilityFacadeEjb) CaseFacadeEjb(de.symeda.sormas.backend.caze.CaseFacadeEjb) DistrictService(de.symeda.sormas.backend.infrastructure.district.DistrictService) SurveillanceReportDto(de.symeda.sormas.api.caze.surveillancereport.SurveillanceReportDto) Valid(javax.validation.Valid) Predicate(javax.persistence.criteria.Predicate) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) LocalBean(javax.ejb.LocalBean) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) Stateless(javax.ejb.Stateless) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) FacilityService(de.symeda.sormas.backend.infrastructure.facility.FacilityService) CaseService(de.symeda.sormas.backend.caze.CaseService) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) NotNull(javax.validation.constraints.NotNull) RegionFacadeEjb(de.symeda.sormas.backend.infrastructure.region.RegionFacadeEjb) RegionService(de.symeda.sormas.backend.infrastructure.region.RegionService) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer) Collectors(java.util.stream.Collectors) Captions(de.symeda.sormas.api.i18n.Captions) List(java.util.List) SurveillanceReportFacade(de.symeda.sormas.api.caze.surveillancereport.SurveillanceReportFacade) UserService(de.symeda.sormas.backend.user.UserService) DistrictFacadeEjb(de.symeda.sormas.backend.infrastructure.district.DistrictFacadeEjb) User(de.symeda.sormas.backend.user.User) SurveillanceReportCriteria(de.symeda.sormas.api.caze.surveillancereport.SurveillanceReportCriteria) Optional(java.util.Optional) User(de.symeda.sormas.backend.user.User) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer) Predicate(javax.persistence.criteria.Predicate) SurveillanceReportDto(de.symeda.sormas.api.caze.surveillancereport.SurveillanceReportDto)

Example 18 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class SurveillanceReportFacadeEjb method restorePseudonymizedDto.

private void restorePseudonymizedDto(SurveillanceReportDto dto, SurveillanceReport existingReport, SurveillanceReportDto existingDto) {
    if (existingDto != null) {
        boolean inJurisdiction = caseService.inJurisdictionOrOwned(existingReport.getCaze());
        User currentUser = userService.getCurrentUser();
        Pseudonymizer pseudonymizer = Pseudonymizer.getDefault(userService::hasRight);
        pseudonymizer.restoreUser(existingReport.getCreatingUser(), currentUser, dto, dto::setCreatingUser);
        pseudonymizer.restorePseudonymizedValues(SurveillanceReportDto.class, dto, existingDto, inJurisdiction);
    }
}
Also used : User(de.symeda.sormas.backend.user.User) Pseudonymizer(de.symeda.sormas.backend.util.Pseudonymizer)

Example 19 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class NotificationServiceTest method testSendNotifications_filterUserMessagesByroles.

@Test
public void testSendNotifications_filterUserMessagesByroles() throws NotificationDeliveryFailedException {
    TestDataCreator.RDCF rdcf = creator.createRDCF();
    UserDto survSup = creator.createUser(rdcf, "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
    UserDto caseSup = creator.createUser(rdcf, "Case", "Sup", UserRole.CASE_SUPERVISOR);
    UserDto contSup = creator.createUser(rdcf, "Cont", "Sup", UserRole.CONTACT_SUPERVISOR);
    User survSupUser = getUserService().getByReferenceDto(survSup.toReference());
    User survOffUser = getUserService().getByReferenceDto(caseSup.toReference());
    User contSupUser = getUserService().getByReferenceDto(contSup.toReference());
    Mockito.doAnswer(invocation -> {
        Map<User, String> userMessages = (Map<User, String>) invocation.getArgument(0);
        assertThat(userMessages.size(), is(2));
        assertThat(userMessages.get(survSupUser), is("Test message SS"));
        assertThat(userMessages.get(survOffUser), is("Test message SO"));
        return null;
    }).when(messagingService).sendEmail(any(), any(), any());
    notificationService.sendNotifications(NotificationType.CASE_LAB_RESULT_ARRIVED, MessageSubject.LAB_RESULT_ARRIVED, () -> {
        Map<User, String> userMessages = new HashMap<>();
        userMessages.put(survOffUser, "Test message SO");
        userMessages.put(survSupUser, "Test message SS");
        userMessages.put(contSupUser, "Test message CS");
        return userMessages;
    });
    Mockito.verify(messagingService, Mockito.times(1)).sendEmail(any(), any(), any());
    Mockito.verify(messagingService, Mockito.times(1)).sendSms(any(), any(), any());
}
Also used : User(de.symeda.sormas.backend.user.User) HashMap(java.util.HashMap) UserDto(de.symeda.sormas.api.user.UserDto) TestDataCreator(de.symeda.sormas.backend.TestDataCreator) HashMap(java.util.HashMap) Map(java.util.Map) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest) Test(org.junit.Test)

Example 20 with User

use of de.symeda.sormas.backend.user.User in project SORMAS-Project by hzi-braunschweig.

the class NotificationServiceTest method testSendNotifications_additionalUsers.

@Test
public void testSendNotifications_additionalUsers() throws NotificationDeliveryFailedException {
    TestDataCreator.RDCF rdcf = creator.createRDCF();
    Region region = getRegionService().getByReferenceDto(rdcf.region);
    UserDto survSup = creator.createUser(rdcf, "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
    UserDto caseSup = creator.createUser(rdcf, "Case", "Sup", UserRole.CASE_SUPERVISOR);
    User caseSupUser = getUserService().getByReferenceDto(caseSup.toReference());
    Mockito.when(userService.getAllByRegionsAndUserRoles(any(), any())).then(invocation -> {
        // load only for SURVEILLANCE_SUPERVISOR, so the additional CASE_SUPERVISOR user will be added in the notification service
        return getUserService().getAllByRegionsAndUserRoles((List<Region>) invocation.getArgument(0), UserRole.SURVEILLANCE_SUPERVISOR);
    });
    Mockito.doAnswer(invocation -> {
        Map<User, String> userMessages = (Map<User, String>) invocation.getArgument(0);
        assertThat(userMessages.size(), is(2));
        assertThat(userMessages.get(getUserService().getByReferenceDto(survSup.toReference())), is("Test message"));
        assertThat(userMessages.get(getUserService().getByReferenceDto(caseSup.toReference())), is("Test message"));
        return null;
    }).when(messagingService).sendEmail(any(), any(), any());
    notificationService.sendNotifications(NotificationType.VISIT_COMPLETED, Collections.singletonList(region), Collections.singletonList(caseSupUser), MessageSubject.VISIT_COMPLETED, "Test message");
    Mockito.verify(messagingService, Mockito.times(1)).sendEmail(any(), any(), any());
    Mockito.verify(messagingService, Mockito.times(1)).sendSms(any(), any(), any());
}
Also used : User(de.symeda.sormas.backend.user.User) UserDto(de.symeda.sormas.api.user.UserDto) TestDataCreator(de.symeda.sormas.backend.TestDataCreator) Region(de.symeda.sormas.backend.infrastructure.region.Region) HashMap(java.util.HashMap) Map(java.util.Map) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest) Test(org.junit.Test)

Aggregations

User (de.symeda.sormas.backend.user.User)138 Predicate (javax.persistence.criteria.Predicate)61 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)51 List (java.util.List)48 Region (de.symeda.sormas.backend.infrastructure.region.Region)43 Collections (java.util.Collections)42 ArrayList (java.util.ArrayList)40 DataHelper (de.symeda.sormas.api.utils.DataHelper)38 Date (java.util.Date)38 Stateless (javax.ejb.Stateless)38 EJB (javax.ejb.EJB)37 LocalBean (javax.ejb.LocalBean)37 District (de.symeda.sormas.backend.infrastructure.district.District)36 Collectors (java.util.stream.Collectors)36 UserRole (de.symeda.sormas.api.user.UserRole)34 UserService (de.symeda.sormas.backend.user.UserService)33 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)33 Case (de.symeda.sormas.backend.caze.Case)32 Root (javax.persistence.criteria.Root)32 Disease (de.symeda.sormas.api.Disease)31