Search in sources :

Example 26 with UserRole

use of de.symeda.sormas.api.user.UserRole in project SORMAS-Project by hzi-braunschweig.

the class CaseBackendTest method testTaskReassignmentAfterChangedCaseCommunity.

@Test
public void testTaskReassignmentAfterChangedCaseCommunity() throws DaoException {
    CaseDao caseDao = DatabaseHelper.getCaseDao();
    Case caze = TestEntityCreator.createCase();
    caze.setRegion(caze.getResponsibleRegion());
    caze.setDistrict(caze.getResponsibleDistrict());
    caze.setCommunity(caze.getResponsibleCommunity());
    caseDao.saveAndSnapshot(caze);
    User user = ConfigProvider.getUser();
    user.setCommunity(caze.getCommunity());
    UserRole userRole = UserRole.COMMUNITY_OFFICER;
    Set<UserRole> userRoles = new HashSet<>();
    userRoles.add(userRole);
    user.setUserRoles(userRoles);
    DatabaseHelper.getUserDao().saveAndSnapshot(user);
    TaskDao taskDao = DatabaseHelper.getTaskDao();
    Task task = TestEntityCreator.createCaseTask(caze, TaskStatus.PENDING, user);
    caze = caseDao.queryUuidBasic(caze.getUuid());
    assertEquals(TestHelper.USER_UUID, caze.getSurveillanceOfficer().getUuid());
    assertEquals(caze.getResponsibleRegion().getUuid(), TestHelper.REGION_UUID);
    assertEquals(caze.getResponsibleDistrict().getUuid(), TestHelper.DISTRICT_UUID);
    assertEquals(caze.getResponsibleCommunity().getUuid(), TestHelper.COMMUNITY_UUID);
    assertEquals(caze.getRegion().getUuid(), TestHelper.REGION_UUID);
    assertEquals(caze.getDistrict().getUuid(), TestHelper.DISTRICT_UUID);
    assertEquals(caze.getCommunity().getUuid(), TestHelper.COMMUNITY_UUID);
    assertEquals(caze.getHealthFacility().getUuid(), TestHelper.FACILITY_UUID);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.USER_UUID, task.getAssigneeUser().getUuid());
    // ResponsibleDistrict and ResponsibleCommunity changed,
    // but District and Community still in user's jurisdiction
    District secondDistrict = DatabaseHelper.getDistrictDao().queryUuid(TestHelper.SECOND_DISTRICT_UUID);
    Community secondCommunity = DatabaseHelper.getCommunityDao().queryUuid(TestHelper.SECOND_COMMUNITY_UUID);
    caze.setResponsibleDistrict(secondDistrict);
    caze.setResponsibleCommunity(secondCommunity);
    caze.setDistrict(secondDistrict);
    caseDao.saveAndSnapshot(caze);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.USER_UUID, task.getAssigneeUser().getUuid());
    caze = caseDao.queryUuidBasic(caze.getUuid());
    assertEquals(TestHelper.SECOND_USER_UUID, caze.getSurveillanceOfficer().getUuid());
    // Case not in user's jurisdiction anymore
    caze.setCommunity(null);
    caseDao.saveAndSnapshot(caze);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.SECOND_USER_UUID, task.getAssigneeUser().getUuid());
}
Also used : Task(de.symeda.sormas.app.backend.task.Task) User(de.symeda.sormas.app.backend.user.User) UserRole(de.symeda.sormas.api.user.UserRole) CaseDao(de.symeda.sormas.app.backend.caze.CaseDao) TaskDao(de.symeda.sormas.app.backend.task.TaskDao) District(de.symeda.sormas.app.backend.region.District) Community(de.symeda.sormas.app.backend.region.Community) Case(de.symeda.sormas.app.backend.caze.Case) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with UserRole

use of de.symeda.sormas.api.user.UserRole in project SORMAS-Project by hzi-braunschweig.

the class CaseBackendTest method testEditCasePermissionWhenFacilityDoesNotMatchAndCaseNotCreatedByUser.

@Test
public void testEditCasePermissionWhenFacilityDoesNotMatchAndCaseNotCreatedByUser() {
    Case caze = TestEntityCreator.createCase();
    caze.setHealthFacility(null);
    UserRole userRole = UserRole.HOSPITAL_INFORMANT;
    Set<UserRole> userRoles = new HashSet<>();
    userRoles.add(userRole);
    ConfigProvider.getUser().setUserRoles(userRoles);
    ConfigProvider.getUser().setHealthFacility(DatabaseHelper.getFacilityDao().queryUuid(TestHelper.FACILITY_UUID));
    ConfigProvider.getUser().setUuid("");
    assertFalse(CaseEditAuthorization.isCaseEditAllowed(caze));
}
Also used : UserRole(de.symeda.sormas.api.user.UserRole) Case(de.symeda.sormas.app.backend.caze.Case) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 28 with UserRole

use of de.symeda.sormas.api.user.UserRole in project SORMAS-Project by hzi-braunschweig.

the class CaseBackendTest method testTaskReassignmentAfterChangedCaseFacility.

