Search in sources :

Example 16 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class UserServiceImpl method getUsers.

/**
 * @see UserService#getUsers(String, List, boolean, Integer, Integer)
 */
@Override
@Transactional(readOnly = true)
public List<User> getUsers(String name, List<Role> roles, boolean includeRetired, Integer start, Integer length) throws APIException {
    if (name != null) {
        name = StringUtils.replace(name, ", ", " ");
    }
    if (roles == null) {
        roles = new ArrayList<>();
    }
    // if the authenticated role is in the list of searched roles, then all
    // persons should be searched
    Role authRole = getRole(RoleConstants.AUTHENTICATED);
    if (roles.contains(authRole)) {
        return dao.getUsers(name, new ArrayList<>(), includeRetired, start, length);
    }
    // add the requested roles and all child roles for consideration
    Set<Role> allRoles = new HashSet<>();
    for (Role r : roles) {
        allRoles.add(r);
        allRoles.addAll(r.getAllChildRoles());
    }
    return dao.getUsers(name, new ArrayList<>(allRoles), includeRetired, start, length);
}
Also used : Role(org.openmrs.Role) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class EncounterServiceTest method canEditAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToEditEncounters.

/**
 * @see EncounterService#canEditAllEncounterTypes(User)
 */
@Test
public void canEditAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToEditEncounters() {
    EncounterService encounterService = Context.getEncounterService();
    EncounterType encounterType = new EncounterType("testing", "desc");
    Privilege editPrivilege = Context.getUserService().getPrivilege("Some Privilege For Edit Encounter Types");
    encounterType.setEditPrivilege(editPrivilege);
    encounterService.saveEncounterType(encounterType);
    User user = Context.getUserService().getUserByUsername("test_user");
    assertNotNull(user);
    assertFalse(encounterService.canEditAllEncounterTypes(user));
    Role role = Context.getUserService().getRole("Provider");
    role.addPrivilege(editPrivilege);
    user.addRole(role);
    assertTrue(encounterService.canEditAllEncounterTypes(user));
}
Also used : Role(org.openmrs.Role) EncounterRole(org.openmrs.EncounterRole) User(org.openmrs.User) EncounterType(org.openmrs.EncounterType) Privilege(org.openmrs.Privilege) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 18 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class EncounterServiceTest method getEncounter_shouldReturnEncounterIfUserIsAllowedToViewIt.

/**
 * @see EncounterService#getEncounter(Integer)
 */
@Test
public void getEncounter_shouldReturnEncounterIfUserIsAllowedToViewIt() {
    // get encounter that has type with view privilege set
    Encounter encounter = getEncounterWithViewPrivilege();
    User user = Context.getUserService().getUserByUsername("test_user");
    assertNotNull(user);
    // add required privilege to role in which this user is
    Role role = Context.getUserService().getRole("Provider");
    role.addPrivilege(encounter.getEncounterType().getViewPrivilege());
    user.addRole(role);
    // and authenticate under it's account
    Context.becomeUser(user.getSystemId());
    // have to add privilege in order to be able to call getEncounter(Integer) method
    Context.addProxyPrivilege(PrivilegeConstants.GET_ENCOUNTERS);
    assertNotNull(Context.getEncounterService().getEncounter(encounter.getId()));
}
Also used : Role(org.openmrs.Role) EncounterRole(org.openmrs.EncounterRole) User(org.openmrs.User) Encounter(org.openmrs.Encounter) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 19 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class EncounterServiceTest method canEditEncounter_shouldReturnTrueIfUserCanEditEncounter.

/**
 * @see EncounterService#canEditEncounter(Encounter, User)
 */
@Test
public void canEditEncounter_shouldReturnTrueIfUserCanEditEncounter() {
    // get encounter that has type with edit privilege set
    Encounter encounter = getEncounterWithEditPrivilege();
    User user = Context.getUserService().getUserByUsername("test_user");
    assertNotNull(user);
    // add required privilege to role in which this user is
    Role role = Context.getUserService().getRole("Provider");
    role.addPrivilege(encounter.getEncounterType().getEditPrivilege());
    user.addRole(role);
    assertTrue(Context.getEncounterService().canEditEncounter(encounter, user));
}
Also used : Role(org.openmrs.Role) EncounterRole(org.openmrs.EncounterRole) User(org.openmrs.User) Encounter(org.openmrs.Encounter) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 20 with Role

use of org.openmrs.Role in project openmrs-core by openmrs.

the class EncounterServiceTest method canViewAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToViewEncounters.

/**
 * @see EncounterService#canViewAllEncounterTypes(User)
 */
@Test
public void canViewAllEncounterTypes_shouldReturnTrueIfUserIsGrantedToViewEncounters() {
    EncounterService encounterService = Context.getEncounterService();
    EncounterType encounterType = new EncounterType("testing", "desc");
    Privilege viewPrivilege = Context.getUserService().getPrivilege("Some Privilege For View Encounter Types");
    encounterType.setViewPrivilege(viewPrivilege);
    encounterService.saveEncounterType(encounterType);
    User user = Context.getUserService().getUserByUsername("test_user");
    assertNotNull(user);
    assertFalse(encounterService.canViewAllEncounterTypes(user));
    Role role = Context.getUserService().getRole("Provider");
    role.addPrivilege(viewPrivilege);
    user.addRole(role);
    assertTrue(encounterService.canViewAllEncounterTypes(user));
}
Also used : Role(org.openmrs.Role) EncounterRole(org.openmrs.EncounterRole) User(org.openmrs.User) EncounterType(org.openmrs.EncounterType) Privilege(org.openmrs.Privilege) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Aggregations

Role (org.openmrs.Role)42 Test (org.junit.Test)27 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)26 User (org.openmrs.User)15 BindException (org.springframework.validation.BindException)8 Errors (org.springframework.validation.Errors)8 Privilege (org.openmrs.Privilege)7 EncounterRole (org.openmrs.EncounterRole)6 HashSet (java.util.HashSet)5 Person (org.openmrs.Person)5 PersonName (org.openmrs.PersonName)4 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)3 Encounter (org.openmrs.Encounter)3 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 EncounterType (org.openmrs.EncounterType)2 RelationshipType (org.openmrs.RelationshipType)2