Search in sources :

Example 11 with AdminSession

use of com.zimbra.cs.session.AdminSession in project zm-mailbox by Zimbra.

the class GetQuotaUsage method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    int limit = (int) request.getAttributeLong(AdminConstants.A_LIMIT, Integer.MAX_VALUE);
    if (limit == 0)
        limit = Integer.MAX_VALUE;
    int offset = (int) request.getAttributeLong(AdminConstants.A_OFFSET, 0);
    String domain = request.getAttribute(AdminConstants.A_DOMAIN, null);
    String sortBy = request.getAttribute(AdminConstants.A_SORT_BY, SORT_TOTAL_USED);
    boolean sortAscending = request.getAttributeBool(AdminConstants.A_SORT_ASCENDING, false);
    boolean refresh = request.getAttributeBool(AdminConstants.A_REFRESH, false);
    if (!(sortBy.equals(SORT_TOTAL_USED) || sortBy.equals(SORT_PERCENT_USED) || sortBy.equals(SORT_QUOTA_LIMIT) || sortBy.equals(SORT_ACCOUNT)))
        throw ServiceException.INVALID_REQUEST("sortBy must be percentUsed or totalUsed", null);
    // Note: isDomainAdminOnly *always* returns false for pure ACL based AccessManager
    if (isDomainAdminOnly(zsc)) {
        // need a domain, if domain is not specified, use the authed admins own domain.
        if (domain == null)
            domain = getAuthTokenAccountDomain(zsc).getName();
        // sanity check
        if (domain == null)
            throw AccountServiceException.INVALID_REQUEST("no domain", null);
    }
    Domain d = null;
    if (domain != null) {
        d = prov.get(Key.DomainBy.name, domain);
        if (d == null)
            throw AccountServiceException.NO_SUCH_DOMAIN(domain);
    }
    // if we don't have a domain, only allow system admin
    if (d != null)
        checkDomainRight(zsc, d, Admin.R_getDomainQuotaUsage);
    else
        checkRight(zsc, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
    boolean allServers = d != null && request.getAttributeBool(AdminConstants.A_ALL_SERVERS, false);
    List<AccountQuota> quotas = null;
    QuotaUsageParams params = new QuotaUsageParams(d, sortBy, sortAscending);
    AdminSession session = (AdminSession) getSession(zsc, Session.Type.ADMIN);
    if (session != null) {
        QuotaUsageParams cachedParams = getCachedQuotaUsage(session, allServers);
        if (cachedParams != null && cachedParams.equals(params) && !refresh) {
            quotas = cachedParams.getResult();
        }
    }
    if (quotas == null) {
        if (allServers) {
            quotas = delegateRequestToAllServers(request.clone(), zsc.getRawAuthToken(), sortBy, sortAscending, prov);
            // explicitly set the result
            params.setResult(quotas);
        } else {
            quotas = params.doSearch();
        }
        if (session != null) {
            setCachedQuotaUsage(session, params, allServers);
        }
    }
    Element response = zsc.createElement(AdminConstants.GET_QUOTA_USAGE_RESPONSE);
    int i, limitMax = offset + limit;
    for (i = offset; i < limitMax && i < quotas.size(); i++) {
        AccountQuota quota = quotas.get(i);
        Element account = response.addElement(AdminConstants.E_ACCOUNT);
        account.addAttribute(AdminConstants.A_NAME, quota.name);
        account.addAttribute(AdminConstants.A_ID, quota.id);
        account.addAttribute(AdminConstants.A_QUOTA_USED, quota.quotaUsed);
        account.addAttribute(AdminConstants.A_QUOTA_LIMIT, quota.quotaLimit);
    }
    response.addAttribute(AdminConstants.A_MORE, i < quotas.size());
    response.addAttribute(AdminConstants.A_SEARCH_TOTAL, quotas.size());
    return response;
}
Also used : ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) AdminSession(com.zimbra.cs.session.AdminSession) Element(com.zimbra.common.soap.Element) Domain(com.zimbra.cs.account.Domain) Provisioning(com.zimbra.cs.account.Provisioning)

Aggregations

AdminSession (com.zimbra.cs.session.AdminSession)11 Element (com.zimbra.common.soap.Element)9 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)9 Provisioning (com.zimbra.cs.account.Provisioning)7 Domain (com.zimbra.cs.account.Domain)5 NamedEntry (com.zimbra.cs.account.NamedEntry)3 SearchDirectoryOptions (com.zimbra.cs.account.SearchDirectoryOptions)3 Server (com.zimbra.cs.account.Server)3 Account (com.zimbra.cs.account.Account)2 Session (com.zimbra.cs.session.Session)2 HashSet (java.util.HashSet)2 DomainBy (com.zimbra.common.account.Key.DomainBy)1 ServiceException (com.zimbra.common.service.ServiceException)1 AccountServiceException (com.zimbra.cs.account.AccountServiceException)1 AuthToken (com.zimbra.cs.account.AuthToken)1 EntrySearchFilter (com.zimbra.cs.account.EntrySearchFilter)1 LdapEntrySearchFilter (com.zimbra.cs.account.ldap.LdapEntrySearchFilter)1 SoapProvisioning (com.zimbra.cs.account.soap.SoapProvisioning)1 DbMailbox (com.zimbra.cs.db.DbMailbox)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1