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.sal.utils.StatusCode;
19 import org.opendaylight.controller.usermanager.IUserManager;
20 import org.opendaylight.controller.usermanager.UserConfig;
21 import org.springframework.stereotype.Controller;
22 import org.springframework.web.bind.annotation.PathVariable;
23 import org.springframework.web.bind.annotation.RequestMapping;
24 import org.springframework.web.bind.annotation.RequestMethod;
25 import org.springframework.web.bind.annotation.RequestParam;
26 import org.springframework.web.bind.annotation.ResponseBody;
28 import com.google.gson.Gson;
31 @RequestMapping("/admin")
32 public class DaylightWebAdmin {
33 @RequestMapping("/users")
35 public List<UserConfig> getUsers() {
36 IUserManager userManager = (IUserManager) ServiceHelper
37 .getGlobalInstance(IUserManager.class, this);
38 if (userManager == null) {
42 List<UserConfig> userConfList = userManager.getLocalUserList();
48 * Password in clear text, moving to HTTP/SSL soon
50 @RequestMapping(value = "/users", method = RequestMethod.POST)
52 public String saveLocalUserConfig(
53 @RequestParam(required = true) String json,
54 @RequestParam(required = true) String action,
55 HttpServletRequest request) {
57 IUserManager userManager = (IUserManager) ServiceHelper
58 .getGlobalInstance(IUserManager.class, this);
59 if (userManager == null) {
60 return "Internal Error";
63 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
64 return "Operation not permitted";
67 Gson gson = new Gson();
68 UserConfig config = gson.fromJson(json, UserConfig.class);
70 Status result = (action.equals("add")) ? userManager
71 .addLocalUser(config) : userManager.removeLocalUser(config);
73 return result.getDescription();
76 @RequestMapping(value = "/users/{username}", method = RequestMethod.POST)
78 public String removeLocalUser(@PathVariable("username") String userName,
79 HttpServletRequest request) {
81 String username = request.getUserPrincipal().getName();
82 if (username.equals(userName)) {
83 return "Invalid Request: User cannot delete itself";
86 IUserManager userManager = (IUserManager) ServiceHelper
87 .getGlobalInstance(IUserManager.class, this);
88 if (userManager == null) {
89 return "Internal Error";
92 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
93 return "Operation not permitted";
96 return userManager.removeLocalUser(userName).getDescription();
99 @RequestMapping(value = "/users/password/{username}", method = RequestMethod.POST)
101 public Status changePassword(@PathVariable("username") String username, HttpServletRequest request,
102 @RequestParam("currentPassword") String currentPassword, @RequestParam("newPassword") String newPassword) {
103 IUserManager userManager = (IUserManager) ServiceHelper
104 .getGlobalInstance(IUserManager.class, this);
105 if (userManager == null) {
106 return new Status(StatusCode.GONE, "User Manager not found");
109 if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
110 return new Status(StatusCode.FORBIDDEN, "Operation not permitted");
113 if (newPassword.isEmpty()) {
114 return new Status(StatusCode.BADREQUEST, "Empty passwords not allowed");
117 Status status = userManager.changeLocalUserPassword(username, currentPassword, newPassword);
123 * Is the operation permitted for the given level
127 private boolean authorize(IUserManager userManager, UserLevel level,
128 HttpServletRequest request) {
129 String username = request.getUserPrincipal().getName();
130 UserLevel userLevel = userManager.getUserLevel(username);
131 return userLevel.toNumber() <= level.toNumber();