Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / security / portal / UserManager.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.security.portal;
27
28 import java.io.File;
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;
39
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;
43
44 import com.google.gson.Gson;
45 import com.google.gson.GsonBuilder;
46 import com.google.gson.reflect.TypeToken;
47
48 /**
49  * Basic file based user storage.
50  */
51 public class UserManager {
52
53   private File usersFile;
54
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();
58
59   private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
60
61   /**
62    *
63    * @param usersFile a file to store the users
64    */
65   public UserManager(File usersFile) {
66     this.usersFile = usersFile;
67   }
68
69   /**
70    * Returns all users stored.
71    *
72    * @return a list of users.
73    */
74   public List<EcompUser> getUsers() {
75     Type collectionType = new TypeToken<List<EcompUser>>() {
76     }.getType();
77
78     Optional<String> users = read(usersFile);
79     if (users.isPresent()) {
80       return GSON.fromJson(users.get(), collectionType);
81     }
82
83     return new ArrayList<>();
84   }
85
86   /**
87    * Returns a stored user.
88    *
89    * @param loginId the identifier of the user
90    * @return an optional user.
91    */
92   public Optional<EcompUser> getUser(String loginId) {
93     if (!getUsers().isEmpty()) {
94       return getUsers().stream().filter(u -> loginId.equals(u.getLoginId())).findFirst();
95     }
96     return Optional.empty();
97   }
98
99   /**
100    * Stores a user if not already stored.
101    *
102    * @param user the user to be stored
103    * @throws IOException
104    */
105   public void pushUser(EcompUser user) throws IOException {
106     WRITE_LOCK.lock();
107     try {
108       if (!getUser(user.getLoginId()).isPresent()) {
109         addUser(getUsers(), user);
110       }
111     } finally {
112       WRITE_LOCK.unlock();
113     }
114   }
115
116   /**
117    * Replaces an existing user.
118    *
119    * @param loginId the id of the user
120    * @param user the new user details
121    * @throws IOException
122    */
123   public void editUser(String loginId, EcompUser user) throws IOException {
124     WRITE_LOCK.lock();
125     try {
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);
130       }
131     } finally {
132       WRITE_LOCK.unlock();
133     }
134   }
135
136   /**
137    * Gets the roles assigned to a user.
138    *
139    * @param loginId the id of the user
140    * @return the assigned roles
141    */
142   public List<EcompRole> getUserRoles(String loginId) {
143     List<EcompRole> roles = new ArrayList<>();
144     roles.addAll(getUser(loginId).orElseGet(EcompUser::new).getRoles());
145     return roles;
146   }
147
148   public static List<EcompRole> getRoles() {
149     return RolesConfig.getInstance().getRoles();
150   }
151
152   private void addUser(List<EcompUser> users, EcompUser user) throws IOException {
153     users.add(user);
154     write(users);
155   }
156
157   private void write(List<EcompUser> users) throws IOException {
158     Files.write(usersFile.toPath(), GSON.toJson(users).getBytes());
159   }
160
161   private Optional<String> read(File file) {
162     READ_LOCK.lock();
163     try {
164       return Optional.of(new String(Files.readAllBytes(file.toPath())));
165     } catch (IOException e) { // NOSONAR
166       return Optional.empty();
167     } finally {
168       READ_LOCK.unlock();
169     }
170   }
171 }