Search in sources :

Example 66 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class AccountManagerImpl method validateAndUpdateUsernameIfNeeded.

/**
 * Validates the user 'username' if provided. The 'username' cannot be blank (when provided).
 * <ul>
 *  <li> If the 'username' is not provided, we do not update it (setting to null) in the User POJO.
 *  <li> If the 'username' is blank, we throw an {@link InvalidParameterValueException}.
 *  <li> The username must be unique in each domain. Therefore, if there is already another user with the same username, an {@link InvalidParameterValueException} is thrown.
 * </ul>
 */
protected void validateAndUpdateUsernameIfNeeded(UpdateUserCmd updateUserCmd, UserVO user, Account account) {
    String userName = updateUserCmd.getUsername();
    if (userName == null) {
        return;
    }
    if (StringUtils.isBlank(userName)) {
        throw new InvalidParameterValueException("Username cannot be empty.");
    }
    List<UserVO> duplicatedUsers = _userDao.findUsersByName(userName);
    for (UserVO duplicatedUser : duplicatedUsers) {
        if (duplicatedUser.getId() == user.getId()) {
            continue;
        }
        Account duplicatedUserAccountWithUserThatHasTheSameUserName = _accountDao.findById(duplicatedUser.getAccountId());
        if (duplicatedUserAccountWithUserThatHasTheSameUserName.getDomainId() == account.getDomainId()) {
            DomainVO domain = _domainDao.findById(duplicatedUserAccountWithUserThatHasTheSameUserName.getDomainId());
            throw new InvalidParameterValueException(String.format("Username [%s] already exists in domain [id=%s,name=%s]", duplicatedUser.getUsername(), domain.getUuid(), domain.getName()));
        }
    }
    user.setUsername(userName);
}
Also used : DomainVO(com.cloud.domain.DomainVO) VpnUserVO(com.cloud.network.VpnUserVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException)

Example 67 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class UsageServiceImpl method checkDomainAdminAccountAccess.

private void checkDomainAdminAccountAccess(Long accountId, Long domainId) {
    Account account = _accountService.getAccount(accountId);
    boolean matchFound = false;
    if (account.getDomainId() == domainId) {
        matchFound = true;
    } else {
        // Check if the account is in a child domain of this domain admin.
        List<DomainVO> childDomains = _domainDao.findAllChildren(_domainDao.findById(domainId).getPath(), domainId);
        for (DomainVO domainVO : childDomains) {
            if (account.getDomainId() == domainVO.getId()) {
                matchFound = true;
                break;
            }
        }
    }
    if (!matchFound) {
        throw new PermissionDeniedException("Domain admins may only retrieve usage records for accounts in their own domain and child domains.");
    }
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 68 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class DomainManagerImpl method deleteDomain.

@Override
@ActionEvent(eventType = EventTypes.EVENT_DOMAIN_DELETE, eventDescription = "deleting Domain", async = true)
public boolean deleteDomain(long domainId, Boolean cleanup) {
    Account caller = getCaller();
    DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        throw new InvalidParameterValueException("Failed to delete domain " + domainId + ", domain not found");
    } else if (domainId == Domain.ROOT_DOMAIN) {
        throw new PermissionDeniedException("Can't delete ROOT domain");
    }
    _accountMgr.checkAccess(caller, domain);
    return deleteDomain(domain, cleanup);
}
Also used : DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent)

Example 69 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class DomainManagerImpl method cleanupDomain.

