Skip to content

Commit

Permalink
1622 - added deleted_at to user_owner_mapping table and created creat…
Browse files Browse the repository at this point in the history
…eUserOwnerMapping and deleteUserOwnerMapping API's
  • Loading branch information
Vladysl committed Feb 21, 2024
1 parent 57a4ec3 commit ffa9deb
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.OWNER_ASSOCIATION_MANAGE;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.OWNER_CREATE;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.OWNER_DELETE;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.OWNER_RELATION_MANAGE;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.OWNER_UPDATE;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.POLICY_CREATE;
import static org.opendatadiscovery.oddplatform.dto.policy.PolicyPermissionDto.POLICY_DELETE;
Expand Down Expand Up @@ -149,6 +150,14 @@ NO_CONTEXT, new PathPatternParserServerWebExchangeMatcher("/api/owner_associatio
NO_CONTEXT, new PathPatternParserServerWebExchangeMatcher(
"/api/owner_association_request/{owner_association_request_id}", PUT),
OWNER_ASSOCIATION_MANAGE),
new SecurityRule(
NO_CONTEXT, new PathPatternParserServerWebExchangeMatcher(
"/api/owners/mapping", POST),
OWNER_RELATION_MANAGE),
new SecurityRule(
NO_CONTEXT, new PathPatternParserServerWebExchangeMatcher(
"/api/owners/mapping/{owner_id}", DELETE),
OWNER_RELATION_MANAGE),
new SecurityRule(NO_CONTEXT, new PathPatternParserServerWebExchangeMatcher("/api/policies", POST),
POLICY_CREATE),
new SecurityRule(NO_CONTEXT, new PathPatternParserServerWebExchangeMatcher("/api/policies/{policy_id}", PUT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.opendatadiscovery.oddplatform.api.contract.model.Owner;
import org.opendatadiscovery.oddplatform.api.contract.model.OwnerFormData;
import org.opendatadiscovery.oddplatform.api.contract.model.OwnerList;
import org.opendatadiscovery.oddplatform.api.contract.model.UserOwnerMappingFormData;
import org.opendatadiscovery.oddplatform.service.OwnerService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -52,4 +53,20 @@ public Mono<ResponseEntity<Owner>> updateOwner(final Long ownerId,
.flatMap(form -> ownerService.update(ownerId, form))
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<Owner>>
createUserOwnerMapping(final Mono<UserOwnerMappingFormData> userOwnerMappingFormData,
final ServerWebExchange exchange) {
return userOwnerMappingFormData
.flatMap(ownerService::createUserOwnerMapping)
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<Void>> deleteActiveUserOwnerMapping(final Long ownerId,
final ServerWebExchange exchange) {
return ownerService.deleteActiveUserOwnerMapping(ownerId)
.thenReturn(ResponseEntity.noContent().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.Collection;
import org.opendatadiscovery.oddplatform.model.tables.pojos.OwnerPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RolePojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.UserOwnerMappingPojo;

public record OwnerDto(OwnerPojo ownerPojo, Collection<RolePojo> roles) {
public record OwnerDto(OwnerPojo ownerPojo,
Collection<RolePojo> roles,
UserOwnerMappingPojo associatedUser) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public enum PolicyPermissionDto {
OWNER_UPDATE(MANAGEMENT),
OWNER_DELETE(MANAGEMENT),
OWNER_ASSOCIATION_MANAGE(MANAGEMENT),
OWNER_RELATION_MANAGE(MANAGEMENT),
DIRECT_OWNER_SYNC(MANAGEMENT),
POLICY_CREATE(MANAGEMENT),
POLICY_UPDATE(MANAGEMENT),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ public Mono<Long> getTotalActivitiesCount(final OffsetDateTime beginDate,
final var countQuery = DSL.selectCount()
.from(ACTIVITY)
.join(DATA_ENTITY).on(DATA_ENTITY.ID.eq(ACTIVITY.DATA_ENTITY_ID))
.leftJoin(USER_OWNER_MAPPING).on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(ACTIVITY.CREATED_BY));
.leftJoin(USER_OWNER_MAPPING)
.on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(ACTIVITY.CREATED_BY)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()));
addJoins(countQuery, datasourceId, namespaceId, tagIds, ownerIds);
final List<Condition> conditions =
getCommonConditions(beginDate, endDate, datasourceId, namespaceId, tagIds, ownerIds, userIds, eventType);
Expand All @@ -172,7 +174,9 @@ public Mono<Long> getMyObjectsActivitiesCount(final OffsetDateTime beginDate,
final var countQuery = DSL.selectCount()
.from(ACTIVITY)
.join(DATA_ENTITY).on(DATA_ENTITY.ID.eq(ACTIVITY.DATA_ENTITY_ID))
.leftJoin(USER_OWNER_MAPPING).on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(ACTIVITY.CREATED_BY));
.leftJoin(USER_OWNER_MAPPING)
.on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(ACTIVITY.CREATED_BY)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()));
addJoins(countQuery, datasourceId, namespaceId, tagIds, List.of(currentOwnerId));
final List<Condition> conditions = getCommonConditions(beginDate, endDate, datasourceId, namespaceId, tagIds,
List.of(currentOwnerId), userIds, eventType);
Expand All @@ -191,7 +195,9 @@ public Mono<Long> getDependentActivitiesCount(final OffsetDateTime beginDate,
final var countQuery = DSL.selectCount()
.from(ACTIVITY)
.join(DATA_ENTITY).on(DATA_ENTITY.ID.eq(ACTIVITY.DATA_ENTITY_ID))
.leftJoin(USER_OWNER_MAPPING).on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(ACTIVITY.CREATED_BY));
.leftJoin(USER_OWNER_MAPPING)
.on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(ACTIVITY.CREATED_BY)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()));
addJoins(countQuery, datasourceId, namespaceId, tagIds, List.of());
final List<Condition> conditions = getCommonConditions(beginDate, endDate, datasourceId, namespaceId, tagIds,
List.of(), userIds, eventType);
Expand All @@ -211,7 +217,9 @@ private SelectJoinStep<?> buildBaseQuery(final Long datasourceId, final Long nam
final var query = DSL.select(selectFields)
.from(ACTIVITY)
.join(DATA_ENTITY).on(DATA_ENTITY.ID.eq(ACTIVITY.DATA_ENTITY_ID))
.leftJoin(USER_OWNER_MAPPING).on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(ACTIVITY.CREATED_BY))
.leftJoin(USER_OWNER_MAPPING)
.on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(ACTIVITY.CREATED_BY)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()))
.leftJoin(OWNER).on(OWNER.ID.eq(USER_OWNER_MAPPING.OWNER_ID));
return addJoins(query, datasourceId, namespaceId, tagIds, ownerIds);
}
Expand Down Expand Up @@ -263,6 +271,7 @@ private List<Condition> getCommonConditions(final OffsetDateTime beginDate,
}
if (CollectionUtils.isNotEmpty(userIds)) {
conditions.add(USER_OWNER_MAPPING.OWNER_ID.in(userIds));
conditions.add(USER_OWNER_MAPPING.DELETED_AT.isNull());
}
return conditions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public Mono<AlertDto> get(final long id) {
.select(jsonArrayAgg(field(ALERT_CHUNK.asterisk().toString())).as(ALERT_CHUNK_FIELD))
.from(ALERT)
.join(DATA_ENTITY).on(DATA_ENTITY.ODDRN.eq(ALERT.DATA_ENTITY_ODDRN))
.leftJoin(USER_OWNER_MAPPING).on(ALERT.STATUS_UPDATED_BY.eq(USER_OWNER_MAPPING.OIDC_USERNAME))
.leftJoin(USER_OWNER_MAPPING)
.on(ALERT.STATUS_UPDATED_BY.eq(USER_OWNER_MAPPING.OIDC_USERNAME)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()))
.leftJoin(OWNER).on(USER_OWNER_MAPPING.OWNER_ID.eq(OWNER.ID))
.join(ALERT_CHUNK).on(ALERT_CHUNK.ALERT_ID.eq(ALERT.ID))
.where(ALERT.ID.eq(id))
Expand All @@ -100,7 +102,9 @@ public Mono<List<AlertDto>> get(final List<Long> ids) {
.select(jsonArrayAgg(field(ALERT_CHUNK.asterisk().toString())).as(ALERT_CHUNK_FIELD))
.from(ALERT)
.join(DATA_ENTITY).on(DATA_ENTITY.ODDRN.eq(ALERT.DATA_ENTITY_ODDRN))
.leftJoin(USER_OWNER_MAPPING).on(ALERT.STATUS_UPDATED_BY.eq(USER_OWNER_MAPPING.OIDC_USERNAME))
.leftJoin(USER_OWNER_MAPPING)
.on(ALERT.STATUS_UPDATED_BY.eq(USER_OWNER_MAPPING.OIDC_USERNAME)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()))
.leftJoin(OWNER).on(USER_OWNER_MAPPING.OWNER_ID.eq(OWNER.ID))
.join(ALERT_CHUNK).on(ALERT_CHUNK.ALERT_ID.eq(ALERT.ID))
.where(ALERT.ID.in(ids))
Expand Down Expand Up @@ -493,7 +497,8 @@ private SelectSeekStepN<Record> createAlertOuterSelect(final Select<? extends Re
.join(DATA_ENTITY)
.on(DATA_ENTITY.ODDRN.eq(alertCte.field(ALERT.DATA_ENTITY_ODDRN)))
.leftJoin(USER_OWNER_MAPPING)
.on(alertCte.field(ALERT.STATUS_UPDATED_BY).eq(USER_OWNER_MAPPING.OIDC_USERNAME))
.on(alertCte.field(ALERT.STATUS_UPDATED_BY).eq(USER_OWNER_MAPPING.OIDC_USERNAME)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()))
.leftJoin(OWNER).on(USER_OWNER_MAPPING.OWNER_ID.eq(OWNER.ID))
.join(ALERT_CHUNK).on(ALERT_CHUNK.ALERT_ID.eq(alertCte.field(ALERT.ID)))
.groupBy(groupByFields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
Expand Down Expand Up @@ -59,7 +58,8 @@ public Mono<OwnerAssociationRequestDto> getDto(final long id) {
.from(OWNER_ASSOCIATION_REQUEST)
.join(requestOwner).on(OWNER_ASSOCIATION_REQUEST.OWNER_ID.eq(requestOwner.ID))
.leftJoin(USER_OWNER_MAPPING)
.on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(OWNER_ASSOCIATION_REQUEST.STATUS_UPDATED_BY))
.on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(OWNER_ASSOCIATION_REQUEST.STATUS_UPDATED_BY)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()))
.leftJoin(statusUpdatedOwner)
.on(USER_OWNER_MAPPING.OWNER_ID.eq(statusUpdatedOwner.ID))
.where(OWNER_ASSOCIATION_REQUEST.ID.eq(id));
Expand Down Expand Up @@ -95,7 +95,8 @@ public Mono<Page<OwnerAssociationRequestDto>> getDtoList(final int page,
.from(cte.getName())
.join(requestOwner).on(cte.field(OWNER_ASSOCIATION_REQUEST.OWNER_ID).eq(requestOwner.ID))
.leftJoin(USER_OWNER_MAPPING)
.on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(cte.field(OWNER_ASSOCIATION_REQUEST.STATUS_UPDATED_BY)))
.on(USER_OWNER_MAPPING.OIDC_USERNAME.eq(cte.field(OWNER_ASSOCIATION_REQUEST.STATUS_UPDATED_BY))
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()))
.leftJoin(statusUpdatedOwner)
.on(USER_OWNER_MAPPING.OWNER_ID.eq(statusUpdatedOwner.ID));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.opendatadiscovery.oddplatform.repository.reactive;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import org.jooq.Condition;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Select;
import org.jooq.SelectConditionStep;
Expand All @@ -15,6 +18,7 @@
import org.opendatadiscovery.oddplatform.dto.OwnerDto;
import org.opendatadiscovery.oddplatform.model.tables.pojos.OwnerPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RolePojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.UserOwnerMappingPojo;
import org.opendatadiscovery.oddplatform.model.tables.records.OwnerRecord;
import org.opendatadiscovery.oddplatform.repository.util.JooqQueryHelper;
import org.opendatadiscovery.oddplatform.repository.util.JooqReactiveOperations;
Expand All @@ -33,6 +37,7 @@
import static org.opendatadiscovery.oddplatform.model.Tables.OWNER_ASSOCIATION_REQUEST;
import static org.opendatadiscovery.oddplatform.model.Tables.OWNER_TO_ROLE;
import static org.opendatadiscovery.oddplatform.model.Tables.ROLE;
import static org.opendatadiscovery.oddplatform.model.Tables.USER_OWNER_MAPPING;

