8000 Load the organizations managing a user once per session by ivayloatns87 · Pull Request #55775 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Load the organizations managing a user once per session - #55775

Open
ivayloatns87 wants to merge 1 commit into
keycloak:mainfrom
ivayloatns87:issue-55257-managed-member-lookup
Open

ivayloatns87 wants to merge 1 commit into
keycloak:mainfrom
ivayloatns87:issue-55257-managed-member-lookup

Conversation

@ivayloatns87
Copy link
Copy Markdown

Closes #55257

JpaOrganizationProvider.isManagedMember now answers from the ids of the organizations that manage the user, loaded with one query per session, so Organizations.resolveOrganization and Organizations.isReadOnlyOrganizationMember no longer run two queries for every organization of the user.

Decisions and assumptions:

  • The ids are kept in a KeycloakSession attribute keyed by realm and user id, following existing session-scoped lookups such as the authSession.user.<id> attribute. No new cache or provider API is added, and the organization-selection order is unchanged.
  • The attribute is cleared after every membership change in the provider (addMember, addManagedMember, updateMembershipType, removeMember, and therefore organization removal), before the join and leave events fire.
  • throwIfManagedByAnotherOrg clears the attribute after taking the pessimistic lock on the user, so the check still reads memberships committed by a concurrent request instead of a set loaded earlier in the same request.
  • Users from a user storage provider without a local entity still return false without a query. An organization that does not exist or belongs to another realm now returns false instead of failing in getOrganizationGroup, since validating it would bring back one query per organization.
  • The query result is small: a user can be a managed member of at most one organization.

I used an AI coding agent (Claude Code) to write this change and its test; I reviewed and tested it.

@ivayloatns87
ivayloatns87 marked this pull request as ready for review December 3, 2026 09:03
@ivayloatns87
ivayloatns87 requested a review from a team as a code owner December 3, 2026 09:03
Copilot AI balanced review requested due to automatic review settings December 3, 2026 09:03
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new test would also pass with the original per-organization queries, leaving the optimization unprotected without query-count assertions and reload verification after membership changes.

1 open issue
What changed in this PR

Reduces repeated database queries during organization membership checks by caching managing-organization IDs within each Keycloak session.

Changes:

  • Adds a realm-scoped query for managing-organization IDs.
  • Reuses results and invalidates them after membership changes and locking.
  • Adds integration coverage for membership updates, login, and removal.
File Description
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​organization/​member/​OrganizationMemberTest.java Tests managed membership among multiple organizations.
model/​jpa/​src/​main/​java/​org/​keycloak/​organization/​jpa/​JpaOrganizationProvider.java Adds session-scoped lookup caching and invalidation.
model/​jpa/​src/​main/​java/​org/​keycloak/​models/​jpa/​entities/​OrganizationEntity.java Defines the managing-organization ID query.

Review effort: Balanced

Closes keycloak#55257

Signed-off-by: Ivaylo Atanasov <ivayloatns87@gmail.com>
@ivayloatns87
ivayloatns87 force-pushed the issue-55257-managed-member-lookup branch from 7c1b54b to 6df3036 Compare December 3, 2026 09:16
Copilot AI balanced review requested due to automatic review settings December 3, 2026 09:16
Copilot AI left a comment
Copy link
Copy Markdown
Contributor 8000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the change to reduce repeated organization-membership queries.

1 open issue

Review effort: Balanced

@ivayloatns87
Copy link
Copy Markdown
Author

@martin-kanis this implements the per-session approach you suggested in #55257. The workflow runs are waiting for approval whenever you have a moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Login without an organization scope runs two queries per organization for unmanaged members

2 participants

0