Adding back-end support for UI filters
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / security / portal / PortalRestAPIServiceImpl.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.onap.aai.sparky.security.portal;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.text.MessageFormat;
28 import java.util.LinkedHashSet;
29 import java.util.List;
30
31 import javax.servlet.http.HttpServletRequest;
32
33 import org.onap.aai.sparky.security.EcompSso;
34 import org.onap.aai.sparky.security.portal.config.PortalAuthenticationConfig;
35 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
36 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
37 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
38 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
39 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Responds to ECOMP Portal's REST queries for user and role information and management.
45  */
46 public class PortalRestAPIServiceImpl implements IPortalRestAPIService {
47
48   private static final Logger LOG = LoggerFactory.getLogger(PortalRestAPIServiceImpl.class);
49   private static final String ERROR_MESSAGE = "Failed to {0} user [loginId:{1}]";
50
51   /**
52    * @return the userManager
53    */
54   public UserManager getUserManager() {
55     return userManager;
56   }
57
58   /**
59    * @param userManager the userManager to set
60    */
61   public void setUserManager(UserManager userManager) {
62     this.userManager = userManager;
63   }
64
65   /**
66    * @return the log
67    */
68   public static Logger getLog() {
69     return LOG;
70   }
71
72   /**
73    * @return the errorMessage
74    */
75   public static String getErrorMessage() {
76     return ERROR_MESSAGE;
77   }
78
79   private UserManager userManager;
80
81   /**
82    * Initialise user manager.
83    */
84   public PortalRestAPIServiceImpl() {
85     userManager = new UserManager(new File(TierSupportUiConstants.USERS_FILE_LOCATION));
86   }
87
88   /////////////////////////////////////////////////////////////////////////////
89   // User interface
90   /////////////////////////////////////////////////////////////////////////////
91
92   /*
93    * (non-Javadoc)
94    *
95    * @see
96    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUser(com.att.fusion.core.
97    * restful.domain.EcompUser)
98    */
99   @Override
100   public void pushUser(EcompUser user) throws PortalAPIException {
101     LOG.debug("Push user [loginId:" + user.getLoginId() + "]");
102
103     if (userManager.getUser(user.getLoginId()).isPresent()) {
104       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId())
105           + ", user is already stored";
106       LOG.error(message);
107       throw new PortalAPIException(message);
108     }
109
110     try {
111       userManager.pushUser(user);
112     } catch (IOException e) {
113       String message = getMessage(ERROR_MESSAGE, "push", user.getLoginId());
114       LOG.error(message, e);
115       throw new PortalAPIException(message, e);
116     }
117   }
118
119   /*
120    * (non-Javadoc)
121    *
122    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#editUser(java.lang.String,
123    * com.att.fusion.core.restful.domain.EcompUser)
124    */
125   @Override
126   public void editUser(String loginId, EcompUser user) throws PortalAPIException {
127     LOG.debug("Edit user [loginId:" + loginId + "]");
128
129     userManager.getUser(loginId).orElseThrow(() -> {
130       String message = getMessage(ERROR_MESSAGE, "edit", loginId) + ", unknown user";
131       LOG.error(message);
132       return new PortalAPIException(message);
133     });
134
135     try {
136       userManager.editUser(loginId, user);
137     } catch (IOException e) {
138       String message = getMessage(ERROR_MESSAGE, "edit", loginId);
139       LOG.error(message, e);
140       throw new PortalAPIException(message, e);
141     }
142   }
143
144   /*
145    * (non-Javadoc)
146    *
147    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUser(java.lang.String)
148    */
149   @Override
150   public EcompUser getUser(String loginId) throws PortalAPIException {
151     LOG.debug("Get user [loginId:" + loginId + "]");
152     return userManager.getUser(loginId).orElseThrow(() -> {
153       String message = getMessage(ERROR_MESSAGE, "get", loginId) + ", unknown user";
154       LOG.error(message);
155       return new PortalAPIException(message);
156     });
157   }
158
159   /*
160    * (non-Javadoc)
161    *
162    * @see com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUsers()
163    */
164   @Override
165   public List<EcompUser> getUsers() throws PortalAPIException {
166     LOG.debug("Get users");
167     return userManager.getUsers();
168   }
169
170   @Override
171   public String getUserId(HttpServletRequest request) throws PortalAPIException {
172     return EcompSso.validateEcompSso(request);
173   }
174
175   /////////////////////////////////////////////////////////////////////////////
176   // Role interface
177   /////////////////////////////////////////////////////////////////////////////
178
179   @Override
180   public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
181     LOG.debug("Get available roles");
182     return UserManager.getRoles();
183   }
184
185   /*
186    * (non-Javadoc)
187    *
188    * @see
189    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#getUserRoles(java.lang.String)
190    */
191   @Override
192   public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
193     LOG.debug("Get user roles");
194     return userManager.getUserRoles(loginId);
195   }
196
197   /*
198    * (non-Javadoc)
199    *
200    * @see
201    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#pushUserRole(java.lang.String,
202    * java.util.List)
203    */
204   @Override
205   public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
206     LOG.debug("Push user role [loginId:" + loginId + "]");
207     try {
208       EcompUser user = getUser(loginId);
209       if (roles != null) {
210         user.setRoles(new LinkedHashSet<EcompRole>(roles));
211       } else {
212         user.setRoles(new LinkedHashSet<EcompRole>());
213       }
214       editUser(loginId, user);
215     } catch (PortalAPIException e) {
216       String message = getMessage(ERROR_MESSAGE, "push role", loginId);
217       LOG.error(message);
218       throw new PortalAPIException(message, e);
219     }
220   }
221
222   /////////////////////////////////////////////////////////////////////////////
223   // Security interface
224   /////////////////////////////////////////////////////////////////////////////
225
226   /*
227    * (non-Javadoc)
228    *
229    * @see
230    * com.att.fusion.core.onboarding.crossapi.IPortalRestAPIService#isAppAuthenticated(javax.servlet.
231    * http.HttpServletRequest)
232    */
233   @Override
234   public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
235     LOG.debug("Authentication request");
236     PortalAuthenticationConfig config = PortalAuthenticationConfig.getInstance();
237     String restUsername = request.getHeader(PortalAuthenticationConfig.PROP_USERNAME);
238     String restPassword = request.getHeader(PortalAuthenticationConfig.PROP_PASSWORD);
239     return restUsername != null && restPassword != null && restUsername.equals(config.getUsername())
240         && restPassword.equals(config.getPassword());
241   }
242
243   private String getMessage(String message, Object... args) {
244     MessageFormat formatter = new MessageFormat("");
245     formatter.applyPattern(message);
246     return formatter.format(args);
247   }
248 }