Merge "Add IfNewHostNotify to DeviceManager"
[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.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;
27
28 import com.google.gson.Gson;
29
30 @Controller
31 @RequestMapping("/admin")
32 public class DaylightWebAdmin {
33     @RequestMapping("/users")
34     @ResponseBody
35     public List<UserConfig> getUsers() {
36         IUserManager userManager = (IUserManager) ServiceHelper
37                 .getGlobalInstance(IUserManager.class, this);
38         if (userManager == null) {
39             return null;
40         }
41
42         List<UserConfig> userConfList = userManager.getLocalUserList();
43
44         return userConfList;
45     }
46
47     /*
48      * Password in clear text, moving to HTTP/SSL soon
49      */
50     @RequestMapping(value = "/users", method = RequestMethod.POST)
51     @ResponseBody
52     public String saveLocalUserConfig(
53             @RequestParam(required = true) String json,
54             @RequestParam(required = true) String action,
55             HttpServletRequest request) {
56
57         IUserManager userManager = (IUserManager) ServiceHelper
58                 .getGlobalInstance(IUserManager.class, this);
59         if (userManager == null) {
60             return "Internal Error";
61         }
62
63         if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
64             return "Operation not permitted";
65         }
66
67         Gson gson = new Gson();
68         UserConfig config = gson.fromJson(json, UserConfig.class);
69
70         Status result = (action.equals("add")) ? userManager
71                 .addLocalUser(config) : userManager.removeLocalUser(config);
72
73         return result.getDescription();
74     }
75
76     @RequestMapping(value = "/users/{username}", method = RequestMethod.POST)
77     @ResponseBody
78     public String removeLocalUser(@PathVariable("username") String userName,
79             HttpServletRequest request) {
80
81         String username = request.getUserPrincipal().getName();
82         if (username.equals(userName)) {
83             return "Invalid Request: User cannot delete itself";
84         }
85
86         IUserManager userManager = (IUserManager) ServiceHelper
87                 .getGlobalInstance(IUserManager.class, this);
88         if (userManager == null) {
89             return "Internal Error";
90         }
91
92         if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
93             return "Operation not permitted";
94         }
95
96         return userManager.removeLocalUser(userName).getDescription();
97     }
98
99     @RequestMapping(value = "/users/password/{username}", method = RequestMethod.POST)
100     @ResponseBody
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");
107         }
108
109         if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
110             return new Status(StatusCode.FORBIDDEN, "Operation not permitted");
111         }
112
113         if (newPassword.isEmpty()) {
114             return new Status(StatusCode.BADREQUEST, "Empty passwords not allowed");
115         }
116
117         Status status = userManager.changeLocalUserPassword(username, currentPassword, newPassword);
118
119         return status;
120     }
121
122     /**
123      * Is the operation permitted for the given level
124      *
125      * @param level
126      */
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();
132     }
133 }