Skip to content

Commit

Permalink
Merge pull request #30 from ZennoLab/requiredActions
Browse files Browse the repository at this point in the history
supported passing requiredActions to new User
  • Loading branch information
daniel-frak authored May 16, 2021
2 parents 1b39e21 + 22dba08 commit 0966d9b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ The GET request will have to return user data as a JSON response in the form:
"key": ["value"]
},
"roles": ["string"],
"groups": ["string"]
"groups": ["string"],
"requiredActions": ["requiredActions"]
}
```

Expand Down Expand Up @@ -83,7 +84,8 @@ The response might look like this:
"likes": ["cats", "dogs", "cookies"]
},
"roles": ["admin"],
"groups": ["migrated_users"]
"groups": ["migrated_users"],
"requiredActions": ["CONFIGURE_TOTP", "UPDATE_PASSWORD", "UPDATE_PROFILE", "update_user_locale"]
}
```

Expand Down Expand Up @@ -173,4 +175,4 @@ automatically map legacy groups to Keycloak groups, by specifying the mapping in
### Migrate unmapped groups

This switch can be toggled to decide whether groups which are not defined in the legacy group conversion map should be
migrated anyway or simply ignored.
migrated anyway or simply ignored.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class LegacyUser {
private Map<String, List<String>> attributes;
private List<String> roles;
private List<String> groups;
private List<String> requiredActions;

public String getId() {
return id;
Expand Down Expand Up @@ -100,6 +101,14 @@ public void setGroups(List<String> groups) {
this.groups = groups;
}

public List<String> getRequiredActions() {
return requiredActions;
}

public void setRequiredActions(List<String> requiredActions) {
this.requiredActions = requiredActions;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -119,12 +128,13 @@ public boolean equals(Object o) {
Objects.equals(lastName, legacyUser.lastName) &&
Objects.equals(attributes, legacyUser.attributes) &&
Objects.equals(roles, legacyUser.roles) &&
Objects.equals(groups, legacyUser.groups);
Objects.equals(groups, legacyUser.groups) &&
Objects.equals(requiredActions, legacyUser.requiredActions);
}

@Override
public int hashCode() {
return Objects.hash(id, username, email, firstName, lastName, isEnabled, isEmailVerified, attributes,
roles, groups);
roles, groups, requiredActions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public UserModel create(LegacyUser legacyUser, RealmModel realm) {
getGroupModels(legacyUser, realm)
.forEach(userModel::joinGroup);

if (legacyUser.getRequiredActions() != null) {
legacyUser.getRequiredActions()
.forEach(userModel::addRequiredAction);
}

return userModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ void shouldGetAndSetGroups() {
assertEquals(expectedValue, user.getGroups());
}

@Test
void shouldGetAndSetRequiredActions() {
var user = new LegacyUser();
var expectedValue = singletonList("value1");
user.setRequiredActions(expectedValue);
assertEquals(expectedValue, user.getRequiredActions());
}

@Test
void testEquals() {
EqualsVerifier.simple().forClass(LegacyUser.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TestUserModel implements UserModel {
private Map<String, List<String>> attributes = new HashMap<>();
private Set<RoleModel> roles = new HashSet<>();
private Set<GroupModel> groups = new HashSet<>();
private Set<String> requiredActions = new HashSet<>();
private String federationLink;

public TestUserModel(String username) {
Expand Down Expand Up @@ -181,17 +182,17 @@ public Map<String, List<String>> getAttributes() {

@Override
public Set<String> getRequiredActions() {
throw new RuntimeException("Not implemented");
return this.requiredActions;
}

@Override
public void addRequiredAction(String action) {
throw new RuntimeException("Not implemented");
this.requiredActions.add(action);
}

@Override
public void removeRequiredAction(String action) {
throw new RuntimeException("Not implemented");
this.requiredActions.remove(action);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,24 @@ void setsFederationLink() {

assertEquals(MODEL_ID, result.getFederationLink());
}

@Test
void migratesRequiredActions() {
final UserProvider userProvider = mock(UserProvider.class);
final RealmModel realm = mock(RealmModel.class);
final String username = "user";

when(session.userLocalStorage())
.thenReturn(userProvider);
when(userProvider.addUser(realm, username))
.thenReturn(new TestUserModel(username));

LegacyUser legacyUser = createLegacyUser(username);
legacyUser.setRequiredActions(List.of("CONFIGURE_TOTP", "UPDATE_PASSWORD"));

var result = userModelFactory.create(legacyUser, realm);

assertEquals(Set.of("CONFIGURE_TOTP", "UPDATE_PASSWORD"), result.getRequiredActions());
}

}

0 comments on commit 0966d9b

Please sign in to comment.