🔍 Code Extractor

function update_access_tokens

Maturity: 34

Updates access control tokens for nodes in a Neo4j graph database by traversing relationships from token carrier nodes and storing accumulated access keys.

File:
/tf/active/vicechatdev/dbinit.py
Lines:
9 - 26
Complexity:
moderate

Purpose

This function manages access control in a Neo4j graph database by propagating access tokens from parent nodes (Configuration, Projects, Project, People, Compounds, Library) to child nodes within 5 relationship hops. It either updates a single node specified by UID or all nodes in the graph, setting a 'Keys' property containing comma-separated UIDs of all token carrier nodes that have paths to the target node.

Source Code

def update_access_tokens(graph, UID=None):
        token_carriers=['Configuration','Projects','Project','People','Compounds','Library']
        if UID!=None:
            all_nodes=[UID]
            print(all_nodes)
        else:
            all_nodes=graph.run("match (x) return collect(x.UID)").evaluate()
        for n in all_nodes:
            all_keys=[]
            for k in token_carriers:
                print(k)
                keys=graph.run("match (x:"+k+")-[*..5]->(y {UID:'"+n+"'}) return collect(distinct x.UID)").evaluate()
                #print(keys)
                all_keys.extend(keys)
            if all_keys!=[]:
                #print("match (y {UID:'"+n+"'}) set y.Keys='"+",".join(all_keys))
                out=graph.run("match (y {UID:'"+n+"'}) set y.Keys='"+",".join(all_keys)+"'")
        return

Parameters

Name Type Default Kind
graph - - positional_or_keyword
UID - None positional_or_keyword

Parameter Details

graph: A Neo4j graph database connection object (likely from py2neo or similar Neo4j driver) that provides a 'run' method for executing Cypher queries. This object must be connected and authenticated to the target database.

UID: Optional string parameter representing a unique identifier for a specific node to update. If None (default), the function will update access tokens for all nodes in the graph. If provided, only the node with this UID will be updated.

Return Value

Returns None (implicit return). The function performs side effects by updating the 'Keys' property on nodes in the Neo4j database but does not return any value.

Dependencies

  • neo4j_driver
  • uuid
  • config
  • time
  • datetime
  • hashlib

Required Imports

from neo4j_driver import *
import uuid
import config
import time
import datetime as dt
from uuid import uuid4
import hashlib

Usage Example

from neo4j_driver import *
import config

# Establish Neo4j connection
graph = Graph(config.NEO4J_URI, auth=(config.NEO4J_USER, config.NEO4J_PASSWORD))

# Update access tokens for all nodes
update_access_tokens(graph)

# Update access tokens for a specific node
update_access_tokens(graph, UID='node-uuid-12345')

Best Practices

  • This function uses string concatenation for Cypher queries which is vulnerable to injection attacks. Consider using parameterized queries instead.
  • The function performs potentially expensive graph traversals (up to 5 hops) for each node, which may be slow on large graphs. Consider batching or optimizing queries.
  • Print statements should be removed or replaced with proper logging for production use.
  • The function modifies the database without transaction management or error handling. Add try-except blocks and transaction rollback capabilities.
  • The 'Keys' property stores comma-separated values as a string, which is not ideal for querying. Consider using array properties or separate relationship nodes.
  • Ensure the graph connection has appropriate write permissions before calling this function.
  • For large graphs, consider running this function during off-peak hours or implementing progress tracking.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function push_changes 50.1% similar

    Updates a node's properties in a Neo4j graph database by matching on UID and setting new property values.

    From: /tf/active/vicechatdev/offline_docstore_multi_vice.py
  • function add_document_to_graph 44.4% similar

    Creates nodes and relationships in a Neo4j graph database for a processed document, including its text and table chunks, connecting it to a folder hierarchy.

    From: /tf/active/vicechatdev/offline_docstore_multi.py
  • function add_document_to_graph_v1 42.4% similar

    Creates a Neo4j graph node for a processed document and connects it to a folder hierarchy, along with its text and table chunks.

    From: /tf/active/vicechatdev/offline_docstore_multi_vice.py
  • class User 41.8% similar

    A user management class that handles authentication, authorization, user profiles, preferences, file management, and logging for a Panel-based web application with Neo4j backend.

    From: /tf/active/vicechatdev/userclass.py
  • function get_documents 40.7% similar

    Retrieves filtered and paginated documents from a Neo4j graph database with permission-based access control, supporting multiple filter criteria and search functionality.

    From: /tf/active/vicechatdev/document_controller_backup.py
← Back to Browse