Search in sources :

Example 1 with Role

use of org.openmrs.Role in project openmrs-module-pihcore by PIH.

the class UserSetup method registerUser.

public static User registerUser(String username, String firstname, String lastName, String gender, String locale, String passwordHash, String salt, String email, String roles, String providerRole) {
    User user = Context.getUserService().getUserByUsername(username);
    if (user == null) {
        user = new User();
        user.setUsername(username);
        user.setEmail(email);
        Person person = new Person();
        person.addName(new PersonName(firstname, null, lastName));
        person.setGender(gender);
        user.setPerson(person);
        for (String role : roles.split("\\|")) {
            Role r = Context.getUserService().getRole(role);
            if (r == null) {
                throw new IllegalArgumentException("Unable to find role: " + role);
            }
            user.addRole(r);
        }
        Context.getUserService().createUser(user, UUID.randomUUID().toString());
        if (StringUtils.isNotBlank(passwordHash) && StringUtils.isNotBlank(salt)) {
            Context.getAdministrationService().executeSQL("update users set password = '" + passwordHash + "', salt='" + salt + "' where username = '" + username + "'", false);
        }
        PihAccountDomainWrapper account = Context.getService(PihCoreService.class).newPihAccountDomainWrapper(person);
        account.setDefaultLocale(LocaleUtility.fromSpecification(locale));
        for (ProviderRole pr : Context.getService(ProviderManagementService.class).getAllProviderRoles(false)) {
            if (pr.getName().equalsIgnoreCase(providerRole)) {
                account.setProviderRole(pr);
            }
        }
        account.save();
    }
    return user;
}
Also used : Role(org.openmrs.Role) ProviderRole(org.openmrs.module.providermanagement.ProviderRole) PihCoreService(org.openmrs.module.pihcore.service.PihCoreService) PersonName(org.openmrs.PersonName) User(org.openmrs.User) ProviderManagementService(org.openmrs.module.providermanagement.api.ProviderManagementService) ProviderRole(org.openmrs.module.providermanagement.ProviderRole) PihAccountDomainWrapper(org.openmrs.module.pihcore.account.PihAccountDomainWrapper) Person(org.openmrs.Person)

Example 2 with Role

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

the class UserServiceTest method getRoleByUuid_shouldFindObjectGivenValidUuid.

/**
 * @see UserService#getRoleByUuid(String)
 */
@Test
public void getRoleByUuid_shouldFindObjectGivenValidUuid() {
    String uuid = "3480cb6d-c291-46c8-8d3a-96dc33d199fb";
    Role role = userService.getRoleByUuid(uuid);
    Assert.assertEquals("Provider", role.getRole());
}
Also used : Role(org.openmrs.Role) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 3 with Role

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

the class CreateCoreUuids method getUUIDs.

// @Test
@SkipBaseSetup
public void getUUIDs() throws Exception {
    Context.authenticate("admin", "test");
    System.out.println("db: " + OpenmrsConstants.DATABASE_NAME);
    Map<String, List<? extends OpenmrsMetadata>> coremetadatas = new LinkedHashMap<>();
    coremetadatas.put("field_type", Context.getFormService().getAllFieldTypes(true));
    coremetadatas.put("person_attribute_type", Context.getPersonService().getAllPersonAttributeTypes(true));
    coremetadatas.put("encounter_type", Context.getEncounterService().getAllEncounterTypes(true));
    coremetadatas.put("concept_datatype", Context.getConceptService().getAllConceptDatatypes(true));
    coremetadatas.put("concept_class", Context.getConceptService().getAllConceptClasses(true));
    coremetadatas.put("patient_identifier_type", Context.getPatientService().getAllPatientIdentifierTypes(true));
    coremetadatas.put("location", Context.getLocationService().getAllLocations(true));
    coremetadatas.put("hl7_source", Context.getHL7Service().getAllHL7Sources());
    for (Map.Entry<String, List<? extends OpenmrsMetadata>> entry : coremetadatas.entrySet()) {
        System.out.println("new table: " + entry.getKey());
        for (OpenmrsMetadata obj : entry.getValue()) {
            String output = "<update tableName=\"" + entry.getKey() + "\"><column name=\"uuid\" value=\"" + obj.getUuid() + "\"/><where>" + entry.getKey() + "_id" + "= '" + obj.getId() + "' and name = '" + obj.getName().replace("'", "\\'") + "'</where></update>";
            System.out.println(output);
        }
    }
    // /////////////////////////////////////
    // exceptions:
    // 
    // relationship types
    System.out.println("Relationship type");
    for (RelationshipType type : Context.getPersonService().getAllRelationshipTypes()) {
        String output = "<update tableName=\"relationship_type\"><column name=\"uuid\" value=\"" + type.getUuid() + "\"/><where> relationship_type_id = '" + type.getRelationshipTypeId() + "' and a_is_to_b = '" + type.getaIsToB().replace("'", "\\'") + "' and b_is_to_a = '" + type.getbIsToA().replace("'", "\\'") + "'</where></update>";
        System.out.println(output);
    }
    // roles:
    System.out.println("Roles");
    for (Role role : Context.getUserService().getAllRoles()) {
        String output = "<update tableName=\"role\"><column name=\"uuid\" value=\"" + role.getUuid() + "\"/><where> role = '" + role.getRole().replace("'", "\\'") + "'</where></update>";
        System.out.println(output);
    }
    // user:
    System.out.println("Users");
    for (User user : Context.getUserService().getAllUsers()) {
        String output = "<update tableName=\"users\"><column name=\"uuid\" value=\"" + user.getUuid() + "\"/><where> user_id = '" + user.getUserId() + "' and system_id = '" + user.getSystemId().replace("'", "\\'") + "'</where></update>";
        System.out.println(output);
    }
}
Also used : Role(org.openmrs.Role) User(org.openmrs.User) RelationshipType(org.openmrs.RelationshipType) List(java.util.List) OpenmrsMetadata(org.openmrs.OpenmrsMetadata) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Role

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

the class RoleValidatorTest method validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues.

/**
 * @see RoleValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfAllRequiredFieldsHaveProperValues() {
    Role role = new Role();
    role.setRole("Bowling race car driver");
    role.setDescription("You don't bowl or race fast cars");
    Errors errors = new BindException(role, "type");
    new RoleValidator().validate(role, errors);
    Assert.assertFalse(errors.hasErrors());
}
Also used : Role(org.openmrs.Role) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 5 with Role

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

the class RoleValidatorTest method validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace.

/**
 * @see RoleValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldPassValidationIfDescriptionIsNullOrEmptyOrWhitespace() {
    Role role = new Role();
    role.setRole("Bowling race car driver");
    role.setDescription(null);
    Errors errors = new BindException(role, "type");
    new RoleValidator().validate(role, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
    role.setDescription("");
    errors = new BindException(role, "role");
    new RoleValidator().validate(role, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
    role.setDescription(" ");
    errors = new BindException(role, "role");
    new RoleValidator().validate(role, errors);
    Assert.assertFalse(errors.hasFieldErrors("description"));
}
Also used : Role(org.openmrs.Role) Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) 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