summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/UserRolesController.java
blob: 9f89051df3de1448822bcc1f22232bac247623b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 *
 */
package org.onap.portalapp.portal.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.cxf.transport.http.HTTPException;
import org.onap.portalapp.controller.EPRestrictedBaseController;
import org.onap.portalapp.portal.domain.EPRole;
import org.onap.portalapp.portal.domain.EPUser;
import org.onap.portalapp.portal.domain.EPUserAppCatalogRoles;
import org.onap.portalapp.portal.domain.EcompAuditLog;
import org.onap.portalapp.portal.domain.ExternalSystemAccess;
import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
import org.onap.portalapp.portal.logging.aop.EPAuditLog;
import org.onap.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
import org.onap.portalapp.portal.logging.logic.EPLogUtil;
import org.onap.portalapp.portal.service.AdminRolesService;
import org.onap.portalapp.portal.service.ApplicationsRestClientService;
import org.onap.portalapp.portal.service.SearchService;
import org.onap.portalapp.portal.service.UserRolesService;
import org.onap.portalapp.portal.transport.AppNameIdIsAdmin;
import org.onap.portalapp.portal.transport.AppWithRolesForUser;
import org.onap.portalapp.portal.transport.AppsListWithAdminRole;
import org.onap.portalapp.portal.transport.ExternalRequestFieldsValidator;
import org.onap.portalapp.portal.transport.FieldsValidator;
import org.onap.portalapp.portal.transport.RoleInAppForUser;
import org.onap.portalapp.portal.transport.UserApplicationRoles;
import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
import org.onap.portalapp.portal.utils.EcompPortalUtils;
import org.onap.portalapp.portal.utils.PortalConstants;
import org.onap.portalapp.util.EPUserUtils;
import org.onap.portalsdk.core.domain.AuditLog;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.restful.domain.EcompRole;
import org.onap.portalsdk.core.service.AuditService;
import org.onap.portalsdk.core.service.DataAccessService;
import org.onap.portalsdk.core.util.SystemProperties;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
@EPAuditLog
public class UserRolesController extends EPRestrictedBaseController {

