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