X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-backend%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fric%2Fportal%2Fdashboard%2FDashboardUserManager.java;h=6358256f4b8a355f33d78462fd7e81e81711796f;hb=662cc613e80c4079fdf5a54ce3135652cb9ad9a1;hp=5561c0e0caac7a5f2a9d33305fa35e3efb01601e;hpb=50fb3b40ec45a65ba7c687e290a3d7f491484b49;p=portal%2Fric-dashboard.git diff --git a/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/DashboardUserManager.java b/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/DashboardUserManager.java index 5561c0e0..6358256f 100644 --- a/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/DashboardUserManager.java +++ b/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/DashboardUserManager.java @@ -22,20 +22,16 @@ package org.oransc.ric.portal.dashboard; import java.io.File; import java.io.IOException; import java.lang.invoke.MethodHandles; +import java.nio.file.Files; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; -import org.onap.portalsdk.core.restful.domain.EcompRole; import org.onap.portalsdk.core.restful.domain.EcompUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; /** @@ -43,19 +39,20 @@ import com.fasterxml.jackson.databind.ObjectMapper; * * This first implementation serializes user details to a file. * - * TODO: migrate to a database. + * Migrate to a database someday? */ public class DashboardUserManager { private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - public static final String USER_FILE_PATH = "/tmp/dashboard-users.json"; + // This default value is only useful for development and testing. + public static final String USER_FILE_PATH = "dashboard-users.json"; private final File userFile; private final List users; /** - * convenience constructor that uses default file path + * Development/test-only constructor that uses default file path. * * @param clear * If true, start empty and remove any existing file. @@ -69,13 +66,13 @@ public class DashboardUserManager { logger.debug("ctor: removing file {}", userFile.getAbsolutePath()); File f = new File(DashboardUserManager.USER_FILE_PATH); if (f.exists()) - f.delete(); + Files.delete(f.toPath()); users.clear(); } } /** - * Uses specified file path + * Constructur that accepts a file path * * @param userFilePath * File path @@ -124,7 +121,7 @@ public class DashboardUserManager { return null; } - private void saveUsers() throws JsonGenerationException, JsonMappingException, IOException { + private void saveUsers() throws IOException { final ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(userFile, users); } @@ -133,7 +130,7 @@ public class DashboardUserManager { * Allow at most one thread to create a user at one time. */ public synchronized void createUser(EcompUser user) throws PortalAPIException { - logger.debug("createUser: loginId is " + user.getLoginId()); + logger.debug("createUser: loginId {}", user.getLoginId()); if (users.contains(user)) throw new PortalAPIException("User exists: " + user.getLoginId()); users.add(user); @@ -149,7 +146,7 @@ public class DashboardUserManager { * last-edit-wins of course. */ public synchronized void updateUser(String loginId, EcompUser user) throws PortalAPIException { - logger.debug("editUser: loginId is " + loginId); + logger.debug("editUser: loginId {}", loginId); int index = users.indexOf(user); if (index < 0) throw new PortalAPIException("User does not exist: " + user.getLoginId()); @@ -162,22 +159,4 @@ public class DashboardUserManager { } } - // Test infrastructure - public static void main(String[] args) throws Exception { - DashboardUserManager dum = new DashboardUserManager(false); - EcompUser user = new EcompUser(); - user.setActive(true); - user.setLoginId("demo"); - user.setFirstName("First"); - user.setLastName("Last"); - EcompRole role = new EcompRole(); - role.setId(1L); - role.setName(DashboardConstants.ROLE_NAME_ADMIN); - Set roles = new HashSet<>(); - roles.add(role); - user.setRoles(roles); - dum.createUser(user); - logger.debug("Created user {}", user); - } - }