243b61eb0a4281a031a61f252a21cebc48d60836
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / PersUserAppServiceImpl.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
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.service;
21
22 import java.util.List;
23
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.context.annotation.EnableAspectJAutoProxy;
26 import org.springframework.stereotype.Service;
27 import org.springframework.transaction.annotation.Transactional;
28
29 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.openecomp.portalsdk.core.service.DataAccessService;
31 import org.openecomp.portalapp.portal.domain.EPApp;
32 import org.openecomp.portalapp.portal.domain.EPUser;
33 import org.openecomp.portalapp.portal.domain.EPUserApp;
34 import org.openecomp.portalapp.portal.domain.PersUserAppSelection;
35 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
36
37 @Service("persUserAppService")
38 @Transactional
39 @org.springframework.context.annotation.Configuration
40 @EnableAspectJAutoProxy
41 @EPMetricsLog
42 public class PersUserAppServiceImpl implements PersUserAppService {
43
44         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PersUserAppServiceImpl.class);
45
46         @Autowired
47         private DataAccessService dataAccessService;
48         @Autowired
49         private AdminRolesService adminRolesService;
50         @Autowired
51         private UserRolesService userRolesService;
52
53         /*
54          * (non-Javadoc)
55          * 
56          * @see org.openecomp.portalapp.portal.service.UserAppSelectService#
57          * setAppCatalogSelection(org.openecomp.portalapp.portal.domain.EPUser,
58          * org.openecomp.portalapp.portal.transport.AppCatalogSelection)
59          */
60         @Override
61         public void setPersUserAppValue(EPUser user, EPApp app, boolean select, boolean pending) {
62                 if (user == null || app == null)
63                         throw new IllegalArgumentException("setPersUserAppValue: Null values");
64
65                 // Find the record for this user-app combo, if any
66                 String filter = " where user_id = " + Long.toString(user.getId()) + " and app_id = "
67                                 + Long.toString(app.getId());
68                 @SuppressWarnings("unchecked")
69                 List<PersUserAppSelection> persList = dataAccessService.getList(PersUserAppSelection.class, filter, null, null);
70
71                 // Key constraint limits to 1 row
72                 PersUserAppSelection persRow = null;
73                 if (persList.size() == 1)
74                         persRow = persList.get(0);
75                 else
76                         persRow = new PersUserAppSelection(null, user.getId(), app.getId(), null);
77
78                 if (app.getOpen()) {
79                         // Pending status is not meaningful for open apps.
80                         if (pending)
81                                 logger.error(EELFLoggerDelegate.errorLogger,
82                                                 "setPersUserAppValue: invalid request, ignoring set-pending for open app");
83
84                         // Open apps have same behavior for regular and admin users
85                         if (select) {
86                                 // Selection of an open app requires a record
87                                 persRow.setStatusCode("S"); // show
88                                 dataAccessService.saveDomainObject(persRow, null);
89                         } else {
90                                 // De-selection of an open app requires no record
91                                 if (persRow.getId() != null)
92                                         dataAccessService.deleteDomainObject(persRow, null);
93                         }
94                 } else {
95                         // Non-open app.
96
97                         // Pending overrides select.
98                         if (pending) {
99                                 persRow.setStatusCode("P");
100                                 dataAccessService.saveDomainObject(persRow, null);
101                         } else {
102                                 // Behavior depends on Portal (super) admin status, bcos an
103                                 // admin can force an app onto the dashboard.
104                                 boolean isPortalAdmin = adminRolesService.isSuperAdmin(user);
105                                 boolean adminUserHasAppRole = false;
106                                 if (isPortalAdmin) {
107                                         List<EPUserApp> roles = userRolesService.getCachedAppRolesForUser(app.getId(), user.getId());
108                                         adminUserHasAppRole = (roles.size() > 0);
109                                         logger.debug(EELFLoggerDelegate.debugLogger, "setPersUserAppValue: app {}, admin user {}, role count {}",
110                                                         app.getId(), user.getId(), roles.size());
111                                 }
112
113                                 if (select) {
114                                         if (isPortalAdmin) {
115                                                 // The special case: portal admin
116                                                 persRow.setStatusCode("S"); // show
117                                                 dataAccessService.saveDomainObject(persRow, null);
118                                         } else {
119                                                 // User has role-based access to the app.
120                                                 // Showing an accessible app requires no record.
121                                                 if (persRow.getId() != null)
122                                                         dataAccessService.deleteDomainObject(persRow, null);
123                                         }
124                                 } // select
125                                 else {
126                                         if (isPortalAdmin && !adminUserHasAppRole) {
127                                                 // The special case: portal admin, no role
128                                                 if (persRow.getId() != null)
129                                                         dataAccessService.deleteDomainObject(persRow, null);
130                                         } else {
131                                                 // User has role-based access to the app.
132                                                 // Hiding an accessible app requires a record
133                                                 persRow.setStatusCode("H"); // hide
134                                                 dataAccessService.saveDomainObject(persRow, null);
135                                         }
136                                 } // deselect
137                         }
138                 }
139         }
140
141 }