protected boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOperationException, ResourceUnavailableException {
    s_logger.debug("Cleaning up domain id=" + domainId);
    boolean success = true;
    DomainVO domainHandle = _domainDao.findById(domainId);
    {
        domainHandle.setState(Domain.State.Inactive);
        _domainDao.update(domainId, domainHandle);
        SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
        sc.addAnd("parent", SearchCriteria.Op.EQ, domainId);
        List<DomainVO> domains = _domainDao.search(sc, null);
        SearchCriteria<DomainVO> sc1 = _domainDao.createSearchCriteria();
        sc1.addAnd("path", SearchCriteria.Op.LIKE, "%" + "replace(" + domainHandle.getPath() + ", '%', '[%]')" + "%");
        List<DomainVO> domainsToBeInactivated = _domainDao.search(sc1, null);
        // update all subdomains to inactive so no accounts/users can be created
        for (DomainVO domain : domainsToBeInactivated) {
            domain.setState(Domain.State.Inactive);
            _domainDao.update(domain.getId(), domain);
        }
        // cleanup sub-domains first
        for (DomainVO domain : domains) {
            success = (success && cleanupDomain(domain.getId(), domain.getAccountId()));
            if (!success) {
                s_logger.warn("Failed to cleanup domain id=" + domain.getId());
            }
        }
    }
    // delete users which will also delete accounts and release resources for those accounts
    SearchCriteria<AccountVO> sc = _accountDao.createSearchCriteria();
    sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
    List<AccountVO> accounts = _accountDao.search(sc, null);
    for (AccountVO account : accounts) {
        if (account.getType() != Account.ACCOUNT_TYPE_PROJECT) {
            s_logger.debug("Deleting account " + account + " as a part of domain id=" + domainId + " cleanup");
            boolean deleteAccount = _accountMgr.deleteAccount(account, CallContext.current().getCallingUserId(), getCaller());
            if (!deleteAccount) {
                s_logger.warn("Failed to cleanup account id=" + account.getId() + " as a part of domain cleanup");
            }
            success = (success && deleteAccount);
        } else {
            ProjectVO project = _projectDao.findByProjectAccountId(account.getId());
            s_logger.debug("Deleting project " + project + " as a part of domain id=" + domainId + " cleanup");
            boolean deleteProject = _projectMgr.deleteProject(getCaller(), CallContext.current().getCallingUserId(), project);
            if (!deleteProject) {
                s_logger.warn("Failed to cleanup project " + project + " as a part of domain cleanup");
            }
            success = (success && deleteProject);
        }
    }
    // delete the domain shared networks
    boolean networksDeleted = true;
    s_logger.debug("Deleting networks for domain id=" + domainId);
    List<Long> networkIds = _networkDomainDao.listNetworkIdsByDomain(domainId);
    CallContext ctx = CallContext.current();
    ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(ctx.getCallingUserId()), ctx.getCallingAccount());
    for (Long networkId : networkIds) {
        s_logger.debug("Deleting network id=" + networkId + " as a part of domain id=" + domainId + " cleanup");
        if (!_networkMgr.destroyNetwork(networkId, context, false)) {
            s_logger.warn("Unable to destroy network id=" + networkId + " as a part of domain id=" + domainId + " cleanup.");
            networksDeleted = false;
        } else {
            s_logger.debug("Network " + networkId + " successfully deleted as a part of domain id=" + domainId + " cleanup.");
        }
    }
    // don't proceed if networks failed to cleanup. The cleanup will be performed for inactive domain once again
    if (!networksDeleted) {
        s_logger.debug("Failed to delete the shared networks as a part of domain id=" + domainId + " clenaup");
        return false;
    }
    // don't remove the domain if there are accounts required cleanup
    boolean deleteDomainSuccess = true;
    List<AccountVO> accountsForCleanup = _accountDao.findCleanupsForRemovedAccounts(domainId);
    if (accountsForCleanup.isEmpty()) {
        // release dedication if any, before deleting the domain
        List<DedicatedResourceVO> dedicatedResources = _dedicatedDao.listByDomainId(domainId);
        if (dedicatedResources != null && !dedicatedResources.isEmpty()) {
            s_logger.debug("Releasing dedicated resources for domain" + domainId);
            for (DedicatedResourceVO dr : dedicatedResources) {
                if (!_dedicatedDao.remove(dr.getId())) {
                    s_logger.warn("Fail to release dedicated resources for domain " + domainId);
                    return false;
                }
            }
        }
        // delete domain
        _messageBus.publish(_name, MESSAGE_PRE_REMOVE_DOMAIN_EVENT, PublishScope.LOCAL, domainHandle);
        deleteDomainSuccess = _domainDao.remove(domainId);
        _messageBus.publish(_name, MESSAGE_REMOVE_DOMAIN_EVENT, PublishScope.LOCAL, domainHandle);
        // Delete resource count and resource limits entries set for this domain (if there are any).
        _resourceCountDao.removeEntriesByOwner(domainId, ResourceOwnerType.Domain);
        _resourceLimitDao.removeEntriesByOwner(domainId, ResourceOwnerType.Domain);
    } else {
        s_logger.debug("Can't delete the domain yet because it has " + accountsForCleanup.size() + "accounts that need a cleanup");
        return false;
    }
    return success && deleteDomainSuccess;
}
Also used : CallContext(org.apache.cloudstack.context.CallContext) ReservationContextImpl(com.cloud.vm.ReservationContextImpl) SearchCriteria(com.cloud.utils.db.SearchCriteria) ProjectVO(com.cloud.projects.ProjectVO) ReservationContext(com.cloud.vm.ReservationContext) DomainVO(com.cloud.domain.DomainVO) List(java.util.List) ArrayList(java.util.ArrayList) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO)

Example 70 with DomainVO

use of com.cloud.domain.DomainVO in project cloudstack by apache.

the class DomainManagerImpl method getDomainChildrenIds.

@Override
public Set<Long> getDomainChildrenIds(String parentDomainPath) {
    Set<Long> childDomains = new HashSet<Long>();
    SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
    sc.addAnd("path", SearchCriteria.Op.LIKE, parentDomainPath + "%");
    List<DomainVO> domains = _domainDao.search(sc, null);
    for (DomainVO domain : domains) {
        childDomains.add(domain.getId());
    }
    return childDomains;
}
Also used : DomainVO(com.cloud.domain.DomainVO) HashSet(java.util.HashSet)

Aggregations

DomainVO (com.cloud.domain.DomainVO)196 Account (com.cloud.user.Account)85 AccountVO (com.cloud.user.AccountVO)64 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)53 DomainDao (com.cloud.domain.dao.DomainDao)30 Field (java.lang.reflect.Field)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)29 SslCertDao (com.cloud.network.dao.SslCertDao)29 AccountManager (com.cloud.user.AccountManager)29 SslCertVO (com.cloud.network.dao.SslCertVO)27 List (java.util.List)26 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)24 Pair (com.cloud.utils.Pair)24 Domain (com.cloud.domain.Domain)23 Filter (com.cloud.utils.db.Filter)23 File (java.io.File)23 IOException (java.io.IOException)23 FileUtils.readFileToString (org.apache.commons.io.FileUtils.readFileToString)23 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)22