account check made explicit - cleanup (#6122)

Co-authored-by: Daan Hoogland <dahn@onecht.net>
This commit is contained in:
dahn 2022-03-21 03:41:06 +01:00 committed by GitHub
parent 88d77c86a7
commit 3e4e417389
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
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);
//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);
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 {
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);
} 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);