Security/ Package Name changes
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / PersUserAppServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.onap.portalapp.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.context.annotation.EnableAspectJAutoProxy;
48 import org.springframework.stereotype.Service;
49 import org.springframework.transaction.annotation.Transactional;
50 import org.hibernate.criterion.Criterion;
51 import org.hibernate.criterion.Restrictions;
52 import org.onap.portalapp.portal.domain.EPApp;
53 import org.onap.portalapp.portal.domain.EPUser;
54 import org.onap.portalapp.portal.domain.EPUserApp;
55 import org.onap.portalapp.portal.domain.PersUserAppSelection;
56 import org.onap.portalapp.portal.domain.PersUserWidgetSelection;
57 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.onap.portalsdk.core.service.DataAccessService;
60
61 @Service("persUserAppService")
62 @Transactional
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
65 @EPMetricsLog
66 public class PersUserAppServiceImpl implements PersUserAppService {
67
68         private static final String APP_ID = "appId";
69
70         private static final String USER_ID = "userId";
71
72         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PersUserAppServiceImpl.class);
73
74         @Autowired
75         private DataAccessService dataAccessService;
76         @Autowired
77         private AdminRolesService adminRolesService;
78         @Autowired
79         private UserRolesService userRolesService;
80
81         /*
82          * (non-Javadoc)
83          * 
84          * @see org.openecomp.portalapp.portal.service.UserAppSelectService#
85          * setAppCatalogSelection(org.openecomp.portalapp.portal.domain.EPUser,
86          * org.openecomp.portalapp.portal.transport.AppCatalogSelection)
87          */
88         @Override
89         public void setPersUserAppValue(EPUser user, EPApp app, boolean select, boolean pending) {
90                 if (user == null || app == null)
91                         throw new IllegalArgumentException("setPersUserAppValue: Null values");
92
93                 // Find the record for this user-app combo, if any
94                 List<PersUserAppSelection> persList = getUsersAppSelection(user, app);
95
96                 // Key constraint limits to 1 row
97                 PersUserAppSelection persRow = null;
98                 if (persList.size() == 1)
99                         persRow = persList.get(0);
100                 else
101                         persRow = new PersUserAppSelection(null, user.getId(), app.getId(), null);
102
103                 if (app.getOpen()) {
104                         // Pending status is not meaningful for open apps.
105                         if (pending)
106                                 logger.error(EELFLoggerDelegate.errorLogger,
107                                                 "setPersUserAppValue: invalid request, ignoring set-pending for open app");
108
109                         // Open apps have same behavior for regular and admin users
110                         if (select) {
111                                 // Selection of an open app requires a record
112                                 persRow.setStatusCode("S"); // show
113                                 dataAccessService.saveDomainObject(persRow, null);
114                         } else {
115                                 // De-selection of an open app requires no record
116                                 if (persRow.getId() != null)
117                                         dataAccessService.deleteDomainObject(persRow, null);
118                         }
119                 } else {
120                         // Non-open app.
121
122                         // Pending overrides select.
123                         if (pending) {
124                                 persRow.setStatusCode("P");
125                                 dataAccessService.saveDomainObject(persRow, null);
126                         } else {
127                                 // Behavior depends on Portal (super) admin status, bcos an
128                                 // admin can force an app onto the dashboard.
129                                 boolean isPortalAdmin = adminRolesService.isSuperAdmin(user);
130                                 boolean adminUserHasAppRole = false;
131                                 if (isPortalAdmin) {
132                                         List<EPUserApp> roles = userRolesService.getCachedAppRolesForUser(app.getId(), user.getId());
133                                         adminUserHasAppRole = (roles.size() > 0);
134                                         logger.debug(EELFLoggerDelegate.debugLogger, "setPersUserAppValue: app {}, admin user {}, role count {}",
135                                                         app.getId(), user.getId(), roles.size());
136                                 }
137
138                                 if (select) {
139                                         if (isPortalAdmin) {
140                                                 // The special case: portal admin
141                                                 persRow.setStatusCode("S"); // show
142                                                 dataAccessService.saveDomainObject(persRow, null);
143                                         } else {
144                                                 // User has role-based access to the app.
145                                                 // Showing an accessible app requires no record.
146                                                 if (persRow.getId() != null)
147                                                         dataAccessService.deleteDomainObject(persRow, null);
148                                         }
149                                 } // select
150                                 else {
151                                         if (isPortalAdmin && !adminUserHasAppRole) {
152                                                 // The special case: portal admin, no role
153                                                 if (persRow.getId() != null)
154                                                         dataAccessService.deleteDomainObject(persRow, null);
155                                         } else {
156                                                 // User has role-based access to the app.
157                                                 // Hiding an accessible app requires a record
158                                                 persRow.setStatusCode("H"); // hide
159                                                 dataAccessService.saveDomainObject(persRow, null);
160                                         }
161                                 } // deselect
162                         }
163                 }
164         }
165
166         @SuppressWarnings("unchecked")
167         private List<PersUserAppSelection> getUsersAppSelection(EPUser user, EPApp app) {
168                 List<Criterion> restrictionsList = new ArrayList<Criterion>();
169                 Criterion userIdCriterion = Restrictions.eq("userId", user.getId());
170                 Criterion appIdCriterion = Restrictions.eq("appId", app.getId());
171                 restrictionsList.add(Restrictions.and(userIdCriterion, appIdCriterion));
172                 return (List<PersUserAppSelection>) dataAccessService.getList(PersUserAppSelection.class, null, restrictionsList, null);
173         }
174
175 }