X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fweb%2Froot%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fweb%2FDaylightWeb.java;h=ca37f4b7c19658ca14facf9af29d6d83d2cd07cb;hb=0ae12c54560ef14cb8c08beef4553f7523d41578;hp=296a8fbbd6c684bc0c441b5b05900842d3e0c7ea;hpb=0cc147bcc963544380071b7d101ece8bbea55849;p=controller.git diff --git a/opendaylight/web/root/src/main/java/org/opendaylight/controller/web/DaylightWeb.java b/opendaylight/web/root/src/main/java/org/opendaylight/controller/web/DaylightWeb.java index 296a8fbbd6..ca37f4b7c1 100644 --- a/opendaylight/web/root/src/main/java/org/opendaylight/controller/web/DaylightWeb.java +++ b/opendaylight/web/root/src/main/java/org/opendaylight/controller/web/DaylightWeb.java @@ -1,4 +1,3 @@ - /* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * @@ -9,20 +8,29 @@ package org.opendaylight.controller.web; +import java.io.FileInputStream; import java.util.HashMap; import java.util.Map; +import java.util.Properties; +import java.util.Set; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import org.opendaylight.controller.configuration.IConfigurationContainerService; import org.opendaylight.controller.configuration.IConfigurationService; +import org.opendaylight.controller.containermanager.IContainerAuthorization; +import org.opendaylight.controller.sal.authorization.Privilege; +import org.opendaylight.controller.sal.authorization.Resource; import org.opendaylight.controller.sal.authorization.UserLevel; import org.opendaylight.controller.sal.utils.ServiceHelper; import org.opendaylight.controller.sal.utils.Status; import org.opendaylight.controller.sal.utils.StatusCode; import org.opendaylight.controller.usermanager.IUserManager; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @@ -31,73 +39,163 @@ import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping("/") public class DaylightWeb { @RequestMapping(value = "") - public String index(Model model) { - IUserManager userManager = (IUserManager) ServiceHelper + public String index(Model model, HttpServletRequest request) { + IUserManager userManager = (IUserManager) ServiceHelper .getGlobalInstance(IUserManager.class, this); if (userManager == null) { - return "User Manager is not available"; + return "User Manager is not available"; } - - String username = SecurityContextHolder.getContext().getAuthentication().getName(); + + String username = request.getUserPrincipal().getName(); + model.addAttribute("username", username); - model.addAttribute("role", userManager.getUserLevel(username).toNumber()); - + model.addAttribute("role", userManager.getUserLevel(username) + .toNumber()); + return "main"; } + /** + * Read the version.properties file for the property + * + * @param request + * @return String value configured in the version.properties file + */ + @RequestMapping(value="/versionProperty/{property}", method = RequestMethod.GET) + @ResponseBody + public String getVersion(HttpServletRequest request, @PathVariable("property") String property) { + Properties prop = new Properties(); + try { + prop.load(new FileInputStream("version.properties")); + return prop.getProperty(property+".version"); + } catch (Exception e) { + return null; + } + } @RequestMapping(value = "web.json") @ResponseBody - public Map> bundles() { - Object[] instances = ServiceHelper.getGlobalInstances(IDaylightWeb.class, - this, null); + public Map> bundles(HttpServletRequest request) { + Object[] instances = ServiceHelper.getGlobalInstances( + IDaylightWeb.class, this, null); Map> bundles = new HashMap>(); Map entry; IDaylightWeb bundle; - String userName = SecurityContextHolder.getContext().getAuthentication().getName(); - IUserManager userManger = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this); + String username = request.getUserPrincipal().getName(); + IUserManager userManger = (IUserManager) ServiceHelper + .getGlobalInstance(IUserManager.class, this); for (Object instance : instances) { bundle = (IDaylightWeb) instance; - if (userManger != null && - bundle.isAuthorized(userManger.getUserLevel(userName))) { - entry = new HashMap(); - entry.put("name", bundle.getWebName()); - entry.put("order", bundle.getWebOrder()); - bundles.put(bundle.getWebId(), entry); + if (userManger != null + && bundle.isAuthorized(userManger.getUserLevel(username))) { + entry = new HashMap(); + entry.put("name", bundle.getWebName()); + entry.put("order", bundle.getWebOrder()); + bundles.put(bundle.getWebId(), entry); } } return bundles; } - + @RequestMapping(value = "save", method = RequestMethod.POST) @ResponseBody - public String save() { - String username = SecurityContextHolder.getContext().getAuthentication().getName(); - IUserManager userManager = (IUserManager) ServiceHelper - .getGlobalInstance(IUserManager.class, this); - if (userManager == null) return "User Manager is not available"; - + public String save(HttpServletRequest request) { + String username = request.getUserPrincipal().getName(); + IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this); + if (userManager == null) { + return "User Manager is not available"; + } UserLevel level = userManager.getUserLevel(username); - if (level == UserLevel.NETWORKOPERATOR) { - return "Save not permitted for Operator"; + Status status; + switch (level) { + case SYSTEMADMIN: + case NETWORKADMIN: + IConfigurationService configService = (IConfigurationService) ServiceHelper.getGlobalInstance( + IConfigurationService.class, this); + if (configService != null) { + status = configService.saveConfigurations(); + } else { + status = new Status(StatusCode.NOSERVICE, "Configuration Service is not available"); + } + break; + case NETWORKOPERATOR: + case CONTAINERUSER: + IContainerAuthorization containerAuth = (IContainerAuthorization) ServiceHelper.getGlobalInstance( + IContainerAuthorization.class, this); + if (containerAuth != null) { + boolean oneSaved = false; + Set authorizedContainers = containerAuth.getAllResourcesforUser(username); + if (authorizedContainers.isEmpty()) { + status = new Status(StatusCode.UNAUTHORIZED, "User is not authorized for any container"); + } else { + for (Resource container : authorizedContainers) { + if (container.getPrivilege() == Privilege.WRITE) { + String containerName = (String)container.getResource(); + IConfigurationContainerService containerConfigService = (IConfigurationContainerService) ServiceHelper + .getInstance(IConfigurationContainerService.class, containerName, this); + if (containerConfigService != null) { + status = containerConfigService.saveConfigurations(); + if (status.isSuccess()) { + oneSaved = true; + } + } + } + } + if (oneSaved) { + status = new Status(StatusCode.SUCCESS); + } else { + status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user"); + } + } + } else { + status = new Status(StatusCode.NOSERVICE, "Container Authorization Service is not available"); + } + break; + case APPUSER: + case NOUSER: + default: + status = new Status(StatusCode.UNAUTHORIZED, "Operation not allowed for current user"); + break; } - - Status status = new Status(StatusCode.UNAUTHORIZED, - "Operation not allowed for current user"); - if (level == UserLevel.NETWORKADMIN || level == UserLevel.SYSTEMADMIN) { - IConfigurationService configService = (IConfigurationService) ServiceHelper - .getGlobalInstance(IConfigurationService.class, this); - if (configService != null) { - status = configService.saveConfigurations(); - } - } - + // This function will eventually return a Status return status.getDescription(); } - + + @RequestMapping(value = "logout") + public String logout(Map model, final HttpServletRequest request) { + + IUserManager userManager = (IUserManager) ServiceHelper + .getGlobalInstance(IUserManager.class, this); + if (userManager == null) { + return "User Manager is not available"; + } + String username = request.getUserPrincipal().getName(); + HttpSession session = request.getSession(false); + if (session != null) { + if (username != null) { + userManager.userLogout(username); + } + session.invalidate(); + + } + return "redirect:" + "/"; + } + @RequestMapping(value = "login") - public String login(Map model, final HttpServletResponse response) { - response.setHeader("X-Page-Location", "/login"); - return "login"; - } + public String login(Model model, final HttpServletRequest request, + final HttpServletResponse response) { + // response.setHeader("X-Page-Location", "/login"); + IUserManager userManager = (IUserManager) ServiceHelper + .getGlobalInstance(IUserManager.class, this); + if (userManager == null) { + return "User Manager is not available"; + } + + String username = request.getUserPrincipal().getName(); + + model.addAttribute("username", username); + model.addAttribute("role", userManager.getUserLevel(username) + .toNumber()); + return "forward:" + "/"; + } -} \ No newline at end of file +}