Search in sources :

Example 1 with GeoprismUserDTO

use of net.geoprism.GeoprismUserDTO in project geoprism-registry by terraframe.

the class RegistryAccountController method newInstance.

/**
 * Returns all roles associated with the given {@link OrganizationDTO} codes.
 * If no codes are provided then return all roles defined in the registry.
 *
 * @param request
 * @param organizationCodes
 *          comma separated list of {@link OrganizationDTO} codes.
 * @return
 * @throws JSONException
 */
@Endpoint(method = ServletMethod.POST, error = ErrorSerialization.JSON)
public ResponseIF newInstance(ClientRequestIF request, @RequestParamter(name = "organizationCodes") String organizationCodes) throws JSONException {
    String[] orgCodeArray = null;
    if (organizationCodes != null) {
        JSONArray arr = new JSONArray(organizationCodes);
        orgCodeArray = new String[arr.length()];
        for (int i = 0; i < arr.length(); i++) {
            orgCodeArray[i] = arr.getString(i);
        }
    } else {
        orgCodeArray = new String[0];
    }
    GeoprismUserDTO user = UserInviteDTO.newUserInst(request);
    RegistryRole[] registryRoles = this.accountService.getRolesForOrganization(request.getSessionId(), orgCodeArray);
    JsonArray rolesJSONArray = this.createRoleMap(registryRoles);
    RestResponse response = new RestResponse();
    response.set("user", user);
    response.set("roles", new JSONArray(rolesJSONArray.toString()));
    return response;
}
Also used : JsonArray(com.google.gson.JsonArray) RegistryRole(org.commongeoregistry.adapter.metadata.RegistryRole) RestResponse(com.runwaysdk.mvc.RestResponse) JSONArray(org.json.JSONArray) Endpoint(com.runwaysdk.mvc.Endpoint) GeoprismUserDTO(net.geoprism.GeoprismUserDTO) Endpoint(com.runwaysdk.mvc.Endpoint)

Example 2 with GeoprismUserDTO

use of net.geoprism.GeoprismUserDTO in project geoprism-registry by terraframe.

the class AccountServiceControllerTest method createAndApplyUserWithOrgRoles.

/**
 * Test returning possible roles that can be assigned to a person for a given
 * organization.
 */
