Skip to content

Commit

Permalink
Optimize email_in_org_cached
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhai committed Sep 27, 2024
1 parent 3c89bb5 commit 245b057
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions llmstack/apps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def has_write_permission(self, user):
self.owner == user
or user.email in self.write_accessible_by
or (
Profile.email_in_org_cached(self.owner_profile.organization, user.email)
Profile.user_in_org_cached(self.owner_profile.organization, user)
and self.visibility >= AppVisibility.ORGANIZATION
and self.owner_profile.organization.settings.default_app_access_permission == AppAccessPermission.WRITE
)
Expand All @@ -402,7 +402,7 @@ def has_read_permission(self, user):
or self.has_write_permission(user)
or user.email in self.read_accessible_by
or (
Profile.email_in_org_cached(self.owner_profile.organization, user.email)
Profile.user_in_org_cached(self.owner_profile.organization, user)
and self.visibility >= AppVisibility.ORGANIZATION
)
)
Expand Down
14 changes: 7 additions & 7 deletions llmstack/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,13 @@ def get_by_encrypted_uuid(cls, encrypted_uuid):

@staticmethod
@cache
def email_in_org_cached(organization, email):
profiles = Profile.objects.filter(user__email=email)
return (
profiles.exists()
and profiles.values("organization").distinct().count() == 1
and profiles.first().organization == organization
)
def user_in_org_cached(organization, user):
profile = Profile.objects.get(user=user)

if not profile:
return False

return profile.organization == organization


class DefaultProfile(AbstractProfile):
Expand Down
4 changes: 2 additions & 2 deletions llmstack/data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def has_read_permission(self, user):
self.owner == user
or user.email in self.read_accessible_by
or (
Profile.email_in_org_cached(self.profile.organization, user.email)
Profile.user_in_org_cached(self.profile.organization, user)
and self.visibility == DataSourceVisibility.ORGANIZATION
)
)
Expand All @@ -183,7 +183,7 @@ def has_write_permission(self, user):
self.owner == user
or user.email in self.write_accessible_by
or (
Profile.email_in_org_cached(self.profile.organization, user.email)
Profile.user_in_org_cached(self.profile.organization, user)
and self.visibility == DataSourceVisibility.ORGANIZATION
and self.profile.organization.settings.default_datasource_access_permission
== DataSourceAccessPermission.WRITE
Expand Down

0 comments on commit 245b057

Please sign in to comment.