Search in sources :

Example 1 with UserAccountJoinVO

use of com.cloud.api.query.vo.UserAccountJoinVO in project cloudstack by apache.

the class QueryManagerImpl method searchForUsersInternal.

private Pair<List<UserAccountJoinVO>, Integer> searchForUsersInternal(ListUsersCmd cmd) throws PermissionDeniedException {
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    boolean listAll = cmd.listAll();
    Long id = cmd.getId();
    if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
        long currentId = CallContext.current().getCallingUser().getId();
        if (id != null && currentId != id.longValue()) {
            throw new PermissionDeniedException("Calling user is not authorized to see the user requested by id");
        }
        id = currentId;
    }
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), null, permittedAccounts, domainIdRecursiveListProject, listAll, false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(UserAccountJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    Object username = cmd.getUsername();
    Object type = cmd.getAccountType();
    Object accountName = cmd.getAccountName();
    Object state = cmd.getState();
    Object keyword = cmd.getKeyword();
    SearchBuilder<UserAccountJoinVO> sb = _userAccountJoinDao.createSearchBuilder();
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    sb.and("username", sb.entity().getUsername(), SearchCriteria.Op.LIKE);
    if (id != null && id == 1) {
        // system user should NOT be searchable
        List<UserAccountJoinVO> emptyList = new ArrayList<UserAccountJoinVO>();
        return new Pair<List<UserAccountJoinVO>, Integer>(emptyList, 0);
    } else if (id != null) {
        sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    } else {
        // this condition is used to exclude system user from the search
        // results
        sb.and("id", sb.entity().getId(), SearchCriteria.Op.NEQ);
    }
    sb.and("type", sb.entity().getAccountType(), SearchCriteria.Op.EQ);
    sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
    sb.and("accountName", sb.entity().getAccountName(), SearchCriteria.Op.EQ);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    if ((accountName == null) && (domainId != null)) {
        sb.and("domainPath", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
    }
    SearchCriteria<UserAccountJoinVO> sc = sb.create();
    // building ACL condition
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (keyword != null) {
        SearchCriteria<UserAccountJoinVO> ssc = _userAccountJoinDao.createSearchCriteria();
        ssc.addOr("username", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("firstname", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("lastname", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("email", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("accountName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("accountType", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("username", SearchCriteria.Op.SC, ssc);
    }
    if (username != null) {
        sc.setParameters("username", username);
    }
    if (id != null) {
        sc.setParameters("id", id);
    } else {
        // Don't return system user, search builder with NEQ
        sc.setParameters("id", 1);
    }
    if (type != null) {
        sc.setParameters("type", type);
    }
    if (accountName != null) {
        sc.setParameters("accountName", accountName);
        if (domainId != null) {
            sc.setParameters("domainId", domainId);
        }
    } else if (domainId != null) {
        DomainVO domainVO = _domainDao.findById(domainId);
        sc.setParameters("domainPath", domainVO.getPath() + "%");
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    return _userAccountJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.user.Account) Ternary(com.cloud.utils.Ternary) ArrayList(java.util.ArrayList) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) DomainVO(com.cloud.domain.DomainVO) UserAccountJoinVO(com.cloud.api.query.vo.UserAccountJoinVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Pair(com.cloud.utils.Pair)

Example 2 with UserAccountJoinVO

use of com.cloud.api.query.vo.UserAccountJoinVO in project cloudstack by apache.

the class ViewResponseHelper method createProjectAccountResponse.

public static List<ProjectAccountResponse> createProjectAccountResponse(ProjectAccountJoinVO... projectAccounts) {
    List<ProjectAccountResponse> responseList = new ArrayList<ProjectAccountResponse>();
    for (ProjectAccountJoinVO proj : projectAccounts) {
        ProjectAccountResponse resp = ApiDBUtils.newProjectAccountResponse(proj);
        // update user list
        Account caller = CallContext.current().getCallingAccount();
        if (ApiDBUtils.isAdmin(caller)) {
            List<UserAccountJoinVO> users = ApiDBUtils.findUserViewByAccountId(proj.getAccountId());
            resp.setUsers(ViewResponseHelper.createUserResponse(users.toArray(new UserAccountJoinVO[users.size()])));
        }
        responseList.add(resp);
    }
    return responseList;
}
Also used : Account(com.cloud.user.Account) UserAccountJoinVO(com.cloud.api.query.vo.UserAccountJoinVO) ArrayList(java.util.ArrayList) ProjectAccountJoinVO(com.cloud.api.query.vo.ProjectAccountJoinVO) ProjectAccountResponse(org.apache.cloudstack.api.response.ProjectAccountResponse)

Example 3 with UserAccountJoinVO

use of com.cloud.api.query.vo.UserAccountJoinVO in project cloudstack by apache.

the class AccountJoinDaoImpl method newAccountResponse.

@Override
public AccountResponse newAccountResponse(ResponseView view, AccountJoinVO account) {
    AccountResponse accountResponse = new AccountResponse();
    accountResponse.setId(account.getUuid());
    accountResponse.setName(account.getAccountName());
    accountResponse.setAccountType(account.getType());
    accountResponse.setDomainId(account.getDomainUuid());
    accountResponse.setDomainName(account.getDomainName());
    accountResponse.setState(account.getState().toString());
    accountResponse.setNetworkDomain(account.getNetworkDomain());
    accountResponse.setDefaultZone(account.getDataCenterUuid());
    accountResponse.setIsDefault(account.isDefault());
    // get network stat
    accountResponse.setBytesReceived(account.getBytesReceived());
    accountResponse.setBytesSent(account.getBytesSent());
    boolean fullView = (view == ResponseView.Full && _acctMgr.isRootAdmin(account.getId()));
    setResourceLimits(account, fullView, accountResponse);
    //get resource limits for projects
    long projectLimit = ApiDBUtils.findCorrectResourceLimit(account.getProjectLimit(), account.getId(), ResourceType.project);
    String projectLimitDisplay = (fullView || projectLimit == -1) ? "Unlimited" : String.valueOf(projectLimit);
    long projectTotal = (account.getProjectTotal() == null) ? 0 : account.getProjectTotal();
    String projectAvail = (fullView || projectLimit == -1) ? "Unlimited" : String.valueOf(projectLimit - projectTotal);
    accountResponse.setProjectLimit(projectLimitDisplay);
    accountResponse.setProjectTotal(projectTotal);
    accountResponse.setProjectAvailable(projectAvail);
    // set async job
    if (account.getJobId() != null) {
        accountResponse.setJobId(account.getJobUuid());
        accountResponse.setJobStatus(account.getJobStatus());
    }
    // adding all the users for an account as part of the response obj
    List<UserAccountJoinVO> usersForAccount = ApiDBUtils.findUserViewByAccountId(account.getId());
    List<UserResponse> userResponses = ViewResponseHelper.createUserResponse(usersForAccount.toArray(new UserAccountJoinVO[usersForAccount.size()]));
    accountResponse.setUsers(userResponses);
    // set details
    accountResponse.setDetails(ApiDBUtils.getAccountDetails(account.getId()));
    accountResponse.setObjectName("account");
    // add all the acl groups for an account
    accountResponse.setGroups(_acctMgr.listAclGroupsByAccount(account.getId()));
    return accountResponse;
}
Also used : UserResponse(org.apache.cloudstack.api.response.UserResponse) UserAccountJoinVO(com.cloud.api.query.vo.UserAccountJoinVO) AccountResponse(org.apache.cloudstack.api.response.AccountResponse)

Aggregations

UserAccountJoinVO (com.cloud.api.query.vo.UserAccountJoinVO)3 Account (com.cloud.user.Account)2 ArrayList (java.util.ArrayList)2 ProjectAccountJoinVO (com.cloud.api.query.vo.ProjectAccountJoinVO)1 DomainVO (com.cloud.domain.DomainVO)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)1 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)1 Pair (com.cloud.utils.Pair)1 Ternary (com.cloud.utils.Ternary)1 Filter (com.cloud.utils.db.Filter)1 AccountResponse (org.apache.cloudstack.api.response.AccountResponse)1 ProjectAccountResponse (org.apache.cloudstack.api.response.ProjectAccountResponse)1 UserResponse (org.apache.cloudstack.api.response.UserResponse)1