c547a9de5f458b87f6509b3acb2fb4db9956af20
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / PersUserAppServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.service;
39
40 import java.util.List;
41
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.EnableAspectJAutoProxy;
44 import org.springframework.stereotype.Service;
45 import org.springframework.transaction.annotation.Transactional;
46
47 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
48 import org.openecomp.portalsdk.core.service.DataAccessService;
49 import org.openecomp.portalapp.portal.domain.EPApp;
50 import org.openecomp.portalapp.portal.domain.EPUser;
51 import org.openecomp.portalapp.portal.domain.EPUserApp;
52 import org.openecomp.portalapp.portal.domain.PersUserAppSelection;
53 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
54
55 @Service("persUserAppService")
56 @Transactional
57 @org.springframework.context.annotation.Configuration
58 @EnableAspectJAutoProxy
59 @EPMetricsLog
60 public class PersUserAppServiceImpl implements PersUserAppService {
61
62         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PersUserAppServiceImpl.class);
63
64         @Autowired
65         private DataAccessService dataAccessService;
66         @Autowired
67         private AdminRolesService adminRolesService;
68         @Autowired
69         private UserRolesService userRolesService;
70
71         /*
72          * (non-Javadoc)
73          * 
74          * @see org.openecomp.portalapp.portal.service.UserAppSelectService#
75          * setAppCatalogSelection(org.openecomp.portalapp.portal.domain.EPUser,
76          * org.openecomp.portalapp.portal.transport.AppCatalogSelection)
77          */
78         @Override
79         public void setPersUserAppValue(EPUser user, EPApp app, boolean select, boolean pending) {
80                 if (user == null || app == null)
81                         throw new IllegalArgumentException("setPersUserAppValue: Null values");
82
83                 // Find the record for this user-app combo, if any
84                 String filter = " where user_id = " + Long.toString(user.getId()) + " and app_id = "
85                                 + Long.toString(app.getId());
86                 @SuppressWarnings("unchecked")
87                 List<PersUserAppSelection> persList = dataAccessService.getList(PersUserAppSelection.class, filter, null, null);
88
89                 // Key constraint limits to 1 row
90                 PersUserAppSelection persRow = null;
91                 if (persList.size() == 1)
92                         persRow = persList.get(0);
93                 else
94                         persRow = new PersUserAppSelection(null, user.getId(), app.getId(), null);
95
96                 if (app.getOpen()) {
97                         // Pending status is not meaningful for open apps.
98                         if (pending)
99                                 logger.error(EELFLoggerDelegate.errorLogger,
100                                                 "setPersUserAppValue: invalid request, ignoring set-pending for open app");
101
102                         // Open apps have same behavior for regular and admin users
103                         if (select) {
104                                 // Selection of an open app requires a record
105                                 persRow.setStatusCode("S"); // show
106                                 dataAccessService.saveDomainObject(persRow, null);
107                         } else {
108                                 // De-selection of an open app requires no record
109                                 if (persRow.getId() != null)
110                                         dataAccessService.deleteDomainObject(persRow, null);
111                         }
112                 } else {
113                         // Non-open app.
114
115                         // Pending overrides select.
116                         if (pending) {
117                                 persRow.setStatusCode("P");
118                                 dataAccessService.saveDomainObject(persRow, null);
119                         } else {
120                                 // Behavior depends on Portal (super) admin status, bcos an
121                                 // admin can force an app onto the dashboard.
122                                 boolean isPortalAdmin = adminRolesService.isSuperAdmin(user);
123                                 boolean adminUserHasAppRole = false;
124                                 if (isPortalAdmin) {
125                                         List<EPUserApp> roles = userRolesService.getCachedAppRolesForUser(app.getId(), user.getId());
126                                         adminUserHasAppRole = (roles.size() > 0);
127                                         logger.debug(EELFLoggerDelegate.debugLogger, "setPersUserAppValue: app {}, admin user {}, role count {}",
128                                                         app.getId(), user.getId(), roles.size());
129                                 }
130
131                                 if (select) {
132                                         if (isPortalAdmin) {
133                                                 // The special case: portal admin
134                                                 persRow.setStatusCode("S"); // show
135                                                 dataAccessService.saveDomainObject(persRow, null);
136                                         } else {
137                                                 // User has role-based access to the app.
138                                                 // Showing an accessible app requires no record.
139                                                 if (persRow.getId() != null)
140                                                         dataAccessService.deleteDomainObject(persRow, null);
141                                         }
142                                 } // select
143                                 else {
144                                         if (isPortalAdmin && !adminUserHasAppRole) {
145                                                 // The special case: portal admin, no role
146                                                 if (persRow.getId() != null)
147                                                         dataAccessService.deleteDomainObject(persRow, null);
148                                         } else {
149                                                 // User has role-based access to the app.
150                                                 // Hiding an accessible app requires a record
151                                                 persRow.setStatusCode("H"); // hide
152                                                 dataAccessService.saveDomainObject(persRow, null);
153                                         }
154                                 } // deselect
155                         }
156                 }
157         }
158
159 }