@Test
public void testTaskReassignmentAfterChangedCaseFacility() throws DaoException {
    CaseDao caseDao = DatabaseHelper.getCaseDao();
    Case caze = TestEntityCreator.createCase();
    User user = ConfigProvider.getUser();
    user.setHealthFacility(caze.getHealthFacility());
    UserRole userRole = UserRole.HOSPITAL_INFORMANT;
    Set<UserRole> userRoles = new HashSet<>();
    userRoles.add(userRole);
    user.setUserRoles(userRoles);
    DatabaseHelper.getUserDao().saveAndSnapshot(user);
    TaskDao taskDao = DatabaseHelper.getTaskDao();
    Task task = TestEntityCreator.createCaseTask(caze, TaskStatus.PENDING, user);
    caze = caseDao.queryUuidBasic(caze.getUuid());
    assertEquals(TestHelper.USER_UUID, caze.getSurveillanceOfficer().getUuid());
    assertEquals(caze.getResponsibleRegion().getUuid(), TestHelper.REGION_UUID);
    assertEquals(caze.getResponsibleDistrict().getUuid(), TestHelper.DISTRICT_UUID);
    assertEquals(caze.getResponsibleCommunity().getUuid(), TestHelper.COMMUNITY_UUID);
    assertEquals(caze.getHealthFacility().getUuid(), TestHelper.FACILITY_UUID);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.USER_UUID, task.getAssigneeUser().getUuid());
    // Case not in user's jurisdiction anymore
    caze.setResponsibleDistrict(DatabaseHelper.getDistrictDao().queryUuid(TestHelper.SECOND_DISTRICT_UUID));
    caze.setCommunity(null);
    caseDao.saveAndSnapshot(caze);
    caze = caseDao.queryUuidBasic(caze.getUuid());
    assertEquals(TestHelper.SECOND_USER_UUID, caze.getSurveillanceOfficer().getUuid());
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.USER_UUID, task.getAssigneeUser().getUuid());
    caze.setHealthFacility(null);
    caseDao.saveAndSnapshot(caze);
    task = taskDao.queryUuid(task.getUuid());
    assertEquals(TestHelper.SECOND_USER_UUID, task.getAssigneeUser().getUuid());
}
Also used : Task(de.symeda.sormas.app.backend.task.Task) User(de.symeda.sormas.app.backend.user.User) UserRole(de.symeda.sormas.api.user.UserRole) CaseDao(de.symeda.sormas.app.backend.caze.CaseDao) TaskDao(de.symeda.sormas.app.backend.task.TaskDao) Case(de.symeda.sormas.app.backend.caze.Case) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with UserRole

use of de.symeda.sormas.api.user.UserRole in project SORMAS-Project by hzi-braunschweig.

the class UserDao method getAllInJurisdiction.

public List<User> getAllInJurisdiction() {
    try {
        QueryBuilder builder = queryBuilder();
        Where where = builder.where();
        where.eq(AbstractDomainObject.SNAPSHOT, false);
        User currentUser = ConfigProvider.getUser();
        // create role filters for national and regional users
        List<UserRole> nationalOrRegionalRoles = UserRole.getWithJurisdictionLevels(JurisdictionLevel.NATION, JurisdictionLevel.REGION);
        for (int i = 0; i < nationalOrRegionalRoles.size(); i++) {
            UserRole role = nationalOrRegionalRoles.get(i);
            createRoleFilter(role, where);
        }
        where.or(nationalOrRegionalRoles.size());
        // conjunction by default, jurisdiction filter should not be empty because of combining role and jurisdiction filters by OR
        // if the current user is a national one than it should see all users
        Where jurisdictionFilter = where.raw("1=1");
        if (currentUser.getHealthFacility() != null) {
            where.and(jurisdictionFilter, where.eq(User.HEALTH_FACILITY + "_id", currentUser.getHealthFacility()));
        } else if (currentUser.getPointOfEntry() != null) {
            where.and(jurisdictionFilter, where.eq(User.POINT_OF_ENTRY + "_id", currentUser.getPointOfEntry()));
        } else if (currentUser.getCommunity() != null) {
            where.and(jurisdictionFilter, where.eq(User.COMMUNITY + "_id", currentUser.getCommunity()));
        } else if (currentUser.getDistrict() != null) {
            where.and(jurisdictionFilter, where.eq(User.DISTRICT + "_id", currentUser.getDistrict()));
        } else if (currentUser.getRegion() != null) {
            where.and(jurisdictionFilter, where.eq(User.REGION + "_id", currentUser.getRegion()));
        }
        // combine role filters and jurisdiction filter with OR(=> roleFilter OR jurisdictionFilter)
        where.or(2);
        // combine snapshot filter with AND (=> snapshot AND (role OR jurisdiction))
        where.and(2);
        return builder.query();
    } catch (SQLException e) {
        Log.e(getTableName(), "Could not perform getAllInJurisdiction");
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) UserRole(de.symeda.sormas.api.user.UserRole) QueryBuilder(com.j256.ormlite.stmt.QueryBuilder) Where(com.j256.ormlite.stmt.Where)

Aggregations

UserRole (de.symeda.sormas.api.user.UserRole)29 Test (org.junit.Test)7 HashSet (java.util.HashSet)6 Case (de.symeda.sormas.app.backend.caze.Case)5 User (de.symeda.sormas.backend.user.User)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)5 UserDto (de.symeda.sormas.api.user.UserDto)4 User (de.symeda.sormas.app.backend.user.User)4 Predicate (javax.persistence.criteria.Predicate)4 CaseDao (de.symeda.sormas.app.backend.caze.CaseDao)3 District (de.symeda.sormas.app.backend.region.District)3 Task (de.symeda.sormas.app.backend.task.Task)3 TaskDao (de.symeda.sormas.app.backend.task.TaskDao)3 Set (java.util.Set)3 AuthProvider (de.symeda.sormas.api.AuthProvider)2 JurisdictionLevel (de.symeda.sormas.api.user.JurisdictionLevel)2 UserRight (de.symeda.sormas.api.user.UserRight)2 Community (de.symeda.sormas.app.backend.region.Community)2 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)2 RDCF (de.symeda.sormas.backend.TestDataCreator.RDCF)2