1 package de.dlr.shepard.neo4Core.io;
2
3 import org.apache.commons.lang3.ArrayUtils;
4
5 import de.dlr.shepard.neo4Core.entities.Permissions;
6 import de.dlr.shepard.neo4Core.entities.User;
7 import de.dlr.shepard.neo4Core.entities.UserGroup;
8 import de.dlr.shepard.util.PermissionType;
9 import io.swagger.v3.oas.annotations.media.Schema;
10 import io.swagger.v3.oas.annotations.media.Schema.AccessMode;
11 import jakarta.validation.constraints.NotNull;
12 import lombok.Data;
13 import lombok.NoArgsConstructor;
14
15 @Data
16 @NoArgsConstructor
17 @Schema(name = "Permissions")
18 public class PermissionsIO {
19
20 @Schema(accessMode = AccessMode.READ_ONLY)
21 private long entityId;
22
23 private String owner;
24
25 private PermissionType permissionType;
26
27 @NotNull
28 private String[] reader;
29
30 @NotNull
31 private String[] writer;
32
33 private long[] readerGroupIds = {};
34
35 private long[] writerGroupIds = {};
36
37 @NotNull
38 private String[] manager;
39
40 public PermissionsIO(Permissions permissions) {
41 this.entityId = permissions.getEntity().getId();
42 this.permissionType = permissions.getPermissionType();
43 this.owner = permissions.getOwner() != null ? permissions.getOwner().getUsername() : null;
44 this.reader = permissions.getReader().stream().map(User::getUsername).toArray(String[]::new);
45 this.writer = permissions.getWriter().stream().map(User::getUsername).toArray(String[]::new);
46 this.readerGroupIds = ArrayUtils
47 .toPrimitive(permissions.getReaderGroups().stream().map(UserGroup::getId).toArray(Long[]::new));
48 this.writerGroupIds = ArrayUtils
49 .toPrimitive(permissions.getWriterGroups().stream().map(UserGroup::getId).toArray(Long[]::new));
50 this.manager = permissions.getManager().stream().map(User::getUsername).toArray(String[]::new);
51 }
52
53 }