Search in sources :

Example 6 with SearchDirectoryOptions

use of com.zimbra.cs.account.SearchDirectoryOptions in project zm-mailbox by Zimbra.

the class TestProvAlias method searchAliasesInDomain.

private List<NamedEntry> searchAliasesInDomain(Domain domain) throws ServiceException {
    SearchDirectoryOptions options = new SearchDirectoryOptions();
    options.setTypes(SearchDirectoryOptions.ObjectType.aliases);
    options.setDomain(domain);
    options.setFilterString(FilterId.UNITTEST, null);
    return prov.searchDirectory(options);
}
Also used : SearchDirectoryOptions(com.zimbra.cs.account.SearchDirectoryOptions)

Example 7 with SearchDirectoryOptions

use of com.zimbra.cs.account.SearchDirectoryOptions in project zm-mailbox by Zimbra.

the class TestSearchDirectory method searchDirectory.

private List searchDirectory(String key, boolean expectTooMany) throws Exception {
    final String FILTER_1 = "(|(uid=*%n*)(cn=*%n*)(sn=*%n*)(gn=*%n*)(displayName=*%n*)(zimbraId=%n)(mail=*%n*)(zimbraMailAlias=*%n*)(zimbraMailDeliveryAddress=*%n*)(zimbraDomainName=*%n*))";
    final String[] ATTRS = new String[] { "displayName", "zimbraId", "zimbraMailHost", "uid", "zimbraAccountStatus", "zimbraLastLogonTimestamp", "description", "zimbraMailStatus", "zimbraCalResType", "zimbraDomainType", "zimbraDomainName" };
    /*
        int flags = 0;
        flags |= Provisioning.SD_ACCOUNT_FLAG;
        flags |= Provisioning.SD_ALIAS_FLAG;
        flags |= Provisioning.SD_DISTRIBUTION_LIST_FLAG;
        flags |= Provisioning.SD_CALENDAR_RESOURCE_FLAG;
        flags |= Provisioning.SD_DOMAIN_FLAG;
        */
    String query = expandFilter(FILTER_1, key);
    SearchDirectoryOptions options = new SearchDirectoryOptions();
    options.setDomain(null);
    options.setMaxResults(5000);
    options.setFilterString(null, query);
    options.setReturnAttrs(ATTRS);
    options.setSortOpt(SortOpt.SORT_ASCENDING);
    options.setSortAttr("name");
    options.setConvertIDNToAscii(true);
    List results = null;
    boolean good = false;
    try {
        results = prov.searchDirectory(options);
        good = true;
    } catch (AccountServiceException e) {
        if (expectTooMany && e.getCode().equals(AccountServiceException.TOO_MANY_SEARCH_RESULTS)) {
            good = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertTrue(good);
    return results;
}
Also used : SearchDirectoryOptions(com.zimbra.cs.account.SearchDirectoryOptions) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ArrayList(java.util.ArrayList) DistributionList(com.zimbra.cs.account.DistributionList) List(java.util.List) AccountServiceException(com.zimbra.cs.account.AccountServiceException)

Example 8 with SearchDirectoryOptions

use of com.zimbra.cs.account.SearchDirectoryOptions in project zm-mailbox by Zimbra.

the class SearchCalendarResources 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);
    boolean applyCos = request.getAttributeBool(AdminConstants.A_APPLY_COS, true);
    String sortBy = request.getAttribute(AdminConstants.A_SORT_BY, null);
    boolean sortAscending = request.getAttributeBool(AdminConstants.A_SORT_ASCENDING, true);
    String attrsStr = request.getAttribute(AdminConstants.A_ATTRS, null);
    String[] attrs = attrsStr == null ? null : attrsStr.split(",");
    EntrySearchFilter filter = GalExtraSearchFilter.parseSearchFilter(request);
    // Note: isDomainAdminOnly *always* returns false for pure ACL based AccessManager
    if (isDomainAdminOnly(zsc)) {
        if (domain == null) {
            domain = getAuthTokenAccountDomain(zsc).getName();
        } else {
            checkDomainRight(zsc, domain, AdminRight.PR_ALWAYS_ALLOW);
        }
    }
    Domain d = null;
    if (domain != null) {
        d = prov.get(Key.DomainBy.name, domain);
        if (d == null)
            throw AccountServiceException.NO_SUCH_DOMAIN(domain);
    }
    AdminAccessControl aac = AdminAccessControl.getAdminAccessControl(zsc);
    AdminAccessControl.SearchDirectoryRightChecker rightChecker = new AdminAccessControl.SearchDirectoryRightChecker(aac, prov, null);
    // filter is not RFC 2254 escaped
    // query is RFC 2254 escaped
    String query = LdapEntrySearchFilter.toLdapCalendarResourcesFilter(filter);
    SearchDirectoryOptions options = new SearchDirectoryOptions();
    options.setDomain(d);
    options.setTypes(SearchDirectoryOptions.ObjectType.resources);
    options.setFilterString(FilterId.ADMIN_SEARCH, query);
    options.setReturnAttrs(attrs);
    options.setSortOpt(sortAscending ? SortOpt.SORT_ASCENDING : SortOpt.SORT_DESCENDING);
    options.setSortAttr(sortBy);
    options.setConvertIDNToAscii(true);
    List<NamedEntry> resources;
    int limitMax = offset + limit;
    AdminSession session = (AdminSession) getSession(zsc, Session.Type.ADMIN);
    if (session != null) {
        resources = session.searchDirectory(options, offset, rightChecker);
    } else {
        resources = prov.searchDirectory(options);
        resources = rightChecker.getAllowed(resources, limitMax);
    }
    Element response = zsc.createElement(AdminConstants.SEARCH_CALENDAR_RESOURCES_RESPONSE);
    int numEntries;
    for (numEntries = offset; numEntries < limitMax && numEntries < resources.size(); numEntries++) {
        NamedEntry entry = resources.get(numEntries);
        ToXML.encodeCalendarResource(response, (CalendarResource) entry, applyCos, null, aac.getAttrRightChecker(entry));
    }
    response.addAttribute(AdminConstants.A_MORE, numEntries < resources.size());
    response.addAttribute(AdminConstants.A_SEARCH_TOTAL, resources.size());
    return response;
}
Also used : SearchDirectoryOptions(com.zimbra.cs.account.SearchDirectoryOptions) Element(com.zimbra.common.soap.Element) LdapEntrySearchFilter(com.zimbra.cs.account.ldap.LdapEntrySearchFilter) EntrySearchFilter(com.zimbra.cs.account.EntrySearchFilter) Provisioning(com.zimbra.cs.account.Provisioning) NamedEntry(com.zimbra.cs.account.NamedEntry) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) AdminSession(com.zimbra.cs.session.AdminSession) Domain(com.zimbra.cs.account.Domain)

