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.
25 package org.onap.aai.sparky.security.portal;
28 import java.io.IOException;
29 import java.lang.reflect.Type;
30 import java.nio.file.Files;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.concurrent.locks.Lock;
35 import java.util.concurrent.locks.ReadWriteLock;
36 import java.util.concurrent.locks.ReentrantReadWriteLock;
37 import java.util.stream.Collectors;
39 import org.onap.aai.sparky.security.portal.config.RolesConfig;
40 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
41 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
43 import com.google.gson.Gson;
44 import com.google.gson.GsonBuilder;
45 import com.google.gson.reflect.TypeToken;
48 * Basic file based user storage.
50 public class UserManager {
52 private File usersFile;
54 private static final ReadWriteLock LOCK = new ReentrantReadWriteLock(true);
55 private static final Lock READ_LOCK = LOCK.readLock();
56 private static final Lock WRITE_LOCK = LOCK.writeLock();
58 private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
62 * @param usersFile a file to store the users
64 public UserManager(File usersFile) {
65 this.usersFile = usersFile;
69 * Returns all users stored.
71 * @return a list of users.
73 public List<EcompUser> getUsers() {
74 Type collectionType = new TypeToken<List<EcompUser>>() {
77 Optional<String> users = read(usersFile);
78 if (users.isPresent()) {
79 return GSON.fromJson(users.get(), collectionType);
82 return new ArrayList<>();
86 * Returns a stored user.
88 * @param loginId the identifier of the user
89 * @return an optional user.
91 public Optional<EcompUser> getUser(String loginId) {
92 if (!getUsers().isEmpty()) {
93 return getUsers().stream().filter(u -> loginId.equals(u.getLoginId())).findFirst();
95 return Optional.empty();
99 * Stores a user if not already stored.
101 * @param user the user to be stored
102 * @throws IOException
104 public void pushUser(EcompUser user) throws IOException {
107 if (!getUser(user.getLoginId()).isPresent()) {
108 addUser(getUsers(), user);
116 * Replaces an existing user.
118 * @param loginId the id of the user
119 * @param user the new user details
120 * @throws IOException
122 public void editUser(String loginId, EcompUser user) throws IOException {
125 if (getUser(loginId).isPresent()) {
126 List<EcompUser> users = getUsers().stream().filter(u -> !u.getLoginId().equals(loginId))
127 .collect(Collectors.toList());
128 addUser(users, user);
136 * Gets the roles assigned to a user.
138 * @param loginId the id of the user
139 * @return the assigned roles
141 public List<EcompRole> getUserRoles(String loginId) {
142 List<EcompRole> roles = new ArrayList<>();
143 roles.addAll(getUser(loginId).orElseGet(EcompUser::new).getRoles());
147 public static List<EcompRole> getRoles() {
148 return RolesConfig.getInstance().getRoles();
151 private void addUser(List<EcompUser> users, EcompUser user) throws IOException {
156 private void write(List<EcompUser> users) throws IOException {
157 Files.write(usersFile.toPath(), GSON.toJson(users).getBytes());
160 private Optional<String> read(File file) {
163 return Optional.of(new String(Files.readAllBytes(file.toPath())));
164 } catch (IOException e) { // NOSONAR
165 return Optional.empty();