Added Junits for ONAP-SDK-APP
[policy/engine.git] / ONAP-SDK-APP / src / main / java / org / onap / portalapp / service / AdminAuthExtension.java
1 /*-
2  * ================================================================================
3  * ONAP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.onap.portalapp.service;
21
22 import java.util.Set;
23
24 import org.onap.policy.model.Roles;
25 import org.onap.policy.rest.dao.CommonClassDao;
26 import org.onap.policy.rest.jpa.UserInfo;
27 import org.onap.portalapp.service.IAdminAuthExtension;
28 import org.onap.portalsdk.core.domain.Role;
29 import org.onap.portalsdk.core.domain.User;
30 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33 import org.springframework.transaction.annotation.Transactional;
34
35
36 @Service("adminAuthExtension")
37 @Transactional
38 /**
39  * Provides empty implementations of the methods in IAdminAuthExtension.
40  */
41 public class AdminAuthExtension implements IAdminAuthExtension {
42
43         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AdminAuthExtension.class);
44
45         @Autowired
46         CommonClassDao commonClassDao;
47         
48         public void setCommonClassDao(CommonClassDao dao){
49                 commonClassDao = dao;
50         }
51         
52         
53         /*
54          * (non-Javadoc)
55          * @see org.onap.portalapp.service.IAdminAuthExtension#saveUserExtension(org.onap.portalsdk.core.domain.User)
56          */
57         public void saveUserExtension(User user) {
58                 logger.debug("saveUserExtension");
59                 savePolicyRole(user);
60         }
61
62         /*
63          * (non-Javadoc)
64          * @see org.onap.portalapp.service.IAdminAuthExtension#editUserExtension(org.onap.portalsdk.core.domain.User)
65          */
66         public void editUserExtension(User user) {
67                 logger.debug("editUserExtension");
68         }
69
70         /*
71          * (non-Javadoc)
72          * @see org.onap.portalapp.service.IAdminAuthExtension#saveUserRoleExtension(java.util.Set, org.onap.portalsdk.core.domain.User)
73          */
74         public void saveUserRoleExtension(Set<Role> roles, User user) {
75                 logger.debug("saveUserRoleExtension");
76                 savePolicyRole(user);
77         }
78         
79         private void savePolicyRole(User user){
80                 logger.info("User Object Recieved");
81                 try{
82                         Roles roles1 = new Roles();
83                         roles1.setName(user.getFullName());
84                         roles1.setLoginId(user.getLoginId());
85                         if(user.getRoles() != null){
86                                 String query = "delete from Roles where loginid='"+user.getLoginId()+"'";
87                                 commonClassDao.updateQuery(query);
88                                 for(Role role : user.getRoles()){ 
89                                         logger.info("User Role"+role);
90                                         if("Policy Super Admin".equalsIgnoreCase(role.getName().trim()) || "System Administrator".equalsIgnoreCase(role.getName().trim()) || "Standard User".equalsIgnoreCase(role.getName().trim()) ){
91                                                 roles1.setRole("super-admin");
92                                         }else if("Policy Super Editor".equalsIgnoreCase(role.getName().trim())){
93                                                 roles1.setRole("super-editor");
94                                         }else if("Policy Super Guest".equalsIgnoreCase(role.getName().trim())){
95                                                 roles1.setRole("super-guest");
96                                         }else if("Policy Admin".equalsIgnoreCase(role.getName().trim())){
97                                                 roles1.setRole("admin");
98                                         }else if("Policy Editor".equalsIgnoreCase(role.getName().trim())){
99                                                 roles1.setRole("editor");
100                                         }else if("Policy Guest".equalsIgnoreCase(role.getName().trim())){
101                                                 roles1.setRole("guest");
102                                         }       
103                                         commonClassDao.save(roles1);
104                                 }       
105                         }
106                         
107                         UserInfo userInfo = new UserInfo();
108                         userInfo.setUserLoginId(user.getLoginId());
109                         userInfo.setUserName(user.getFullName());
110                         commonClassDao.save(userInfo);
111                         logger.info("User Object Updated Successfully");
112                 }
113                 catch(Exception e){
114                         logger.error("Exception caused while Setting role to Policy DB"+e);
115                 }
116         }
117
118 }