def activate(email): token = request.headers.get("Lambda-Token") if token != security_middleware._secret: # logger.debug("Invalid token: " + str(token) + " VS " + str(security_middleware._secret)) # logger.debug("Header received:\n" + str(request.headers)) return Response("Invalid Authorization Token", status=401, content_type="text/plain") try: user_data = UsersRDB.get_by_email(email, include_deleted=False) if not user_data: return Response("No such user found", status=400, content_type="text/plain") if user_data["status"] == "ACTIVE": return Response("User already activated", status=200, content_type="text/plain") user_data["status"] = "ACTIVE" UsersRDB.update(user_data) full_rsp = Response("User successfully activated", status=200, content_type="text/plain") except Exception as e: log_msg = "/activate: Exception = " + str(e) logger.error(log_msg) rsp_status = 500 rsp_txt = "/activate INTERNAL SERVER ERROR." full_rsp = Response(rsp_txt, status=rsp_status, content_type="text/plain") return full_rsp
def user(): last_name = request.args.get("last_name") first_name = request.args.get("first_name") email = request.args.get("email") password = request.args.get("password") status = request.args.get("status") inputs = log_and_extract_input( demo, { "last_name": last_name, "first_name": first_name, "email": email, "password": password, "status": status }) user_info = inputs["path_params"] try: user_data = UsersRDB.get_by_email(user_info["email"], include_deleted=False) if not user_data: return Response("No such user found", status=400, content_type="text/plain") if inputs["method"] == "GET": etag_server = ETag.getMD5(user_data) rsp_txt = json.dumps(user_data) full_rsp = Response(rsp_txt, status=200, content_type="application/json", headers={"ETag": etag_server}) elif inputs["method"] == "PUT": # check etag before updating anything etag_client = request.headers.get("ETag", None) etag_server = ETag.getMD5(user_data) if etag_client is None: Response("No ETag provided, please sign in first", status=403, content_type="text/plain") if etag_client != etag_server: return Response("ETag mismatch, please pull the latest data", status=412, content_type="text/plain") # update the data temp_data = {} for k, v in user_data.items(): # if the value for certain columns are not specified (None) or empty (""), keep the original data if user_info.get(k, "") not in [None, ""]: # update column to new data temp_data[k] = user_info.get(k, "") else: # keep original data temp_data[k] = v # don't allow set to delete in PUT method if temp_data.get("status") == "DELETED": return Response("Please use DELETE method instead", status=403, content_type="text/plain") res = UsersRDB.update(temp_data) if res == 0: rsp_txt = "Nothing updated" else: rsp_txt = "User successfully updated" full_rsp = Response(rsp_txt, status=200, content_type="text/plain") elif inputs["method"] == "DELETE": temp_data = {} for k, v in user_data.items(): if user_info.get(k, None): temp_data[k] = user_info.get(k, None) else: temp_data[k] = v temp_data["status"] = "DELETED" res = UsersRDB.update(temp_data) rsp_txt = "User successfully set to deleted state" full_rsp = Response(rsp_txt, status=200, content_type="text/plain") except Exception as e: log_msg = "/user: Exception = " + str(e) logger.error(log_msg) rsp_status = 500 rsp_txt = "INTERNAL SERVER ERROR. Please take COMSE6156 -- Cloud Native Applications." full_rsp = Response(rsp_txt, status=rsp_status, content_type="text/plain") return full_rsp