diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4c0260d..4312734 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,12 +2,10 @@ + - - - - + @@ -176,8 +174,8 @@ - - + + @@ -776,9 +774,10 @@ + @@ -849,6 +848,70 @@ - + @@ -1940,8 +2003,8 @@ - - + + diff --git a/src/main/java/com/henrypump/poc/WebServer.java b/src/main/java/com/henrypump/poc/WebServer.java index 0fd376d..aaf1e98 100644 --- a/src/main/java/com/henrypump/poc/WebServer.java +++ b/src/main/java/com/henrypump/poc/WebServer.java @@ -49,37 +49,42 @@ public class WebServer{ public void handle(HttpExchange t) throws IOException { JSONObject respJSON = new JSONObject(); Map params = queryToMap(t.getRequestURI().getQuery()); + String action = ""; + String user = ""; for(Map.Entry param_map : params.entrySet()){ String key = param_map.getKey(); String param = param_map.getValue(); - switch (key){ - case "start": - if(param.equals("true")) { - attachedPOC.thisWell.start("web"); - respJSON.put("startCommand", "true"); - respJSON.put("status", attachedPOC.thisWell.getRunStatusString()); - } else { - respJSON.put("startCommand", "invalid value - " + param); - } + switch (key) { + case "cmd": + action = param; break; - - case "stop": - if(param.equals("true")) { - attachedPOC.thisWell.stop("web"); - respJSON.put("stopCommand", "true"); - respJSON.put("status", attachedPOC.thisWell.getRunStatusString()); - } else { - respJSON.put("stopCommand", "invalid value - " + param); - } + case "user": + user = param; break; - - default: respJSON.put(key, "not implemented"); } - + } + if ((user.length() > 0) && (action.length() > 0)) { + switch (action) { + case "start": + attachedPOC.thisWell.start(user); + respJSON.put("startCommand", "true"); + respJSON.put("status", attachedPOC.thisWell.getRunStatusString()); + break; + case "stop": + attachedPOC.thisWell.stop(user); + respJSON.put("stopCommand", "true"); + respJSON.put("status", attachedPOC.thisWell.getRunStatusString()); + break; + default: + respJSON.put(action, "not implemented"); + break; + } + } else { + respJSON.put("bad request", "cmd and user parameters required"); } String response = respJSON.toJSONString(); t.sendResponseHeaders(200, response.getBytes().length); diff --git a/www/pocwww/pocwww/__init__.py b/www/pocwww/pocwww/__init__.py index 17fa1ee..98fad67 100644 --- a/www/pocwww/pocwww/__init__.py +++ b/www/pocwww/pocwww/__init__.py @@ -175,9 +175,8 @@ def main(global_config, **settings): config.add_route("json_cmd_stop", "/json/cmd/stop", factory='pocwww.security.UserLoginFactory') config.add_route("json_cmd_shake", "/json/cmd/shake", factory='pocwww.security.UserLoginFactory') config.add_route("json_update_poc_address", "/json/updatepocaddress", factory='pocwww.security.UserLoginFactory') - - config.add_route("json_newuser", "/json/newuser", factory='pocwww.security.UserLoginFactory') - config.add_route("json_getusers", "/json/users", factory='pocwww.security.UserLoginFactory') + + config.add_route("json_users", "/json/users", factory='pocwww.security.UserLoginFactory') config.scan() return config.make_wsgi_app() diff --git a/www/pocwww/pocwww/json.py b/www/pocwww/pocwww/json.py index fb0afd7..df48b9e 100644 --- a/www/pocwww/pocwww/json.py +++ b/www/pocwww/pocwww/json.py @@ -133,7 +133,7 @@ def json_updateconfig(request): jsb = request.json_body new_config = {} new_config['timestamp'] = datetime.utcnow() - new_config['storedBy'] = "web" + new_config['storedBy'] = request.authenticated_userid new_config['wellName'] = jsb['wellName'] new_config['tapers'] = [] for p in conv_to_float: @@ -163,7 +163,7 @@ def json_updateconfig(request): @view_config(route_name="json_cmd_start", renderer="prettyjson", permission="control") def json_start(request): address = get_poc_address(request) or 'localhost' - start_url = "http://{}:8000/command?start=true".format(address) + start_url = "http://{}:8000/command?cmd=start&user={}".format(address, request.authenticated_userid) r = requests.get(start_url) return r.text if r.status_code == 200 else {"status": "failure sending command"} @@ -171,7 +171,7 @@ def json_start(request): @view_config(route_name="json_cmd_stop", renderer="prettyjson", permission="control") def json_stop(request): address = get_poc_address(request) or 'localhost' - stop_url = "http://{}:8000/command?stop=true".format(address) + stop_url = "http://{}:8000/command?cmd=stop&user={}".format(address, request.authenticated_userid) r = requests.get(stop_url) return r.text if r.status_code == 200 else {"status": "failure sending command"} @@ -194,7 +194,7 @@ def json_update_poc_address(request): return {"status": "failure"} -@view_config(route_name="json_newuser", renderer="prettyjson", request_method='POST', permission="edit") +@view_config(route_name="json_users", renderer="prettyjson", request_method='POST', permission="edit") def json_newuser(request): jsb = request.json_body if request.db['users'].count({"username": jsb['username']}) > 0: @@ -214,10 +214,35 @@ def json_newuser(request): return {'status': "OK"} -@view_config(route_name="json_getusers", renderer="prettyjson", permission="edit") +@view_config(route_name="json_users", renderer="prettyjson", permission="edit", request_method='GET') def json_getuser(request): user_list = [] users = list(request.db['users'].find()) for user in users: user_list.append(user['username']) return {'users': user_list} + + +@view_config(route_name="json_users", renderer="prettyjson", permission="edit", request_method='DELETE') +def json_deleteuser(request): + request.db['users'].remove({'username': request.json_body['username']}) + user_list = [] + users = list(request.db['users'].find()) + for user in users: + user_list.append(user['username']) + return {'users': user_list} + +@view_config(route_name="json_users", renderer="prettyjson", request_method='PUT', permission="edit") +def json_newuser(request): + jsb = request.json_body + if len(jsb['username']) < 5: + fail_reason = "The username must be at least 5 characters" + return {"status": 'fail', "info": fail_reason} + + elif len(jsb['password']) < 5: + fail_reason = "The password must be at least 5 characters" + return {"status": 'fail', "info": fail_reason} + + else: + set_return = set_password(request, jsb['username'], jsb['password']) + return {'status': "OK"} diff --git a/www/pocwww/pocwww/templates/register.jinja2 b/www/pocwww/pocwww/templates/register.jinja2 index b76bed6..41c8f04 100644 --- a/www/pocwww/pocwww/templates/register.jinja2 +++ b/www/pocwww/pocwww/templates/register.jinja2 @@ -15,23 +15,49 @@
- +
-
- -