Search in sources :

Example 16 with NamedEntry

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

the class TestLdapProvSearchDirectory method dnSubtreeMatchFilter.

@Test
public void dnSubtreeMatchFilter() throws Exception {
    String SUB_DOMAIN_BASE = genDomainSegmentName() + "." + baseDomainName();
    String SUB_DOMAIN_NAME = "sub." + SUB_DOMAIN_BASE;
    Domain subDomain = provUtil.createDomain(SUB_DOMAIN_NAME);
    String SUB_SUB_DOMAIN_NAME = "sub." + SUB_DOMAIN_NAME;
    Domain subSubDomain = provUtil.createDomain(SUB_SUB_DOMAIN_NAME);
    // create objects in subDomain
    Account acct = provUtil.createAccount(genAcctNameLocalPart("acct"), subDomain);
    DistributionList dl = provUtil.createDistributionList(genGroupNameLocalPart("dl"), subDomain);
    DynamicGroup dg = provUtil.createDynamicGroup(genGroupNameLocalPart("dg"), subDomain);
    // create objects in subSubDomain
    Account acctSub = provUtil.createAccount(genAcctNameLocalPart("acct"), subSubDomain);
    DistributionList dlSub = provUtil.createDistributionList(genGroupNameLocalPart("dl"), subSubDomain);
    DynamicGroup dgSub = provUtil.createDynamicGroup(genGroupNameLocalPart("dg"), subSubDomain);
    SearchDirectoryOptions options;
    List<NamedEntry> entries;
    try {
        SKIP_FOR_INMEM_LDAP_SERVER(SkipTestReason.DN_SUBTREE_MATCH_FILTER);
        // do not specify a domain, so DnSubtreeMatchFilter won't be appened again in Ldapprovisioning
        options = new SearchDirectoryOptions();
        options.setTypes(ObjectType.accounts, ObjectType.distributionlists, ObjectType.dynamicgroups);
        options.setSortOpt(SortOpt.SORT_ASCENDING);
        options.setFilterString(FilterId.UNITTEST, ((LdapDomain) subDomain).getDnSubtreeMatchFilter().toFilterString());
        entries = prov.searchDirectory(options);
        Verify.verifyEquals(Lists.newArrayList(acct, dg, dl), entries, true);
    } catch (ProvTest.SkippedForInMemLdapServerException e) {
    }
    // specify a domain, search for accounts, distribution lists, and dynamic groups
    options = new SearchDirectoryOptions(subDomain);
    options.setTypes(ObjectType.accounts, ObjectType.distributionlists, ObjectType.dynamicgroups);
    options.setSortOpt(SortOpt.SORT_ASCENDING);
    options.setFilterString(FilterId.UNITTEST, null);
    entries = prov.searchDirectory(options);
    Verify.verifyEquals(Lists.newArrayList(acct, dg, dl), entries, true);
    // specify a domain, search for accounts, distribution lists
    options = new SearchDirectoryOptions(subDomain);
    options.setTypes(ObjectType.accounts, ObjectType.distributionlists);
    options.setSortOpt(SortOpt.SORT_ASCENDING);
    options.setFilterString(FilterId.UNITTEST, null);
    entries = prov.searchDirectory(options);
    Verify.verifyEquals(Lists.newArrayList(acct, dl), entries, true);
    // specify a domain, search for dynamic groups
    options = new SearchDirectoryOptions(subDomain);
    options.setTypes(ObjectType.dynamicgroups);
    options.setSortOpt(SortOpt.SORT_ASCENDING);
    options.setFilterString(FilterId.UNITTEST, null);
    entries = prov.searchDirectory(options);
    Verify.verifyEquals(Lists.newArrayList(dg), entries, true);
    // specify a domain, search for accounts and dynamic groups
    options = new SearchDirectoryOptions(subDomain);
    options.setTypes(ObjectType.accounts, ObjectType.dynamicgroups);
    options.setSortOpt(SortOpt.SORT_ASCENDING);
    options.setFilterString(FilterId.UNITTEST, null);
    entries = prov.searchDirectory(options);
    Verify.verifyEquals(Lists.newArrayList(acct, dg), entries, true);
    // cleanup
    deleteAccount(acct);
    deleteGroup(dl);
    deleteGroup(dg);
    deleteAccount(acctSub);
    deleteGroup(dlSub);
    deleteGroup(dgSub);
    deleteDomain(subSubDomain);
    deleteDomain(subDomain);
}
Also used : Account(com.zimbra.cs.account.Account) DynamicGroup(com.zimbra.cs.account.DynamicGroup) SearchDirectoryOptions(com.zimbra.cs.account.SearchDirectoryOptions) NamedEntry(com.zimbra.cs.account.NamedEntry) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) Domain(com.zimbra.cs.account.Domain) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) DistributionList(com.zimbra.cs.account.DistributionList) ProvTest(com.zimbra.qa.unittest.prov.ProvTest) ProvTest(com.zimbra.qa.unittest.prov.ProvTest)

Example 17 with NamedEntry

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

the class TestLdapProvSearchDirectory method accountsByGrants.

