135 lines
3.9 KiB
Python
135 lines
3.9 KiB
Python
from db_logging import log_change
|
|
|
|
|
|
def ensure_entity_active(cursor, entity_id):
|
|
"""
|
|
Ensure an entity exists and is active.
|
|
Revoked entities are immutable.
|
|
"""
|
|
cursor.execute("SELECT status FROM entity WHERE id = %s", (entity_id,))
|
|
row = cursor.fetchone()
|
|
if row is None:
|
|
raise ValueError("Entity does not exist")
|
|
if row["status"] != "active":
|
|
raise ValueError("Entity is not active")
|
|
|
|
|
|
def insert_creator(cursor, name, public_key):
|
|
"""
|
|
Creators are persons with property 'creator' in the property table.
|
|
"""
|
|
cursor.execute(
|
|
"""
|
|
INSERT INTO entity (name, type, public_key, status)
|
|
VALUES (%s, 'person', %s, 'active')
|
|
RETURNING id
|
|
""",
|
|
(name, public_key),
|
|
)
|
|
creator_id = cursor.fetchone()["id"]
|
|
|
|
# Mark as creator via property table (schema: property(id, property_name))
|
|
cursor.execute(
|
|
"""
|
|
INSERT INTO property (id, property_name)
|
|
VALUES (%s, %s)
|
|
ON CONFLICT (id, property_name) DO NOTHING
|
|
""",
|
|
(creator_id, "creator"),
|
|
)
|
|
|
|
log_change(cursor, f"Created creator entity {creator_id} with name {name}")
|
|
return creator_id
|
|
|
|
|
|
def enroll_person(cursor, name, public_key, creator_id):
|
|
ensure_entity_active(cursor, creator_id)
|
|
|
|
cursor.execute(
|
|
"""
|
|
INSERT INTO entity (name, type, public_key, creator, status)
|
|
VALUES (%s, 'person', %s, %s, 'active')
|
|
RETURNING id
|
|
""",
|
|
(name, public_key, creator_id),
|
|
)
|
|
person_id = cursor.fetchone()["id"]
|
|
|
|
log_change(cursor, f"Enrolled person {person_id} under creator {creator_id}")
|
|
return person_id
|
|
|
|
|
|
def create_group(cursor, name, public_key, creator_id):
|
|
ensure_entity_active(cursor, creator_id)
|
|
|
|
cursor.execute(
|
|
"""
|
|
INSERT INTO entity (name, type, public_key, creator, status)
|
|
VALUES (%s, 'group', %s, %s, 'active')
|
|
RETURNING id
|
|
""",
|
|
(name, public_key, creator_id),
|
|
)
|
|
group_id = cursor.fetchone()["id"]
|
|
|
|
log_change(cursor, f"Created group {group_id} under creator {creator_id}")
|
|
return group_id
|
|
|
|
|
|
def create_alias(cursor, target_entity_id):
|
|
ensure_entity_active(cursor, target_entity_id)
|
|
|
|
cursor.execute(
|
|
"""
|
|
INSERT INTO entity (name, type, creator, status)
|
|
VALUES (%s, 'alias', %s, 'active')
|
|
RETURNING id
|
|
""",
|
|
(f"alias_for_{target_entity_id}", target_entity_id),
|
|
)
|
|
alias_id = cursor.fetchone()["id"]
|
|
|
|
log_change(cursor, f"Created alias {alias_id} for entity {target_entity_id}")
|
|
return alias_id
|
|
|
|
|
|
def get_entity(cursor, entity_id):
|
|
cursor.execute("SELECT * FROM entity WHERE id = %s", (entity_id,))
|
|
return cursor.fetchone()
|
|
|
|
|
|
def set_entity_status(cursor, entity_id, status, changed_by):
|
|
"""
|
|
Only active entities can change status. Once revoked, immutable.
|
|
"""
|
|
ensure_entity_active(cursor, entity_id)
|
|
|
|
cursor.execute("UPDATE entity SET status = %s WHERE id = %s", (status, entity_id))
|
|
log_change(cursor, f"Set status of entity {entity_id} to {status} by {changed_by}")
|
|
|
|
|
|
def set_symmetrical_key(cursor, entity_id, key_value, changed_by):
|
|
ensure_entity_active(cursor, entity_id)
|
|
|
|
cursor.execute(
|
|
"UPDATE entity SET symmetrical_key = %s WHERE id = %s",
|
|
(key_value, entity_id),
|
|
)
|
|
log_change(cursor, f"Set symmetrical_key for entity {entity_id} by {changed_by}")
|
|
|
|
|
|
def get_symmetrical_key(cursor, entity_id):
|
|
cursor.execute("SELECT symmetrical_key FROM entity WHERE id = %s", (entity_id,))
|
|
row = cursor.fetchone()
|
|
return row["symmetrical_key"] if row else None
|
|
|
|
|
|
def set_entity_keys(cursor, entity_id, public_key, changed_by):
|
|
ensure_entity_active(cursor, entity_id)
|
|
|
|
cursor.execute(
|
|
"UPDATE entity SET public_key = %s WHERE id = %s",
|
|
(public_key, entity_id),
|
|
)
|
|
log_change(cursor, f"Updated public key for entity {entity_id} by {changed_by}")
|