function update_access_tokens
Updates access control tokens for nodes in a Neo4j graph database by traversing relationships from token carrier nodes and storing accumulated access keys.
/tf/active/vicechatdev/dbinit.py
9 - 26
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_driveruuidconfigtimedatetimehashlib
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.
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function update_user_v1 55.7% similar
-
function update_node 52.9% similar
-
function update_user 52.1% similar
-
function push_changes 50.1% similar
-
function log_user_action 49.3% similar