    private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserRolesController.class);

    @Autowired
    private SearchService searchService;
    @Autowired
    private AdminRolesService adminRolesService;
    private @Autowired UserRolesService userRolesService;
    @Autowired
    private ApplicationsRestClientService applicationsRestClientService;
    @Autowired
    private AuditService auditService;
    @Autowired
	private DataAccessService dataAccessService;

    private static final String FAILURE = "failure";

    /**
     * RESTful service method to fetch users in the WebPhone external service
     *
     * @param request HttpServletRequest
     * @param searchString search string
     * @param response HttpServletResponse
     * @return array of found users as json
     */
    @GetMapping(value = { "/portalApi/queryUsers" }, produces = "application/json")
    public String getPhoneBookSearchResult(HttpServletRequest request, @RequestParam("search") String searchString,
            HttpServletResponse response) {
        EPUser user = EPUserUtils.getUserSession(request);
        String searchResult = null;
        if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdmin(user)
                && !adminRolesService.isRoleAdmin(user)) {
            EcompPortalUtils.setBadPermissions(user, response, "getPhoneBookSearchResult");
        } else {
            searchString = searchString.trim();
            if (searchString.length() > 2) {
                searchResult = searchService.searchUsersInPhoneBook(searchString);
            } else {
                logger.info(EELFLoggerDelegate.errorLogger,
                        "getPhoneBookSearchResult - too short search string: " + searchString);
            }
        }
        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/queryUsers", "result =", searchResult);

        return searchResult;
    }

    /**
     * RESTful service method to fetch applications where user is admin
     *
     * @param request HttpServletRequest
     * @param orgUserId search string
     * @param response HttpServletResponse
     * @return for GET: array of all applications with boolean isAdmin=true/false for each application
     */
    @GetMapping(value = { "/portalApi/adminAppsRoles" }, produces = "application/json")
    public AppsListWithAdminRole getAppsWithAdminRoleStateForUser(HttpServletRequest request,
            @RequestParam("user") String orgUserId, HttpServletResponse response) {

        EPUser user = EPUserUtils.getUserSession(request);
        AppsListWithAdminRole result = null;
        if (!adminRolesService.isSuperAdmin(user)) {
            EcompPortalUtils.setBadPermissions(user, response, "getAppsWithAdminRoleStateForUser");
        } else {
            if (EcompPortalUtils.legitimateUserId(orgUserId)) {
                result = adminRolesService.getAppsWithAdminRoleStateForUser(orgUserId);
            } else {
                logger.info(EELFLoggerDelegate.errorLogger,
                        "getAppsWithAdminRoleStateForUser - parms error, no Organization User ID");
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            }
        }

        StringBuilder adminAppRoles = new StringBuilder();
        if (result != null) {
            if (!result.appsRoles.isEmpty()) {
                adminAppRoles.append("User '" + result.orgUserId + "' has admin role to the apps = {");
                for (AppNameIdIsAdmin adminAppRole : result.appsRoles) {
                    if (adminAppRole.isAdmin) {
                        adminAppRoles.append(adminAppRole.appName + ", ");
                    }
                }
                adminAppRoles.append("}.");
            } else {
                adminAppRoles.append("User '" + result.orgUserId + "' has no Apps with Admin Role.");
            }
        } else {
            logger.error(EELFLoggerDelegate.errorLogger,
                    "putAppWithUserRoleStateForUser: getAppsWithAdminRoleStateForUser result is null");
        }

        logger.info(EELFLoggerDelegate.errorLogger, adminAppRoles.toString());

        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/adminAppsRoles", "get result =", result);

        return result;
    }

    /**
     *
     * @param request HttpServletRequest
     * @param newAppsListWithAdminRoles new apps
     * @param response HttpServletResponse
     * @return FieldsValidator
     */
    @PutMapping(value = { "/portalApi/adminAppsRoles" }, produces = "application/json")
    public FieldsValidator putAppsWithAdminRoleStateForUser(HttpServletRequest request,
            @RequestBody AppsListWithAdminRole newAppsListWithAdminRoles, HttpServletResponse response) {

        // newAppsListWithAdminRoles.appsRoles
        FieldsValidator fieldsValidator = new FieldsValidator();
        StringBuilder newAppRoles = new StringBuilder();
        if (newAppsListWithAdminRoles != null) {
            if (!newAppsListWithAdminRoles.appsRoles.isEmpty()) {
                newAppRoles
                        .append("User '" + newAppsListWithAdminRoles.orgUserId + "' has admin role to the apps = { ");
                for (AppNameIdIsAdmin adminAppRole : newAppsListWithAdminRoles.appsRoles) {
                    if (adminAppRole.isAdmin) {
                        newAppRoles.append(adminAppRole.appName + " ,");
                    }
                }
                newAppRoles.deleteCharAt(newAppRoles.length() - 1);
                newAppRoles.append("}.");
            } else {
                newAppRoles.append("User '" + newAppsListWithAdminRoles.orgUserId + "' has no Apps with Admin Role.");
            }
        } else {
            logger.error(EELFLoggerDelegate.errorLogger,
                    "putAppWithUserRoleStateForUser: putAppsWithAdminRoleStateForUser result is null");
            fieldsValidator.httpStatusCode = (long) HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        }

        logger.info(EELFLoggerDelegate.errorLogger, newAppRoles.toString());

        EPUser user = EPUserUtils.getUserSession(request);
        boolean changesApplied = false;

        if (!adminRolesService.isSuperAdmin(user)) {
            EcompPortalUtils.setBadPermissions(user, response, "putAppsWithAdminRoleStateForUser");
        } else {
            changesApplied = adminRolesService.setAppsWithAdminRoleStateForUser(newAppsListWithAdminRoles);
            AuditLog auditLog = new AuditLog();
            auditLog.setUserId(user.getId());
            auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_UPDATE_ACCOUNT_ADMIN);
            if (newAppsListWithAdminRoles != null) {
                auditLog.setAffectedRecordId(newAppsListWithAdminRoles.orgUserId);
            }
            auditLog.setComments(
                    EcompPortalUtils.truncateString(newAppRoles.toString(), PortalConstants.AUDIT_LOG_COMMENT_SIZE));
            auditService.logActivity(auditLog, null);
			String auditMessageInfo = EPLogUtil.formatAuditLogMessage("UserRolesController.putAppsWithAdminRoleStateForUser",
                    EcompAuditLog.CD_ACTIVITY_UPDATE_ACCOUNT_ADMIN, user.getOrgUserId(),
                    newAppsListWithAdminRoles.orgUserId, newAppRoles.toString());		

			EPLogUtil.logAuditMessage(logger, auditMessageInfo);
            
        }
        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/adminAppsRoles", "put result =", changesApplied);

        return fieldsValidator;
    }

    /**
     * It returns a list of user app roles for single app
     *
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param orgUserId user ID
     * @param appid application ID
     * @param extRequestValue set to false if request is from users page otherwise true
     * @return List<RoleInAppForUser>
     */
    @GetMapping(value = { "/portalApi/userAppRoles" }, produces = "application/json")
    public List<RoleInAppForUser> getAppRolesForUser(HttpServletRequest request, @RequestParam("user") String orgUserId,
            @RequestParam("app") Long appid, @RequestParam("externalRequest") Boolean extRequestValue,
            @RequestParam("isSystemUser") Boolean isSystemUser,
            HttpServletResponse response) {
        EPUser user = EPUserUtils.getUserSession(request);
        List<RoleInAppForUser> result = null;
        String feErrorString = "";
        if (!adminRolesService.isAccountAdmin(user) && !adminRolesService.isRoleAdmin(user)) {
            logger.debug(EELFLoggerDelegate.debugLogger,
                    "getAppRolesForUser: Accountadminpermissioncheck {}, RoleAdmincheck {}",
                    adminRolesService.isAccountAdmin(user), adminRolesService.isRoleAdmin(user));
            EcompPortalUtils.setBadPermissions(user, response, "getAppRolesForUser");
            feErrorString = EcompPortalUtils.getFEErrorString(true, response.getStatus());
        } else {
        	try {
        		if(orgUserId!=null) {
	        		EPUser localUser  = getUserInfo(orgUserId,applicationsRestClientService);
	        		if(localUser !=null) {
	        			if(localUser.isSystemUser()) {
	        				isSystemUser = true;
	        			}
	        		}
        		}
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger, "isSystemUser update failed", e);
			}
            if ((!isSystemUser && EcompPortalUtils.legitimateUserId(orgUserId)) || isSystemUser) {
                result = userRolesService.getAppRolesForUser(appid, orgUserId, extRequestValue, user);
                logger.debug(EELFLoggerDelegate.debugLogger, "getAppRolesForUser: result {}, appId {}", result, appid);
                int responseCode = EcompPortalUtils.getExternalAppResponseCode();
                if (responseCode != 0 && responseCode != 200) {
                    // external error
                    response.setStatus(responseCode);
                    feErrorString = EcompPortalUtils.getFEErrorString(false, responseCode);
                } else if (result == null) {
                    // If the result is null, there was an internal onap error
                    // in the service call.
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    feErrorString = EcompPortalUtils.getFEErrorString(true,
                            HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                }
            } else {
                logger.info(EELFLoggerDelegate.errorLogger, "getAppRolesForUser - no Organization User ID");
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                feErrorString = EcompPortalUtils.getFEErrorString(true, HttpServletResponse.SC_BAD_REQUEST);
            }
        }

        StringBuilder sbUserApps = new StringBuilder();
        if (result != null && !result.isEmpty()) {
            sbUserApps.append("User '" + orgUserId + "' has Roles={");
            for (RoleInAppForUser appRole : result) {
                if (appRole.isApplied) {
                    sbUserApps.append(appRole.roleName + ", ");
                }
            }
            sbUserApps.append("} assigned to the appId '" + appid + "'.");
        } else {
            // Not sure creating an empty object will make any difference
            // but would like to give it a shot for defect #DE221057
            if (result == null) {
                result = new ArrayList<>();
            }
            sbUserApps.append("User '" + orgUserId + "' and appid " + appid + " has no roles");
        }
        logger.info(EELFLoggerDelegate.errorLogger, sbUserApps.toString());

        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userAppRoles", "get result =", result);
        if (feErrorString != "") {
            logger.debug(EELFLoggerDelegate.debugLogger, "LR: FEErrorString to header: " + feErrorString);

            response.addHeader("FEErrorString", feErrorString);
            response.addHeader("Access-Control-Expose-Headers", "FEErrorString");
        }
        return result;
    }

    @PutMapping(value = { "/portalApi/userAppRoles" }, produces = "application/json")
    public PortalRestResponse<String> putAppWithUserRoleStateForUser(HttpServletRequest request,
            @RequestBody AppWithRolesForUser newAppRolesForUser, HttpServletResponse response) {
        // FieldsValidator fieldsValidator = new FieldsValidator();
        PortalRestResponse<String> portalResponse = new PortalRestResponse<>();
        StringBuilder sbUserApps = new StringBuilder();
        if (newAppRolesForUser != null) {
            sbUserApps.append("User '" + newAppRolesForUser.getOrgUserId());
            if (newAppRolesForUser.getAppId() != null && !newAppRolesForUser.getAppRoles().isEmpty()) {
                sbUserApps.append("' has roles = { ");
                for (RoleInAppForUser appRole : newAppRolesForUser.getAppRoles()) {
                    if (appRole.isApplied) {
                        sbUserApps.append(appRole.roleName + " ,");
                    }
                }
                sbUserApps.deleteCharAt(sbUserApps.length() - 1);
                sbUserApps.append("} assigned for the app " + newAppRolesForUser.getAppId());
            } else {
                sbUserApps.append("' has no roles assigned for app " + newAppRolesForUser.getAppId());
            }
        }
        logger.info(EELFLoggerDelegate.applicationLogger, "putAppWithUserRoleStateForUser: {}", sbUserApps.toString());

        EPUser user = EPUserUtils.getUserSession(request);
        // boolean changesApplied = false;
        ExternalRequestFieldsValidator changesApplied = null;

        if (!adminRolesService.isAccountAdmin(user) && !adminRolesService.isRoleAdmin(user)) {
            EcompPortalUtils.setBadPermissions(user, response, "putAppWithUserRoleStateForUser");
        } else if (newAppRolesForUser == null) {
            logger.error(EELFLoggerDelegate.errorLogger, "putAppWithUserRoleStateForUser: newAppRolesForUser is null");
        } else {
            changesApplied = userRolesService.setAppWithUserRoleStateForUser(user, newAppRolesForUser);
            try {
                if (changesApplied.isResult()) {
                    logger.info(EELFLoggerDelegate.applicationLogger,
                            "putAppWithUserRoleStateForUser: succeeded for app {}, user {}",
                            newAppRolesForUser.getAppId(),
                            newAppRolesForUser.getAppId());

                    MDC.put(EPCommonSystemProperties.AUDITLOG_BEGIN_TIMESTAMP,
                            EPEELFLoggerAdvice.getCurrentDateTimeUTC());
                    AuditLog auditLog = new AuditLog();
                    auditLog.setUserId(user.getId());
                    auditLog.setActivityCode(EcompAuditLog.CD_ACTIVITY_UPDATE_USER);
                    auditLog.setAffectedRecordId(newAppRolesForUser.getOrgUserId());
                    auditLog.setComments(EcompPortalUtils.truncateString(sbUserApps.toString(),
                            PortalConstants.AUDIT_LOG_COMMENT_SIZE));
                    auditService.logActivity(auditLog, null);
        			String auditMessageInfo = EPLogUtil.formatAuditLogMessage("UserRolesController.putAppWithUserRoleStateForUser",
                            EcompAuditLog.CD_ACTIVITY_UPDATE_USER, user.getOrgUserId(),
                            newAppRolesForUser.getOrgUserId(), sbUserApps.toString());		

        			EPLogUtil.logAuditMessage(logger, auditMessageInfo);

        			portalResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);

                }
                if (!changesApplied.isResult())
                    throw new Exception(changesApplied.getDetailMessage());

            } catch (Exception e) {
                logger.error(EELFLoggerDelegate.errorLogger,
                        "putAppWithUserRoleStateForUser: failed for app {}, user {}", newAppRolesForUser.getAppId(),
                        newAppRolesForUser.getOrgUserId(), e);
                portalResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.getMessage(), null);
            }
        }

        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userAppRoles", "put result =", changesApplied);
        return portalResponse;
    }

    @GetMapping(value = { "/portalApi/updateRemoteUserProfile" }, produces = "application/json")
    public PortalRestResponse<String> updateRemoteUserProfile(HttpServletRequest request,
            HttpServletResponse response) {

        String updateRemoteUserFlag = FAILURE;
        try {
            // saveNewUser = userService.saveNewUser(newUser);
            String orgUserId = request.getParameter("loginId");
            Long appId = Long.parseLong(request.getParameter("appId"));
            userRolesService.updateRemoteUserProfile(orgUserId, appId);

        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "updateRemoteUserProfile failed", e);
            return new PortalRestResponse<>(PortalRestStatusEnum.OK, updateRemoteUserFlag, e.getMessage());
        }
        return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, updateRemoteUserFlag, "");

    }

    @GetMapping(value = { "/portalApi/app/{appId}/users" }, produces = "application/json")
    public List<UserApplicationRoles> getUsersFromAppEndpoint(HttpServletRequest request,
            @PathVariable("appId") Long appId) throws HTTPException {
        try {
            logger.debug(EELFLoggerDelegate.debugLogger, "/portalApi/app/{}/users was invoked", appId);
            return userRolesService.getUsersFromAppEndpoint(appId);
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "getUsersFromAppEndpoint failed", e);
            return new ArrayList<>();
        }
    }

    @GetMapping(value = { "/portalApi/app/{appId}/roles" }, produces = "application/json")
    public List<EcompRole> testGetRoles(HttpServletRequest request, @PathVariable("appId") Long appId)
            throws HTTPException {
        EcompRole[] appRoles = applicationsRestClientService.get(EcompRole[].class, appId, "/roles");
        List<EcompRole> rolesList = Arrays.asList(appRoles);
        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/app/{appId}/roles", "response for appId=" + appId,
                rolesList);

        return rolesList;
    }

    @GetMapping(value = { "/portalApi/admin/import/app/{appId}/roles" }, produces = "application/json")
    public List<EPRole> importRolesFromRemoteApplication(HttpServletRequest request, @PathVariable("appId") Long appId)
            throws HTTPException {
        List<EPRole> rolesList = userRolesService.importRolesFromRemoteApplication(appId);
        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/admin/import/app/{appId}/roles",
                "response for appId=" + appId, rolesList);

        return rolesList;
    }

    @GetMapping(value = { "/portalApi/app/{appId}/user/{orgUserId}/roles" }, produces = "application/json")
    public EcompRole testGetRoles(HttpServletRequest request, @PathVariable("appId") Long appId,
            @PathVariable("orgUserId") String orgUserId) throws Exception {
        if (!EcompPortalUtils.legitimateUserId(orgUserId)) {
            String msg = "Error /user/<user>/roles not legitimate orgUserId = " + orgUserId;
            logger.error(EELFLoggerDelegate.errorLogger, msg);
            throw new Exception(msg);
        }
        EcompRole[] roles = applicationsRestClientService.get(EcompRole[].class, appId,
                String.format("/user/%s/roles", orgUserId));
        if (roles.length != 1) {
            String msg = "Error /user/<user>/roles returned array. expected size 1 recieved size = " + roles.length;
            logger.error(EELFLoggerDelegate.errorLogger, msg);
            throw new Exception(msg);
        }

        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/app/{appId}/user/{orgUserId}/roles",
                "response for appId='" + appId + "' and orgUserId='" + orgUserId + "'", roles[0]);
        return roles[0];
    }

    @PutMapping(value = { "/portalApi/saveUserAppRoles" }, produces = "application/json")
    public FieldsValidator putAppWithUserRoleRequest(HttpServletRequest request,
            @RequestBody AppWithRolesForUser newAppRolesForUser, HttpServletResponse response) {
        FieldsValidator fieldsValidator = null;
        try {

            EPUser user = EPUserUtils.getUserSession(request);
            fieldsValidator = userRolesService.putUserAppRolesRequest(newAppRolesForUser, user);
            response.setStatus(fieldsValidator.httpStatusCode.intValue());

        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "putAppWithUserRoleRequest failed", e);

        }
        // return fieldsValidator;
        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/saveUserAppRoles", "PUT result =",
                response.getStatus());
        return fieldsValidator;
    }

    @GetMapping(value = { "/portalApi/appCatalogRoles" }, produces = "application/json")
    public List<EPUserAppCatalogRoles> getUserAppCatalogRoles(HttpServletRequest request,
            @RequestParam("appName") String appName) {
        EPUser user = EPUserUtils.getUserSession(request);
        List<EPUserAppCatalogRoles> userAppRoleList = null;
        try {
            userAppRoleList = userRolesService.getUserAppCatalogRoles(user, appName);
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "putUserWidgetsSortPref failed", e);

        }
        Collections.sort(userAppRoleList, getUserAppCatalogRolesComparator);
        EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userApplicationRoles", "result =", userAppRoleList);

        return userAppRoleList;

    }

    private Comparator<EPUserAppCatalogRoles> getUserAppCatalogRolesComparator =
            new Comparator<EPUserAppCatalogRoles>() {
                public int compare(EPUserAppCatalogRoles o1, EPUserAppCatalogRoles o2) {
                    return o1.getRolename().compareTo(o2.getRolename());
                }
            };

    @GetMapping(value = "/portalApi/externalRequestAccessSystem",
            produces = "application/json")
    public ExternalSystemAccess readExternalRequestAccess(HttpServletRequest request) {
        ExternalSystemAccess result = null;
        try {
            result = userRolesService.getExternalRequestAccess();
            EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/externalRequestAccessSystem", "GET result =",
                    result);
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "readExternalRequestAccess failed: " + e.getMessage());
        }
        return result;
    }

    @GetMapping(value = { "/portalApi/checkIfUserIsSuperAdmin" }, produces = "application/json")
    public boolean checkIfUserIsSuperAdmin(HttpServletRequest request,
            HttpServletResponse response) {
        EPUser user = EPUserUtils.getUserSession(request);
        boolean isSuperAdmin = false;
        try {
            isSuperAdmin = adminRolesService.isSuperAdmin(user);
        } catch (Exception e) {
            logger.error(EELFLoggerDelegate.errorLogger, "checkIfUserIsSuperAdmin failed: " + e.getMessage());
        }
        return isSuperAdmin;
    }
    
    /**
	 * 
	 * @param userId
	 * @param app
	 * @param applicationsRestClientService
	 * @return EPUser
	 * @throws HTTPException
	 */
	protected EPUser getUserInfo(String userId, ApplicationsRestClientService applicationsRestClientService)
			throws HTTPException {
		@SuppressWarnings("unchecked")
		List<EPUser> userList = (List<EPUser>) dataAccessService
				.executeQuery("from EPUser where orgUserId='" + userId + "'", null);
		if (userList != null && !userList.isEmpty())
			return userList.get(0);
		else
			return null;
	}
}