Skip to content

Commit 82b6971

Browse files
Pearl1594Pearl Dsilva
andauthored
server: Handle listProjects API to list projects with user as members when listAll=true (#4316)
* added defensive checks for avoiding NPE and list projects API fix * list projects with account name provided to not include users in the account in response Co-authored-by: Pearl Dsilva <pearl.dsilva@shapeblue.com>
1 parent 87e08f8 commit 82b6971

5 files changed

Lines changed: 23 additions & 7 deletions

File tree

server/src/main/java/com/cloud/acl/AffinityGroupAccessChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public boolean checkAccess(Account caller, ControlledEntity entity, AccessType a
8080
//check if the group belongs to a project
8181
User user = CallContext.current().getCallingUser();
8282
ProjectVO project = _projectDao.findByProjectAccountId(group.getAccountId());
83-
ProjectAccount userProjectAccount = _projectAccountDao.findByProjectIdUserId(project.getId(), user.getAccountId(), user.getId());
8483
if (project != null) {
84+
ProjectAccount userProjectAccount = _projectAccountDao.findByProjectIdUserId(project.getId(), user.getAccountId(), user.getId());
8585
if (userProjectAccount != null) {
8686
if (AccessType.ModifyProject.equals(accessType) && _projectAccountDao.canUserModifyProject(project.getId(), user.getAccountId(), user.getId())) {
8787
return true;

server/src/main/java/com/cloud/acl/DomainChecker.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.cloud.user.User;
6262
import com.cloud.user.dao.AccountDao;
6363
import com.cloud.utils.component.AdapterBase;
64+
import com.cloud.utils.exception.CloudRuntimeException;
6465

6566
@Component
6667
public class DomainChecker extends AdapterBase implements SecurityChecker {
@@ -199,6 +200,9 @@ public boolean checkAccess(Account caller, ControlledEntity entity, AccessType a
199200
private boolean checkOperationPermitted(Account caller, ControlledEntity entity) {
200201
User user = CallContext.current().getCallingUser();
201202
Project project = projectDao.findByProjectAccountId(entity.getAccountId());
203+
if (project == null) {
204+
throw new CloudRuntimeException("Unable to find project to which the entity belongs to");
205+
}
202206
ProjectAccount projectUser = _projectAccountDao.findByProjectIdUserId(project.getId(), user.getAccountId(), user.getId());
203207
String apiCommandName = CallContext.current().getApiName();
204208

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,15 +1484,19 @@ private Pair<List<ProjectJoinVO>, Integer> listProjectsInternal(ListProjectsCmd
14841484
}
14851485

14861486
if (accountId != null) {
1487-
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
1487+
if (userId == null) {
1488+
sb.and().op("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
1489+
sb.and("userIdNull", sb.entity().getUserId(), Op.NULL);
1490+
sb.cp();
1491+
} else {
1492+
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
1493+
}
14881494
}
14891495

14901496
if (userId != null) {
14911497
sb.and().op("userId", sb.entity().getUserId(), Op.EQ);
14921498
sb.or("userIdNull", sb.entity().getUserId(), Op.NULL);
14931499
sb.cp();
1494-
} else {
1495-
sb.and("userIdNull", sb.entity().getUserId(), Op.NULL);
14961500
}
14971501

14981502
SearchCriteria<ProjectJoinVO> sc = sb.create();

server/src/main/java/com/cloud/network/NetworkModelImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,9 @@ public void checkNetworkPermissions(Account owner, Network network) {
16581658
if (owner.getType() != Account.ACCOUNT_TYPE_PROJECT && networkOwner.getType() == Account.ACCOUNT_TYPE_PROJECT) {
16591659
User user = CallContext.current().getCallingUser();
16601660
Project project = projectDao.findByProjectAccountId(network.getAccountId());
1661+
if (project == null) {
1662+
throw new CloudRuntimeException("Unable to find project to which the network belongs to");
1663+
}
16611664
ProjectAccount projectAccountUser = _projectAccountDao.findByProjectIdUserId(project.getId(), user.getAccountId(), user.getId());
16621665
if (projectAccountUser != null) {
16631666
if (!_projectAccountDao.canUserAccessProjectAccount(user.getAccountId(), user.getId(), network.getAccountId())) {

server/src/main/java/com/cloud/projects/ProjectManagerImpl.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ public Project createProject(final String name, final String displayText, String
239239
}
240240

241241
User user = validateUser(userId, accountId, domainId);
242+
if (user != null) {
243+
owner = _accountDao.findById(user.getAccountId());
244+
}
242245

243246
//do resource limit check
244247
_resourceLimitMgr.checkResourceLimit(owner, ResourceType.project);
@@ -559,9 +562,11 @@ public boolean canAccessProjectAccount(Account caller, long accountId) {
559562
}
560563
User user = CallContext.current().getCallingUser();
561564
ProjectVO project = _projectDao.findByProjectAccountId(accountId);
562-
ProjectAccount userProjectAccount = _projectAccountDao.findByProjectIdUserId(project.getId(), user.getAccountId(), user.getId());
563-
if (userProjectAccount != null) {
564-
return _projectAccountDao.canUserAccessProjectAccount(user.getAccountId(), user.getId(), accountId);
565+
if (project != null) {
566+
ProjectAccount userProjectAccount = _projectAccountDao.findByProjectIdUserId(project.getId(), user.getAccountId(), user.getId());
567+
if (userProjectAccount != null) {
568+
return _projectAccountDao.canUserAccessProjectAccount(user.getAccountId(), user.getId(), accountId);
569+
}
565570
}
566571
return _projectAccountDao.canAccessProjectAccount(caller.getId(), accountId);
567572
}

0 commit comments

Comments
 (0)