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