Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions server/src/main/java/com/cloud/projects/ProjectManagerImpl.java
Original file line number Diff line number Diff line change
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