Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds endpoints to manually create users and groups from the provider #863

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import de.terrestris.shogun.lib.model.Group;
import de.terrestris.shogun.lib.model.User;
import de.terrestris.shogun.lib.service.GroupService;
import de.terrestris.shogun.lib.service.security.provider.GroupProviderService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
Expand All @@ -36,12 +38,15 @@
@ConditionalOnExpression("${controller.groups.enabled:true}")
@Log4j2
@Tag(
name = "Users",
description = "The endpoints to manage users"
name = "Groups",
description = "The endpoints to manage groups"
)
@SecurityRequirement(name = "bearer-key")
public class GroupController extends BaseController<GroupService, Group> {

@Autowired
private GroupProviderService groupProviderService;

@GetMapping("/user/{id}")
@ResponseStatus(HttpStatus.OK)
public List<Group> findByUser(@PathVariable("id") Long userId) {
Expand Down Expand Up @@ -93,4 +98,9 @@ public List<User> getGroupMembers(@PathVariable("id") String keycloakId) {
}
}

@PostMapping("/createFromProvider")
@ResponseStatus(HttpStatus.OK)
public void createFromProvider() {
groupProviderService.createAllGroups();
}
Comment on lines +101 to +105
Copy link
Member

Choose a reason for hiding this comment

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

Can we add some notes for the OpenAPI spec? The controllers in the BaseController might be a good inspiration.

And can we also add some error handling?

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,35 @@

import de.terrestris.shogun.lib.model.User;
import de.terrestris.shogun.lib.service.UserService;
import de.terrestris.shogun.lib.service.security.provider.UserProviderService;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
@ConditionalOnExpression("${controller.users.enabled:true}")
@Log4j2
@Tag(
name = "Users",
description = "The endpoints to manage users"
)
@SecurityRequirement(name = "bearer-key")
public class UserController extends BaseController<UserService, User> { }
public class UserController extends BaseController<UserService, User> {

@Autowired
private UserProviderService userProviderService;

@PostMapping("/createFromProvider")
@ResponseStatus(HttpStatus.OK)
public void createFromProvider() {
userProviderService.createAllUsers();
}
Comment on lines +47 to +51
Copy link
Member

Choose a reason for hiding this comment

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

See above.

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ public interface GroupProviderService<UserType, GroupType> {

Group<GroupType> findOrCreateByProviderId(String providerGroupId);

void createAllGroups();
Copy link
Member

Choose a reason for hiding this comment

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

Just wondering, do we need to flag this commit as breaking?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not breaking anything, is it? It's a new feature.

Copy link
Member

Choose a reason for hiding this comment

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

Since it's changing an interface, project implementations would need to include the implementation of the newly added method.

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ public interface UserProviderService<T> {

Optional<User<T>> getUserFromAuthentication(Authentication authentication);

void createAllUsers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,9 @@ public Group<GroupRepresentation> findOrCreateByProviderId(String keycloakGroupI
return group;
}

@Override
public void createAllGroups() {
var groupIds = keycloakUtil.getAllGroupIds();
groupIds.forEach(this::findOrCreateByProviderId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,9 @@ public Optional<User<UserRepresentation>> getUserFromAuthentication(Authenticati
return (Optional) userRepository.findByAuthProviderId(keycloakUserId);
}

@Override
public void createAllUsers() {
var userIds = keycloakUtil.getAllUserIds();
userIds.forEach(this::findOrCreateByProviderId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.keycloak.admin.client.CreatedResponseUtil;
import org.keycloak.admin.client.resource.*;
import org.keycloak.representations.IDToken;
import org.keycloak.representations.idm.AbstractUserRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
Expand Down Expand Up @@ -57,6 +58,11 @@ public UserResource getUserResource(String id) {
return kcUsers.get(id);
}

public List<String> getAllUserIds() {
UsersResource kcUsers = this.keycloakRealm.users();
return kcUsers.list().stream().map(AbstractUserRepresentation::getId).toList();
}

public GroupResource getGroupResource(Group<GroupRepresentation> group) {
GroupsResource kcGroups = this.keycloakRealm.groups();
return kcGroups.group(group.getAuthProviderId());
Expand All @@ -67,6 +73,11 @@ public GroupResource getGroupResource(String id) {
return kcGroups.group(id);
}

public List<String> getAllGroupIds() {
GroupsResource kcGroups = this.keycloakRealm.groups();
return kcGroups.groups().stream().map(GroupRepresentation::getId).toList();
}

public void addUserToGroup(User<UserRepresentation> user, Group<GroupRepresentation> group) {
UserResource kcUser = this.getUserResource(user);
GroupResource kcGroup = this.getGroupResource(group);
Expand Down
Loading