c5cd5c90475165099afc2b4fae2dd2fcd48106ab
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / UserRolesController.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.cxf.transport.http.HTTPException;
32 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
33 import org.openecomp.portalapp.portal.domain.EPRole;
34 import org.openecomp.portalapp.portal.domain.EPUser;
35 import org.openecomp.portalapp.portal.domain.EPUserAppCatalogRoles;
36 import org.openecomp.portalapp.portal.domain.EcompAuditLog;
37 import org.openecomp.portalapp.portal.domain.ExternalSystemAccess;
38 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
39 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
40 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
41 import org.openecomp.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
42 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
43 import org.openecomp.portalapp.portal.service.AdminRolesService;
44 import org.openecomp.portalapp.portal.service.ApplicationsRestClientService;
45 import org.openecomp.portalapp.portal.service.SearchService;
46 import org.openecomp.portalapp.portal.service.UserRolesService;
47 import org.openecomp.portalapp.portal.transport.AppNameIdIsAdmin;
48 import org.openecomp.portalapp.portal.transport.AppWithRolesForUser;
49 import org.openecomp.portalapp.portal.transport.AppsListWithAdminRole;
50 import org.openecomp.portalapp.portal.transport.FieldsValidator;
51 import org.openecomp.portalapp.portal.transport.RoleInAppForUser;
52 import org.openecomp.portalapp.portal.transport.UserApplicationRoles;
53 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
54 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
55 import org.openecomp.portalapp.portal.utils.PortalConstants;
56 import org.openecomp.portalapp.util.EPUserUtils;
57 import org.openecomp.portalsdk.core.domain.AuditLog;
58 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
60 import org.openecomp.portalsdk.core.service.AuditService;
61 import org.openecomp.portalsdk.core.util.SystemProperties;
62 import org.slf4j.MDC;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.context.annotation.EnableAspectJAutoProxy;
65 import org.springframework.web.bind.annotation.PathVariable;
66 import org.springframework.web.bind.annotation.RequestBody;
67 import org.springframework.web.bind.annotation.RequestMapping;
68 import org.springframework.web.bind.annotation.RequestMethod;
69 import org.springframework.web.bind.annotation.RequestParam;
70 import org.springframework.web.bind.annotation.RestController;
71
72 @RestController
73 @org.springframework.context.annotation.Configuration
74 @EnableAspectJAutoProxy
75 @EPAuditLog
76 public class UserRolesController extends EPRestrictedBaseController {
77
78         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserRolesController.class);
79
80         @Autowired
81         private SearchService searchService;
82         @Autowired
83         private AdminRolesService adminRolesService;
84         private @Autowired UserRolesService userRolesService;
85         @Autowired
86         private ApplicationsRestClientService applicationsRestClientService;
87         @Autowired
88         private AuditService auditService;
89
90         private static final String FAILURE = "failure";
91
92         /**
93          * RESTful service method to fetch users in the WebPhone external service
94          * 
95          * @param request
96          *            HttpServletRequest
97          * @param searchString
98          *            search string
99          * @param response
100          *            HttpServletResponse
101          * @return array of found users as json
102          */
103         @RequestMapping(value = { "/portalApi/queryUsers" }, method = RequestMethod.GET, produces = "application/json")
104         public String getPhoneBookSearchResult(HttpServletRequest request, @RequestParam("search") String searchString,
105                         HttpServletResponse response) {
106                 EPUser user = EPUserUtils.getUserSession(request);
107                 String searchResult = null;
108                 if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdmin(user)) {
109                         EcompPortalUtils.setBadPermissions(user, response, "getPhoneBookSearchResult");
110                 } else {
111                         searchString = searchString.trim();
112                         if (searchString.length() > 2) {
113                                 searchResult = searchService.searchUsersInPhoneBook(searchString);
114                         } else {
115                                 logger.info(EELFLoggerDelegate.errorLogger,
116                                                 "getPhoneBookSearchResult - too short search string: " + searchString);
117                         }
118                 }
119                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/queryUsers", "result =", searchResult);
120
121                 return searchResult;
122         }
123
124         /**
125          * RESTful service method to fetch applications where user is admin
126          * 
127          * @param request
128          *            HttpServletRequest
129          * @param orgUserId
130          *            search string
131          * @param response
132          *            HttpServletResponse
133          * @return for GET: array of all applications with boolean
134          *         isAdmin=true/false for each application
135          */
136         @RequestMapping(value = { "/portalApi/adminAppsRoles" }, method = {
137                         RequestMethod.GET }, produces = "application/json")
138         public AppsListWithAdminRole getAppsWithAdminRoleStateForUser(HttpServletRequest request,
139                         @RequestParam("user") String orgUserId, HttpServletResponse response) {
140
141                 EPUser user = EPUserUtils.getUserSession(request);
142                 AppsListWithAdminRole result = null;
143                 if (!adminRolesService.isSuperAdmin(user)) {
144                         EcompPortalUtils.setBadPermissions(user, response, "getAppsWithAdminRoleStateForUser");
145                 } else {
146                         if (EcompPortalUtils.legitimateUserId(orgUserId)) {
147                                 result = adminRolesService.getAppsWithAdminRoleStateForUser(orgUserId);
148                         } else {
149                                 logger.info(EELFLoggerDelegate.errorLogger,
150                                                 "getAppsWithAdminRoleStateForUser - parms error, no Organization User ID");
151                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
152                         }
153                 }
154
155                 StringBuilder adminAppRoles = new StringBuilder();
156                 if(result != null){
157                         if ( result.appsRoles.size() >= 1) {
158                                 adminAppRoles.append("User '" + result.orgUserId + "' has admin role to the apps = {");
159                                 for (AppNameIdIsAdmin adminAppRole : result.appsRoles) {
160                                         if (adminAppRole.isAdmin) {
161                                                 adminAppRoles.append(adminAppRole.appName + ", ");
162                                         }
163                                 }
164                                 adminAppRoles.append("}.");
165                         } else {
166                                 adminAppRoles.append("User '" + result.orgUserId + "' has no Apps with Admin Role.");
167                         }
168                 }else{
169                         logger.error(EELFLoggerDelegate.errorLogger, "putAppWithUserRoleStateForUser: getAppsWithAdminRoleStateForUser result is null");
170                 }
171                 
172                 logger.info(EELFLoggerDelegate.errorLogger, adminAppRoles.toString());
173
174                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/adminAppsRoles", "get result =", result);
175
176                 return result;
177         }
178
179         /**
180          * 
181          * @param request
182          *            HttpServletRequest
183          * @param newAppsListWithAdminRoles
184          *            new apps
185          * @param response
186          *            HttpServletResponse
187          * @return FieldsValidator
188          */
189         @RequestMapping(value = { "/portalApi/adminAppsRoles" }, method = {
190                         RequestMethod.PUT }, produces = "application/json")
191         public FieldsValidator putAppsWithAdminRoleStateForUser(HttpServletRequest request,
192                         @RequestBody AppsListWithAdminRole newAppsListWithAdminRoles, HttpServletResponse response) {
193
194                 // newAppsListWithAdminRoles.appsRoles
195                 FieldsValidator fieldsValidator = new FieldsValidator();
196                 StringBuilder newAppRoles = new StringBuilder();
197                 if(newAppsListWithAdminRoles != null ){
198                         if (newAppsListWithAdminRoles.appsRoles.size() >= 1) {
199                                 newAppRoles.append("User '" + newAppsListWithAdminRoles.orgUserId + "' has admin role to the apps = { ");
200                                 for (AppNameIdIsAdmin adminAppRole : newAppsListWithAdminRoles.appsRoles) {
201                                         if (adminAppRole.isAdmin) {
202                                                 newAppRoles.append(adminAppRole.appName + " ,");
203                                         }
204                                 }
205                                 newAppRoles.deleteCharAt(newAppRoles.length() - 1);
206                                 newAppRoles.append("}.");
207                         } else {
208                                 newAppRoles.append("User '" + newAppsListWithAdminRoles.orgUserId + "' has no Apps with Admin Role.");
209                         }
210                 }else{
211                         logger.error(EELFLoggerDelegate.errorLogger, "putAppWithUserRoleStateForUser: putAppsWithAdminRoleStateForUser result is null");
212                 }
213                 
214                 logger.info(EELFLoggerDelegate.errorLogger, newAppRoles.toString());
215
216                 EPUser user = EPUserUtils.getUserSession(request);
217                 boolean changesApplied = false;
218
219                 if (!adminRolesService.isSuperAdmin(user)) {
220                         EcompPortalUtils.setBadPermissions(user, response, "putAppsWithAdminRoleStateForUser");
221                 } else {
222                         changesApplied = adminRolesService.setAppsWithAdminRoleStateForUser(newAppsListWithAdminRoles);
223                         AuditLog auditLog = new AuditLog();
224                         auditLog.setUserId(user.getId());
225                         auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_UPDATE_ACCOUNT_ADMIN);
226                         auditLog.setAffectedRecordId(newAppsListWithAdminRoles.orgUserId);
227                         auditLog.setComments(EcompPortalUtils.truncateString(newAppRoles.toString(), PortalConstants.AUDIT_LOG_COMMENT_SIZE));
228                         auditService.logActivity(auditLog, null);
229
230                         MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
231                         MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
232                         EcompPortalUtils.calculateDateTimeDifferenceForLog(
233                                         MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
234                                         MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
235                         logger.info(EELFLoggerDelegate.auditLogger,
236                                         EPLogUtil.formatAuditLogMessage("UserRolesController.putAppsWithAdminRoleStateForUser",
237                                                         EcompAuditLog.CD_ACTIVITY_UPDATE_ACCOUNT_ADMIN, user.getOrgUserId(),
238                                                         newAppsListWithAdminRoles.orgUserId, newAppRoles.toString()));
239                         MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
240                         MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
241                         MDC.remove(SystemProperties.MDC_TIMER);
242                 }
243                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/adminAppsRoles", "put result =", changesApplied);
244
245                 return fieldsValidator;
246         }
247
248         /**
249          * It returns a list of user app roles for single app
250          * 
251          * @param request
252          *            HttpServletRequest
253          * @param response
254          *            HttpServletResponse
255          * @param orgUserId
256          *            user ID
257          * @param appid
258          *            application ID
259          * @param extRequestValue
260          *            set to false if request is from users page otherwise true
261          * @return List<RoleInAppForUser>
262          */
263         @RequestMapping(value = { "/portalApi/userAppRoles" }, method = {
264                         RequestMethod.GET }, produces = "application/json")
265         public List<RoleInAppForUser> getAppRolesForUser(HttpServletRequest request, @RequestParam("user") String orgUserId,
266                         @RequestParam("app") Long appid, @RequestParam("externalRequest") Boolean extRequestValue,
267                         HttpServletResponse response) {
268                 EPUser user = EPUserUtils.getUserSession(request);
269                 List<RoleInAppForUser> result = null;
270                 String feErrorString = "";
271                 if (!adminRolesService.isAccountAdmin(user)) {
272                         EcompPortalUtils.setBadPermissions(user, response, "getAppRolesForUser");
273                         feErrorString = EcompPortalUtils.getFEErrorString(true, response.getStatus());
274                 } else {
275                         if (EcompPortalUtils.legitimateUserId(orgUserId)) {
276                                 result = userRolesService.getAppRolesForUser(appid, orgUserId, extRequestValue);
277                                 int responseCode = EcompPortalUtils.getExternalAppResponseCode();
278                                 if (responseCode != 0 && responseCode != 200) {
279                                         // external error
280                                         response.setStatus(responseCode);
281                                         feErrorString = EcompPortalUtils.getFEErrorString(false, responseCode);
282                                 } else if (result == null) {
283                                         // If the result is null, there was an internal ecomp error
284                                         // in the service call.
285                                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
286                                         feErrorString = EcompPortalUtils.getFEErrorString(true,
287                                                         HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
288                                 }
289                         } else {
290                                 logger.info(EELFLoggerDelegate.errorLogger, "getAppRolesForUser - no Organization User ID");
291                                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
292                                 feErrorString = EcompPortalUtils.getFEErrorString(true, HttpServletResponse.SC_BAD_REQUEST);
293                         }
294                 }
295
296                 StringBuilder sbUserApps = new StringBuilder();
297                 if (result != null && result.size() >= 1) {
298                         sbUserApps.append("User '" + orgUserId + "' has Roles={");
299                         for (RoleInAppForUser appRole : result) {
300                                 if (appRole.isApplied) {
301                                         sbUserApps.append(appRole.roleName + ", ");
302                                 }
303                         }
304                         sbUserApps.append("} assigned to the appId '" + appid + "'.");
305                 } else {
306                         // Not sure creating an empty object will make any difference
307                         // but would like to give it a shot for defect #DE221057
308                         if (result == null) {
309                                 result = new ArrayList<RoleInAppForUser>();
310                         }
311                         sbUserApps.append("User '" + orgUserId + "' and appid " + appid + " has no roles");
312                 }
313                 logger.info(EELFLoggerDelegate.errorLogger, sbUserApps.toString());
314
315                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userAppRoles", "get result =", result);
316                 if (feErrorString != "") {
317                         logger.debug(EELFLoggerDelegate.debugLogger, "LR: FEErrorString to header: " + feErrorString);
318
319                         response.addHeader("FEErrorString", feErrorString);
320                         response.addHeader("Access-Control-Expose-Headers", "FEErrorString");
321                 }
322                 return result;
323         }
324
325         @RequestMapping(value = { "/portalApi/userAppRoles" }, method = {
326                         RequestMethod.PUT }, produces = "application/json")
327         public FieldsValidator putAppWithUserRoleStateForUser(HttpServletRequest request,
328                         @RequestBody AppWithRolesForUser newAppRolesForUser, HttpServletResponse response) {
329                 FieldsValidator fieldsValidator = new FieldsValidator();
330                 StringBuilder sbUserApps = new StringBuilder();
331                 if (newAppRolesForUser != null) {
332                         sbUserApps.append("User '" + newAppRolesForUser.orgUserId);
333                         if (newAppRolesForUser.appRoles != null && newAppRolesForUser.appRoles.size() >= 1) {
334                                 sbUserApps.append("' has roles = { ");
335                                 for (RoleInAppForUser appRole : newAppRolesForUser.appRoles) {
336                                         if (appRole.isApplied) {
337                                                 sbUserApps.append(appRole.roleName + " ,");
338                                         }
339                                 }
340                                 sbUserApps.deleteCharAt(sbUserApps.length() - 1);
341                                 sbUserApps.append("} assigned for the app " + newAppRolesForUser.appId);
342                         } else {
343                                 sbUserApps.append("' has no roles assigned for app " + newAppRolesForUser.appId);
344                         }
345                 }
346                 logger.info(EELFLoggerDelegate.applicationLogger, "putAppWithUserRoleStateForUser: {}", sbUserApps.toString());
347
348                 EPUser user = EPUserUtils.getUserSession(request);
349                 boolean changesApplied = false;
350                 if (!adminRolesService.isAccountAdmin(user)) {
351                         EcompPortalUtils.setBadPermissions(user, response, "putAppWithUserRoleStateForUser");
352                 } else if(newAppRolesForUser==null){
353                         logger.error(EELFLoggerDelegate.errorLogger, "putAppWithUserRoleStateForUser: newAppRolesForUser is null");
354                 } else{
355                         changesApplied = userRolesService.setAppWithUserRoleStateForUser(user, newAppRolesForUser);
356                         if (changesApplied) {
357                                 logger.info(EELFLoggerDelegate.applicationLogger,
358                                                 "putAppWithUserRoleStateForUser: succeeded for app {}, user {}", newAppRolesForUser.appId,
359                                                 newAppRolesForUser.orgUserId);
360
361                                 MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
362                                 AuditLog auditLog = new AuditLog();
363                                 auditLog.setUserId(user.getId());
364                                 auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_UPDATE_USER);
365                                 auditLog.setAffectedRecordId(newAppRolesForUser.orgUserId);
366                                 auditLog.setComments(EcompPortalUtils.truncateString(sbUserApps.toString(), PortalConstants.AUDIT_LOG_COMMENT_SIZE));
367                                 auditService.logActivity(auditLog, null);
368                                 
369                                 MDC.put(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP, EPEELFLoggerAdvice.getCurrentDateTimeUTC());
370                                 EcompPortalUtils.calculateDateTimeDifferenceForLog(
371                                                 MDC.get(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP),
372                                                 MDC.get(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP));
373                                 logger.info(EELFLoggerDelegate.auditLogger,
374                                                 EPLogUtil.formatAuditLogMessage("UserRolesController.putAppWithUserRoleStateForUser",
375                                                                 EcompAuditLog.CD_ACTIVITY_UPDATE_USER, user.getOrgUserId(),
376                                                                 newAppRolesForUser.orgUserId, sbUserApps.toString()));
377                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP);
378                                 MDC.remove(EPCommonSystemProperties.AUDITLOG_END_TIMESTAMP);
379                                 MDC.remove(SystemProperties.MDC_TIMER);
380                         } else {
381                                 logger.error(EELFLoggerDelegate.errorLogger,
382                                                 "putAppWithUserRoleStateForUser: failed for app {}, user {}", newAppRolesForUser.appId,
383                                                 newAppRolesForUser.orgUserId);
384                         }
385                 }
386
387                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userAppRoles", "put result =", changesApplied);
388                 return fieldsValidator;
389         }
390
391         @RequestMapping(value = { "/portalApi/updateRemoteUserProfile" }, method = {
392                         RequestMethod.GET }, produces = "application/json")
393         public PortalRestResponse<String> updateRemoteUserProfile(HttpServletRequest request,
394                         HttpServletResponse response) {
395
396                 String updateRemoteUserFlag = FAILURE;
397                 try {
398                         // saveNewUser = userService.saveNewUser(newUser);
399                         String orgUserId = request.getParameter("loginId");
400                         Long appId = Long.parseLong(request.getParameter("appId"));
401                         userRolesService.updateRemoteUserProfile(orgUserId, appId);
402
403                 } catch (Exception e) {
404                         return new PortalRestResponse<String>(PortalRestStatusEnum.OK, updateRemoteUserFlag, e.getMessage());
405                 }
406                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, updateRemoteUserFlag, "");
407
408         }
409
410         @RequestMapping(value = { "/portalApi/app/{appId}/users" }, method = {
411                         RequestMethod.GET }, produces = "application/json")
412         public List<UserApplicationRoles> getUsersFromAppEndpoint(HttpServletRequest request,
413                         @PathVariable("appId") Long appId) throws HTTPException {
414                 try {
415                         logger.debug(EELFLoggerDelegate.debugLogger, "/portalApi/app/{}/users was invoked", appId);
416                         List<UserApplicationRoles> appUsers = userRolesService.getUsersFromAppEndpoint(appId);
417                         return appUsers;
418                 } catch (Exception e) {
419                         logger.error(EELFLoggerDelegate.errorLogger,
420                                         "Exception occurred while performing UserRolesController.getUsersFromAppEndpoint. Details: "
421                                                         + EcompPortalUtils.getStackTrace(e));
422                         return new ArrayList<UserApplicationRoles>();
423                 }
424         }
425
426         @RequestMapping(value = { "/portalApi/app/{appId}/roles" }, method = {
427                         RequestMethod.GET }, produces = "application/json")
428         public List<EcompRole> testGetRoles(HttpServletRequest request, @PathVariable("appId") Long appId)
429                         throws HTTPException {
430                 EcompRole[] appRoles = applicationsRestClientService.get(EcompRole[].class, appId, "/roles");
431                 List<EcompRole> rolesList = Arrays.asList(appRoles);
432                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/app/{appId}/roles", "response for appId=" + appId,
433                                 rolesList);
434
435                 return rolesList;
436         }
437
438         @RequestMapping(value = { "/portalApi/admin/import/app/{appId}/roles" }, method = {
439                         RequestMethod.GET }, produces = "application/json")
440         public List<EPRole> importRolesFromRemoteApplication(HttpServletRequest request, @PathVariable("appId") Long appId)
441                         throws HTTPException {
442                 List<EPRole> rolesList = userRolesService.importRolesFromRemoteApplication(appId);
443                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/admin/import/app/{appId}/roles",
444                                 "response for appId=" + appId, rolesList);
445
446                 return rolesList;
447         }
448
449         @RequestMapping(value = { "/portalApi/app/{appId}/user/{orgUserId}/roles" }, method = {
450                         RequestMethod.GET }, produces = "application/json")
451         public EcompRole testGetRoles(HttpServletRequest request, @PathVariable("appId") Long appId,
452                         @PathVariable("orgUserId") String orgUserId) throws Exception {
453                 if (!EcompPortalUtils.legitimateUserId(orgUserId)) {
454                         String msg = "Error /user/<user>/roles not legitimate orgUserId = " + orgUserId;
455                         logger.error(EELFLoggerDelegate.errorLogger, msg);
456                         throw new Exception(msg);
457                 }
458                 EcompRole[] roles = applicationsRestClientService.get(EcompRole[].class, appId,
459                                 String.format("/user/%s/roles", orgUserId));
460                 if (roles.length != 1) {
461                         String msg = "Error /user/<user>/roles returned array. expected size 1 recieved size = " + roles.length;
462                         logger.error(EELFLoggerDelegate.errorLogger, msg);
463                         throw new Exception(msg);
464                 }
465
466                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/app/{appId}/user/{orgUserId}/roles",
467                                 "response for appId='" + appId + "' and orgUserId='" + orgUserId + "'", roles[0]);
468                 return roles[0];
469         }
470
471         @RequestMapping(value = { "/portalApi/saveUserAppRoles" }, method = {
472                         RequestMethod.PUT }, produces = "application/json")
473         public FieldsValidator putAppWithUserRoleRequest(HttpServletRequest request,
474                         @RequestBody AppWithRolesForUser newAppRolesForUser, HttpServletResponse response) {
475                 FieldsValidator fieldsValidator = null;
476                 try {
477
478                         EPUser user = EPUserUtils.getUserSession(request);
479                         fieldsValidator = userRolesService.putUserAppRolesRequest(newAppRolesForUser, user);
480                         response.setStatus(fieldsValidator.httpStatusCode.intValue());
481
482                 } catch (Exception e) {
483                         logger.error(EELFLoggerDelegate.errorLogger, "putAppWithUserRoleRequest failed", e);
484
485                 }
486                 // return fieldsValidator;
487                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/saveUserAppRoles", "PUT result =",
488                                 response.getStatus());
489                 return fieldsValidator;
490         }
491
492         @RequestMapping(value = { "/portalApi/appCatalogRoles" }, method = {
493                         RequestMethod.GET }, produces = "application/json")
494         public List<EPUserAppCatalogRoles> getUserAppCatalogRoles(HttpServletRequest request,
495                         @RequestParam("appName") String appName) {
496                 EPUser user = EPUserUtils.getUserSession(request);
497                 List<EPUserAppCatalogRoles> userAppRoleList = null;
498                 try {
499                         userAppRoleList = userRolesService.getUserAppCatalogRoles(user, appName);
500                 } catch (Exception e) {
501                         logger.error(EELFLoggerDelegate.errorLogger, "putUserWidgetsSortPref failed", e);
502
503                 }
504                 Collections.sort(userAppRoleList, getUserAppCatalogRolesComparator);
505                 EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userApplicationRoles", "result =", userAppRoleList);
506
507                 return userAppRoleList;
508
509         }
510
511         private Comparator<EPUserAppCatalogRoles> getUserAppCatalogRolesComparator = new Comparator<EPUserAppCatalogRoles>() {
512                 public int compare(EPUserAppCatalogRoles o1, EPUserAppCatalogRoles o2) {
513                         return o1.getRolename().compareTo(o2.getRolename());
514                 }
515         };
516
517         @RequestMapping(value = "/portalApi/externalRequestAccessSystem", method = RequestMethod.GET, produces = "application/json")
518         public ExternalSystemAccess readExternalRequestAccess(HttpServletRequest request) {
519                 ExternalSystemAccess result = null;
520                 try {
521                         result = userRolesService.getExternalRequestAccess();
522                         EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/externalRequestAccessSystem", "GET result =",
523                                         result);
524                 } catch (Exception e) {
525                         logger.error(EELFLoggerDelegate.errorLogger, "readExternalRequestAccess failed: " + e.getMessage());
526                 }
527                 return result;
528         }
529
530 }