Implementing API to allow Admins to modify config of devices of any user
As any user can add or remove devices from their account, there needed to be a way by which Admins can manage the user devices. The Admins and higher user roles should have the access to modify the config of devices of any user. This blog post explains how an API has been implemented to facilitate Admins and higher user roles to change config of devices of any user.
Implementing a servlet to allow changing review status of a Skill
The basic task of the servlet is to allow Admin and higher user roles to modify the config of devices of any user. The Admin should be allowed to edit the name of the device and also the room of the device, similar to how a user can edit his own devices.
Here is the implementation of the API:
- The API should be usable to only the users who have a user role Admin or higher. Only those with minimum Admin rights should be allowed to control what Skills are displayed on the CMS site. This is implemented as follows:
@Override public UserRole getMinimalUserRole() { return UserRole.ADMIN; }
- The endpoint for the API is ‘/cms/modifyUserDevices.json’. This is implemented as follows:
@Override public String getAPIPath() { return "/cms/modifyUserDevices.json"; }
- The main method of the servlet is the serviceImpl() method. This is where the actual code goes which will be executed each time the API is called. This is implemented as follows:
JSONObject result = new JSONObject(true); Collection<ClientIdentity> authorized = DAO.getAuthorizedClients(); List<String> keysList = new ArrayList<String>(); authorized.forEach(client -> keysList.add(client.toString())); String[] keysArray = keysList.toArray(new String[keysList.size()]); List<JSONObject> userList = new ArrayList<JSONObject>(); for (Client client : authorized) { JSONObject json = client.toJSON(); if(json.get("name").equals(email)) { ClientIdentity identity = new ClientIdentity(ClientIdentity.Type.email, client.getName()); Authorization authorization = DAO.getAuthorization(identity); ClientCredential clientCredential = new ClientCredential(ClientCredential.Type.passwd_login, identity.getName()); Authentication authentication = DAO.getAuthentication(clientCredential); Accounting accounting = DAO.getAccounting(authorization.getIdentity()); if(accounting.getJSON().has("devices")) { JSONObject userDevice = accounting.getJSON().getJSONObject("devices"); if(userDevice.has(macid)) { JSONObject deviceInfo = userDevice.getJSONObject(macid); deviceInfo.put("name", name); deviceInfo.put("room", room); } else { throw new APIException(400, "Specified device does not exist."); } } else { json.put("devices", ""); } accounting.commit(); } }
Firstly, the list of authorized clients is fetched using DAO.getAuthorizedClients() and is put in an ArrayList. Then we traverse through each element of this ArrayList and check if the device exists by checking if there’s a key-value pair corresponding to the macid passed in the query parameter. If the device doesn’t exist, then an exception is thrown. However, if the macid exists in the traversed element of the ArrayList, then we put the name and the room of the device as passed as query parameters in that particular element of the ArrayList, so as to overwrite the existing name and room of the device of the user.
This is how an API has been implemented which allows Admins and higher user roles to modify the config of devices of any user.
Resources
- Tutorialspoint: JSON in Java
- Stackoverflow:
You must be logged in to post a comment.