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