2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.controller;
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.List;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.annotation.EnableAspectJAutoProxy;
32 import org.springframework.web.bind.annotation.PathVariable;
33 import org.springframework.web.bind.annotation.RequestBody;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36 import org.springframework.web.bind.annotation.RestController;
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.json.JSONObject;
40 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
41 import org.openecomp.portalapp.portal.domain.AdminUserApplications;
42 import org.openecomp.portalapp.portal.domain.AppIdAndNameTransportModel;
43 import org.openecomp.portalapp.portal.domain.AppsResponse;
44 import org.openecomp.portalapp.portal.domain.EPApp;
45 import org.openecomp.portalapp.portal.domain.EPUser;
46 import org.openecomp.portalapp.portal.domain.EcompApp;
47 import org.openecomp.portalapp.portal.domain.UserRole;
48 import org.openecomp.portalapp.portal.domain.UserRoles;
49 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
50 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
51 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
52 import org.openecomp.portalapp.portal.service.AdminRolesService;
53 import org.openecomp.portalapp.portal.service.EPAppService;
54 import org.openecomp.portalapp.portal.service.PersUserAppService;
55 import org.openecomp.portalapp.portal.service.UserService;
56 import org.openecomp.portalapp.portal.transport.FieldsValidator;
57 import org.openecomp.portalapp.portal.transport.LocalRole;
58 import org.openecomp.portalapp.portal.transport.OnboardingApp;
59 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
60 import org.openecomp.portalapp.util.EPUserUtils;
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
66 public class AppsController extends EPRestrictedBaseController {
68 static final String FAILURE = "failure";
69 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsController.class);
72 AdminRolesService adminRolesService;
74 EPAppService appService;
76 PersUserAppService persUserAppService;
78 UserService userService;
81 * RESTful service method to fetch all Applications available to watch for
86 @RequestMapping(value = { "/portalApi/userApps" }, method = RequestMethod.GET, produces = "application/json")
87 public List<EcompApp> getUserApps(HttpServletRequest request, HttpServletResponse response) {
88 EPUser user = EPUserUtils.getUserSession(request);
89 List<EcompApp> ecompApps = null;
93 EcompPortalUtils.setBadPermissions(user, response, "getUserApps");
95 ecompApps = appService.transformAppsToEcompApps(appService.getUserApps(user));
96 EcompPortalUtils.logAndSerializeObject("/portalApi/userApps", "GET result =", ecompApps);
98 } catch (Exception e) {
99 logger.error(EELFLoggerDelegate.errorLogger,
100 "Exception occurred while performing getUserApps operation, Details: "
101 + EcompPortalUtils.getStackTrace(e));
108 * Create new application's contact us details.
113 @RequestMapping(value = "/portalApi/saveNewUser", method = RequestMethod.POST, produces = "application/json")
114 public PortalRestResponse<String> saveNewUser(HttpServletRequest request,@RequestBody EPUser newUser) {
115 EPUser user = EPUserUtils.getUserSession(request);
117 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
118 "New User cannot be null or empty");
120 if (!(adminRolesService.isSuperAdmin(user) || adminRolesService.isAccountAdmin(user))){
121 if(!user.getLoginId().equalsIgnoreCase(newUser.getLoginId()))
122 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
126 String checkDuplicate = request.getParameter("isCheck");
127 String saveNewUser = FAILURE;
129 saveNewUser = userService.saveNewUser(newUser,checkDuplicate);
130 } catch (Exception e) {
131 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, saveNewUser, e.getMessage());
133 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, saveNewUser, "");
137 * RESTful service method to fetch all applications accessible to the
138 * current user, with personalizations.
142 @RequestMapping(value = { "/portalApi/persUserApps" }, method = RequestMethod.GET, produces = "application/json")
143 public List<EcompApp> getPersUserApps(HttpServletRequest request, HttpServletResponse response) throws IOException {
144 EPUser user = EPUserUtils.getUserSession(request);
145 List<EcompApp> ecompApps = null;
148 EcompPortalUtils.setBadPermissions(user, response, "getPersUserApps");
150 List<EPApp> apps = null;
151 if (adminRolesService.isSuperAdmin(user))
152 apps = appService.getPersAdminApps(user);
154 apps = appService.getPersUserApps(user);
155 ecompApps = appService.transformAppsToEcompApps(apps);
156 EcompPortalUtils.logAndSerializeObject("/portalApi/userPersApps", "GET result =", ecompApps);
158 } catch (Exception e) {
159 logger.error(EELFLoggerDelegate.errorLogger, "Failed in getPersUserApps", e);
160 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
166 * RESTful service method to fetch applications for which the current user
167 * is an Administrator
171 @RequestMapping(value = { "/portalApi/adminApps" }, method = RequestMethod.GET, produces = "application/json")
172 public List<AppIdAndNameTransportModel> getAdminApps(HttpServletRequest request, HttpServletResponse response) {
173 EPUser user = EPUserUtils.getUserSession(request);
174 List<AppIdAndNameTransportModel> adminApps = null;
177 if (!adminRolesService.isAccountAdmin(user)) {
178 EcompPortalUtils.setBadPermissions(user, response, "getAdminApps");
180 adminApps = appService.getAdminApps(user);
181 EcompPortalUtils.logAndSerializeObject("/portalApi/adminApps", "GET result =", adminApps);
183 } catch (Exception e) {
184 logger.error(EELFLoggerDelegate.errorLogger,
185 "Exception occurred while performing getAdminApps operation, Details: "
186 + EcompPortalUtils.getStackTrace(e));
193 * RESTful service method to fetch Applications in which the logged in user
194 * is an Administrator
198 @RequestMapping(value = {
199 "/portalApi/appsForSuperAdminAndAccountAdmin" }, method = RequestMethod.GET, produces = "application/json")
200 public List<AppIdAndNameTransportModel> getAppsForSuperAdminAndAccountAdmin(HttpServletRequest request,
201 HttpServletResponse response) {
202 EPUser user = EPUserUtils.getUserSession(request);
203 List<AppIdAndNameTransportModel> adminApps = null;
206 if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdmin(user)) {
207 EcompPortalUtils.setBadPermissions(user, response, "getAdminApps");
209 adminApps = appService.getAppsForSuperAdminAndAccountAdmin(user);
210 EcompPortalUtils.logAndSerializeObject("/portalApi/appsForSuperAdminAndAccountAdmin", "GET result =",
213 } catch (Exception e) {
214 logger.error(EELFLoggerDelegate.errorLogger,
215 "Exception occurred while performing getAppsForSuperAdminAndAccountAdmin operation, Details: "
216 + EcompPortalUtils.getStackTrace(e));
223 * RESTful service method to fetch Application Administrators to Super
224 * Administrator user. Attention: Users which have Super Administrator roles
225 * only are not included!
229 @RequestMapping(value = { "/portalApi/accountAdmins" }, method = RequestMethod.GET, produces = "application/json")
230 public List<AdminUserApplications> getAppsAdministrators(HttpServletRequest request, HttpServletResponse response) {
231 EPUser user = EPUserUtils.getUserSession(request);
232 List<AdminUserApplications> admins = null;
235 if (!adminRolesService.isSuperAdmin(user)) {
236 EcompPortalUtils.setBadPermissions(user, response, "getAppsAdministrators");
238 admins = appService.getAppsAdmins();
239 EcompPortalUtils.logAndSerializeObject("/portalApi/accountAdmins", "GET result =", admins);
241 } catch (Exception e) {
242 logger.error(EELFLoggerDelegate.errorLogger,
243 "Exception occurred while performing getAppsAdministrators operation, Details: "
244 + EcompPortalUtils.getStackTrace(e));
250 @RequestMapping(value = { "/portalApi/availableApps" }, method = RequestMethod.GET, produces = "application/json")
251 public List<AppsResponse> getApps(HttpServletRequest request, HttpServletResponse response) {
252 EPUser user = EPUserUtils.getUserSession(request);
253 List<AppsResponse> apps = null;
255 if (!adminRolesService.isSuperAdmin(user)) {
256 EcompPortalUtils.setBadPermissions(user, response, "getApps");
258 apps = appService.getAllApps(false);
259 EcompPortalUtils.logAndSerializeObject("/portalApi/availableApps", "GET result =", apps);
261 } catch (Exception e) {
262 logger.error(EELFLoggerDelegate.errorLogger,
263 "Exception occurred while performing getApps operation, Details: "
264 + EcompPortalUtils.getStackTrace(e));
271 * Gets all apps, both active and inactive; i.e., all on-boarded apps,
272 * regardless of enabled status.
276 * @return List of applications
279 @RequestMapping(value = {
280 "/portalApi/allAvailableApps" }, method = RequestMethod.GET, produces = "application/json")
281 public List<AppsResponse> getAllApps(HttpServletRequest request, HttpServletResponse response) {
282 EPUser user = EPUserUtils.getUserSession(request);
283 List<AppsResponse> apps = null;
285 if (!adminRolesService.isSuperAdmin(user)) {
286 EcompPortalUtils.setBadPermissions(user, response, "getApps");
288 apps = appService.getAllApps(true);
289 EcompPortalUtils.logAndSerializeObject("/portalApi/availableApps", "GET result =", apps);
291 } catch (Exception e) {
292 logger.error(EELFLoggerDelegate.errorLogger, "Failed in getAllApps", e);
298 @RequestMapping(value = { "/portalApi/appsFullList" }, method = RequestMethod.GET, produces = "application/json")
299 public List<EcompApp> getAppsFullList(HttpServletRequest request, HttpServletResponse response) {
300 EPUser user = EPUserUtils.getUserSession(request);
301 List<EcompApp> ecompApps = null;
303 EcompPortalUtils.setBadPermissions(user, response, "getAppsFullList");
305 ecompApps = appService.getEcompAppAppsFullList();
306 EcompPortalUtils.logAndSerializeObject("/portalApi/appsFullList", "GET result =", ecompApps);
312 @RequestMapping(value = { "/portalApi/userProfile" }, method = RequestMethod.GET, produces = "application/json")
313 public UserRoles getUserProfile(HttpServletRequest request, HttpServletResponse response) {
314 EPUser user = EPUserUtils.getUserSession(request);
315 UserRoles userAndRoles = null;
318 EcompPortalUtils.setBadPermissions(user, response, "getUserProfile");
321 userAndRoles = appService.getUserProfile(user.getLoginId());
322 // If no roles are defined, treat this user as a guest.
323 if (user.isGuest() || userAndRoles == null) {
324 logger.debug(EELFLoggerDelegate.debugLogger, "getUserProfile: treating user {} as guest",
326 UserRole userRole = new UserRole();
327 userRole.setOrgUserId(user.getLoginId());
328 userRole.setFirstName(user.getFirstName());
329 userRole.setLastName(user.getLastName());
330 userRole.setRoleId(-1L);
331 userRole.setRoleName("Guest");
332 userRole.setUser_Id(-1L);
333 userAndRoles = new UserRoles(userRole);
336 } catch (Exception e) {
337 logger.error(EELFLoggerDelegate.errorLogger, "Failed to get user names and roles", e);
340 EcompPortalUtils.logAndSerializeObject("/portalApi/userProfile", "getUserProfile result =", userAndRoles);
344 @RequestMapping(value = { "/portalApi/currentUserProfile/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
345 public String getCurrentUserProfile(HttpServletRequest request, @PathVariable("loginId") String loginId) {
347 Map<String,String> map = new HashMap<String,String>();
350 user = (EPUser) userService.getUserByUserId(loginId).get(0);
351 map.put("firstName", user.getFirstName());
352 map.put("lastName", user.getLastName());
353 map.put("email", user.getEmail());
354 map.put("loginId", user.getLoginId());
355 map.put("loginPwd",user.getLoginPwd());
356 map.put("middleInitial",user.getMiddleInitial());
357 } catch (Exception e) {
358 logger.error(EELFLoggerDelegate.errorLogger, "Failed to get user info", e);
361 JSONObject j = new JSONObject(map);;
365 @RequestMapping(value = { "/portalApi/appRoles/{appId}" }, method = {
366 RequestMethod.GET }, produces = "application/json")
367 public List<LocalRole> getAppRoles(HttpServletRequest request, @PathVariable("appId") Long appId) {
368 List<LocalRole> roleList = null;
370 roleList = appService.getAppRoles(appId);
371 EcompPortalUtils.logAndSerializeObject("/portalApi/appRoles/" + appId, "GET result =", roleList);
372 } catch (Exception e) {
373 logger.error(EELFLoggerDelegate.errorLogger,
374 "Exception occurred while performing getAppRoles operation, Details: "
375 + EcompPortalUtils.getStackTrace(e));
381 @RequestMapping(value = { "/portalApi/onboardingApps" }, method = RequestMethod.GET, produces = "application/json")
382 public List<OnboardingApp> getOnboardingApps(HttpServletRequest request, HttpServletResponse response) {
383 EPUser user = EPUserUtils.getUserSession(request);
384 List<OnboardingApp> onboardingApps = null;
386 if (!adminRolesService.isSuperAdmin(user)) {
387 EcompPortalUtils.setBadPermissions(user, response, "getOnboardingApps");
389 onboardingApps = appService.getOnboardingApps();
390 EcompPortalUtils.logAndSerializeObject("/portalApi/onboardingApps", "GET result =",
391 "onboardingApps of size " + onboardingApps.size());
393 } catch (Exception e) {
394 logger.error(EELFLoggerDelegate.errorLogger,
395 "Exception occurred while performing getOnboardingApps operation, Details: "
396 + EcompPortalUtils.getStackTrace(e));
399 return onboardingApps;
402 @RequestMapping(value = { "/portalApi/onboardingApps" }, method = RequestMethod.PUT, produces = "application/json")
403 public FieldsValidator putOnboardingApp(HttpServletRequest request,
404 @RequestBody OnboardingApp modifiedOnboardingApp, HttpServletResponse response) {
405 FieldsValidator fieldsValidator = null;
407 EPUser user = EPUserUtils.getUserSession(request);
408 if (!adminRolesService.isSuperAdmin(user)) {
409 EcompPortalUtils.setBadPermissions(user, response, "putOnboardingApp");
411 modifiedOnboardingApp.normalize();
412 fieldsValidator = appService.modifyOnboardingApp(modifiedOnboardingApp, user);
413 response.setStatus(fieldsValidator.httpStatusCode.intValue());
415 } catch (Exception e) {
416 logger.error(EELFLoggerDelegate.errorLogger,
417 "Exception occurred while performing putOnboardingApps operation, Details: "
418 + EcompPortalUtils.getStackTrace(e));
421 EcompPortalUtils.logAndSerializeObject("/portalApi/onboardingApps", "PUT result =", response.getStatus());
422 return fieldsValidator;
425 @RequestMapping(value = { "/portalApi/onboardingApps" }, method = RequestMethod.POST, produces = "application/json")
426 public FieldsValidator postOnboardingApp(HttpServletRequest request, @RequestBody OnboardingApp newOnboardingApp,
427 HttpServletResponse response) {
428 FieldsValidator fieldsValidator = null;
430 EPUser user = EPUserUtils.getUserSession(request);
431 if (!adminRolesService.isSuperAdmin(user)) {
432 EcompPortalUtils.setBadPermissions(user, response, "postOnboardingApps");
434 newOnboardingApp.normalize();
435 fieldsValidator = appService.addOnboardingApp(newOnboardingApp, user);
436 response.setStatus(fieldsValidator.httpStatusCode.intValue());
438 } catch (Exception e) {
439 logger.error(EELFLoggerDelegate.errorLogger,
440 "Exception occurred while performing postOnboardingApps operation, Details: "
441 + EcompPortalUtils.getStackTrace(e));
444 EcompPortalUtils.logAndSerializeObject("/portalApi/onboardingApps", "POST result =", response.getStatus());
445 return fieldsValidator;
448 @RequestMapping(value = { "/portalApi/onboardingApps/{appId}" }, method = {
449 RequestMethod.DELETE }, produces = "application/json")
450 public FieldsValidator deleteOnboardingApp(HttpServletRequest request, @PathVariable("appId") Long appId,
451 HttpServletResponse response) {
452 FieldsValidator fieldsValidator = null;
455 EPUser user = EPUserUtils.getUserSession(request);
456 if (!adminRolesService.isSuperAdmin(user)) {
457 EcompPortalUtils.setBadPermissions(user, response, "deleteOnboardingApps");
459 fieldsValidator = appService.deleteOnboardingApp(user, appId);
460 response.setStatus(fieldsValidator.httpStatusCode.intValue());
462 } catch (Exception e) {
463 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
466 EcompPortalUtils.logAndSerializeObject("/portalApi/onboardingApps" + appId, "DELETE result =",
467 response.getStatus());
468 return fieldsValidator;