Search in sources :

Example 31 with Role

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);
}
Also used : Role(org.openmrs.Role) User(org.openmrs.User) Alert(org.openmrs.notification.Alert)

Example 32 with Role

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);
}
Also used : Role(org.openmrs.Role) HashSet(java.util.HashSet) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 33 with Role

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);
}
Also used : Role(org.openmrs.Role) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 34 with 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());
}
Also used : Role(org.openmrs.Role) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 35 with Role

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");
}
Also used : Role(org.openmrs.Role) User(org.openmrs.User) Privilege(org.openmrs.Privilege) HashSet(java.util.HashSet) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

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