Modify AccountResponse to return groups that account belongs to.

This commit is contained in:
Min Chen 2013-09-30 14:14:38 -07:00
parent d4d3c69a25
commit de0904ba63
8 changed files with 448 additions and 36 deletions

View File

@ -438,4 +438,9 @@ public class AccountResponse extends BaseResponse implements ResourceLimitAndCou
public void setIsDefault(Boolean isDefault) {
this.isDefault = isDefault;
}
public void setGroups(List<AclGroupResponse> groups) {
this.groups = groups;
}
}

View File

@ -376,7 +376,8 @@
<bean id="AclGroupAccountMapDaoImpl" class="org.apache.cloudstack.acl.dao.AclGroupAccountMapDaoImpl"/>
<bean id="AclGroupRoleMapDaoImpl" class="org.apache.cloudstack.acl.dao.AclGroupRoleMapDaoImpl"/>
<bean id="AclApiPermissionDaoImpl" class="org.apache.cloudstack.acl.dao.AclApiPermissionDaoImpl"/>
<bean id="AclEntityPermissionDaoImpl" class="org.apache.cloudstack.acl.dao.AclEntityPermissionDaoImpl"/>
<bean id="AclEntityPermissionDaoImpl" class="org.apache.cloudstack.acl.dao.AclEntityPermissionDaoImpl"/>
<bean id="AclServiceImpl" class="org.apache.cloudstack.acl.AclServiceImpl"/>
<!--

View File

@ -641,6 +641,7 @@ public class ApiDBUtils {
_serviceOfferingDetailsDao = serviceOfferingDetailsDao;
_accountService = accountService;
_aclRoleJoinDao = aclRoleJoinDao;
_aclGroupJoinDao = aclGroupJoinDao;
}
// ///////////////////////////////////////////////////////////
@ -1512,6 +1513,10 @@ public class ApiDBUtils {
return _userAccountJoinDao.searchByAccountId(accountId);
}
public static List<AclGroupJoinVO> findAclGroupByAccountId(long accountId) {
return _aclGroupJoinDao.findAclGroupsByAccount(accountId);
}
public static ProjectAccountResponse newProjectAccountResponse(ProjectAccountJoinVO proj) {
return _projectAccountJoinDao.newProjectAccountResponse(proj);
}

View File

@ -21,15 +21,18 @@ import java.util.List;
import javax.ejb.Local;
import javax.inject.Inject;
import org.apache.cloudstack.api.response.AccountResponse;
import org.apache.cloudstack.api.response.ResourceLimitAndCountResponse;
import org.apache.cloudstack.api.response.UserResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.api.response.AccountResponse;
import org.apache.cloudstack.api.response.AclGroupResponse;
import org.apache.cloudstack.api.response.ResourceLimitAndCountResponse;
import org.apache.cloudstack.api.response.UserResponse;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.query.ViewResponseHelper;
import com.cloud.api.query.vo.AccountJoinVO;
import com.cloud.api.query.vo.AclGroupJoinVO;
import com.cloud.api.query.vo.UserAccountJoinVO;
import com.cloud.configuration.Resource.ResourceType;
import com.cloud.user.Account;
@ -43,7 +46,7 @@ import com.cloud.utils.db.SearchCriteria;
public class AccountJoinDaoImpl extends GenericDaoBase<AccountJoinVO, Long> implements AccountJoinDao {
public static final Logger s_logger = Logger.getLogger(AccountJoinDaoImpl.class);
private SearchBuilder<AccountJoinVO> acctIdSearch;
private final SearchBuilder<AccountJoinVO> acctIdSearch;
@Inject
public AccountManager _accountMgr;
@ -53,7 +56,7 @@ public class AccountJoinDaoImpl extends GenericDaoBase<AccountJoinVO, Long> impl
acctIdSearch.and("id", acctIdSearch.entity().getId(), SearchCriteria.Op.EQ);
acctIdSearch.done();
this._count = "select count(distinct id) from account_view WHERE ";
_count = "select count(distinct id) from account_view WHERE ";
}
@Override
@ -100,6 +103,11 @@ public class AccountJoinDaoImpl extends GenericDaoBase<AccountJoinVO, Long> impl
accountResponse.setDetails(ApiDBUtils.getAccountDetails(account.getId()));
accountResponse.setObjectName("account");
// add all the acl groups for an account
List<AclGroupJoinVO> groupsForAccount = ApiDBUtils.findAclGroupByAccountId(account.getId());
List<AclGroupResponse> groupResponses = ViewResponseHelper.createAclGroupResponses(groupsForAccount);
accountResponse.setGroups(groupResponses);
return accountResponse;
}

