3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.web;
12 import java.util.HashMap;
15 import javax.servlet.http.HttpServletResponse;
17 import org.opendaylight.controller.configuration.IConfigurationService;
18 import org.opendaylight.controller.sal.authorization.UserLevel;
19 import org.opendaylight.controller.sal.utils.ServiceHelper;
20 import org.opendaylight.controller.sal.utils.Status;
21 import org.opendaylight.controller.sal.utils.StatusCode;
22 import org.opendaylight.controller.usermanager.IUserManager;
23 import org.springframework.security.core.context.SecurityContextHolder;
24 import org.springframework.stereotype.Controller;
25 import org.springframework.ui.Model;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
28 import org.springframework.web.bind.annotation.ResponseBody;
33 @RequestMapping(value = "")
34 public String index(Model model) {
35 IUserManager userManager = (IUserManager) ServiceHelper
36 .getGlobalInstance(IUserManager.class, this);
37 if (userManager == null) {
38 return "User Manager is not available";
41 String username = SecurityContextHolder.getContext().getAuthentication().getName();
42 model.addAttribute("username", username);
43 model.addAttribute("role", userManager.getUserLevel(username).toNumber());
48 @RequestMapping(value = "web.json")
50 public Map<String, Map<String, Object>> bundles() {
51 Object[] instances = ServiceHelper.getGlobalInstances(IOneWeb.class,
53 Map<String, Map<String, Object>> bundles = new HashMap<String, Map<String, Object>>();
54 Map<String, Object> entry;
56 String userName = SecurityContextHolder.getContext().getAuthentication().getName();
57 IUserManager userManger = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
58 for (Object instance : instances) {
59 bundle = (IOneWeb) instance;
60 if (userManger != null &&
61 bundle.isAuthorized(userManger.getUserLevel(userName))) {
62 entry = new HashMap<String, Object>();
63 entry.put("name", bundle.getWebName());
64 entry.put("order", bundle.getWebOrder());
65 bundles.put(bundle.getWebId(), entry);
71 @RequestMapping(value = "save", method = RequestMethod.POST)
73 public String save() {
74 String username = SecurityContextHolder.getContext().getAuthentication().getName();
75 IUserManager userManager = (IUserManager) ServiceHelper
76 .getGlobalInstance(IUserManager.class, this);
77 if (userManager == null) return "User Manager is not available";
79 UserLevel level = userManager.getUserLevel(username);
80 if (level == UserLevel.NETWORKOPERATOR) {
81 return "Save not permitted for Operator";
84 Status status = new Status(StatusCode.UNAUTHORIZED,
85 "Operation not allowed for current user");
86 if (level == UserLevel.NETWORKADMIN || level == UserLevel.SYSTEMADMIN) {
87 IConfigurationService configService = (IConfigurationService) ServiceHelper
88 .getGlobalInstance(IConfigurationService.class, this);
89 if (configService != null) {
90 status = configService.saveConfigurations();
94 return status.getDescription();
97 @RequestMapping(value = "login")
98 public String login(Map<String, Object> model, final HttpServletResponse response) {
99 response.setHeader("X-Page-Location", "/login");