Skip to content

Commit 6616635

Browse files
committed
support multiple flow users
1 parent 6cfb83b commit 6616635

File tree

18 files changed

+264
-30
lines changed

18 files changed

+264
-30
lines changed

src/main/java/com/flowci/common/RequestContextHolder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.flowci.common;
22

33
import com.flowci.common.model.RequestContext;
4+
import com.flowci.user.model.User;
45
import org.springframework.stereotype.Component;
56

67
@Component
@@ -12,8 +13,8 @@ public RequestContext getContext() {
1213
return CONTEXT_HOLDER.get();
1314
}
1415

15-
public String getUser() {
16+
public Long getUserId() {
1617
// TODO: user auth
17-
return "flowci";
18+
return User.SYSTEM_USER;
1819
}
1920
}

src/main/java/com/flowci/common/model/EntityBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public abstract class EntityBase implements Serializable {
2121

2222
protected Instant createdAt;
2323

24-
protected String createdBy;
24+
protected Long createdBy;
2525

2626
protected Instant updatedAt;
2727

28-
protected String updatedBy;
28+
protected Long updatedBy;
2929

3030
@Converter
3131
public abstract static class ObjectAttributeConverter<T> implements AttributeConverter<T, String> {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.flowci.flow.business;
2+
3+
import com.flowci.flow.model.Flow;
4+
import jakarta.annotation.Nullable;
5+
import org.springframework.data.domain.PageRequest;
6+
7+
import java.util.List;
8+
9+
public interface ListFlows {
10+
List<Flow> invoke(@Nullable Long rootId, PageRequest pageRequest);
11+
}

src/main/java/com/flowci/flow/business/impl/CreateFlowImpl.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import com.flowci.flow.business.FetchTemplateContent;
77
import com.flowci.flow.model.CreateFlowParam;
88
import com.flowci.flow.model.Flow;
9+
import com.flowci.flow.model.FlowUser;
910
import com.flowci.flow.model.FlowYaml;
1011
import com.flowci.flow.repo.FlowRepo;
12+
import com.flowci.flow.repo.FlowUserRepo;
1113
import com.flowci.flow.repo.FlowYamlRepo;
1214
import lombok.AllArgsConstructor;
1315
import lombok.extern.slf4j.Slf4j;
@@ -23,6 +25,8 @@ public class CreateFlowImpl implements CreateFlow {
2325

2426
private final FlowYamlRepo flowYamlRepo;
2527

28+
private final FlowUserRepo flowUserRepo;
29+
2630
private final FetchTemplateContent fetchTemplateContent;
2731

2832
private final RequestContextHolder requestContextHolder;
@@ -32,6 +36,7 @@ public class CreateFlowImpl implements CreateFlow {
3236
public Flow invoke(CreateFlowParam param) {
3337
var flow = flowRepo.save(toObject(param));
3438
flowYamlRepo.save(toObject(flow, param));
39+
flowUserRepo.save(toObject(flow));
3540
log.info("Created flow: {} by user {}", flow.getName(), flow.getCreatedBy());
3641
return flow;
3742
}
@@ -41,19 +46,16 @@ private Flow toObject(CreateFlowParam param) {
4146
flow.setName(param.name());
4247
flow.setType(Flow.Type.FLOW);
4348
flow.setVariables(Variables.EMPTY);
44-
flow.setCreatedBy(requestContextHolder.getUser());
45-
flow.setUpdatedBy(requestContextHolder.getUser());
46-
47-
if (param.rootId() != null) {
48-
flow.setParentId(param.rootId());
49-
}
50-
49+
flow.setParentId(param.rootId() == null ? Flow.ROOT_ID : param.rootId());
50+
flow.setCreatedBy(requestContextHolder.getUserId());
51+
flow.setUpdatedBy(requestContextHolder.getUserId());
5152
return flow;
5253
}
5354

5455
private FlowYaml toObject(Flow flow, CreateFlowParam param) {
5556
var flowYaml = new FlowYaml();
5657
flowYaml.setId(flow.getId());
58+
flowYaml.setYaml("");
5759
flowYaml.setCreatedBy(flow.getCreatedBy());
5860
flowYaml.setUpdatedBy(flow.getUpdatedBy());
5961

@@ -63,4 +65,13 @@ private FlowYaml toObject(Flow flow, CreateFlowParam param) {
6365

6466
return flowYaml;
6567
}
68+
69+
private FlowUser toObject(Flow flow) {
70+
var fu = new FlowUser();
71+
fu.setFlowId(flow.getId());
72+
fu.setUserId(flow.getCreatedBy());
73+
fu.setCreatedBy(flow.getCreatedBy());
74+
fu.setUpdatedBy(flow.getUpdatedBy());
75+
return fu;
76+
}
6677
}

src/main/java/com/flowci/flow/business/impl/InitRootGroupImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.flowci.flow.business.InitRootGroup;
55
import com.flowci.flow.model.Flow;
66
import com.flowci.flow.repo.FlowRepo;
7+
import com.flowci.user.model.User;
78
import lombok.AllArgsConstructor;
89
import lombok.extern.slf4j.Slf4j;
910
import org.springframework.stereotype.Component;
@@ -28,8 +29,8 @@ public void invoke() {
2829
root.setName(Flow.ROOT_NAME);
2930
root.setParentId(Flow.ROOT_ID);
3031
root.setVariables(Variables.EMPTY);
31-
root.setCreatedBy("system");
32-
root.setUpdatedBy("system");
32+
root.setCreatedBy(User.SYSTEM_USER);
33+
root.setUpdatedBy(User.SYSTEM_USER);
3334
flowRepo.save(root);
3435
log.info("Root flow group '{}' is created", root.getName());
3536
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.flowci.flow.business.impl;
2+
3+
import com.flowci.common.RequestContextHolder;
4+
import com.flowci.flow.business.ListFlows;
5+
import com.flowci.flow.model.Flow;
6+
import com.flowci.flow.repo.FlowRepo;
7+
import jakarta.annotation.Nullable;
8+
import lombok.AllArgsConstructor;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.data.domain.PageRequest;
11+
import org.springframework.stereotype.Component;
12+
13+
import java.util.List;
14+
15+
@Slf4j
16+
@Component
17+
@AllArgsConstructor
18+
public class ListFlowsImpl implements ListFlows {
19+
20+
private final FlowRepo flowRepo;
21+
22+
private final RequestContextHolder requestContextHolder;
23+
24+
@Override
25+
public List<Flow> invoke(@Nullable Long rootId, PageRequest pageRequest) {
26+
if (rootId == null) {
27+
rootId = Flow.ROOT_ID;
28+
}
29+
30+
return flowRepo.findAllByParentIdAndUserIdOrderByCreatedAt(
31+
rootId,
32+
requestContextHolder.getUserId(),
33+
pageRequest
34+
);
35+
}
36+
}

src/main/java/com/flowci/flow/model/Flow.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import lombok.EqualsAndHashCode;
88

99
@Data
10-
@Entity(name = "flows")
10+
@Entity
11+
@Table(name = "flows")
1112
@EqualsAndHashCode(callSuper = false, of = "id")
1213
public final class Flow extends EntityBase {
1314

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.flowci.flow.model;
2+
3+
import com.flowci.common.model.EntityBase;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.IdClass;
6+
import jakarta.persistence.Table;
7+
import lombok.Data;
8+
import lombok.EqualsAndHashCode;
9+
10+
@Data
11+
@Entity
12+
@Table(name = "flows_user")
13+
@IdClass(FlowUser.Id.class)
14+
@EqualsAndHashCode(of = {"flowId", "userId"}, callSuper = false)
15+
public class FlowUser extends EntityBase {
16+
17+
public record Id(Long flowId, Long userId) {
18+
}
19+
20+
@jakarta.persistence.Id
21+
private Long flowId;
22+
23+
@jakarta.persistence.Id
24+
private Long userId;
25+
}

src/main/java/com/flowci/flow/model/FlowYaml.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
import com.flowci.common.model.EntityBase;
44
import jakarta.persistence.Entity;
55
import jakarta.persistence.Id;
6+
import jakarta.persistence.Table;
67
import lombok.Data;
78
import lombok.EqualsAndHashCode;
89

910
@Data
10-
@Entity(name = "flows_yaml")
11+
@Entity
12+
@Table(name = "flows_yaml")
1113
@EqualsAndHashCode(callSuper = false, of = "id")
1214
public class FlowYaml extends EntityBase {
1315

1416
@Id
1517
private Long id;
1618

1719
// raw yaml configuration
18-
private String yaml = "";
20+
private String yaml;
1921
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package com.flowci.flow.repo;
22

33
import com.flowci.flow.model.Flow;
4+
import org.springframework.data.domain.Pageable;
45
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
57
import org.springframework.stereotype.Repository;
68

9+
import java.util.List;
10+
711
@Repository
812
public interface FlowRepo extends JpaRepository<Flow, Long> {
13+
14+
@Query("select f from Flow f " +
15+
"where f.id in (select fu.flowId from FlowUser fu where fu.userId = ?2) " +
16+
"and f.parentId = ?1 " +
17+
"order by f.createdAt")
18+
List<Flow> findAllByParentIdAndUserIdOrderByCreatedAt(
19+
Long parentId,
20+
Long userId,
21+
Pageable pageable
22+
);
923
}

0 commit comments

Comments
 (0)