View File

@ -0,0 +1,38 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.api.query.dao;
import java.util.List;
import org.apache.cloudstack.acl.AclGroup;
import org.apache.cloudstack.api.response.AclGroupResponse;
import com.cloud.api.query.vo.AclGroupJoinVO;
import com.cloud.utils.db.GenericDao;
public interface AclGroupJoinDao extends GenericDao<AclGroupJoinVO, Long> {
AclGroupResponse newAclGroupResponse(AclGroupJoinVO group);
AclGroupResponse setAclGroupResponse(AclGroupResponse response, AclGroupJoinVO os);
List<AclGroupJoinVO> newAclGroupView(AclGroup group);
List<AclGroupJoinVO> searchByIds(Long... ids);
List<AclGroupJoinVO> findAclGroupsByAccount(long accountId);
}

View File

@ -0,0 +1,178 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.api.query.dao;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.Local;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.acl.AclGroup;
import org.apache.cloudstack.acl.AclGroupAccountMapVO;
import org.apache.cloudstack.acl.dao.AclGroupAccountMapDao;
import org.apache.cloudstack.api.response.AclGroupResponse;
import org.apache.cloudstack.api.response.AclRoleResponse;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import com.cloud.api.query.vo.AclGroupJoinVO;
import com.cloud.user.AccountManager;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
@Component
@Local(value = {AclGroupJoinDao.class})
public class AclGroupJoinDaoImpl extends GenericDaoBase<AclGroupJoinVO, Long> implements AclGroupJoinDao {
public static final Logger s_logger = Logger.getLogger(AclGroupJoinDaoImpl.class);
private final SearchBuilder<AclGroupJoinVO> grpIdSearch;
private final SearchBuilder<AclGroupJoinVO> grpSearch;
@Inject
public AccountManager _accountMgr;
@Inject
public ConfigurationDao _configDao;
@Inject
public AclGroupAccountMapDao _grpAccountDao;
protected AclGroupJoinDaoImpl() {
grpSearch = createSearchBuilder();
grpSearch.and("idIN", grpSearch.entity().getId(), SearchCriteria.Op.IN);
grpSearch.done();
grpIdSearch = createSearchBuilder();
grpIdSearch.and("id", grpIdSearch.entity().getId(), SearchCriteria.Op.EQ);
grpIdSearch.done();
_count = "select count(distinct id) from acl_group_view WHERE ";
}
@Override
public AclGroupResponse newAclGroupResponse(AclGroupJoinVO group) {
AclGroupResponse response = new AclGroupResponse();
response.setId(group.getUuid());
response.setName(group.getName());
response.setDescription(group.getDescription());
response.setDomainId(group.getDomainUuid());
response.setDomainName(group.getName());
if (group.getAccountId() > 0) {
response.addAccountId(group.getAccountUuid());
}
if (group.getRoleId() > 0) {
AclRoleResponse roleResp = new AclRoleResponse();
roleResp.setId(group.getRoleUuid());
roleResp.setName(group.getRoleName());
response.addRole(roleResp);
}
response.setObjectName("aclgroup");
return response;
}
@Override
public AclGroupResponse setAclGroupResponse(AclGroupResponse response, AclGroupJoinVO group) {
if (group.getAccountId() > 0) {
response.addAccountId(group.getAccountUuid());
}
if (group.getRoleId() > 0) {
AclRoleResponse roleResp = new AclRoleResponse();
roleResp.setId(group.getRoleUuid());
roleResp.setName(group.getRoleName());
response.addRole(roleResp);
}
return response;
}
@Override
public List<AclGroupJoinVO> newAclGroupView(AclGroup group) {
SearchCriteria<AclGroupJoinVO> sc = grpIdSearch.create();
sc.setParameters("id", group.getId());
return searchIncludingRemoved(sc, null, null, false);
}
@Override
public List<AclGroupJoinVO> searchByIds(Long... grpIds) {
// set detail batch query size
int DETAILS_BATCH_SIZE = 2000;
String batchCfg = _configDao.getValue("detail.batch.query.size");
if (batchCfg != null) {
DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
}
// query details by batches
List<AclGroupJoinVO> uvList = new ArrayList<AclGroupJoinVO>();
// query details by batches
int curr_index = 0;
if (grpIds.length > DETAILS_BATCH_SIZE) {
while ((curr_index + DETAILS_BATCH_SIZE) <= grpIds.length) {
Long[] ids = new Long[DETAILS_BATCH_SIZE];
for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
ids[k] = grpIds[j];
}
SearchCriteria<AclGroupJoinVO> sc = grpSearch.create();
sc.setParameters("idIN", ids);
List<AclGroupJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
if (vms != null) {
uvList.addAll(vms);
}
curr_index += DETAILS_BATCH_SIZE;
}
}
if (curr_index < grpIds.length) {
int batch_size = (grpIds.length - curr_index);
// set the ids value
Long[] ids = new Long[batch_size];
for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
ids[k] = grpIds[j];
}
SearchCriteria<AclGroupJoinVO> sc = grpSearch.create();
sc.setParameters("idIN", ids);
List<AclGroupJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
if (vms != null) {
uvList.addAll(vms);
}
}
return uvList;
}
@Override
public List<AclGroupJoinVO> findAclGroupsByAccount(long accountId) {
List<AclGroupAccountMapVO> grpMap = _grpAccountDao.listByAccountId(accountId);
if (grpMap != null && grpMap.size() > 0) {
Set<Long> grpList = new HashSet<Long>();
for (AclGroupAccountMapVO m : grpMap) {
grpList.add(m.getAclGroupId());
}
return searchByIds(grpList.toArray(new Long[grpList.size()]));
}
else{
return null;
}
}
}