@Test
public void accountsByGrants() throws Exception {
    String GRANTEE_ID_1 = LdapUtil.generateUUID();
    String GRANTEE_ID_2 = LdapUtil.generateUUID();
    String GRANTEE_ID_3 = LdapUtil.generateUUID();
    List<String> GRANTEE_IDS = Lists.newArrayList(GRANTEE_ID_1, GRANTEE_ID_2, GRANTEE_ID_3);
    Map<String, Object> attrs1 = Maps.newHashMap();
    attrs1.put(Provisioning.A_zimbraSharedItem, "granteeId:" + GRANTEE_ID_3 + "blah blah");
    Account acct1 = createAccount(genAcctNameLocalPart("1"), attrs1);
    Map<String, Object> attrs2 = Maps.newHashMap();
    attrs2.put(Provisioning.A_zimbraSharedItem, "blah" + "granteeType:pub" + " blah");
    Account acct2 = createAccount(genAcctNameLocalPart("2"), attrs2);
    Map<String, Object> attrs3 = Maps.newHashMap();
    attrs3.put(Provisioning.A_zimbraSharedItem, "blah" + "granteeType:all" + " blah");
    Account acct3 = createAccount(genAcctNameLocalPart("3"), attrs3);
    SearchAccountsOptions searchOpts = new SearchAccountsOptions(new String[] { Provisioning.A_zimbraId, Provisioning.A_displayName, Provisioning.A_zimbraSharedItem });
    ZLdapFilter filter = ZLdapFilterFactory.getInstance().accountsByGrants(GRANTEE_IDS, true, false);
    searchOpts.setFilter(filter);
    // so our assertion below will always work
    searchOpts.setSortOpt(SortOpt.SORT_ASCENDING);
    List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
    Verify.verifyEquals(Lists.newArrayList(acct1, acct2), accounts, true);
    deleteAccount(acct1);
    deleteAccount(acct2);
    deleteAccount(acct3);
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) Account(com.zimbra.cs.account.Account) NamedEntry(com.zimbra.cs.account.NamedEntry) SearchAccountsOptions(com.zimbra.cs.account.SearchAccountsOptions) ProvTest(com.zimbra.qa.unittest.prov.ProvTest)

Example 18 with NamedEntry

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

the class TestLdapProvSearchDirectory method getAllAccounts.

@Test
public void getAllAccounts() throws Exception {
    Account acct3 = createAccount(genAcctNameLocalPart("3"));
    Account acct2 = createAccount(genAcctNameLocalPart("2"));
    Account acct1 = createAccount(genAcctNameLocalPart("1"));
    CalendarResource cr = createCalendarResource(genAcctNameLocalPart("cr"));
    List<NamedEntry> accounts = prov.getAllAccounts(domain);
    Verify.verifyEquals(Lists.newArrayList(acct1, acct2, acct3), accounts, true);
    // test the visitor interface, sorting is not supported with visitor interface
    final List<NamedEntry> acctsByVisitor = Lists.newArrayList();
    NamedEntry.Visitor visitor = new NamedEntry.Visitor() {

        @Override
        public void visit(NamedEntry entry) throws ServiceException {
            acctsByVisitor.add(entry);
        }
    };
    prov.getAllAccounts(domain, visitor);
    Verify.verifyEquals(Lists.newArrayList(acct1, acct2, acct3), accounts, false);
    deleteAccount(acct1);
    deleteAccount(acct2);
    deleteAccount(acct3);
    deleteAccount(cr);
}
Also used : Account(com.zimbra.cs.account.Account) NamedEntry(com.zimbra.cs.account.NamedEntry) CalendarResource(com.zimbra.cs.account.CalendarResource) ProvTest(com.zimbra.qa.unittest.prov.ProvTest)

Example 19 with NamedEntry

use of com.zimbra.cs.account.NamedEntry 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)

Example 20 with NamedEntry

use of com.zimbra.cs.account.NamedEntry 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)

Aggregations

NamedEntry (com.zimbra.cs.account.NamedEntry)109 Account (com.zimbra.cs.account.Account)51 ServiceException (com.zimbra.common.service.ServiceException)24 Domain (com.zimbra.cs.account.Domain)24 Provisioning (com.zimbra.cs.account.Provisioning)23 DistributionList (com.zimbra.cs.account.DistributionList)19 SearchDirectoryOptions (com.zimbra.cs.account.SearchDirectoryOptions)19 ProvTest (com.zimbra.qa.unittest.prov.ProvTest)19 AccountServiceException (com.zimbra.cs.account.AccountServiceException)18 HashSet (java.util.HashSet)17 Entry (com.zimbra.cs.account.Entry)15 HashMap (java.util.HashMap)15 Element (com.zimbra.common.soap.Element)14 Group (com.zimbra.cs.account.Group)14 SearchAccountsOptions (com.zimbra.cs.account.SearchAccountsOptions)12 GuestAccount (com.zimbra.cs.account.GuestAccount)9 MailTarget (com.zimbra.cs.account.MailTarget)8 LdapDomain (com.zimbra.cs.account.ldap.entry.LdapDomain)8 ArrayList (java.util.ArrayList)8 AccessManager (com.zimbra.cs.account.AccessManager)7