Skip to content

Commit

Permalink
Don't load filtered-out parents eagerly on every Object.access
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 committed Dec 11, 2024
1 parent 38a4f65 commit 2d35876
Showing 1 changed file with 3 additions and 27 deletions.
30 changes: 3 additions & 27 deletions mwdb/model/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy import and_, cast, distinct, exists, func, select
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import aliased, column_property, contains_eager
from sqlalchemy.orm import column_property
from sqlalchemy.sql.expression import true

from mwdb.core.capabilities import Capabilities
Expand Down Expand Up @@ -467,35 +467,11 @@ def access(cls, identifier, requestor=None):
if requestor is None:
requestor = g.auth_user

obj = cls.get(identifier)
obj = cls.get(identifier).first()
# If object doesn't exist - it doesn't exist
if obj.first() is None:
if obj is None:
return None

# In that case we want only those parents to which requestor has access.
stmtp = (
db.session.query(Object)
.join(relation, relation.c.parent_id == Object.id)
.filter(
Object.id.in_(
db.session.query(relation.c.parent_id).filter(
relation.c.child_id == obj.first().id
)
)
)
.order_by(relation.c.creation_time.desc())
.filter(requestor.has_access_to_object(Object.id))
)
stmtp = stmtp.subquery()

parent = aliased(Object, stmtp)

obj = (
obj.outerjoin(parent, Object.parents)
.options(contains_eager(Object.parents, alias=parent))
.all()[0]
)

# Ok, now let's check whether requestor has explicit access
if obj.has_explicit_access(requestor):
return obj
Expand Down

0 comments on commit 2d35876

Please sign in to comment.