def put(self):
        """
        Function to change a relationship record
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 2) == False:
            return None

        body_categories = {"relationship_id": 1, "message": 1}
        relationship_dict = errorutil.check_fields(self.request.body.decode(),
                                                   body_categories, self)

        userdata = jwthandler.decode_userdata(
            self.request.headers["Authorization"])

        if relationship_dict == False:
            return None

        relationship_id = relationship_dict["relationship_id"]
        del relationship_dict["relationship_id"]

        if relationshiputil.change_relationship(relationship_id,
                                                relationship_dict,
                                                self) == False:
            return None

        formatted_message = loggerhandler.form_message_dictionary(
            userdata, "relationship", relationship_id, relationship_dict)

        loggerhandler.log_message("change", formatted_message)

        self.write({"message": "Success"})
Beispiel #2
0
    def put(self):
        """
        Function to change a label
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 2) == False:
            return None

        userdata = jwthandler.decode_userdata(self.request.headers["Authorization"])

        body_categories = {"label_id": 1, "label_text": 0, "parent": 0}
        label_dict = errorutil.check_fields(self.request.body.decode(), body_categories, self)

        if label_dict == False:
            return None

        label_id = label_dict["label_id"]
        del label_dict["label_id"]

        if labelutil.change_label(label_id, label_dict, self) == False:
            return None

        formatted_message = loggerhandler.form_message_dictionary(userdata, 
                                                                "label", 
                                                                label_id,
                                                                label_dict)

        loggerhandler.log_message("change", formatted_message)


        self.write({"message":"Success"})
Beispiel #3
0
    def post(self):
        """
        Function to create a link
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        userdata = jwthandler.decode_userdata(
            self.request.headers["Authorization"])

        body_categories = {
            "node_id_1": 1,
            "node_id_2": 1,
            "view_id": 1,
            "relationship_id": 0
        }
        link_dict = errorutil.check_fields(self.request.body.decode(),
                                           body_categories, self)

        link_id = linkutil.create_link(link_dict, self)
        if link_dict == False or link_id == False:
            return None

        formatted_message = loggerhandler.form_message_dictionary(
            userdata, "links",
            linkutil.get_link_id(link_dict["node_id_1"],
                                 link_dict["node_id_2"]), link_dict)

        loggerhandler.log_message("add", formatted_message)

        self.write({"message": "Success", "payload": link_id})
Beispiel #4
0
    def put(self):
        """
        Function to change a type record
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        userdata = jwthandler.decode_userdata(
            self.request.headers["Authorization"])

        body_categories = {"type_id": 1, "type": 1}
        type_dict = errorutil.check_fields(self.request.body.decode(),
                                           body_categories, self)

        if type_dict == False:
            return None

        type_id = type_dict["type_id"]
        del type_dict["type_id"]

        if typeutil.change_type(type_id, type_dict, self) == False:
            return None

        formatted_message = loggerhandler.form_message_dictionary(
            userdata, "node_type", type_id, type_dict)

        loggerhandler.log_message("change", formatted_message)

        self.write({"message": "Success"})
Beispiel #5
0
    def put(self):
        """
        Function to change a node record
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        userdata = jwthandler.decode_userdata(self.request.headers["Authorization"])

        body_categories = {"node_id": 1, "type_id": 0, "view_id": 0, "label_id": 0, "icon":0}
        node_dict = errorutil.check_fields(self.request.body.decode(), body_categories, self)

        if node_dict == False:
            return None

        node_id = node_dict["node_id"]
        del node_dict["node_id"]

        if nodeutil.change_node(node_id, node_dict, self) == False:
            return None

        formatted_message = loggerhandler.form_message_dictionary(userdata, 
                                                                "node", 
                                                                node_id,
                                                                node_dict)
        try:
            loggerhandler.log_message("change", formatted_message)
        except:
            pass

        self.write({"message":"Success"})
Beispiel #6
0
    def delete(self):
        """
        Function to delete a view record
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        userdata = jwthandler.decode_userdata(
            self.request.headers["Authorization"])

        body_categories = {"view_id": 1}
        view_dict = errorutil.check_fields(self.request.body.decode(),
                                           body_categories, self)

        if view_dict == False or viewutil.delete_view(view_dict["view_id"],
                                                      self) == False:
            return None

        formatted_message = loggerhandler.form_delete_message_dictionary(
            userdata, "views", view_dict["view_id"])

        loggerhandler.log_message("delete", formatted_message)

        self.write({"message": "Success"})
