2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.security.portal;
29 import java.io.IOException;
30 import java.lang.reflect.Type;
31 import java.nio.file.Files;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Optional;
35 import java.util.concurrent.locks.Lock;
36 import java.util.concurrent.locks.ReadWriteLock;
37 import java.util.concurrent.locks.ReentrantReadWriteLock;
38 import java.util.stream.Collectors;
40 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
41 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
42 import org.openecomp.sparky.security.portal.config.RolesConfig;
44 import com.google.gson.Gson;
45 import com.google.gson.GsonBuilder;
46 import com.google.gson.reflect.TypeToken;
49 * Basic file based user storage.
51 public class UserManager {
53 private File usersFile;
55 private static final ReadWriteLock LOCK = new ReentrantReadWriteLock(true);
56 private static final Lock READ_LOCK = LOCK.readLock();
57 private static final Lock WRITE_LOCK = LOCK.writeLock();
59 private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
63 * @param usersFile a file to store the users
65 public UserManager(File usersFile) {
66 this.usersFile = usersFile;
70 * Returns all users stored.
72 * @return a list of users.
74 public List<EcompUser> getUsers() {
75 Type collectionType = new TypeToken<List<EcompUser>>() {
78 Optional<String> users = read(usersFile);
79 if (users.isPresent()) {
80 return GSON.fromJson(users.get(), collectionType);
83 return new ArrayList<>();
87 * Returns a stored user.
89 * @param loginId the identifier of the user
90 * @return an optional user.
92 public Optional<EcompUser> getUser(String loginId) {
93 if (!getUsers().isEmpty()) {
94 return getUsers().stream().filter(u -> loginId.equals(u.getLoginId())).findFirst();
96 return Optional.empty();
100 * Stores a user if not already stored.
102 * @param user the user to be stored
103 * @throws IOException
105 public void pushUser(EcompUser user) throws IOException {
108 if (!getUser(user.getLoginId()).isPresent()) {
109 addUser(getUsers(), user);
117 * Replaces an existing user.
119 * @param loginId the id of the user
120 * @param user the new user details
121 * @throws IOException
123 public void editUser(String loginId, EcompUser user) throws IOException {
126 if (getUser(loginId).isPresent()) {
127 List<EcompUser> users = getUsers().stream().filter(u -> !u.getLoginId().equals(loginId))
128 .collect(Collectors.toList());
129 addUser(users, user);
137 * Gets the roles assigned to a user.
139 * @param loginId the id of the user
140 * @return the assigned roles
142 public List<EcompRole> getUserRoles(String loginId) {
143 List<EcompRole> roles = new ArrayList<>();
144 roles.addAll(getUser(loginId).orElseGet(EcompUser::new).getRoles());
148 public static List<EcompRole> getRoles() {
149 return RolesConfig.getInstance().getRoles();
152 private void addUser(List<EcompUser> users, EcompUser user) throws IOException {
157 private void write(List<EcompUser> users) throws IOException {
158 Files.write(usersFile.toPath(), GSON.toJson(users).getBytes());
161 private Optional<String> read(File file) {
164 return Optional.of(new String(Files.readAllBytes(file.toPath())));
165 } catch (IOException e) { // NOSONAR
166 return Optional.empty();