Skip to content

Commit a4773b7

Browse files
Alena ProkharchykAlena Prokharchyk
authored andcommitted
bug 12291: listVms - show non-project resources only if no projectId specified
status 12291: resolved fixed
1 parent c067758 commit a4773b7

10 files changed

Lines changed: 194 additions & 137 deletions

File tree

api/src/com/cloud/event/Event.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
23
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
34
*
45
* This software is licensed under the GNU General Public License v3 or later.
@@ -44,4 +45,5 @@ public enum State {
4445
String getLevel();
4546
long getStartId();
4647
String getParameters();
48+
String getAccountType();
4749
}

core/src/com/cloud/event/EventVO.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ public class EventVO implements Event, Identity {
6969
private long domainId;
7070

7171
@Column(name="account_name", table="account", insertable=false, updatable=false)
72-
private String accountName;
72+
private String accountName;
73+
74+
@Column(name="type", table="account", insertable=false, updatable=false)
75+
private String accountType;
7376

7477
@Column(name="removed", table="account", insertable=false, updatable=false)
7578
private Date removed;
@@ -148,16 +151,12 @@ public void setAccountId(long accountId) {
148151
public long getDomainId() {
149152
return domainId;
150153
}
151-
public void setDomainId(long domainId) {
152-
this.domainId = domainId;
153-
}
154+
154155
@Override
155156
public String getAccountName() {
156157
return accountName;
157-
}
158-
public void setAccountName(String accountName) {
159-
this.accountName = accountName;
160-
}
158+
}
159+
161160
@Override
162161
public int getTotalSize() {
163162
return totalSize;
@@ -196,5 +195,11 @@ public String getUuid() {
196195

197196
public void setUuid(String uuid) {
198197
this.uuid = uuid;
199-
}
198+
}
199+
200+
@Override
201+
public String getAccountType() {
202+
return accountType;
203+
}
204+
200205
}

server/src/com/cloud/api/ApiDBUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public static String getVersion() {
288288
}
289289

290290
public static List<UserVmVO> searchForUserVMs(Criteria c) {
291-
return _userVmMgr.searchForUserVMs(c);
291+
return _userVmMgr.searchForUserVMs(c, true);
292292
}
293293

294294
public static List<? extends StoragePoolVO> searchForStoragePools(Criteria c) {

server/src/com/cloud/server/ManagementServerImpl.java

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,51 +1427,56 @@ public List<EventVO> searchForEvents(ListEventsCmd cmd) {
14271427
String accountName = cmd.getAccountName();
14281428
Long domainId = cmd.getDomainId();
14291429
Long projectId = cmd.getProjectId();
1430-
1431-
if ((caller == null) || isAdmin(caller.getType())) {
1430+
1431+
if (_accountMgr.isAdmin(caller.getType())) {
14321432
isAdmin = true;
14331433
// validate domainId before proceeding
14341434
if (domainId != null) {
1435-
if ((caller != null) && !_domainDao.isChildDomain(caller.getDomainId(), domainId)) {
1436-
throw new PermissionDeniedException("Invalid domain id (" + domainId + ") given, unable to list events.");
1437-
}
1438-
1439-
if (accountName != null) {
1440-
Account userAccount = _accountDao.findAccount(accountName, domainId);
1441-
if (userAccount != null) {
1442-
permittedAccounts.add(userAccount.getId());
1443-
} else {
1444-
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
1445-
}
1446-
}
1447-
} else {
1448-
domainId = ((caller == null) ? DomainVO.ROOT_DOMAIN : caller.getDomainId());
1435+
Domain domain = _domainDao.findById(domainId);
1436+
if (domain == null) {
1437+
throw new InvalidParameterValueException("Unable to find domain by id " + domainId);
1438+
}
1439+
_accountMgr.checkAccess(caller, _domainDao.findById(domainId));
1440+
14491441
if (accountName != null) {
1450-
Account userAccount = _accountDao.findAccount(accountName, domainId);
1451-
if (userAccount != null) {
1452-
permittedAccounts.add(userAccount.getId());
1453-
} else {
1454-
throw new InvalidParameterValueException("DomainId is not specified. Unable to find account " + accountName + " in default root domain " + domainId);
1442+
Account userAccount = _accountDao.findNonProjectAccountIncludingRemoved(accountName, domainId);
1443+
if (userAccount == null) {
1444+
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
14551445
}
1446+
1447+
permittedAccounts.add(userAccount.getId());
14561448
}
1449+
} else {
1450+
domainId = caller.getDomainId();
1451+
if (accountName != null) {
1452+
Account userAccount = _accountDao.findNonProjectAccountIncludingRemoved(accountName, domainId);
1453+
if (userAccount == null) {
1454+
throw new InvalidParameterValueException("Can't find account " + accountName + " in domain id=" + domainId);
1455+
}
1456+
permittedAccounts.add(userAccount.getId());
1457+
}
14571458
}
14581459
} else {
14591460
permittedAccounts.add(caller.getId());
14601461
}
14611462

14621463
//set project information
1464+
boolean skipProjectEvents = true;
14631465
if (projectId != null) {
1464-
permittedAccounts.clear();
1465-
Project project = _projectMgr.getProject(projectId);
1466-
if (project == null) {
1467-
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
1468-
}
1469-
if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
1470-
throw new InvalidParameterValueException("Account " + caller + " can't access project id=" + projectId);
1471-
}
1472-
permittedAccounts.add(project.getProjectAccountId());
1473-
} else if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL){
1474-
permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
1466+
if (projectId == -1) {
1467+
permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
1468+
} else {
1469+
permittedAccounts.clear();
1470+
Project project = _projectMgr.getProject(projectId);
1471+
if (project == null) {
1472+
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
1473+
}
1474+
if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
1475+
throw new InvalidParameterValueException("Account " + caller + " can't access project id=" + projectId);
1476+
}
1477+
permittedAccounts.add(project.getProjectAccountId());
1478+
}
1479+
skipProjectEvents = false;
14751480
}
14761481

14771482
Filter searchFilter = new Filter(EventVO.class, "createDate", false, cmd.getStartIndex(), cmd.getPageSizeVal());
@@ -1503,43 +1508,42 @@ public List<EventVO> searchForEvents(ListEventsCmd cmd) {
15031508
sb.and("createDateB", sb.entity().getCreateDate(), SearchCriteria.Op.BETWEEN);
15041509
sb.and("createDateG", sb.entity().getCreateDate(), SearchCriteria.Op.GTEQ);
15051510
sb.and("createDateL", sb.entity().getCreateDate(), SearchCriteria.Op.LTEQ);
1511+
sb.and("accountType", sb.entity().getAccountType(), SearchCriteria.Op.NEQ);
15061512

1507-
if ((permittedAccounts.isEmpty()) && (accountName == null) && (domainId != null) && isAdmin) {
1508-
// if accountId isn't specified, we can do a domain match for the admin case
1509-
SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
1510-
domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
1511-
sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER);
1513+
if (isAdmin && permittedAccounts.isEmpty() && domainId != null) {
1514+
// if accountId isn't specified, we can do a domain match for the admin case
1515+
SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
1516+
domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
1517+
sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER);
15121518
}
1513-
1519+
15141520
SearchCriteria<EventVO> sc = sb.create();
1521+
if (!permittedAccounts.isEmpty()) {
1522+
sc.setParameters("accountId", permittedAccounts.toArray());
1523+
} else if (domainId != null) {
1524+
sc.setJoinParameters("domainSearch", "path", _domainDao.findById(domainId).getPath() + "%");
1525+
}
1526+
1527+
if (skipProjectEvents) {
1528+
sc.setParameters("accountType", Account.ACCOUNT_TYPE_PROJECT);
1529+
}
1530+
15151531
if (id != null) {
15161532
sc.setParameters("id", id);
15171533
}
1534+
15181535
if (keyword != null) {
15191536
SearchCriteria<EventVO> ssc = _eventDao.createSearchCriteria();
15201537
ssc.addOr("type", SearchCriteria.Op.LIKE, "%" + keyword + "%");
15211538
ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
15221539
ssc.addOr("level", SearchCriteria.Op.LIKE, "%" + keyword + "%");
1523-
15241540
sc.addAnd("level", SearchCriteria.Op.SC, ssc);
15251541
}
15261542

15271543
if (level != null) {
15281544
sc.setParameters("levelEQ", level);
15291545
}
15301546

1531-
if (!permittedAccounts.isEmpty()) {
1532-
sc.setParameters("accountId", permittedAccounts.toArray());
1533-
} else if (domainId != null) {
1534-
if (accountName != null) {
1535-
sc.setParameters("domainIdEQ", domainId);
1536-
sc.setParameters("accountName", "%" + accountName + "%");
1537-
} else if (isAdmin) {
1538-
DomainVO domain = _domainDao.findById(domainId);
1539-
sc.setJoinParameters("domainSearch", "path", domain.getPath() + "%");
1540-
}
1541-
}
1542-
15431547
if (type != null) {
15441548
sc.setParameters("type", type);
15451549
}

server/src/com/cloud/user/AccountManagerImpl.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ public UserVO createUser(String userName, String password, String firstName, Str
683683

684684
checkAccess(UserContext.current().getCaller(), domain);
685685

686-
Account account = _accountDao.findNonDisabledAccount(accountName, domainId);
686+
Account account = _accountDao.findEnabledAccount(accountName, domainId);
687687
if (account == null || account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
688688
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain id=" + domainId + " to create user");
689689
}
@@ -1066,7 +1066,7 @@ public AccountVO updateAccount(UpdateAccountCmd cmd) {
10661066
if(accountId != null){
10671067
account = _accountDao.findById(accountId);
10681068
}else{
1069-
account = _accountDao.findAccount(accountName, domainId);
1069+
account = _accountDao.findEnabledAccount(accountName, domainId);
10701070
}
10711071

10721072
// Check if account exists
@@ -1084,8 +1084,8 @@ public AccountVO updateAccount(UpdateAccountCmd cmd) {
10841084
checkAccess(UserContext.current().getCaller(), _domainMgr.getDomain(account.getDomainId()));
10851085

10861086
// check if the given account name is unique in this domain for updating
1087-
Account duplicateAcccount = _accountDao.findAccount(newAccountName, domainId);
1088-
if (duplicateAcccount != null && duplicateAcccount.getRemoved() == null && duplicateAcccount.getId() != account.getId()) {// allow
1087+
Account duplicateAcccount = _accountDao.findActiveAccount(newAccountName, domainId);
1088+
if (duplicateAcccount != null && duplicateAcccount.getId() != account.getId()) {// allow
10891089
// same
10901090
// account
10911091
// to
@@ -1385,18 +1385,20 @@ public Pair<List<Long>,Long> finalizeAccountDomainForList(Account caller, String
13851385

13861386
//set project information
13871387
if (projectId != null) {
1388-
permittedAccounts.clear();
1389-
Project project = _projectMgr.getProject(projectId);
1390-
if (project == null) {
1391-
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
1392-
}
1393-
if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
1394-
throw new InvalidParameterValueException("Account " + caller + " can't access project id=" + projectId);
1395-
}
1396-
permittedAccounts.add(project.getProjectAccountId());
1397-
} else if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL){
1398-
permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
1399-
}
1388+
if (projectId == -1) {
1389+
permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
1390+
} else {
1391+
permittedAccounts.clear();
1392+
Project project = _projectMgr.getProject(projectId);
1393+
if (project == null) {
1394+
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
1395+
}
1396+
if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
1397+
throw new InvalidParameterValueException("Account " + caller + " can't access project id=" + projectId);
1398+
}
1399+
permittedAccounts.add(project.getProjectAccountId());
1400+
}
1401+
}
14001402

14011403
return new Pair<List<Long>, Long>(permittedAccounts, domainId);
14021404
}

server/src/com/cloud/user/dao/AccountDao.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
package com.cloud.user.dao;
2020

21-
import java.util.Date;
22-
import java.util.List;
23-
21+
import java.util.Date;
22+
import java.util.List;
23+
2424
import com.cloud.user.Account;
2525
import com.cloud.user.AccountVO;
2626
import com.cloud.user.User;
@@ -31,17 +31,24 @@
3131
public interface AccountDao extends GenericDao<AccountVO, Long> {
3232
Pair<User, Account> findUserAccountByApiKey(String apiKey);
3333
List<AccountVO> findAccountsLike(String accountName);
34-
Account findActiveAccount(String accountName, Long domainId);
35-
Account findActiveAccountByName(String accountName);
36-
Account findAccount(String accountName, Long domainId);
3734
List<AccountVO> findActiveAccounts(Long maxAccountId, Filter filter);
3835
List<AccountVO> findRecentlyDeletedAccounts(Long maxAccountId, Date earliestRemovedDate, Filter filter);
3936
List<AccountVO> findNewAccounts(Long minAccountId, Filter filter);
40-
List<AccountVO> findCleanupsForRemovedAccounts(Long domainId);
41-
List<AccountVO> findAdminAccountsForDomain(Long domainId);
37+
List<AccountVO> findCleanupsForRemovedAccounts(Long domainId);
4238
List<AccountVO> findActiveAccountsForDomain(Long domain);
4339
void markForCleanup(long accountId);
4440
List<AccountVO> listAccounts(String accountName, Long domainId, Filter filter);
4541
List<AccountVO> findCleanupsForDisabledAccounts();
46-
Account findNonDisabledAccount(String accountName, Long domainId);
42+
43+
//return account only in enabled state
44+
Account findEnabledAccount(String accountName, Long domainId);
45+
Account findEnabledNonProjectAccount(String accountName, Long domainId);
46+
47+
//returns account even when it's removed
48+
Account findAccountIncludingRemoved(String accountName, Long domainId);
49+
Account findNonProjectAccountIncludingRemoved(String accountName, Long domainId);
50+
51+
//returns only non-removed account
52+
Account findActiveAccount(String accountName, Long domainId);
53+
Account findActiveNonProjectAccount(String accountName, Long domainId);
4754
}

0 commit comments

Comments
 (0)