Beispiel #7
0
    def get(self):
        """
        Function to get nodes or a single node record
        Inputs: Tornado web request
        Output: Node data
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        body_categories = {"node_id":0}
        node_dict = errorutil.check_fields(self.request.arguments, body_categories, self)
        if node_dict == False:
            self.write(nodeutil.get_nodes())
            return None

        if "node_id" in node_dict:
            self.write(nodeutil.get_node(node_dict["node_id"]))
Beispiel #8
0
    def get(self):
        """
        Function to get user data
        Inputs: Tornado web request
        Output: User ID
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 2) == False:
            return None

        body_categories = {"username": 1}
        user_dict = errorutil.check_fields(self.request.arguments,
                                           body_categories, self)

        if user_dict == False:
            return None

        self.write({"user_id": userutil.get_uid(user_dict["username"])})
Beispiel #9
0
    def put(self):
        """
        Function to change a metadata record
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        body_categories = {
            "meta_id": 1,
            "node_id": 0,
            "link_id": 0,
            "category": 0,
            "metadata": 0
        }
        metadata_dict = errorutil.check_fields(self.request.body.decode(),
                                               body_categories, self)

        if "node_id" in metadata_dict and "link_id" in metadata_dict:
            self.set_status(400)
            self.write({
                "message":
                "Too many object id fields. Either use node_id or link_id"
            })
            return None

        userdata = jwthandler.decode_userdata(
            self.request.headers["Authorization"])

        metadata_id = metadata_dict["meta_id"]

        if metadata_dict == False or metadatautil.change_metadata(
                metadata_id, metadata_dict, self) == False:
            return None

        formatted_message = loggerhandler.form_message_dictionary(
            userdata, "metadata", metadata_id, metadata_dict)

        loggerhandler.log_message("change", formatted_message)

        self.write({"message": "Success"})
Beispiel #10
0
    def get(self):
        """
        Function to get all labels or a single label record
        Inputs: Tornado web request
        Output: Label data
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        body_categories = {"label_id":0}
        label_dict = errorutil.check_fields(self.request.arguments, body_categories, self)

        if label_dict == False:
            self.write(labelutil.get_labels())
            return None

        if "label_id" in label_dict:
            self.write(labelutil.get_label(label_dict["label_id"]))
Beispiel #11
0
    def put(self):
        """
        Function to change user data
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 2) == False:
            return None

        body_categories = {"username": 1, "password": 0, "admin": 0}
        user_dict = errorutil.check_fields(self.request.body.decode(),
                                           body_categories, self)

        if user_dict == False or userutil.change_user_fields(user_dict,
                                                             self) == False:
            return None

        self.write({'message': "Success"})
Beispiel #12
0
    def get(self):
        """
        Function to get views or a single view record
        Inputs: Tornado web request
        Output: View data
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        body_categories = {"view_id": 0}
        view_dict = errorutil.check_fields(self.request.arguments,
                                           body_categories, self)

        if view_dict == False:
            self.write(viewutil.get_views())
            return None

        if "view_id" in view_dict:
            self.write(viewutil.get_view(view_dict["view_id"]))
Beispiel #13
0
    def put(self):
        """
        Function to change a user password
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        user_data = jwthandler.decode_userdata(
            self.request.headers["Authorization"])

        body_categories = {"old_password": 1, "new_password": 1}
        password_dict = errorutil.check_fields(self.request.body.decode(),
                                               body_categories, self)
        print(
            userutil.compare_password(user_data["username"],
                                      password_dict["old_password"]))
        if userutil.compare_password(user_data["username"],
                                     password_dict["old_password"]):
            user_id = user_data["uid"]
            if userutil.change_password(user_id,
                                        password_dict["new_password"]):
                self.write({"message": "Success"})
            else:
                self.set_status(500)
                self.write({"message": "Failed to change password"})
                return None
        else:
            self.write({"message": "Failed to change password"})
            return None

        formatted_message = loggerhandler.form_message_dictionary(
            user_data, "user", user_data["uid"], {
                "old_password": "",
                "new_password": ""
            })

        loggerhandler.log_message("add", formatted_message)
        return None
Beispiel #14
0
    def post(self):
        """
        Function to create a metadata record
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        body_categories = {
            "node_id": 0,
            "link_id": 0,
            "category": 1,
            "data": 1
        }
        metadata_dict = errorutil.check_fields(self.request.body.decode(),
                                               body_categories, self)

        userdata = jwthandler.decode_userdata(
            self.request.headers["Authorization"])

        if metadata_dict == False:
            return None
        meta_id = 0
        if ("node_id" not in metadata_dict and "link_id" not in metadata_dict):
            self.set_status(400, "Empty object create request")
            self.write({"message": "Missing link_id or node_id field"})
            return None
        else:
            meta_id = metadatautil.create_category(metadata_dict, self)
            if meta_id == False:
                return None

        formatted_message = loggerhandler.form_message_dictionary(
            userdata, "metadata", meta_id, metadata_dict)

        loggerhandler.log_message("add", formatted_message)

        self.write({"message": "Success", "payload": meta_id})