Example 9 with SearchDirectoryOptions

use of com.zimbra.cs.account.SearchDirectoryOptions in project zm-mailbox by Zimbra.

the class TestLdapProvSearchDirectory method maxResults.

@Test
public void maxResults() throws Exception {
    // must be greator than 1
    int NUM_ACCTS = 3;
    List<Account> accts = Lists.newArrayList();
    for (int i = 0; i < NUM_ACCTS; i++) {
        Map<String, Object> attrs = Maps.newHashMap();
        attrs.put(Provisioning.A_displayName, "acct-" + i);
        String ACCT_LOCALPART = Names.makeAccountNameLocalPart("acct-" + i);
        Account acct = createAccount(ACCT_LOCALPART, attrs);
        accts.add(acct);
    }
    String filter = "(uid=*)";
    String sortAttr = Provisioning.A_displayName;
    String[] returnAttrs = new String[] { Provisioning.A_displayName, Provisioning.A_zimbraId, Provisioning.A_zimbraMailHost, Provisioning.A_uid, Provisioning.A_zimbraAccountStatus, Provisioning.A_zimbraIsAdminAccount, Provisioning.A_zimbraMailStatus };
    SearchDirectoryOptions searchOpts = new SearchDirectoryOptions();
    searchOpts.setDomain(domain);
    searchOpts.setTypes(SearchDirectoryOptions.ObjectType.accounts);
    // unlimited
    searchOpts.setMaxResults(0);
    searchOpts.setFilterString(FilterId.UNITTEST, filter);
    searchOpts.setReturnAttrs(returnAttrs);
    searchOpts.setSortOpt(SortOpt.SORT_ASCENDING);
    searchOpts.setSortAttr(sortAttr);
    List<NamedEntry> result = prov.searchDirectory(searchOpts);
    Verify.verifyEquals(accts, result, true);
    searchOpts.setMaxResults(NUM_ACCTS - 1);
    boolean caughtException = false;
    try {
        prov.searchDirectory(searchOpts);
    } catch (ServiceException e) {
        if (AccountServiceException.TOO_MANY_SEARCH_RESULTS.equals(e.getCode())) {
            caughtException = true;
        }
    }
    assertTrue(caughtException);
    for (Account acct : accts) {
        deleteAccount(acct);
    }
}
Also used : Account(com.zimbra.cs.account.Account) SearchDirectoryOptions(com.zimbra.cs.account.SearchDirectoryOptions) NamedEntry(com.zimbra.cs.account.NamedEntry) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) ProvTest(com.zimbra.qa.unittest.prov.ProvTest)

Example 10 with SearchDirectoryOptions

use of com.zimbra.cs.account.SearchDirectoryOptions in project zm-mailbox by Zimbra.

