use of org.openmrs.Role in project openmrs-core by openmrs.
the class AlertServiceImpl method notifySuperUsers.
/**
* @see org.openmrs.notification.AlertService#notifySuperUsers(String, Exception, Object...)
*/
@Override
public void notifySuperUsers(String messageCode, Exception cause, Object... messageArguments) {
// Generate an internationalized error message with the beginning of the stack trace from cause added onto the end
String message = Context.getMessageSourceService().getMessage(messageCode, messageArguments, Context.getLocale());
if (cause != null) {
StringBuilder stackTrace = new StringBuilder();
for (StackTraceElement traceElement : cause.getStackTrace()) {
stackTrace.append(traceElement);
stackTrace.append("\n");
if (stackTrace.length() >= Alert.TEXT_MAX_LENGTH) {
break;
}
}
message = message + ":" + stackTrace;
// limit message to Alert.TEXT_MAX_LENGTH
message = message.substring(0, Math.min(message.length(), Alert.TEXT_MAX_LENGTH));
}
// Send an alert to all administrators
Alert alert = new Alert(message, Context.getUserService().getUsersByRole(new Role(RoleConstants.SUPERUSER)));
// Set the alert so that if any administrator 'reads' it it will be marked as read for everyone who received it
alert.setSatisfiedByAny(true);
// TODO switch this to use the daemon user when ticket TRUNK-120 is complete
if (alert.getCreator() == null) {
alert.setCreator(new User(1));
}
// save the alert to send it to all administrators
Context.getAlertService().saveAlert(alert);
}
use of org.openmrs.Role in project openmrs-core by openmrs.
the class UserServiceTest method saveRole_shouldThrowErrorIfRoleInheritsFromItself.
/**
* @see UserService#saveRole(Role)
*/
@Test
public void saveRole_shouldThrowErrorIfRoleInheritsFromItself() {
Role parentRole = new Role("parent role");
// Have child inherit parent role
Role childRole = new Role("child role");
Set<Role> inheritsFromParent = new HashSet<>();
inheritsFromParent.add(parentRole);
childRole.setInheritedRoles(inheritsFromParent);
// Now have parent try to inherit the child role.
Set<Role> inheritsFromChild = new HashSet<>();
inheritsFromChild.add(childRole);
parentRole.setInheritedRoles(inheritsFromChild);
expectedException.expect(APIException.class);
expectedException.expectMessage(messages.getMessage("Role.cannot.inherit.descendant"));
userService.saveRole(parentRole);
}
use of org.openmrs.Role in project openmrs-core by openmrs.
the class UserServiceTest method purgeRole_shouldThrowErrorWhenRoleIsACoreRole.
/**
* @see UserService#purgeRole(Role)
*/
@Test
public void purgeRole_shouldThrowErrorWhenRoleIsACoreRole() {
Role role = new Role(RoleConstants.ANONYMOUS);
expectedException.expect(APIException.class);
userService.purgeRole(role);
}
use of org.openmrs.Role in project openmrs-core by openmrs.
the class UserServiceTest method getUsersByRole_shouldFetchUsersAssignedGivenRole.
/**
* @see UserService#getUsersByRole(Role)
*/
@Test
public void getUsersByRole_shouldFetchUsersAssignedGivenRole() {
executeDataSet(XML_FILENAME);
Assert.assertEquals(2, userService.getUsersByRole(new Role("Some Role")).size());
}
use of org.openmrs.Role in project openmrs-core by openmrs.
the class UserServiceTest method saveUser_shouldGrantNewRolesInRolesListToUser.
@Test
public void saveUser_shouldGrantNewRolesInRolesListToUser() {
// add in some basic properties
executeDataSet(XML_FILENAME);
User u = userService.getUserByUsername(ADMIN_USERNAME);
Role role1 = new Role();
role1.setDescription("testing1");
role1.setRole("test1");
Privilege p1 = userService.getAllPrivileges().get(0);
Set<Privilege> privileges1 = new HashSet<>();
privileges1.add(p1);
role1.setPrivileges(privileges1);
Role role2 = new Role();
role2.setDescription("testing2");
role2.setRole("test2");
Privilege p2 = userService.getAllPrivileges().get(0);
Set<Privilege> privileges2 = new HashSet<>();
privileges2.add(p2);
role2.setPrivileges(privileges2);
userService.saveUser(u.addRole(role1));
userService.saveUser(u.addRole(role2));
// so the contents are fetched from the db
Context.evictFromSession(u);
userService.getUser(u.getUserId()).hasRole("test1");
userService.getUser(u.getUserId()).hasRole("test2");
}
Aggregations