@Test
@SuppressWarnings("rawtypes")
public void createAndApplyUserWithOrgRoles() {
    // New Instance
    RestResponse response = (RestResponse) controller.newInstance(clientRequest, "[" + moiOrg.getCode() + "]");
    Pair userPair = (Pair) response.getAttribute("user");
    GeoprismUserDTO user = (GeoprismUserDTO) userPair.getFirst();
    Pair rolesPair = (Pair) response.getAttribute("roles");
    JSONArray roleJSONArray = (JSONArray) rolesPair.getFirst();
    JSONObject jsonUser = new JSONObject();
    jsonUser.put(GeoprismUserDTO.FIRSTNAME, "John");
    jsonUser.put(GeoprismUserDTO.LASTNAME, "Doe");
    jsonUser.put(GeoprismUserDTO.USERNAME, "jdoe6");
    jsonUser.put(GeoprismUserDTO.EMAIL, "john6@doe.com");
    jsonUser.put(GeoprismUserDTO.PASSWORD, "123456");
    // user.setFirstName("John");
    // user.setLastName("Doe");
    // user.setUsername("jdoe6");
    // user.setEmail("john6doe.com");
    // user.setPassword("123456");
    String rmDistrictRole = RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), district.getCode());
    String rmVillageRole = RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), village.getCode());
    // Apply
    response = (RestResponse) controller.apply(clientRequest, jsonUser.toString(), "[" + rmDistrictRole + "," + rmVillageRole + "]");
    userPair = (Pair) response.getAttribute("user");
    jsonUser = (JSONObject) userPair.getFirst();
    // try
    // {
    Pair rolePair = (Pair) response.getAttribute("roles");
    roleJSONArray = (JSONArray) rolePair.getFirst();
    Assert.assertEquals(8, roleJSONArray.length());
    Set<String> rolesFoundSet = new HashSet<String>();
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), village.getCode()));
    this.assertReturnedRoles(roleJSONArray, rolesFoundSet, true);
    rolesFoundSet.add(RegistryRole.Type.getRA_RoleName(moiOrg.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRC_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getAC_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), village.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRC_RoleName(moiOrg.getCode(), village.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getAC_RoleName(moiOrg.getCode(), village.getCode()));
    this.assertReturnedRoles(roleJSONArray, rolesFoundSet, false);
    // Edit
    response = (RestResponse) controller.edit(clientRequest, jsonUser.getString("oid"));
    userPair = (Pair) response.getAttribute("user");
    jsonUser = (JSONObject) userPair.getFirst();
    rolesPair = (Pair) response.getAttribute("roles");
    roleJSONArray = (JSONArray) rolesPair.getFirst();
    Assert.assertEquals(8, roleJSONArray.length());
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), village.getCode()));
    this.assertReturnedRoles(roleJSONArray, rolesFoundSet, true);
    rolesFoundSet.add(RegistryRole.Type.getRA_RoleName(moiOrg.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRC_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getAC_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), village.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRC_RoleName(moiOrg.getCode(), village.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getAC_RoleName(moiOrg.getCode(), village.getCode()));
    this.assertReturnedRoles(roleJSONArray, rolesFoundSet, false);
    // Apply to change and roles
    // user.put(GeoprismUserDTO.LASTNAME, "Dwayne");
    user.setLastName("Dwayne");
    String rcVillageRole = RegistryRole.Type.getRC_RoleName(moiOrg.getCode(), village.getCode());
    response = (RestResponse) controller.apply(clientRequest, jsonUser.toString(), "[" + rmDistrictRole + "," + rcVillageRole + "]");
    userPair = (Pair) response.getAttribute("user");
    jsonUser = (JSONObject) userPair.getFirst();
    Assert.assertEquals("Dwayne", user.getLastName());
    rolePair = (Pair) response.getAttribute("roles");
    roleJSONArray = (JSONArray) rolePair.getFirst();
    Assert.assertEquals(8, roleJSONArray.length());
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRC_RoleName(moiOrg.getCode(), village.getCode()));
    this.assertReturnedRoles(roleJSONArray, rolesFoundSet, true);
    rolesFoundSet = new HashSet<String>();
    rolesFoundSet.add(RegistryRole.Type.getRA_RoleName(moiOrg.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRC_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getAC_RoleName(moiOrg.getCode(), district.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRM_RoleName(moiOrg.getCode(), village.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getRC_RoleName(moiOrg.getCode(), village.getCode()));
    rolesFoundSet.add(RegistryRole.Type.getAC_RoleName(moiOrg.getCode(), village.getCode()));
    this.assertReturnedRoles(roleJSONArray, rolesFoundSet, false);
// }
// finally
// {
// controller.remove(clientRequest, user.getOid());
// }
}
Also used : JSONObject(org.json.JSONObject) RestResponse(com.runwaysdk.mvc.RestResponse) JSONArray(org.json.JSONArray) Pair(com.runwaysdk.Pair) GeoprismUserDTO(net.geoprism.GeoprismUserDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with GeoprismUserDTO

use of net.geoprism.GeoprismUserDTO in project geoprism-registry by terraframe.

the class AccountServiceControllerTest method createUser.

private JSONObject createUser(String userName, String roleNames) {
    RestResponse response = (RestResponse) controller.newInstance(clientRequest, "[" + moiOrg.getCode() + "]");
    Pair userPair = (Pair) response.getAttribute("user");
    GeoprismUserDTO user = (GeoprismUserDTO) userPair.getFirst();
    Pair rolesPair = (Pair) response.getAttribute("roles");
    JSONArray roleJSONArray = (JSONArray) rolesPair.getFirst();
    JSONObject jsonUser = new JSONObject();
    jsonUser.put(GeoprismUserDTO.FIRSTNAME, "Some Firstname");
    jsonUser.put(GeoprismUserDTO.LASTNAME, "Some Lastame");
    jsonUser.put(GeoprismUserDTO.USERNAME, userName);
    jsonUser.put(GeoprismUserDTO.EMAIL, userName);
    jsonUser.put(GeoprismUserDTO.PASSWORD, "123456");
    response = (RestResponse) controller.apply(clientRequest, jsonUser.toString(), "[" + roleNames + "]");
    userPair = (Pair) response.getAttribute("user");
    jsonUser = (JSONObject) userPair.getFirst();
    return jsonUser;
}
Also used : JSONObject(org.json.JSONObject) RestResponse(com.runwaysdk.mvc.RestResponse) JSONArray(org.json.JSONArray) Pair(com.runwaysdk.Pair) GeoprismUserDTO(net.geoprism.GeoprismUserDTO)

Aggregations

RestResponse (com.runwaysdk.mvc.RestResponse)3 GeoprismUserDTO (net.geoprism.GeoprismUserDTO)3 JSONArray (org.json.JSONArray)3 Pair (com.runwaysdk.Pair)2 JSONObject (org.json.JSONObject)2 JsonArray (com.google.gson.JsonArray)1 Endpoint (com.runwaysdk.mvc.Endpoint)1 HashSet (java.util.HashSet)1 RegistryRole (org.commongeoregistry.adapter.metadata.RegistryRole)1 Test (org.junit.Test)1