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