2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sparky.security.portal;
26 import java.io.IOException;
27 import java.lang.reflect.Type;
28 import java.nio.file.Files;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.concurrent.locks.Lock;
33 import java.util.concurrent.locks.ReadWriteLock;
34 import java.util.concurrent.locks.ReentrantReadWriteLock;
35 import java.util.stream.Collectors;
37 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
38 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
39 import org.openecomp.sparky.security.portal.config.RolesConfig;
41 import com.google.gson.Gson;
42 import com.google.gson.GsonBuilder;
43 import com.google.gson.reflect.TypeToken;
46 * Basic file based user storage.
48 public class UserManager {
50 private File usersFile;
52 private static final ReadWriteLock LOCK = new ReentrantReadWriteLock(true);
53 private static final Lock READ_LOCK = LOCK.readLock();
54 private static final Lock WRITE_LOCK = LOCK.writeLock();
56 private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
60 * @param usersFile a file to store the users
62 public UserManager(File usersFile) {
63 this.usersFile = usersFile;
67 * Returns all users stored.
69 * @return a list of users.
71 public List<EcompUser> getUsers() {
72 Type collectionType = new TypeToken<List<EcompUser>>() {
75 Optional<String> users = read(usersFile);
76 if (users.isPresent()) {
77 return GSON.fromJson(users.get(), collectionType);
80 return new ArrayList<>();
84 * Returns a stored user.
86 * @param loginId the identifier of the user
87 * @return an optional user.
89 public Optional<EcompUser> getUser(String loginId) {
90 if (!getUsers().isEmpty()) {
91 return getUsers().stream().filter(u -> loginId.equals(u.getLoginId())).findFirst();
93 return Optional.empty();
97 * Stores a user if not already stored.
99 * @param user the user to be stored
100 * @throws IOException
102 public void pushUser(EcompUser user) throws IOException {
105 if (!getUser(user.getLoginId()).isPresent()) {
106 addUser(getUsers(), user);
114 * Replaces an existing user.
116 * @param loginId the id of the user
117 * @param user the new user details
118 * @throws IOException
120 public void editUser(String loginId, EcompUser user) throws IOException {
123 if (getUser(loginId).isPresent()) {
124 List<EcompUser> users = getUsers().stream().filter(u -> !u.getLoginId().equals(loginId))
125 .collect(Collectors.toList());
126 addUser(users, user);
134 * Gets the roles assigned to a user.
136 * @param loginId the id of the user
137 * @return the assigned roles
139 public List<EcompRole> getUserRoles(String loginId) {
140 List<EcompRole> roles = new ArrayList<>();
141 roles.addAll(getUser(loginId).orElseGet(EcompUser::new).getRoles());
145 public static List<EcompRole> getRoles() {
146 return RolesConfig.getInstance().getRoles();
149 private void addUser(List<EcompUser> users, EcompUser user) throws IOException {
154 private void write(List<EcompUser> users) throws IOException {
155 Files.write(usersFile.toPath(), GSON.toJson(users).getBytes());
158 private Optional<String> read(File file) {
161 return Optional.of(new String(Files.readAllBytes(file.toPath())));
162 } catch (IOException e) { // NOSONAR
163 return Optional.empty();