@Repository
public class ReactiveOwnerRepositoryImpl extends ReactiveAbstractSoftDeleteCRUDRepository<OwnerRecord, OwnerPojo>
Expand All @@ -59,13 +64,20 @@ public Flux<OwnerPojo> get(final Collection<Long> ownerIds) {

@Override
public Mono<OwnerDto> getDto(final Long id) {
final List<Field<?>> groupByFields = Stream.of(OWNER.fields(), USER_OWNER_MAPPING.fields())
.flatMap(Arrays::stream)
.toList();

final var dtoQuery = DSL.select(OWNER.fields())
.select(jsonArrayAgg(field(ROLE.asterisk().toString())).as(AGG_ROLE_FIELD))
.select(USER_OWNER_MAPPING.asterisk())
.from(OWNER)
.leftJoin(OWNER_TO_ROLE).on(OWNER.ID.eq(OWNER_TO_ROLE.OWNER_ID))
.leftJoin(ROLE).on(OWNER_TO_ROLE.ROLE_ID.eq(ROLE.ID))
.leftJoin(USER_OWNER_MAPPING).on(USER_OWNER_MAPPING.OWNER_ID.eq(OWNER.ID)
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()))
.where(OWNER.ID.eq(id))
.groupBy(OWNER.fields());
.groupBy(groupByFields);
return jooqReactiveOperations.mono(dtoQuery)
.map(this::mapRecordToDto);
}
Expand All @@ -83,14 +95,21 @@ public Mono<Page<OwnerDto>> list(final int page,
List.of(new OrderByField(idField, SortOrder.ASC)), (page - 1) * size, size);
final Table<? extends Record> ownerCTE = ownerSelect.asTable("owner_cte");

