🔍 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 boundaries.
  • The 'Keys' property stores comma-separated values as a string, which is not ideal for querying. Consider using array properties or separate relationship nodes.
  • No validation is performed on the UID parameter. Add input validation to prevent errors.
  • The function assumes all token carrier nodes have a 'UID' property. Ensure database schema consistency before running.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function update_user_v1 55.7% similar

    Updates user information in a Neo4j graph database, including username, full name, email, department, and active status, with automatic audit logging.

    From: /tf/active/vicechatdev/CDocs/controllers/admin_controller.py
  • function update_node 52.9% similar

    Updates properties of a Neo4j graph database node identified by its unique UID, automatically adding a modification timestamp.

    From: /tf/active/vicechatdev/CDocs/db/db_operations.py
  • function update_user 52.1% similar

    Updates an existing user's information in a Neo4j database, including profile fields, password, and role assignments.

    From: /tf/active/vicechatdev/CDocs/controllers/admin_controller.py
  • 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 log_user_action 49.3% similar

    Creates an audit event node in a graph database to log user actions, connecting it to both an audit trail and the user who performed the action.

    From: /tf/active/vicechatdev/CDocs/utils/audit_trail.py
← Back to Browse