Beispiel #15
0
    def post(self):
        """
        Function to authenticate a user
        Inputs: Tornado web request
        Output: Success message; Encrypted JWT token and user payload data
        Caveats: Authentication needs to be passed
        """
        body_categories = {"username": 1, "password": 1}
        user_dict = errorutil.check_fields(self.request.body.decode(),
                                           body_categories, self)

        if user_dict == False or userutil.user_exists(
                user_dict["username"]) == False:
            statement = "Failed login attempt %s : %s", (user_dict["username"],
                                                         user_dict["password"])
            flhandler.log_debug_to_file(statement)
            self.set_status(401)
            self.write({"message": "Authentication failed"})
            return None
        if (userutil.compare_password(user_dict["username"],
                                      user_dict["password"]) == False):
            statement = "Failed login attempt %s : %s", (user_dict["username"],
                                                         user_dict["password"])
            flhandler.log_debug_to_file(statement)
            self.set_status(401)
            self.write({"message": "Authentication failed"})
            return None

        username = user_dict["username"].lower()
        token = jwthandler.create_token(userutil.get_uid(username), username,
                                        (userutil.get_privilege(username) + 1))

        self.add_header("Authorization", token)
        self.write({
            "message": "Authenticated",
            "payload": {
                "User": username,
                "privilege": (userutil.get_privilege(username) + 1) >= 2
            }
        })
    def get(self):
        """
        Function to get relationships or a single relationship record
        Inputs: Tornado web request
        Output: Relationship data
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        body_categories = {"relationship_id": 0}
        relationship_dict = errorutil.check_fields(self.request.arguments,
                                                   body_categories, self)

        if relationship_dict == False:
            self.write(relationshiputil.get_relationships())
            return None

        if "relationship_id" in relationship_dict:
            self.write(
                relationshiputil.get_relationships(
                    relationship_dict["relationship_id"]))
Beispiel #17
0
    def post(self):
        """
        Function to register a new password
        Inputs: Tornado web request
        Output: Success message
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 0) == False:
            return None

        userdata = jwthandler.decode_userdata(
            self.request.headers["Authorization"])

        body_categories = {"username": 1, "password": 1, "privilege": 0}
        user_dict = errorutil.check_fields(self.request.body.decode(),
                                           body_categories, self)

        if user_dict == False or userutil.create_user(user_dict,
                                                      self) == False:
            return None

        username = user_dict["username"]

        token = jwthandler.create_token(userutil.get_uid(username), username,
                                        userutil.get_privilege(username) + 1)
        self.add_header("token", token)

        #sanitise password for logs
        del user_dict["password"]

        formatted_message = loggerhandler.form_message_dictionary(
            userdata, "user", userutil.get_uid(username), user_dict)

        loggerhandler.log_message("add", formatted_message)

        self.write({'message': "Success"})
Beispiel #18
0
    def get(self):
        """
        Function to get metadata or a single metadata record
        Inputs: Tornado web request
        Output: Metadata data
        Caveats: Authentication needs to be passed
        """
        if jwthandler.authorize_action(self, 1) == False:
            return None

        body_categories = {"node_id": 0, "link_id": 0}
        metadata_dict = errorutil.check_fields(self.request.arguments,
                                               body_categories, self)
        if metadata_dict == False:
            self.set_status(400)
            self.write({"message": "Empty get request"})
            return None

        if "node_id" in metadata_dict:
            self.write(metadatautil.get_node_metadata(
                metadata_dict["node_id"]))
        else:
            self.write(metadatautil.get_link_metadata(
                metadata_dict["link_id"]))