1 package de.dlr.shepard.neo4Core.services;
2
3 import de.dlr.shepard.neo4Core.dao.PermissionsDAO;
4 import de.dlr.shepard.neo4Core.dao.UserDAO;
5 import de.dlr.shepard.neo4Core.dao.UserGroupDAO;
6 import de.dlr.shepard.neo4Core.entities.Permissions;
7 import de.dlr.shepard.neo4Core.entities.User;
8 import de.dlr.shepard.neo4Core.entities.UserGroup;
9 import de.dlr.shepard.neo4Core.io.PermissionsIO;
10 import io.quarkus.logging.Log;
11 import jakarta.enterprise.context.RequestScoped;
12 import jakarta.inject.Inject;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 @RequestScoped
17 public class PermissionsService {
18
19 private PermissionsDAO permissionsDAO;
20 private UserDAO userDAO;
21 private UserGroupDAO userGroupDAO;
22
23 PermissionsService() {}
24
25 @Inject
26 public PermissionsService(PermissionsDAO permissionsDAO, UserDAO userDAO, UserGroupDAO userGroupDAO) {
27 this.permissionsDAO = permissionsDAO;
28 this.userDAO = userDAO;
29 this.userGroupDAO = userGroupDAO;
30 }
31
32
33
34
35
36
37
38 public Permissions getPermissionsByNeo4jId(long id) {
39 var permissions = permissionsDAO.findByEntityNeo4jId(id);
40 if (permissions == null) {
41 Log.errorf("Permissions with entity id %s is null", id);
42 return null;
43 }
44 return permissions;
45 }
46
47
48
49
50
51
52
53 public Permissions getPermissionsByShepardId(long shepardId) {
54 var permissions = permissionsDAO.findByEntityShepardId(shepardId);
55 if (permissions == null) {
56 Log.errorf("Permissions with shepardId %s is null", shepardId);
57 return null;
58 }
59 return permissions;
60 }
61
62
63
64
65
66
67
68 public Permissions getPermissionsByCollectionShepardId(long shepardId) {
69 var permissions = permissionsDAO.findByCollectionShepardId(shepardId);
70 if (permissions == null) {
71 Log.errorf("Permissions with shepardId %s is null", shepardId);
72 return null;
73 }
74 return permissions;
75 }
76
77
78
79
80
81
82
83 public Permissions createPermissionsByNeo4jId(long entityId) {
84 var permissions = new Permissions();
85 return permissionsDAO.createWithEntityNeo4jId(permissions, entityId);
86 }
87
88
89
90
91
92
93
94
95 public Permissions updatePermissionsByNeo4jId(PermissionsIO permissionsIo, long id) {
96 var permissions = convertPermissionsIO(permissionsIo);
97 var old = getPermissionsByNeo4jId(id);
98 if (old == null) {
99
100 return permissionsDAO.createWithEntityNeo4jId(permissions, id);
101 }
102 old.setOwner(permissions.getOwner());
103 old.setReader(permissions.getReader());
104 old.setWriter(permissions.getWriter());
105 old.setReaderGroups(permissions.getReaderGroups());
106 old.setWriterGroups(permissions.getWriterGroups());
107 old.setManager(permissions.getManager());
108 old.setPermissionType(permissions.getPermissionType());
109 return permissionsDAO.createOrUpdate(old);
110 }
111
112
113
114
115
116
117
118
119 public Permissions updatePermissionsByShepardId(PermissionsIO permissionsIo, long shepardId) {
120 var permissions = convertPermissionsIO(permissionsIo);
121 var old = getPermissionsByShepardId(shepardId);
122 if (old == null) {
123
124 return permissionsDAO.createWithEntityShepardId(permissions, shepardId);
125 }
126 old.setOwner(permissions.getOwner());
127 old.setReader(permissions.getReader());
128 old.setWriter(permissions.getWriter());
129 old.setReaderGroups(permissions.getReaderGroups());
130 old.setWriterGroups(permissions.getWriterGroups());
131 old.setManager(permissions.getManager());
132 old.setPermissionType(permissions.getPermissionType());
133 var ret = permissionsDAO.createOrUpdate(old);
134 return ret;
135 }
136
137 private Permissions convertPermissionsIO(PermissionsIO permissions) {
138 var owner = permissions.getOwner() != null ? userDAO.find(permissions.getOwner()) : null;
139 var permissionType = permissions.getPermissionType();
140 var reader = fetchUsers(permissions.getReader());
141 var writer = fetchUsers(permissions.getWriter());
142 var readerGroups = fetchUserGroups(permissions.getReaderGroupIds());
143 var writerGroups = fetchUserGroups(permissions.getWriterGroupIds());
144 var manager = fetchUsers(permissions.getManager());
145 return new Permissions(owner, reader, writer, readerGroups, writerGroups, manager, permissionType);
146 }
147
148 private List<User> fetchUsers(String[] usernames) {
149 var result = new ArrayList<User>(usernames.length);
150 for (var username : usernames) {
151 if (username == null) {
152 continue;
153 }
154
155 var user = userDAO.find(username);
156 if (user != null) {
157 result.add(user);
158 }
159 }
160 return result;
161 }
162
163 private List<UserGroup> fetchUserGroups(long[] userGroupIds) {
164 var result = new ArrayList<UserGroup>(userGroupIds.length);
165 for (var userGroupId : userGroupIds) {
166 var userGroup = userGroupDAO.findByNeo4jId(userGroupId);
167 if (userGroup != null) {
168 result.add(userGroup);
169 }
170 }
171 return result;
172 }
173 }