2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sparky.security.portal;
24 import java.io.IOException;
25 import java.lang.reflect.Type;
26 import java.nio.file.Files;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.concurrent.locks.Lock;
31 import java.util.concurrent.locks.ReadWriteLock;
32 import java.util.concurrent.locks.ReentrantReadWriteLock;
33 import java.util.stream.Collectors;
35 import org.onap.aai.sparky.security.portal.config.RolesConfig;
36 import org.onap.portalsdk.core.restful.domain.EcompRole;
37 import org.onap.portalsdk.core.restful.domain.EcompUser;
39 import com.google.gson.Gson;
40 import com.google.gson.GsonBuilder;
41 import com.google.gson.reflect.TypeToken;
44 * Basic file based user storage.
46 public class UserManager {
48 private File usersFile;
50 private static final ReadWriteLock LOCK = new ReentrantReadWriteLock(true);
51 private static final Lock READ_LOCK = LOCK.readLock();
52 private static final Lock WRITE_LOCK = LOCK.writeLock();
54 private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
58 * @param usersFile a file to store the users
60 public UserManager(File usersFile) {
61 this.usersFile = usersFile;
65 * Returns all users stored.
67 * @return a list of users.
69 public List<EcompUser> getUsers() {
70 Type collectionType = new TypeToken<List<EcompUser>>() {
73 Optional<String> users = read(usersFile);
74 if (users.isPresent()) {
75 return GSON.fromJson(users.get(), collectionType);
78 return new ArrayList<>();
82 * Returns a stored user.
84 * @param loginId the identifier of the user
85 * @return an optional user.
87 public Optional<EcompUser> getUser(String loginId) {
88 if (!getUsers().isEmpty()) {
89 return getUsers().stream().filter(u -> loginId.equals(u.getLoginId())).findFirst();
91 return Optional.empty();
95 * Stores a user if not already stored.
97 * @param user the user to be stored
100 public void pushUser(EcompUser user) throws IOException {
103 if (!getUser(user.getLoginId()).isPresent()) {
104 addUser(getUsers(), user);
112 * Replaces an existing user.
114 * @param loginId the id of the user
115 * @param user the new user details
116 * @throws IOException
118 public void editUser(String loginId, EcompUser user) throws IOException {
121 if (getUser(loginId).isPresent()) {
122 List<EcompUser> users = getUsers().stream().filter(u -> !u.getLoginId().equals(loginId))
123 .collect(Collectors.toList());
124 addUser(users, user);
132 * Gets the roles assigned to a user.
134 * @param loginId the id of the user
135 * @return the assigned roles
137 public List<EcompRole> getUserRoles(String loginId) {
138 List<EcompRole> roles = new ArrayList<>();
139 roles.addAll(getUser(loginId).orElseGet(EcompUser::new).getRoles());
143 public static List<EcompRole> getRoles() {
144 return RolesConfig.getInstance().getRoles();
147 private void addUser(List<EcompUser> users, EcompUser user) throws IOException {
152 private void write(List<EcompUser> users) throws IOException {
153 Files.write(usersFile.toPath(), GSON.toJson(users).getBytes());
156 private Optional<String> read(File file) {
159 return Optional.of(new String(Files.readAllBytes(file.toPath())));
160 } catch (IOException e) { // NOSONAR
161 return Optional.empty();