mirror of
				https://github.com/open-metadata/OpenMetadata.git
				synced 2025-10-31 10:39:30 +00:00 
			
		
		
		
	* 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:
		
							parent
							
								
									c0b42b6825
								
							
						
					
					
						commit
						a7e39ffd14
					
				| @ -13,17 +13,22 @@ | |||||||
| 
 | 
 | ||||||
| package org.openmetadata.catalog.jdbi3; | package org.openmetadata.catalog.jdbi3; | ||||||
| 
 | 
 | ||||||
|  | import static org.openmetadata.catalog.Entity.TEAM; | ||||||
| import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; | import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; | ||||||
| 
 | 
 | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
|  | import java.util.HashSet; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
|  | import java.util.Set; | ||||||
| import java.util.UUID; | import java.util.UUID; | ||||||
| import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||||
|  | import javax.ws.rs.core.UriInfo; | ||||||
| import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||||
| import org.openmetadata.catalog.Entity; | 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.AuthenticationMechanism; | ||||||
| import org.openmetadata.catalog.entity.teams.Team; | import org.openmetadata.catalog.entity.teams.Team; | ||||||
| import org.openmetadata.catalog.entity.teams.User; | import org.openmetadata.catalog.entity.teams.User; | ||||||
| @ -191,6 +196,35 @@ public class UserRepository extends EntityRepository<User> { | |||||||
|     return validatedRoles; |     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 */ |   /* 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 { |   private List<EntityReference> getRoles(User user) throws IOException { | ||||||
|     List<EntityRelationshipRecord> roleIds = findTo(user.getId(), Entity.USER, Relationship.HAS, Entity.ROLE); |     List<EntityRelationshipRecord> roleIds = findTo(user.getId(), Entity.USER, Relationship.HAS, Entity.ROLE); | ||||||
|  | |||||||
| @ -18,6 +18,7 @@ import io.swagger.annotations.Api; | |||||||
| import io.swagger.v3.oas.annotations.ExternalDocumentation; | import io.swagger.v3.oas.annotations.ExternalDocumentation; | ||||||
| import io.swagger.v3.oas.annotations.Operation; | import io.swagger.v3.oas.annotations.Operation; | ||||||
| import io.swagger.v3.oas.annotations.Parameter; | 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.Content; | ||||||
| import io.swagger.v3.oas.annotations.media.ExampleObject; | import io.swagger.v3.oas.annotations.media.ExampleObject; | ||||||
| import io.swagger.v3.oas.annotations.media.Schema; | 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.JWTAuthMechanism; | ||||||
| import org.openmetadata.catalog.teams.authn.JWTTokenExpiry; | import org.openmetadata.catalog.teams.authn.JWTTokenExpiry; | ||||||
| import org.openmetadata.catalog.type.EntityHistory; | import org.openmetadata.catalog.type.EntityHistory; | ||||||
|  | import org.openmetadata.catalog.type.EntityReference; | ||||||
| import org.openmetadata.catalog.type.Include; | import org.openmetadata.catalog.type.Include; | ||||||
| import org.openmetadata.catalog.type.MetadataOperation; | import org.openmetadata.catalog.type.MetadataOperation; | ||||||
| import org.openmetadata.catalog.util.EntityUtil; | import org.openmetadata.catalog.util.EntityUtil; | ||||||
| @ -292,6 +294,30 @@ public class UserResource extends EntityResource<User, UserRepository> { | |||||||
|     return addHref(uriInfo, user); |     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 |   @GET | ||||||
|   @Path("/{id}/versions/{version}") |   @Path("/{id}/versions/{version}") | ||||||
|   @Operation( |   @Operation( | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Vivek Ratnavel Subramanian
						Vivek Ratnavel Subramanian