Time for action – working with users

To access user-related information, we have to use an identity-related URL, that is, /identity/users. Now, we will use the identity service. Perform the following steps to archive the identity service:

  1. Add a user to the Activiti Engine using the createUser method in the Process_Deployment class through which we will pass the details of the user as follows:
    public static JSONObject createUser() throws JSONException, IOException {
        String uri = REST_URI + "/identity/users";
        JSONObject my_data = new JSONObject();
        my_data.put("id", "irshad");
        my_data.put("firstName", "Irshad");
        my_data.put("lastName", "Mansuri");
        my_data.put("email", "[email protected]");
        my_data.put("password", "irshad");
        Representation response = getClientResource(uri).post(my_data);
        JSONObject object = new JSONObject(response.getText());
        if (object != null) {
          return object;
        }
        return null;
      }
  2. We can create a user using the REST API and also update and delete them with it.
  3. To update a user, we can use the same method as we did for creating a user, except that we have to provide a user id for the update. The URL for the update is /identity/users/{userId}, as shown in the following code:
    public static JSONObject updateUser(String id) throws JSONException, IOException {
        String uri = REST_URI + "/identity/users/" + id;
        JSONObject my_data = new JSONObject();
        my_data.put("email", "[email protected]");
        Representation response = getClientResource(uri).put(my_data);
        JSONObject object = new JSONObject(response.getText());
        if (object != null) {
          return object;
        }
        return null;
    }
  4. To delete a user from the repository, we have to use the deleteUser method. We will add the following method to archive the delete functionality:
    public static void deleteUser(String id) throws JSONException, IOException {
        String uri = REST_URI + "/identity/users/" + id;
                 Representation response = getClientResource(uri).delete(MediaType.ALL);
    }
  5. With the help of the identity service, we can not only access the users but also create and update groups. For groups, we have to provide the URL /identity/groups.
  6. Now we will use the createGroup method to create a group in the repository, which will contain information regarding the group, as shown in the following code:
      public static JSONObject createGroup() throws JSONException, IOException {
        String uri = REST_URI + "/identity/groups";
    
        JSONObject my_data = new JSONObject();
        my_data.put("id", "attunegroup");
        my_data.put("name", "Attune Group");
        my_data.put("type", "security-role");
        Representation response = getClientResource(uri).post(my_data);
        JSONObject object = new JSONObject(response.getText());
        if (object != null) {
          return object;
        }
        return null;
      }
  7. As we know how to update a user using identity services, we can also update a group.
  8. Once we have created a group, we need to add some users to it. For that, we need to create the addGroupMember method where we will provide the group ID as the parameter in which we want to add users, as follows:
      public static JSONObject addGroupMember(String id) throws JSONException, IOException {
        String uri = REST_URI + "/identity/groups/" + id + "/members";
    
        JSONObject my_data = new JSONObject();
        my_data.put("userId", "irshad");
    
        Representation response = getClientResource(uri).post(my_data);
        JSONObject object = new JSONObject(response.getText());
        if (object != null) {
          return object;
        }
        return null;
      }
  9. Just as we can add a member to a group, we can also delete them from it. For that, we need to provide the groupID as well as userId values that we want to delete, as shown in the following code:
      public static void deleteGroupMember(String groupId, String userId) throws JSONException, IOException {
        String uri = REST_URI + "/identity/groups/" + groupId + "/members/"  + userId;
        Representation response = getClientResource(uri).delete(MediaType.ALL);
      }

What just happened?

By the end of this section, you should be able to list, add, and manage users and groups in the repository.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset