Skip to content

Commit

Permalink
setup should run only once for a request
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreyfourmis committed Jan 20, 2024
1 parent 79eebe8 commit d6363c6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@ public abstract class AbstractAdminResource<T extends AdminAuth> {
public AbstractAdminResource(KeycloakSession session) {
this.session = session;
this.realm = session.getContext().getRealm();
this.setup();
}

public final void setup() {
protected AbstractAdminResource(AbstractAdminResource<T> parent) {
this.session = parent.session;
this.realm = parent.realm;
this.auth = parent.auth;
this.adminEvent = parent.adminEvent;
this.user = parent.user;
}

private void setup() {
setupAuth();
setupEvents();
setupProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class TenantInvitationsResource extends AbstractAdminResource<TenantAdmin

private final TenantModel tenant;

public TenantInvitationsResource(KeycloakSession session, TenantModel tenant) {
super(session);
public TenantInvitationsResource(AbstractAdminResource<TenantAdminAuth> parent, TenantModel tenant) {
super(parent);
this.tenant = tenant;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class TenantMembershipsResource extends AbstractAdminResource<TenantAdmin

private final TenantModel tenant;

public TenantMembershipsResource(KeycloakSession session, TenantModel tenant) {
super(session);
public TenantMembershipsResource(AbstractAdminResource<TenantAdminAuth> parent, TenantModel tenant) {
super(parent);
this.tenant = tenant;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class TenantResource extends AbstractAdminResource<TenantAdminAuth> {

private final TenantModel tenant;

public TenantResource(KeycloakSession session, TenantModel tenant) {
super(session);
public TenantResource(AbstractAdminResource<TenantAdminAuth> parent, TenantModel tenant) {
super(parent);
this.tenant = tenant;
}

Expand All @@ -39,11 +39,11 @@ public void deleteTenant() {

@Path("invitations")
public TenantInvitationsResource invitations() {
return new TenantInvitationsResource(session, tenant);
return new TenantInvitationsResource(this, tenant);
}

@Path("memberships")
public TenantMembershipsResource memberships() {
return new TenantMembershipsResource(session, tenant);
return new TenantMembershipsResource(this, tenant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public TenantResource getTenantResource(@PathParam("tenantId") String tenantId)
if (!auth.isTenantAdmin(model)) {
throw new NotAuthorizedException(String.format("Insufficient permission to access %s", tenantId));
} else {
return new TenantResource(session, model);
return new TenantResource(this, model);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public Object getResource() {
if (request != null && "OPTIONS".equals(request.getHttpMethod())) {
return new CorsResource(request);
} else {
TenantsResource resource = new TenantsResource(session);
resource.setup();
return resource;
return new TenantsResource(session);
}
}

Expand Down

0 comments on commit d6363c6

Please sign in to comment.