88e379ab6ba70596463d1204f1002976ebfc84f5
[portal.git] / portal-BE / src / main / java / org / onap / portal / service / AdminRolesService.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.service;
42
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46 import javax.persistence.EntityManager;
47 import org.onap.portal.domain.db.fn.FnRole;
48 import org.onap.portal.domain.db.fn.FnUser;
49 import org.onap.portal.domain.db.fn.FnUserRole;
50 import org.onap.portal.domain.dto.ecomp.UserRole;
51 import org.onap.portal.logging.format.EPAppMessagesEnum;
52 import org.onap.portal.logging.logic.EPLogUtil;
53 import org.onap.portal.service.fn.FnUserRoleService;
54 import org.onap.portal.service.fn.FnUserService;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.stereotype.Service;
58 import org.springframework.transaction.annotation.Transactional;
59
60 @Service
61 @Transactional
62 public class AdminRolesService {
63
64        private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AdminRolesService.class);
65
66        private final Long SYS_ADMIN_ROLE_ID = 1L;
67        private final Long ACCOUNT_ADMIN_ROLE_ID = 999L;
68        private final Long ECOMP_APP_ID = 1L;
69        private final String ADMIN_ACCOUNT = "Is account admin for user {}";
70
71        private final EntityManager entityManager;
72        private final FnUserService fnUserService;
73        private final FnUserRoleService fnUserRoleService;
74
75        @Autowired
76        public AdminRolesService(final EntityManager entityManager,
77                final FnUserService fnUserService, final FnUserRoleService fnUserRoleService) {
78               this.entityManager = entityManager;
79               this.fnUserService = fnUserService;
80               this.fnUserRoleService = fnUserRoleService;
81        }
82
83        public boolean isSuperAdmin(final String orgUserId) {
84               boolean isSuperAdmin;
85               try {
86                      isSuperAdmin = fnUserRoleService
87                              .isSuperAdmin(orgUserId, SYS_ADMIN_ROLE_ID, ECOMP_APP_ID);
88               } catch (Exception e) {
89                      logger.error("isSuperAdmin exception: " + e.toString());
90                      throw e;
91               }
92               logger.info("isSuperAdmin " + isSuperAdmin);
93               return isSuperAdmin;
94        }
95
96        public boolean isAccountAdmin(FnUser user) {
97               try {
98                      final Map<String, Long> userParams = new HashMap<>();
99                      userParams.put("userId", user.getId());
100                      logger.debug(EELFLoggerDelegate.debugLogger, ADMIN_ACCOUNT, user.getId());
101                      List<Integer> userAdminApps;
102                      String query = "select fa.app_id from fn_user_role ur,fn_app fa where ur.user_id =:userId and ur.app_id=fa.app_id and ur.role_id= 999 and (fa.enabled = 'Y' || fa.app_id=1)";
103                      userAdminApps = entityManager.createQuery(query, Integer.class)
104                              .setParameter("userId", user.getId()).getResultList();
105                      logger.debug(EELFLoggerDelegate.debugLogger,
106                              "Is account admin for userAdminApps() - for user {}, found userAdminAppsSize {}",
107                              user.getOrgUserId(), userAdminApps.size());
108
109                      if (user.getId() != null) {
110                             for (FnUserRole userApp : user.getFnUserRoles()) {
111                                    if (userApp.getRoleId().getId().equals(ACCOUNT_ADMIN_ROLE_ID) || (
112                                            userAdminApps.size() > 1)) {
113                                           logger.debug(EELFLoggerDelegate.debugLogger,
114                                                   "Is account admin for userAdminApps() - for user {}, found Id {}",
115                                                   user.getOrgUserId(), userApp.getRoleId().getId());
116                                           return true;
117                                    }
118                             }
119                      }
120               } catch (Exception e) {
121                      EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
122                      logger.error(EELFLoggerDelegate.errorLogger,
123                              "Exception occurred while executing isAccountAdmin operation",
124                              e);
125               }
126               return false;
127        }
128
129        public boolean isUser(FnUser user) {
130               try {
131                      FnUser currentUser = fnUserService.getUser(user.getId()).orElseThrow(Exception::new);
132                      if (currentUser != null && currentUser.getId() != null) {
133                             for (FnUserRole userApp : currentUser.getFnUserRoles()) {
134                                    if (!userApp.getAppId().getId().equals(ECOMP_APP_ID)) {
135                                           FnRole role = userApp.getRoleId();
136                                           if (!role.getId().equals(SYS_ADMIN_ROLE_ID) && !role.getId()
137                                                   .equals(ACCOUNT_ADMIN_ROLE_ID)) {
138                                                  if (role.getActiveYn()) {
139                                                         return true;
140                                                  }
141                                           }
142                                    }
143                             }
144                      }
145               } catch (Exception e) {
146                      EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
147                      logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while executing isUser operation",
148                              e);
149               }
150               return false;
151        }
152 }