View File

@ -0,0 +1,158 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.api.query.vo;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.cloud.utils.db.GenericDao;
@Entity
@Table(name = ("acl_group_view"))
public class AclGroupJoinVO extends BaseViewVO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "uuid")
private String uuid;
@Column(name = "domain_id")
private long domainId;
@Column(name = "domain_uuid")
private String domainUuid;
@Column(name = "domain_name")
private String domainName;
@Column(name = "domain_path")
private String domainPath;
@Column(name = GenericDao.REMOVED_COLUMN)
private Date removed;
@Column(name = GenericDao.CREATED_COLUMN)
private Date created;
@Column(name = "role_id")
private long roleId;
@Column(name = "role_uuid")
private String roleUuid;
@Column(name = "role_name")
private String roleName;
@Column(name = "account_id")
private long accountId;
@Column(name = "account_uuid")
private String accountUuid;
@Column(name = "account_name")
private String accountName;
public AclGroupJoinVO() {
}
@Override
public long getId() {
return id;
}
@Override
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getUuid() {
return uuid;
}
public long getDomainId() {
return domainId;
}
public String getDomainUuid() {
return domainUuid;
}
public String getDomainName() {
return domainName;
}
public String getDomainPath() {
return domainPath;
}
public Date getRemoved() {
return removed;
}
public Date getCreated() {
return created;
}
public long getRoleId() {
return roleId;
}
public String getRoleUuid() {
return roleUuid;
}
public String getRoleName() {
return roleName;
}
public long getAccountId() {
return accountId;
}
public String getAccountUuid() {
return accountUuid;
}
public String getAccountName() {
return accountName;
}
}