the class TestLdapProvSearchDirectory method searchGrantee.

@Test
public void searchGrantee() throws Exception {
    String granteeId = LdapUtil.generateUUID();
    byte gtype = ACL.GRANTEE_USER;
    SearchDirectoryOptions opts = new SearchDirectoryOptions();
    if (gtype == ACL.GRANTEE_USER) {
        opts.addType(SearchDirectoryOptions.ObjectType.accounts);
        opts.addType(SearchDirectoryOptions.ObjectType.resources);
    } else if (gtype == ACL.GRANTEE_GROUP) {
        opts.addType(SearchDirectoryOptions.ObjectType.distributionlists);
    } else if (gtype == ACL.GRANTEE_COS) {
        opts.addType(SearchDirectoryOptions.ObjectType.coses);
    } else if (gtype == ACL.GRANTEE_DOMAIN) {
        opts.addType(SearchDirectoryOptions.ObjectType.domains);
    } else {
        throw ServiceException.INVALID_REQUEST("invalid grantee type for revokeOrphanGrants", null);
    }
    String query = "(" + Provisioning.A_zimbraId + "=" + granteeId + ")";
    opts.setFilterString(FilterId.SEARCH_GRANTEE, query);
    // search the grantee on LDAP master
    opts.setOnMaster(true);
    Provisioning prov = Provisioning.getInstance();
    List<NamedEntry> entries = prov.searchDirectory(opts);
/*
        // logacy code and LDAP trace
        int flags = 0;
        if (gtype == ACL.GRANTEE_USER)
            flags |= (Provisioning.SD_ACCOUNT_FLAG | Provisioning.SD_CALENDAR_RESOURCE_FLAG) ;
        else if (gtype == ACL.GRANTEE_GROUP)
            flags |= Provisioning.SD_DISTRIBUTION_LIST_FLAG;
        else if (gtype == ACL.GRANTEE_COS)
            flags |= Provisioning.SD_COS_FLAG;
        else if (gtype == ACL.GRANTEE_DOMAIN)
            flags |= Provisioning.SD_DOMAIN_FLAG;
        else
            throw ServiceException.INVALID_REQUEST("invalid grantee type for revokeOrphanGrants", null);

        String query = "(" + Provisioning.A_zimbraId + "=" + granteeId + ")";

        Provisioning.SearchOptions opts = new SearchOptions();
        opts.setFlags(flags);
        opts.setQuery(query);
        opts.setOnMaster(true);  // search the grantee on LDAP master

        Provisioning prov = Provisioning.getInstance();
        List<NamedEntry> entries = prov.searchDirectory(opts);

        Oct  9 23:09:26 pshao-macbookpro-2 slapd[73952]: conn=1531 op=209 SRCH base="" scope=2 deref=0 filter="(&(zimbraId=8b435b63-40c3-4de7-b105-869cbafea29b)(|(objectClass=zimbraAccount)(objectClass=zimbraCalendarResource)))"
        */
/*
         // LDAP trace after refactoring
        Oct  9 23:26:59 pshao-macbookpro-2 slapd[73952]: conn=1535 op=209 SRCH base="" scope=2 deref=0 filter="(&(|(objectClass=zimbraAccount)(objectClass=zimbraCalendarResource))(zimbraId=561fcc6d-6a09-432e-8346-3f1752eea3f9))"

         */
}
Also used : SearchDirectoryOptions(com.zimbra.cs.account.SearchDirectoryOptions) NamedEntry(com.zimbra.cs.account.NamedEntry) Provisioning(com.zimbra.cs.account.Provisioning) ProvTest(com.zimbra.qa.unittest.prov.ProvTest)

Aggregations

SearchDirectoryOptions (com.zimbra.cs.account.SearchDirectoryOptions)30 NamedEntry (com.zimbra.cs.account.NamedEntry)19 Account (com.zimbra.cs.account.Account)11 ProvTest (com.zimbra.qa.unittest.prov.ProvTest)9 Domain (com.zimbra.cs.account.Domain)8 DistributionList (com.zimbra.cs.account.DistributionList)7 LdapDomain (com.zimbra.cs.account.ldap.entry.LdapDomain)5 Provisioning (com.zimbra.cs.account.Provisioning)4 Element (com.zimbra.common.soap.Element)3 AccountServiceException (com.zimbra.cs.account.AccountServiceException)3 DynamicGroup (com.zimbra.cs.account.DynamicGroup)3 AdminSession (com.zimbra.cs.session.AdminSession)3 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 ServiceException (com.zimbra.common.service.ServiceException)2 Alias (com.zimbra.cs.account.Alias)2 CalendarResource (com.zimbra.cs.account.CalendarResource)2 LdapDistributionList (com.zimbra.cs.account.ldap.entry.LdapDistributionList)2