Fix #7200 Backend: Add API support to get all teams of type 'Group' f… (#7201)

* Fix #7200 Backend: Add API support to get all teams of type 'Group' for the logged in user

* Address review comments

* Add new API
This commit is contained in:
Vivek Ratnavel Subramanian 2022-09-06 01:14:24 -07:00 committed by GitHub
parent c0b42b6825
commit a7e39ffd14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 0 deletions

View File

@ -13,17 +13,22 @@
package org.openmetadata.catalog.jdbi3;
import static org.openmetadata.catalog.Entity.TEAM;
import static org.openmetadata.common.utils.CommonUtil.listOrEmpty;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.ws.rs.core.UriInfo;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.catalog.Entity;
import org.openmetadata.catalog.api.teams.CreateTeam.TeamType;
import org.openmetadata.catalog.entity.teams.AuthenticationMechanism;
import org.openmetadata.catalog.entity.teams.Team;
import org.openmetadata.catalog.entity.teams.User;
@ -191,6 +196,35 @@ public class UserRepository extends EntityRepository<User> {
return validatedRoles;
}
private List<EntityReference> getTeamChildren(UUID teamId) throws IOException {
if (teamId.equals(organization.getId())) { // For organization all the parentless teams are children
List<String> children = daoCollection.teamDAO().listTeamsUnderOrganization(teamId.toString());
return EntityUtil.populateEntityReferencesById(children, Entity.TEAM);
}
List<EntityRelationshipRecord> children = findTo(teamId, TEAM, Relationship.PARENT_OF, TEAM);
return EntityUtil.populateEntityReferences(children, TEAM);
}
public List<EntityReference> getGroupTeams(UriInfo uriInfo, String userName) throws IOException {
User user = getByName(uriInfo, userName, Fields.EMPTY_FIELDS, Include.ALL);
List<EntityReference> teams = getTeams(user);
return getGroupTeams(teams);
}
private List<EntityReference> getGroupTeams(List<EntityReference> teams) throws IOException {
Set<EntityReference> result = new HashSet<>();
for (EntityReference t : teams) {
Team team = Entity.getEntity(t, Fields.EMPTY_FIELDS, Include.ALL);
if (TeamType.GROUP.equals(team.getTeamType())) {
result.add(t);
} else {
List<EntityReference> children = getTeamChildren(team.getId());
result.addAll(getGroupTeams(children));
}
}
return new ArrayList<>(result);
}
/* Get all the roles that user has been assigned and inherited from the team to User entity */
private List<EntityReference> getRoles(User user) throws IOException {
List<EntityRelationshipRecord> roleIds = findTo(user.getId(), Entity.USER, Relationship.HAS, Entity.ROLE);

View File

@ -18,6 +18,7 @@ import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
@ -65,6 +66,7 @@ import org.openmetadata.catalog.teams.authn.GenerateTokenRequest;
import org.openmetadata.catalog.teams.authn.JWTAuthMechanism;
import org.openmetadata.catalog.teams.authn.JWTTokenExpiry;
import org.openmetadata.catalog.type.EntityHistory;
import org.openmetadata.catalog.type.EntityReference;
import org.openmetadata.catalog.type.Include;
import org.openmetadata.catalog.type.MetadataOperation;
import org.openmetadata.catalog.util.EntityUtil;
@ -292,6 +294,30 @@ public class UserResource extends EntityResource<User, UserRepository> {
return addHref(uriInfo, user);
}
@GET
@Valid
@Path("/loggedInUser/groupTeams")
@Operation(
operationId = "getCurrentLoggedInUserGroupTeams",
summary = "Get group type of teams for current logged in user",
tags = "users",
description = "Get the group type of teams of user who is authenticated and is currently logged in.",
responses = {
@ApiResponse(
responseCode = "200",
description = "The teams of type 'Group' that a user belongs to",
content =
@Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = EntityReference.class)))),
@ApiResponse(responseCode = "404", description = "User not found")
})
public List<EntityReference> getCurrentLoggedInUser(
@Context UriInfo uriInfo, @Context SecurityContext securityContext) throws IOException {
String currentUserName = securityContext.getUserPrincipal().getName();
return dao.getGroupTeams(uriInfo, currentUserName);
}
@GET
@Path("/{id}/versions/{version}")
@Operation(