2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.web;
11 import java.util.List;
13 import javax.servlet.http.HttpServletRequest;
15 import org.opendaylight.controller.sal.authorization.UserLevel;
16 import org.opendaylight.controller.sal.utils.ServiceHelper;
17 import org.opendaylight.controller.sal.utils.Status;
18 import org.opendaylight.controller.usermanager.IUserManager;
19 import org.opendaylight.controller.usermanager.internal.UserConfig;
20 import org.springframework.stereotype.Controller;
21 import org.springframework.web.bind.annotation.PathVariable;
22 import org.springframework.web.bind.annotation.RequestMapping;
23 import org.springframework.web.bind.annotation.RequestMethod;
24 import org.springframework.web.bind.annotation.RequestParam;
25 import org.springframework.web.bind.annotation.ResponseBody;
27 import com.google.gson.Gson;
30 @RequestMapping("/admin")
31 public class DaylightWebAdmin {
32 @RequestMapping("/users")
34 public List<UserConfig> getUsers() {
35 IUserManager userManager = (IUserManager) ServiceHelper
36 .getGlobalInstance(IUserManager.class, this);
37 if (userManager == null) {
41 List<UserConfig> userConfList = userManager.getLocalUserList();
47 * Password in clear text, moving to HTTP/SSL soon
49 @RequestMapping(value = "/users", method = RequestMethod.POST)
51 public String saveLocalUserConfig(
52 @RequestParam(required = true) String json,
53 @RequestParam(required = true) String action,
54 HttpServletRequest request) {
56 IUserManager userManager = (IUserManager) ServiceHelper
57 .getGlobalInstance(IUserManager.class, this);
58 if (userManager == null) {
59 return "Internal Error";
62 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
63 return "Operation not permitted";
66 Gson gson = new Gson();
67 UserConfig config = gson.fromJson(json, UserConfig.class);
69 Status result = (action.equals("add")) ? userManager
70 .addLocalUser(config) : userManager.removeLocalUser(config);
72 return result.getDescription();
75 @RequestMapping(value = "/users/{username}", method = RequestMethod.POST)
77 public String removeLocalUser(@PathVariable("username") String userName,
78 HttpServletRequest request) {
80 String username = request.getUserPrincipal().getName();
81 if (username.equals(userName)) {
82 return "Invalid Request: User cannot delete itself";
85 IUserManager userManager = (IUserManager) ServiceHelper
86 .getGlobalInstance(IUserManager.class, this);
87 if (userManager == null) {
88 return "Internal Error";
91 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
92 return "Operation not permitted";
95 return userManager.removeLocalUser(userName).getDescription();
99 * Is the operation permitted for the given level
103 private boolean authorize(IUserManager userManager, UserLevel level,
104 HttpServletRequest request) {
105 String username = request.getUserPrincipal().getName();
106 UserLevel userLevel = userManager.getUserLevel(username);
107 return userLevel.toNumber() <= level.toNumber();