2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 * https://creativecommons.org/licenses/by/4.0/
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.
36 * ============LICENSE_END============================================
41 package org.onap.portal.service;
43 import java.util.HashMap;
44 import java.util.List;
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.FnUserService;
54 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.stereotype.Service;
59 public class AdminRolesService {
61 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AdminRolesService.class);
63 private final Long SYS_ADMIN_ROLE_ID = 1L;
64 private final Long ACCOUNT_ADMIN_ROLE_ID = 999L;
65 private final Long ECOMP_APP_ID = 1L;
66 private final String ADMIN_ACCOUNT= "Is account admin for user {}";
68 private final EntityManager entityManager;
69 private final FnUserService fnUserService;
72 public AdminRolesService(final EntityManager entityManager,
73 FnUserService fnUserService) {
74 this.entityManager = entityManager;
75 this.fnUserService = fnUserService;
78 public boolean isSuperAdmin(FnUser user) {
79 if ((user != null) && (user.getOrgUserId() != null)) {
80 String sql = "SELECT user.USER_ID, user.org_user_id, userrole.ROLE_ID, userrole.APP_ID FROM fn_user_role userrole "
81 + "INNER JOIN fn_user user ON user.USER_ID = userrole.USER_ID " + "WHERE user.org_user_id = '"
82 + user.getOrgUserId() + "' " + "AND userrole.ROLE_ID = '" + SYS_ADMIN_ROLE_ID + "' "
83 + "AND userrole.APP_ID = '" + ECOMP_APP_ID + "';";
85 List userRoleList = entityManager.createNativeQuery(sql, UserRole.class).getResultList();
86 if (userRoleList != null && userRoleList.size() > 0) {
89 } catch (Exception e) {
90 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
91 logger.error(EELFLoggerDelegate.errorLogger,
92 "Exception occurred while executing isSuperAdmin operation", e);
98 public boolean isAccountAdmin(FnUser user) {
100 final Map<String, Long> userParams = new HashMap<>();
101 userParams.put("userId", user.getId());
102 logger.debug(EELFLoggerDelegate.debugLogger, ADMIN_ACCOUNT, user.getId());
103 List<Integer> userAdminApps;
104 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)";
105 userAdminApps = entityManager.createQuery(query, Integer.class).setParameter("userId", user.getId()).getResultList();
106 logger.debug(EELFLoggerDelegate.debugLogger, "Is account admin for userAdminApps() - for user {}, found userAdminAppsSize {}", user.getOrgUserId(), userAdminApps.size());
109 if (user.getId() != null) {
110 for (FnUserRole userApp : user.getFnUserRoles()) {
111 if (userApp.getRoleId().getId().equals(ACCOUNT_ADMIN_ROLE_ID)||(userAdminApps.size()>1)) {
112 logger.debug(EELFLoggerDelegate.debugLogger, "Is account admin for userAdminApps() - for user {}, found Id {}", user.getOrgUserId(), userApp.getRoleId().getId());
117 } catch (Exception e) {
118 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
119 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while executing isAccountAdmin operation",
125 public boolean isUser(FnUser user) {
127 FnUser currentUser = fnUserService.getUser(user.getId()).orElseThrow(Exception::new);
128 if (currentUser != null && currentUser.getId() != null) {
129 for (FnUserRole userApp : currentUser.getFnUserRoles()) {
130 if (!userApp.getAppId().getId().equals(ECOMP_APP_ID)) {
131 FnRole role = userApp.getRoleId();
132 if (!role.getId().equals(SYS_ADMIN_ROLE_ID) && !role.getId().equals(ACCOUNT_ADMIN_ROLE_ID)) {
133 if (role.getActiveYn()) {
140 } catch (Exception e) {
141 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
142 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while executing isUser operation", e);