-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathDocument.java
More file actions
185 lines (158 loc) · 5.66 KB
/
Document.java
File metadata and controls
185 lines (158 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package io.openaev.database.model;
import static io.openaev.database.model.Tenant.DEFAULT_TENANT_UUID;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.openaev.annotation.Queryable;
import io.openaev.database.audit.ModelBaseListener;
import io.openaev.helper.MultiIdSetSerializer;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.UuidGenerator;
@Setter
@Getter
@Entity
@Table(name = "documents")
@EntityListeners(ModelBaseListener.class)
@NamedEntityGraphs({
@NamedEntityGraph(
name = "Document.tags-scenarios-exercises",
attributeNodes = {
@NamedAttributeNode("tags"),
@NamedAttributeNode("scenarios"),
@NamedAttributeNode("exercises")
})
})
public class Document implements Base {
@Id
@Column(name = "document_id")
@GeneratedValue(generator = "UUID")
@UuidGenerator
@JsonProperty("document_id")
@NotBlank
private String id;
@Column(name = "document_name")
@JsonProperty("document_name")
@Queryable(searchable = true, sortable = true)
@NotBlank
private String name;
@Column(name = "document_target")
@JsonProperty("document_target")
private String target;
@Column(name = "document_description")
@JsonProperty("document_description")
@Queryable(searchable = true, sortable = true)
private String description;
@Column(name = "document_type")
@JsonProperty("document_type")
@Queryable(searchable = true, sortable = true)
@NotBlank
private String type;
@Schema(implementation = String[].class)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "documents_tags",
joinColumns = @JoinColumn(name = "document_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id"))
@JsonSerialize(using = MultiIdSetSerializer.class)
@JsonProperty("document_tags")
@Queryable(sortable = true)
private Set<Tag> tags = new HashSet<>();
@Schema(implementation = String[].class)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "exercises_documents",
joinColumns = @JoinColumn(name = "document_id"),
inverseJoinColumns = @JoinColumn(name = "exercise_id"))
@JsonSerialize(using = MultiIdSetSerializer.class)
@JsonProperty("document_exercises")
private Set<Exercise> exercises = new HashSet<>();
@Schema(implementation = String[].class)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "scenarios_documents",
joinColumns = @JoinColumn(name = "document_id"),
inverseJoinColumns = @JoinColumn(name = "scenario_id"))
@JsonSerialize(using = MultiIdSetSerializer.class)
@JsonProperty("document_scenarios")
private Set<Scenario> scenarios = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "articles_documents",
joinColumns = @JoinColumn(name = "document_id"),
inverseJoinColumns = @JoinColumn(name = "article_id"))
@JsonIgnore
private Set<Article> articles = new HashSet<>();
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "challenges_documents",
joinColumns = @JoinColumn(name = "document_id"),
inverseJoinColumns = @JoinColumn(name = "challenge_id"))
@JsonIgnore
private Set<Challenge> challenges = new HashSet<>();
@ManyToOne
@JoinColumn(name = "tenant_id")
@JsonIgnore
@NotNull
private Tenant tenant = new Tenant(DEFAULT_TENANT_UUID);
@OneToMany(mappedBy = "document", fetch = FetchType.LAZY)
@JsonIgnore
private Set<InjectDocument> injectDocuments = new HashSet<>();
@OneToMany(mappedBy = "fileDropFile", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
private Set<FileDrop> payloadsByFileDrop = new HashSet<>();
@OneToMany(mappedBy = "executableFile", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
private Set<Executable> payloadsByExecutableFile = new HashSet<>();
@OneToMany(mappedBy = "logoDark", fetch = FetchType.LAZY)
@JsonIgnore
private Set<Channel> channelsByLogoDark = new HashSet<>();
@OneToMany(mappedBy = "logoLight", fetch = FetchType.LAZY)
@JsonIgnore
private Set<Channel> channelsByLogoLight = new HashSet<>();
@OneToMany(mappedBy = "logoDark", fetch = FetchType.LAZY)
@JsonIgnore
private Set<SecurityPlatform> securityPlatformsByLogoDark = new HashSet<>();
@OneToMany(mappedBy = "logoLight", fetch = FetchType.LAZY)
@JsonIgnore
private Set<SecurityPlatform> securityPlatformsByLogoLight = new HashSet<>();
@OneToMany(mappedBy = "logoDark", fetch = FetchType.LAZY)
@JsonIgnore
private Set<Exercise> simulationsByLogoDark = new HashSet<>();
@OneToMany(mappedBy = "logoLight", fetch = FetchType.LAZY)
@JsonIgnore
private Set<Exercise> simulationsByLogoLight = new HashSet<>();
@Getter(onMethod_ = @JsonIgnore)
@Transient
private final ResourceType resourceType = ResourceType.DOCUMENT;
@Override
public boolean isUserHasAccess(User user) {
if (user.isAdmin()) {
return true;
}
return exercises.stream().anyMatch(exercise -> exercise.isUserHasAccess(user))
|| scenarios.stream().anyMatch(scenario -> scenario.isUserHasAccess(user));
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || !Base.class.isAssignableFrom(o.getClass())) {
return false;
}
Base base = (Base) o;
return id.equals(base.getId());
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}