bernardodemarco commented on code in PR #11016:
URL: https://github.com/apache/cloudstack/pull/11016#discussion_r2180069759


##########
server/src/main/java/org/apache/cloudstack/consoleproxy/ConsoleAccessManagerImpl.java:
##########
@@ -181,6 +194,78 @@ private void reallyRun() {
         }
     }
 
+    @Override
+    public ListResponse<ConsoleSessionResponse> 
listConsoleSessions(ListConsoleSessionsCmd cmd) {
+        Pair<List<ConsoleSessionVO>, Integer> consoleSessions = 
listConsoleSessionsInternal(cmd);
+        ListResponse<ConsoleSessionResponse> response = new ListResponse<>();
+
+        ResponseObject.ResponseView responseView = 
ResponseObject.ResponseView.Restricted;
+        Long callerId = CallContext.current().getCallingAccountId();
+        if (accountManager.isRootAdmin(callerId)) {
+            responseView = ResponseObject.ResponseView.Full;
+        }
+
+        List<ConsoleSessionResponse> consoleSessionResponses = new 
ArrayList<>();
+        for (ConsoleSessionVO consoleSession : consoleSessions.first()) {
+            ConsoleSessionResponse consoleSessionResponse = 
responseGenerator.createConsoleSessionResponse(consoleSession, responseView);
+            consoleSessionResponses.add(consoleSessionResponse);
+        }
+
+        response.setResponses(consoleSessionResponses, 
consoleSessions.second());
+        return response;
+    }
+
+    protected Pair<List<ConsoleSessionVO>, Integer> 
listConsoleSessionsInternal(ListConsoleSessionsCmd cmd) {
+        CallContext caller = CallContext.current();
+        long domainId = 
getBaseDomainIdToListConsoleSessions(cmd.getDomainId());
+        Long accountId = cmd.getAccountId();
+        Long userId = cmd.getUserId();
+        boolean isRecursive = cmd.isRecursive();
+
+        boolean isCallerNormalUser = 
accountManager.isNormalUser(caller.getCallingAccountId());
+        if (isCallerNormalUser) {
+            accountId = caller.getCallingAccountId();
+            userId = caller.getCallingUserId();
+        }
+
+        List<Long> domainIds = isRecursive ? 
domainDao.getDomainAndChildrenIds(domainId) : List.of(domainId);
+
+        return consoleSessionDao.listConsoleSessions(cmd.getId(), domainIds, 
accountId, userId,
+                cmd.getHostId(), cmd.getStartDate(), cmd.getEndDate(), 
cmd.getInstanceId(),
+                cmd.getConsoleEndpointCreatorAddress(), 
cmd.getClientAddress(), cmd.isActiveOnly(),
+                cmd.getPageSizeVal(), cmd.getStartIndex());
+    }
+
+    /**
+     * Determines the base domain ID for listing console sessions.
+     *
+     * If no domain ID is provided, returns the caller's domain ID. Otherwise,
+     * checks if the caller has access to that domain and returns the provided 
domain ID.
+     *
+     * @param domainId The domain ID to check, can be null
+     * @return The base domain ID to use for listing console sessions
+     * @throws PermissionDeniedException if the caller does not have access to 
the specified domain
+     */
+    protected long getBaseDomainIdToListConsoleSessions(Long domainId) {
+        Account caller = CallContext.current().getCallingAccount();
+        if (domainId == null) {
+            return caller.getDomainId();
+        }
+
+        Domain domain = domainDao.findById(domainId);
+        if (domain == null) {
+            throw new InvalidParameterValueException(String.format("Unable to 
find domain with ID [%s]. Verify the informed domain and try again.", 
domainId));
+        }
+
+        accountManager.checkAccess(caller, domain);
+        return domainId;
+    }
+
+    @Override
+    public ConsoleSession listConsoleSessionById(long id) {
+        return consoleSessionDao.findByIdIncludingRemoved(id);
+    }

Review Comment:
   @JoaoJandre thanks for your review.
   
   Yes, it is necessary. The method is currently being used by the 
`ListConsoleSessionsCmd` class in the `getEntityOwnerId()` method to retrieve 
the account associated with a console session. The API layer does not have 
direct access to the DAO/engine layer. Thus, the `listConsoleSessionById` 
method was introduced in the `ConsoleAccessManagerImpl` class to enable the 
communication between these two layers.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to