final List<Field<?>> groupByFields = Stream.of(ownerCTE.fields(), USER_OWNER_MAPPING.fields())
.flatMap(Arrays::stream)
.toList();

final var query = DSL.with(ownerCTE.getName())
.as(ownerSelect)
.select(ownerCTE.fields())
.select(jsonArrayAgg(field(ROLE.asterisk().toString())).as(AGG_ROLE_FIELD))
.select(USER_OWNER_MAPPING.asterisk())
.from(ownerCTE)
.leftJoin(OWNER_TO_ROLE).on(OWNER_TO_ROLE.OWNER_ID.eq(ownerCTE.field(idField)))
.leftJoin(ROLE).on(ROLE.ID.eq(OWNER_TO_ROLE.ROLE_ID))
.groupBy(ownerCTE.fields());
.leftJoin(USER_OWNER_MAPPING).on(USER_OWNER_MAPPING.OWNER_ID.eq(ownerCTE.field(idField))
.and(USER_OWNER_MAPPING.DELETED_AT.isNull()))
.groupBy(groupByFields);

return jooqReactiveOperations.flux(query)
.collectList()
Expand Down Expand Up @@ -145,12 +164,14 @@ private <T extends Record> Select<T> enrichSelect(final SelectJoinStep<T> select
private OwnerDto mapRecordToDto(final Record r) {
final OwnerPojo pojo = r.into(OWNER).into(OwnerPojo.class);
final Set<RolePojo> roles = jooqRecordHelper.extractAggRelation(r, AGG_ROLE_FIELD, RolePojo.class);
return new OwnerDto(pojo, roles);
final UserOwnerMappingPojo associatedUsers = r.into(USER_OWNER_MAPPING).into(UserOwnerMappingPojo.class);
return new OwnerDto(pojo, roles, associatedUsers);
}

private OwnerDto mapRecordToDto(final Record r, final String cteName) {
final OwnerPojo ownerPojo = jooqRecordHelper.remapCte(r, cteName, OWNER).into(OwnerPojo.class);
final Set<RolePojo> roles = jooqRecordHelper.extractAggRelation(r, AGG_ROLE_FIELD, RolePojo.class);
return new OwnerDto(ownerPojo, roles);
final UserOwnerMappingPojo associatedUsers = r.into(USER_OWNER_MAPPING).into(UserOwnerMappingPojo.class);
return new OwnerDto(ownerPojo, roles, associatedUsers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Mono<UserOwnerMappingPojo> createRelation(final String oidcUsername,
Mono<UserOwnerMappingPojo> deleteRelation(final String oidcUsername,
final String provider);

Mono<UserOwnerMappingPojo> deleteActiveRelationByOwner(final Long ownerId);

Mono<OwnerPojo> getAssociatedOwner(final String oidcUsername,
final String provider);

Expand Down
Loading

0 comments on commit ffa9deb

Please sign in to comment.