You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by nv...@apache.org on 2022/03/21 02:41:35 UTC

[cloudstack] branch main updated: account check made explicit - cleanup (#6122)

This is an automated email from the ASF dual-hosted git repository.

nvazquez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/main by this push:
     new 3e4e417  account check made explicit - cleanup (#6122)
3e4e417 is described below

commit 3e4e417389c4d55c91a71b9abf7605777016c246
Author: dahn <da...@shapeblue.com>
AuthorDate: Mon Mar 21 03:41:06 2022 +0100

    account check made explicit - cleanup (#6122)
    
    Co-authored-by: Daan Hoogland <da...@onecht.net>
---
 .../com/cloud/projects/ProjectManagerImpl.java     | 66 ++++++++++++----------
 1 file changed, 37 insertions(+), 29 deletions(-)

diff --git a/server/src/main/java/com/cloud/projects/ProjectManagerImpl.java b/server/src/main/java/com/cloud/projects/ProjectManagerImpl.java
index ccb917a..9be0dfc 100644
--- a/server/src/main/java/com/cloud/projects/ProjectManagerImpl.java
+++ b/server/src/main/java/com/cloud/projects/ProjectManagerImpl.java
@@ -1210,44 +1210,44 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager, C
                 throw new InvalidParameterValueException("Invitation is expired for account id=" + accountName + " to the project id=" + projectId);
             } else {
                 final ProjectInvitationVO inviteFinal = invite;
-                final Long accountIdFinal = accountId;
+                final Long accountIdFinal = invite.getAccountId() != -1 ? invite.getAccountId() : accountId;
                 final String accountNameFinal = accountName;
-                final User finalUser = user;
-                ProjectInvitationVO finalInvite = invite;
+                final User finalUser = getFinalUser(user, invite);
                 result = Transaction.execute(new TransactionCallback<Boolean>() {
                     @Override
                     public Boolean doInTransaction(TransactionStatus status) {
                         boolean result = true;
 
-                ProjectInvitation.State newState = accept ? ProjectInvitation.State.Completed : ProjectInvitation.State.Declined;
-
-                //update invitation
-                s_logger.debug("Marking invitation " + inviteFinal + " with state " + newState);
-                inviteFinal.setState(newState);
-                result = _projectInvitationDao.update(inviteFinal.getId(), inviteFinal);
-
-                if (result && accept) {
-                    //check if account already exists for the project (was added before invitation got accepted)
-                    if (finalInvite.getForUserId() == -1) {
-                        ProjectAccount projectAccount = _projectAccountDao.findByProjectIdAccountId(projectId, accountIdFinal);
-                        if (projectAccount != null) {
-                            s_logger.debug("Account " + accountNameFinal + " already added to the project id=" + projectId);
-                        } else {
-                            assignAccountToProject(project, accountIdFinal, finalInvite.getAccountRole(), null, finalInvite.getProjectRoleId());
-                        }
-                    } else {
-                        ProjectAccount projectAccount = _projectAccountDao.findByProjectIdUserId(projectId, finalUser.getAccountId(), finalUser.getId());
-                        if (projectAccount != null) {
-                            s_logger.debug("User " + finalUser.getId() + "has already been added to the project id=" + projectId);
+                        ProjectInvitation.State newState = accept ? ProjectInvitation.State.Completed : ProjectInvitation.State.Declined;
+
+                        //update invitation
+                        s_logger.debug("Marking invitation " + inviteFinal + " with state " + newState);
+                        inviteFinal.setState(newState);
+                        result = _projectInvitationDao.update(inviteFinal.getId(), inviteFinal);
+
+                        if (result && accept) {
+                            //check if account already exists for the project (was added before invitation got accepted)
+                            if (inviteFinal.getForUserId() == -1) {
+                                ProjectAccount projectAccount = _projectAccountDao.findByProjectIdAccountId(projectId, accountIdFinal);
+                                if (projectAccount != null) {
+                                    s_logger.debug("Account " + accountNameFinal + " already added to the project id=" + projectId);
+                                } else {
+                                    assignAccountToProject(project, accountIdFinal, inviteFinal.getAccountRole(), null, inviteFinal.getProjectRoleId());
+                                }
+                            } else {
+                                ProjectAccount projectAccount = _projectAccountDao.findByProjectIdUserId(projectId, finalUser.getAccountId(), finalUser.getId());
+                                if (projectAccount != null) {
+                                    s_logger.debug("User " + finalUser.getId() + "has already been added to the project id=" + projectId);
+                                } else {
+                                    assignUserToProject(project, inviteFinal.getForUserId(), finalUser.getAccountId(), inviteFinal.getAccountRole(), inviteFinal.getProjectRoleId());
+                                }
+                            }
                         } else {
-                            assignUserToProject(project, finalInvite.getForUserId(), finalUser.getAccountId(), finalInvite.getAccountRole(), finalInvite.getProjectRoleId());
+                            s_logger.warn("Failed to update project invitation " + inviteFinal + " with state " + newState);
                         }
+                        return result;
                     }
-                } else {
-                    s_logger.warn("Failed to update project invitation " + inviteFinal + " with state " + newState);
-                }
-                return result;
-             }});
+                });
             }
         } else {
             throw new InvalidParameterValueException("Unable to find invitation for account name=" + accountName + " to the project id=" + projectId);
@@ -1256,6 +1256,14 @@ public class ProjectManagerImpl extends ManagerBase implements ProjectManager, C
         return result;
     }
 
+    private User getFinalUser(User user, ProjectInvitationVO invite) {
+        User returnedUser = user;
+        if (invite.getForUserId() != -1 && invite.getForUserId() != user.getId()) {
+            returnedUser = userDao.getUser(invite.getForUserId());
+        }
+        return returnedUser;
+    }
+
     @Override
     public List<Long> listPermittedProjectAccounts(long accountId) {
         return _projectAccountDao.listPermittedAccountIds(accountId);