View File

@ -16,27 +16,37 @@
// under the License.
package com.cloud.api.response;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.ApiResponseGsonHelper;
import com.cloud.api.ApiServer;
import com.cloud.utils.encoding.URLEncoder;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.exception.ExceptionProxyObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.response.*;
import org.apache.log4j.Logger;
import org.apache.cloudstack.api.response.AsyncJobResponse;
import org.apache.cloudstack.api.response.CreateCmdResponse;
import org.apache.cloudstack.api.response.ExceptionResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.SuccessResponse;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.ApiResponseGsonHelper;
import com.cloud.api.ApiServer;
import com.cloud.utils.encoding.URLEncoder;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.exception.ExceptionProxyObject;
public class ApiResponseSerializer {
private static final Logger s_logger = Logger.getLogger(ApiResponseSerializer.class.getName());
@ -100,7 +110,7 @@ public class ApiResponseSerializer {
} else if (result instanceof SuccessResponse) {
sb.append("{ \"success\" : \"").append(((SuccessResponse) result).getSuccess()).append("\"} ");
} else if (result instanceof ExceptionResponse) {
String jsonErrorText = gson.toJson((ExceptionResponse) result);
String jsonErrorText = gson.toJson(result);
jsonErrorText = unescape(jsonErrorText);
sb.append(jsonErrorText);
} else {
@ -209,7 +219,9 @@ public class ApiResponseSerializer {
}
} else if (fieldValue instanceof Collection<?>) {
Collection<?> subResponseList = (Collection<Object>) fieldValue;
boolean usedUuidList = false;
int count = subResponseList.size();
int ind = 0;
boolean beginTagAdded = false;
for (Object value : subResponseList) {
if (value instanceof ResponseObject) {
ResponseObject subObj = (ResponseObject) value;
@ -217,25 +229,32 @@ public class ApiResponseSerializer {
subObj.setObjectName(serializedName.value());
}
serializeResponseObjXML(sb, subObj);
} else if (value instanceof ExceptionProxyObject) {
// Only exception reponses carry a list of
// ExceptionProxyObject objects.
ExceptionProxyObject idProxy = (ExceptionProxyObject) value;
// If this is the first IdentityProxy field
// encountered, put in a uuidList tag.
if (!usedUuidList) {
} else {
// If this is the first field in the list, put in a begin tag
if (!beginTagAdded) {
sb.append("<" + serializedName.value() + ">");
usedUuidList = true;
beginTagAdded = true;
}
sb.append("<" + "uuid" + ">" + idProxy.getUuid() + "</" + "uuid" + ">");
// Append the new descriptive property also.
String idFieldName = idProxy.getDescription();
if (idFieldName != null) {
sb.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
if (value instanceof ExceptionProxyObject) {
// Only exception reponses carry a list of
// ExceptionProxyObject objects.
ExceptionProxyObject idProxy = (ExceptionProxyObject)value;
sb.append("<" + "uuid" + ">" + idProxy.getUuid() + "</" + "uuid" + ">");
// Append the new descriptive property also.
String idFieldName = idProxy.getDescription();
if (idFieldName != null) {
sb.append("<" + "uuidProperty" + ">" + idFieldName + "</" + "uuidProperty" + ">");
}
} else {
sb.append(escapeSpecialXmlChars(value.toString()));
if (ind < count - 1) {
sb.append(", ");
}
}
ind++;
}
}
if (usedUuidList) {
if (beginTagAdded) {
// close the uuidList.
sb.append("</").append(serializedName.value()).append(">");
}