Hi everyone, I have this schema: definition user...
# spicedb
p
Hi everyone, I have this schema: definition user {} definition rol { relation associated: user } definition institution { relation belongs: rol } and I need to responde to this questions: get all intitution by user and get all roles by user for some institution. I answer the question all of user for one institution with this code in python
Copy code
python
"""."""
from authzed.api.v1 import (
    Client,
    Consistency,
    ObjectReference,
    LookupSubjectsRequest,
    LookupResourcesRequest
)
from grpcutil import insecure_bearer_token_credentials
from config import HOST, PORT, TOKEN_CREDENTIAL

client = Client(
    f"{HOST}:{PORT}",
    insecure_bearer_token_credentials(TOKEN_CREDENTIAL),
)

def query_subject(resource_type: str, relation: str, s_type: str, s_id: str):
    """."""

    subject = ObjectReference(object_type=s_type, object_id=s_id)

    return client.LookupSubjects(
        LookupSubjectsRequest(
            subject_object_type=resource_type,
            permission=relation,
            resource=subject,
            consistency=Consistency(fully_consistent=True),
        )
    )

def get_user_by_intitution(institution: str):
    """This function returns all users asociated to an institution."""
    roles = query_subject("rol", "belongs", "institution", institution)
    users = [query_subject("user", "associated", "rol", role.subject_object_id) for role in roles]
    return [user for sublist in users for user in sublist]