Javadoc for controller.web
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / DaylightWebAdmin.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.web;
10
11 import java.util.List;
12
13 import javax.servlet.http.HttpServletRequest;
14
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;
26
27 import com.google.gson.Gson;
28
29 @Controller
30 @RequestMapping("/admin")
31 public class DaylightWebAdmin {
32     @RequestMapping("/users")
33     @ResponseBody
34     public List<UserConfig> getUsers() {
35         IUserManager userManager = (IUserManager) ServiceHelper
36                 .getGlobalInstance(IUserManager.class, this);
37         if (userManager == null) {
38             return null;
39         }
40
41         List<UserConfig> userConfList = userManager.getLocalUserList();
42
43         return userConfList;
44     }
45
46     /*
47      * Password in clear text, moving to HTTP/SSL soon
48      */
49     @RequestMapping(value = "/users", method = RequestMethod.POST)
50     @ResponseBody
51     public String saveLocalUserConfig(
52             @RequestParam(required = true) String json,
53             @RequestParam(required = true) String action,
54             HttpServletRequest request) {
55
56         IUserManager userManager = (IUserManager) ServiceHelper
57                 .getGlobalInstance(IUserManager.class, this);
58         if (userManager == null) {
59             return "Internal Error";
60         }
61
62         if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
63             return "Operation not permitted";
64         }
65
66         Gson gson = new Gson();
67         UserConfig config = gson.fromJson(json, UserConfig.class);
68
69         Status result = (action.equals("add")) ? userManager
70                 .addLocalUser(config) : userManager.removeLocalUser(config);
71
72         return result.getDescription();
73     }
74
75     @RequestMapping(value = "/users/{username}", method = RequestMethod.POST)
76     @ResponseBody
77     public String removeLocalUser(@PathVariable("username") String userName,
78             HttpServletRequest request) {
79
80         String username = request.getUserPrincipal().getName();
81         if (username.equals(userName)) {
82             return "Invalid Request: User cannot delete itself";
83         }
84
85         IUserManager userManager = (IUserManager) ServiceHelper
86                 .getGlobalInstance(IUserManager.class, this);
87         if (userManager == null) {
88             return "Internal Error";
89         }
90
91         if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
92             return "Operation not permitted";
93         }
94
95         return userManager.removeLocalUser(userName).getDescription();
96     }
97
98     /**
99      * Is the operation permitted for the given level
100      * 
101      * @param level
102      */
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();
108     }
109 }