View Javadoc
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     * Searches for permissions in Neo4j.
34     *
35     * @param id identifies the entity that the permissions object belongs to
36     * @return Permissions with matching entity or null
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     * Searches for permissions in Neo4j.
49     *
50     * @param shepardId identifies the entity that the permissions object belongs to
51     * @return Permissions with matching entity or null
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     * Searches for permissions in Neo4j.
64     *
65     * @param shepardId identifies the entity that the permissions object belongs to
66     * @return Permissions with matching entity or null
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     * Create Permissions based on an entity and the owner
79     *
80     * @param entityId identifies the entity
81     * @return The created Permissions object
82     */
83    public Permissions createPermissionsByNeo4jId(long entityId) {
84      var permissions = new Permissions();
85      return permissionsDAO.createWithEntityNeo4jId(permissions, entityId);
86    }
87  
88    /**
89     * Updates the Permissions in Neo4j
90     *
91     * @param permissionsIo the new Permissions object
92     * @param id            identifies the entity
93     * @return the updated Permissions object
94     */
95    public Permissions updatePermissionsByNeo4jId(PermissionsIO permissionsIo, long id) {
96      var permissions = convertPermissionsIO(permissionsIo);
97      var old = getPermissionsByNeo4jId(id);
98      if (old == null) {
99        // There is no old permissions object
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    * Updates the Permissions in Neo4j
114    *
115    * @param permissionsIo the new Permissions object
116    * @param shepardId     identifies the entity
117    * @return the updated Permissions object
118    */
119   public Permissions updatePermissionsByShepardId(PermissionsIO permissionsIo, long shepardId) {
120     var permissions = convertPermissionsIO(permissionsIo);
121     var old = getPermissionsByShepardId(shepardId);
122     if (old == null) {
123       // There is no old permissions object
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 }