2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.List;
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;
57 @Service("persUserAppService")
59 @org.springframework.context.annotation.Configuration
60 @EnableAspectJAutoProxy
62 public class PersUserAppServiceImpl implements PersUserAppService {
64 private static final String APP_ID = "appId";
66 private static final String USER_ID = "userId";
68 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PersUserAppServiceImpl.class);
71 private DataAccessService dataAccessService;
73 private AdminRolesService adminRolesService;
75 private UserRolesService userRolesService;
80 * @see org.onap.portalapp.portal.service.UserAppSelectService#
81 * setAppCatalogSelection(org.onap.portalapp.portal.domain.EPUser,
82 * org.onap.portalapp.portal.transport.AppCatalogSelection)
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");
89 // Find the record for this user-app combo, if any
90 List<PersUserAppSelection> persList = getUsersAppSelection(user, app);
92 // Key constraint limits to 1 row
93 PersUserAppSelection persRow = null;
94 if (persList.size() == 1)
95 persRow = persList.get(0);
97 persRow = new PersUserAppSelection(null, user.getId(), app.getId(), null);
100 // Pending status is not meaningful for open apps.
102 logger.error(EELFLoggerDelegate.errorLogger,
103 "setPersUserAppValue: invalid request, ignoring set-pending for open app");
105 // Open apps have same behavior for regular and admin users
107 // Selection of an open app requires a record
108 persRow.setStatusCode("S"); // show
109 dataAccessService.saveDomainObject(persRow, null);
111 // De-selection of an open app requires no record
112 if (persRow.getId() != null)
113 dataAccessService.deleteDomainObject(persRow, null);
118 // Pending overrides select.
120 persRow.setStatusCode("P");
121 dataAccessService.saveDomainObject(persRow, null);
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;
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());
136 // The special case: portal admin
137 persRow.setStatusCode("S"); // show
138 dataAccessService.saveDomainObject(persRow, null);
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);
147 if (isPortalAdmin && !adminUserHasAppRole) {
148 // The special case: portal admin, no role
149 if (persRow.getId() != null)
150 dataAccessService.deleteDomainObject(persRow, null);
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);
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);