Skip to content

Commit 365c842

Browse files
fix: Add cascading delete to tenant membership role (#43)
1 parent bfa74c6 commit 365c842

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
3+
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.2.xsd">
5+
6+
<changeSet id="20240521-0014-2" author="keycloak-multi-tenancy">
7+
<dropForeignKeyConstraint baseTableName="TENANT_MEMBERSHIP_ROLE" constraintName="FK_TENANT_MEMBERSHIP_ROLE_TENANT_MEMBERSHIP_ID"/>
8+
</changeSet>
9+
10+
<changeSet id="20240521-0014-3" author="keycloak-multi-tenancy">
11+
<addForeignKeyConstraint baseColumnNames="TENANT_MEMBERSHIP_ID" baseTableName="TENANT_MEMBERSHIP_ROLE"
12+
constraintName="FK_TENANT_MEMBERSHIP_ROLE_TENANT_MEMBERSHIP_ID" referencedColumnNames="ID" referencedTableName="TENANT_MEMBERSHIP" onDelete="CASCADE"/>
13+
</changeSet>
14+
</databaseChangeLog>

src/main/resources/META-INF/keycloak-multi-tenancy-changelog-master.xml

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.2.xsd">
33

44
<include file="META-INF/keycloak-multi-tenancy-changelog-20221217-2113.xml"/>
5+
<include file="META-INF/keycloak-multi-tenancy-changelog-20240521-0014.xml"/>
56

67
</databaseChangeLog>

src/test/java/dev/sultanov/keycloak/multitenancy/ApiIntegrationTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,35 @@ void adminUpdatesTenant_shouldReturnConflict_whenUpdatedTenantNameAlreadyExists(
9797
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_CONFLICT);
9898
}
9999
}
100+
101+
@Test
102+
void userRemoval_shouldRemoveTheirMembership() {
103+
// given
104+
var adminUser = keycloakAdminClient.createVerifiedUser();
105+
var tenantResource = adminUser.createTenant();
106+
107+
var user = keycloakAdminClient.createVerifiedUser();
108+
109+
var invitation = new TenantInvitationRepresentation();
110+
invitation.setEmail(user.getUserData().getEmail());
111+
try (var response = tenantResource.invitations().createInvitation(invitation)) {
112+
assertThat(CreatedResponseUtil.getCreatedId(response)).isNotNull();
113+
}
114+
115+
var nextPage = AccountPage.open()
116+
.signIn()
117+
.fillCredentials(user.getUserData().getEmail(), user.getUserData().getPassword())
118+
.signIn();
119+
assertThat(nextPage).isInstanceOf(ReviewInvitationsPage.class);
120+
((ReviewInvitationsPage) nextPage).accept();
121+
122+
// when
123+
try (var response = keycloakAdminClient.getRealmResource().users().delete(user.getUserId())) {
124+
125+
//then
126+
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NO_CONTENT);
127+
assertThat(tenantResource.memberships().listMemberships(null, null, null))
128+
.noneMatch(membership -> membership.getUser().getEmail().equalsIgnoreCase(user.getUserData().getEmail()));
129+
}
130+
}
100131
}

0 commit comments

Comments
 (0)