Skip to content

Commit

Permalink
account check made explicit - cleanup (#6122)
Browse files Browse the repository at this point in the history
Co-authored-by: Daan Hoogland <dahn@onecht.net>
  • Loading branch information
DaanHoogland and Daan Hoogland committed Mar 21, 2022
1 parent 88d77c8 commit 3e4e417
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions server/src/main/java/com/cloud/projects/ProjectManagerImpl.java
Expand Up @@ -1210,44 +1210,44 @@ public boolean updateInvitation(final long projectId, String accountName, Long u
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);
Expand All @@ -1256,6 +1256,14 @@ public Boolean doInTransaction(TransactionStatus status) {
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);
Expand Down

0 comments on commit 3e4e417

Please sign in to comment.