X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dashboard%2Fwebapp-backend%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fric%2Fportal%2Fdashboard%2FDashboardUserManager.java;h=ffb7396fdedfba49035cab2cee7648362c348aa8;hb=592ce20ec359928373de2e7f06214c8f8ad73c20;hp=c5fd10140fa7ac1d682d5b2f76af855a65c5d27a;hpb=7a4a590fb0ebf8772169625cdda327da43c79c6d;p=nonrtric.git diff --git a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/DashboardUserManager.java b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/DashboardUserManager.java index c5fd1014..ffb7396f 100644 --- a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/DashboardUserManager.java +++ b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/DashboardUserManager.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,6 +19,11 @@ */ package org.oransc.ric.portal.dashboard; +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; + import java.io.File; import java.io.IOException; import java.lang.invoke.MethodHandles; @@ -33,152 +38,146 @@ 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; - /** * Provides simple user-management services. - * + * * This first implementation serializes user details to a file. - * + * * TODO: migrate to a database. */ public class DashboardUserManager { - private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - - // 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; - - /** - * Development/test-only constructor that uses default file path. - * - * @param clear - * If true, start empty and remove any existing file. - * - * @throws IOException - * On file error - */ - public DashboardUserManager(boolean clear) throws IOException { - this(USER_FILE_PATH); - if (clear) { - logger.debug("ctor: removing file {}", userFile.getAbsolutePath()); - File f = new File(DashboardUserManager.USER_FILE_PATH); - if (f.exists()) - f.delete(); - users.clear(); - } - } - - /** - * Constructur that accepts a file path - * - * @param userFilePath - * File path - * @throws IOException - * If file cannot be read - */ - public DashboardUserManager(final String userFilePath) throws IOException { - logger.debug("ctor: userfile {}", userFilePath); - if (userFilePath == null) - throw new IllegalArgumentException("Missing or empty user file property"); - userFile = new File(userFilePath); - logger.debug("ctor: managing users in file {}", userFile.getAbsolutePath()); - if (userFile.exists()) { - final ObjectMapper mapper = new ObjectMapper(); - users = mapper.readValue(userFile, new TypeReference>() { - }); - } else { - users = new ArrayList<>(); - } - } - - /** - * Gets the current users. - * - * @return List of EcompUser objects, possibly empty - */ - public List getUsers() { - return this.users; - } - - /** - * Gets the user with the specified login Id - * - * @param loginId - * Desired login Id - * @return User object; null if Id is not known - */ - public EcompUser getUser(String loginId) { - for (EcompUser u : this.users) { - if (u.getLoginId().equals(loginId)) { - logger.debug("getUser: match on {}", loginId); - return u; - } - } - logger.debug("getUser: no match on {}", loginId); - return null; - } - - private void saveUsers() throws JsonGenerationException, JsonMappingException, IOException { - final ObjectMapper mapper = new ObjectMapper(); - mapper.writeValue(userFile, users); - } - - /* - * 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()); - if (users.contains(user)) - throw new PortalAPIException("User exists: " + user.getLoginId()); - users.add(user); - try { - saveUsers(); - } catch (Exception ex) { - throw new PortalAPIException("Save failed", ex); - } - } - - /* - * Allow at most one thread to modify a user at one time. We still have - * last-edit-wins of course. - */ - public synchronized void updateUser(String loginId, EcompUser user) throws PortalAPIException { - logger.debug("editUser: loginId is " + loginId); - int index = users.indexOf(user); - if (index < 0) - throw new PortalAPIException("User does not exist: " + user.getLoginId()); - users.remove(index); - users.add(user); - try { - saveUsers(); - } catch (Exception ex) { - throw new PortalAPIException("Save failed", ex); - } - } - - // 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); - } + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + // 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; + + /** + * Development/test-only constructor that uses default file path. + * + * @param clear + * If true, start empty and remove any existing file. + * + * @throws IOException + * On file error + */ + public DashboardUserManager(boolean clear) throws IOException { + this(USER_FILE_PATH); + if (clear) { + logger.debug("ctor: removing file {}", userFile.getAbsolutePath()); + File f = new File(DashboardUserManager.USER_FILE_PATH); + if (f.exists()) + f.delete(); + users.clear(); + } + } + + /** + * Constructur that accepts a file path + * + * @param userFilePath + * File path + * @throws IOException + * If file cannot be read + */ + public DashboardUserManager(final String userFilePath) throws IOException { + logger.debug("ctor: userfile {}", userFilePath); + if (userFilePath == null) + throw new IllegalArgumentException("Missing or empty user file property"); + userFile = new File(userFilePath); + logger.debug("ctor: managing users in file {}", userFile.getAbsolutePath()); + if (userFile.exists()) { + final ObjectMapper mapper = new ObjectMapper(); + users = mapper.readValue(userFile, new TypeReference>() {}); + } else { + users = new ArrayList<>(); + } + } + + /** + * Gets the current users. + * + * @return List of EcompUser objects, possibly empty + */ + public List getUsers() { + return this.users; + } + + /** + * Gets the user with the specified login Id + * + * @param loginId + * Desired login Id + * @return User object; null if Id is not known + */ + public EcompUser getUser(String loginId) { + for (EcompUser u : this.users) { + if (u.getLoginId().equals(loginId)) { + logger.debug("getUser: match on {}", loginId); + return u; + } + } + logger.debug("getUser: no match on {}", loginId); + return null; + } + + private void saveUsers() throws JsonGenerationException, JsonMappingException, IOException { + final ObjectMapper mapper = new ObjectMapper(); + mapper.writeValue(userFile, users); + } + + /* + * 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()); + if (users.contains(user)) + throw new PortalAPIException("User exists: " + user.getLoginId()); + users.add(user); + try { + saveUsers(); + } catch (Exception ex) { + throw new PortalAPIException("Save failed", ex); + } + } + + /* + * Allow at most one thread to modify a user at one time. We still have + * last-edit-wins of course. + */ + public synchronized void updateUser(String loginId, EcompUser user) throws PortalAPIException { + logger.debug("editUser: loginId is " + loginId); + int index = users.indexOf(user); + if (index < 0) + throw new PortalAPIException("User does not exist: " + user.getLoginId()); + users.remove(index); + users.add(user); + try { + saveUsers(); + } catch (Exception ex) { + throw new PortalAPIException("Save failed", ex); + } + } + + // 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); + } }