Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / security / portal / UserManager.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sparky.security.portal;
22
23 import java.io.File;
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;
34
35 import org.onap.aai.sparky.security.portal.config.RolesConfig;
36 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
37 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
38
39 import com.google.gson.Gson;
40 import com.google.gson.GsonBuilder;
41 import com.google.gson.reflect.TypeToken;
42
43 /**
44  * Basic file based user storage.
45  */
46 public class UserManager {
47
48   private File usersFile;
49
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();
53
54   private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
55
56   /**
57    *
58    * @param usersFile a file to store the users
59    */
60   public UserManager(File usersFile) {
61     this.usersFile = usersFile;
62   }
63
64   /**
65    * Returns all users stored.
66    *
67    * @return a list of users.
68    */
69   public List<EcompUser> getUsers() {
70     Type collectionType = new TypeToken<List<EcompUser>>() {
71     }.getType();
72
73     Optional<String> users = read(usersFile);
74     if (users.isPresent()) {
75       return GSON.fromJson(users.get(), collectionType);
76     }
77
78     return new ArrayList<>();
79   }
80
81   /**
82    * Returns a stored user.
83    *
84    * @param loginId the identifier of the user
85    * @return an optional user.
86    */
87   public Optional<EcompUser> getUser(String loginId) {
88     if (!getUsers().isEmpty()) {
89       return getUsers().stream().filter(u -> loginId.equals(u.getLoginId())).findFirst();
90     }
91     return Optional.empty();
92   }
93
94   /**
95    * Stores a user if not already stored.
96    *
97    * @param user the user to be stored
98    * @throws IOException
99    */
100   public void pushUser(EcompUser user) throws IOException {
101     WRITE_LOCK.lock();
102     try {
103       if (!getUser(user.getLoginId()).isPresent()) {
104         addUser(getUsers(), user);
105       }
106     } finally {
107       WRITE_LOCK.unlock();
108     }
109   }
110
111   /**
112    * Replaces an existing user.
113    *
114    * @param loginId the id of the user
115    * @param user the new user details
116    * @throws IOException
117    */
118   public void editUser(String loginId, EcompUser user) throws IOException {
119     WRITE_LOCK.lock();
120     try {
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);
125       }
126     } finally {
127       WRITE_LOCK.unlock();
128     }
129   }
130
131   /**
132    * Gets the roles assigned to a user.
133    *
134    * @param loginId the id of the user
135    * @return the assigned roles
136    */
137   public List<EcompRole> getUserRoles(String loginId) {
138     List<EcompRole> roles = new ArrayList<>();
139     roles.addAll(getUser(loginId).orElseGet(EcompUser::new).getRoles());
140     return roles;
141   }
142
143   public static List<EcompRole> getRoles() {
144     return RolesConfig.getInstance().getRoles();
145   }
146
147   private void addUser(List<EcompUser> users, EcompUser user) throws IOException {
148     users.add(user);
149     write(users);
150   }
151
152   private void write(List<EcompUser> users) throws IOException {
153     Files.write(usersFile.toPath(), GSON.toJson(users).getBytes());
154   }
155
156   private Optional<String> read(File file) {
157     READ_LOCK.lock();
158     try {
159       return Optional.of(new String(Files.readAllBytes(file.toPath())));
160     } catch (IOException e) { // NOSONAR
161       return Optional.empty();
162     } finally {
163       READ_LOCK.unlock();
164     }
165   }
166 }