2  * ================================================================================
\r 
   4  * ================================================================================
\r 
   5  * Copyright (C) 2017 AT&T Intellectual Property
\r 
   6  * ================================================================================
\r 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   8  * you may not use this file except in compliance with the License.
\r 
   9  * You may obtain a copy of the License at
\r 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  13  * Unless required by applicable law or agreed to in writing, software
\r 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  16  * See the License for the specific language governing permissions and
\r 
  17  * limitations under the License.
\r 
  18  * ================================================================================
\r 
  20 package org.openecomp.portalapp.portal.service;
\r 
  22 import java.util.List;
\r 
  24 import org.springframework.beans.factory.annotation.Autowired;
\r 
  25 import org.springframework.context.annotation.EnableAspectJAutoProxy;
\r 
  26 import org.springframework.stereotype.Service;
\r 
  27 import org.springframework.transaction.annotation.Transactional;
\r 
  29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r 
  30 import org.openecomp.portalsdk.core.service.DataAccessService;
\r 
  31 import org.openecomp.portalapp.portal.domain.EPApp;
\r 
  32 import org.openecomp.portalapp.portal.domain.EPUser;
\r 
  33 import org.openecomp.portalapp.portal.domain.EPUserApp;
\r 
  34 import org.openecomp.portalapp.portal.domain.PersUserAppSelection;
\r 
  35 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
\r 
  37 @Service("persUserAppService")
\r 
  39 @org.springframework.context.annotation.Configuration
\r 
  40 @EnableAspectJAutoProxy
\r 
  42 public class PersUserAppServiceImpl implements PersUserAppService {
\r 
  44         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PersUserAppServiceImpl.class);
\r 
  47         private DataAccessService dataAccessService;
\r 
  49         private AdminRolesService adminRolesService;
\r 
  51         private UserRolesService userRolesService;
\r 
  56          * @see org.openecomp.portalapp.portal.service.UserAppSelectService#
\r 
  57          * setAppCatalogSelection(org.openecomp.portalapp.portal.domain.EPUser,
\r 
  58          * org.openecomp.portalapp.portal.transport.AppCatalogSelection)
\r 
  61         public void setPersUserAppValue(EPUser user, EPApp app, boolean select, boolean pending) {
\r 
  62                 if (user == null || app == null)
\r 
  63                         throw new IllegalArgumentException("setPersUserAppValue: Null values");
\r 
  65                 // Find the record for this user-app combo, if any
\r 
  66                 String filter = " where user_id = " + Long.toString(user.getId()) + " and app_id = "
\r 
  67                                 + Long.toString(app.getId());
\r 
  68                 @SuppressWarnings("unchecked")
\r 
  69                 List<PersUserAppSelection> persList = dataAccessService.getList(PersUserAppSelection.class, filter, null, null);
\r 
  71                 // Key constraint limits to 1 row
\r 
  72                 PersUserAppSelection persRow = null;
\r 
  73                 if (persList.size() == 1)
\r 
  74                         persRow = persList.get(0);
\r 
  76                         persRow = new PersUserAppSelection(null, user.getId(), app.getId(), null);
\r 
  78                 if (app.getOpen()) {
\r 
  79                         // Pending status is not meaningful for open apps.
\r 
  81                                 logger.error(EELFLoggerDelegate.errorLogger,
\r 
  82                                                 "setPersUserAppValue: invalid request, ignoring set-pending for open app");
\r 
  84                         // Open apps have same behavior for regular and admin users
\r 
  86                                 // Selection of an open app requires a record
\r 
  87                                 persRow.setStatusCode("S"); // show
\r 
  88                                 dataAccessService.saveDomainObject(persRow, null);
\r 
  90                                 // De-selection of an open app requires no record
\r 
  91                                 if (persRow.getId() != null)
\r 
  92                                         dataAccessService.deleteDomainObject(persRow, null);
\r 
  97                         // Pending overrides select.
\r 
  99                                 persRow.setStatusCode("P");
\r 
 100                                 dataAccessService.saveDomainObject(persRow, null);
\r 
 102                                 // Behavior depends on Portal (super) admin status, bcos an
\r 
 103                                 // admin can force an app onto the dashboard.
\r 
 104                                 boolean isPortalAdmin = adminRolesService.isSuperAdmin(user);
\r 
 105                                 boolean adminUserHasAppRole = false;
\r 
 106                                 if (isPortalAdmin) {
\r 
 107                                         List<EPUserApp> roles = userRolesService.getCachedAppRolesForUser(app.getId(), user.getId());
\r 
 108                                         adminUserHasAppRole = (roles.size() > 0);
\r 
 109                                         logger.debug(EELFLoggerDelegate.debugLogger, "setPersUserAppValue: app {}, admin user {}, role count {}",
\r 
 110                                                         app.getId(), user.getId(), roles.size());
\r 
 114                                         if (isPortalAdmin && !adminUserHasAppRole) {
\r 
 115                                                 // The special case: portal admin, no role
\r 
 116                                                 persRow.setStatusCode("S"); // show
\r 
 117                                                 dataAccessService.saveDomainObject(persRow, null);
\r 
 119                                                 // User has role-based access to the app.
\r 
 120                                                 // Showing an accessible app requires no record.
\r 
 121                                                 if (persRow.getId() != null)
\r 
 122                                                         dataAccessService.deleteDomainObject(persRow, null);
\r 
 126                                         if (isPortalAdmin && !adminUserHasAppRole) {
\r 
 127                                                 // The special case: portal admin, no role
\r 
 128                                                 if (persRow.getId() != null)
\r 
 129                                                         dataAccessService.deleteDomainObject(persRow, null);
\r 
 131                                                 // User has role-based access to the app.
\r 
 132                                                 // Hiding an accessible app requires a record
\r 
 133                                                 persRow.setStatusCode("H"); // hide
\r 
 134                                                 dataAccessService.saveDomainObject(persRow, null);
\r