5cffe8cd32bf3f43e043f09cbf7c896a46d531ec
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / controller / AppsOSController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.controller;
39
40 import java.util.HashMap;
41 import java.util.Map;
42
43 import javax.servlet.http.HttpServletRequest;
44
45 import org.json.JSONObject;
46 import org.openecomp.portalapp.portal.domain.EPUser;
47 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
48 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
49 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
50 import org.openecomp.portalapp.portal.service.AdminRolesService;
51 import org.openecomp.portalapp.portal.service.EPAppService;
52 import org.openecomp.portalapp.portal.service.PersUserAppService;
53 import org.openecomp.portalapp.portal.service.UserService;
54 import org.openecomp.portalapp.util.EPUserUtils;
55 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.context.annotation.EnableAspectJAutoProxy;
58 import org.springframework.web.bind.annotation.PathVariable;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RequestMethod;
62 import org.springframework.web.bind.annotation.RestController;
63
64 @RestController
65 @org.springframework.context.annotation.Configuration
66 @EnableAspectJAutoProxy
67 @EPAuditLog
68 public class AppsOSController extends AppsController {
69         
70         static final String FAILURE = "failure";
71         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsOSController.class);
72
73         @Autowired
74         AdminRolesService adminRolesService;
75         @Autowired
76         EPAppService appService;
77         @Autowired
78         PersUserAppService persUserAppService;
79         @Autowired
80         UserService userService;
81
82         
83         
84         /**
85          * Create new application's contact us details.
86          * 
87          * @param contactUs
88          * @return
89          */
90         @RequestMapping(value = "/portalApi/saveNewUser", method = RequestMethod.POST, produces = "application/json")
91         public PortalRestResponse<String> saveNewUser(HttpServletRequest request,@RequestBody EPUser newUser) {
92                 EPUser user = EPUserUtils.getUserSession(request);
93                 if (newUser == null)
94                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
95                                         "New User cannot be null or empty");
96                 
97                 if (!(adminRolesService.isSuperAdmin(user) || adminRolesService.isAccountAdmin(user))){
98                         if(!user.getLoginId().equalsIgnoreCase(newUser.getLoginId()))
99                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
100                                                 "UnAuthorized");
101                 }
102                         
103         String checkDuplicate = request.getParameter("isCheck");
104                 String saveNewUser = FAILURE;
105                 try {
106                         saveNewUser = userService.saveNewUser(newUser,checkDuplicate);
107                 } catch (Exception e) {
108                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, saveNewUser, e.getMessage());
109                 }
110                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, saveNewUser, "");
111         }
112         
113         @RequestMapping(value = { "/portalApi/currentUserProfile/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
114         public String getCurrentUserProfile(HttpServletRequest request, @PathVariable("loginId") String loginId) {
115                 
116                 Map<String,String> map = new HashMap<String,String>();
117                 EPUser user = null;
118                 try {
119                          user = (EPUser) userService.getUserByUserId(loginId).get(0);
120                          map.put("firstName", user.getFirstName());
121                      map.put("lastName", user.getLastName());
122                      map.put("email", user.getEmail());
123                          map.put("loginId", user.getLoginId());
124                          map.put("loginPwd",user.getLoginPwd());
125                          map.put("middleInitial",user.getMiddleInitial());
126                 } catch (Exception e) {
127                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to get user info", e);
128                 }
129
130                 JSONObject j = new JSONObject(map);;
131                 return j.toString();
132         }
133
134 }