Search in sources :

Example 51 with BwPrincipal

use of org.bedework.calfacade.BwPrincipal in project bw-calendar-engine by Bedework.

the class Calendars method decomposeVirtualPath.

@Override
public Collection<BwCalendar> decomposeVirtualPath(final String vpath) throws CalFacadeException {
    vpathCalls++;
    if (debug && ((vpathCalls % 250) == 0)) {
        trace("Vpath calls: " + vpathCalls);
        trace(" Vpath hits: " + vpathHits);
        trace(" Vpath size: " + vpathCache.size());
    }
    if ((System.currentTimeMillis() - lastVpathFlush) > vpathFlushPeriod) {
        vpathCache.clear();
        lastVpathFlush = System.currentTimeMillis();
    }
    if (vpathCache.size() > maxVpathCached) {
        info("Flushing vpath cache - reached max size of " + maxVpathCached);
        vpathCache.clear();
    }
    final BwPrincipal pr = getPrincipal();
    final String cacheKey;
    if (pr.getUnauthenticated()) {
        cacheKey = "*|" + vpath;
    } else {
        cacheKey = pr.getPrincipalRef() + "|" + vpath;
    }
    List<BwCalendar> cols = vpathCache.get(cacheKey);
    if (cols != null) {
        vpathHits++;
        return cols;
    }
    cols = new ArrayList<>();
    /* See if the vpath is an actual path - generally the case for
     * personal calendar users.
     */
    BwCalendar startCol = getIdx(vpath);
    if ((startCol != null) && !startCol.getAlias()) {
        cols.add(startCol);
        vpathCache.put(cacheKey, cols);
        return cols;
    }
    final String[] pathEls = normalizeUri(vpath).split("/");
    if (pathEls.length == 0) {
        vpathCache.put(cacheKey, cols);
        return cols;
    }
    /* First, keep adding elements until we get a BwCalendar result.
     * This handles the user root not being accessible
     */
    startCol = null;
    String startPath = "";
    // Element 0 is a zero length string
    int pathi = 1;
    while (pathi < pathEls.length) {
        startPath = Util.buildPath(colPathEndsWithSlash, startPath, "/", pathEls[pathi]);
        pathi++;
        try {
            startCol = getIdx(startPath);
        } catch (final CalFacadeAccessException cfae) {
            startCol = null;
        }
        if (startCol != null) {
            // Found the start collection
            if (debug) {
                debug("Start vpath collection:" + startCol.getPath());
            }
            break;
        }
    }
    if (startCol == null) {
        // Bad vpath
        return null;
    }
    BwCalendar curCol = startCol;
    buildCollection: for (; ; ) {
        cols.add(curCol);
        if (debug) {
            debug("      vpath collection:" + curCol.getPath());
        }
        // Follow the chain of references for curCol until we reach a non-alias
        if (curCol.getInternalAlias()) {
            final BwCalendar nextCol = resolveAliasIdx(curCol, false, false);
            if (nextCol == null) {
                // Bad vpath
                curCol.setDisabled(true);
                curCol.setLastRefreshStatus("400");
                return null;
            }
            curCol = nextCol;
            continue buildCollection;
        }
        /* Not an alias - do we have any more path elements
       */
        if (pathi >= pathEls.length) {
            break buildCollection;
        }
        if (curCol.getCalType() != BwCalendar.calTypeFolder) {
            // Bad vpath
            return null;
        }
        /*
      for (BwCalendar col: getChildren(curCol)) {
        if (col.getName().equals(pathEls[pathi])) {
          // Found our child
          pathi++;
          curCol = col;
          continue buildCollection;
        }
      }
      */
        final BwCalendar col = getIdx(Util.buildPath(colPathEndsWithSlash, curCol.getPath(), "/", pathEls[pathi]));
        if (col == null) {
            /* Child not found - bad vpath */
            return null;
        }
        pathi++;
        curCol = col;
    }
    curCol.setAliasOrigin(startCol);
    vpathCache.put(cacheKey, cols);
    return cols;
}
Also used : BwPrincipal(org.bedework.calfacade.BwPrincipal) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeAccessException(org.bedework.calfacade.exc.CalFacadeAccessException)

Example 52 with BwPrincipal

use of org.bedework.calfacade.BwPrincipal in project bw-calendar-engine by Bedework.

the class EventPropertiesImpl method reindex.

@Override
public int reindex(BwIndexer indexer) throws CalFacadeException {
    BwPrincipal owner;
    if (!isPublicAdmin()) {
        owner = getPrincipal();
    } else {
        owner = getPublicUser();
    }
    Collection<T> ents = coreHdlr.getAll(owner.getPrincipalRef());
    if (Util.isEmpty(ents)) {
        return 0;
    }
    for (T ent : ents) {
        indexer.indexEntity(ent);
    }
    return ents.size();
}
Also used : BwPrincipal(org.bedework.calfacade.BwPrincipal)

Example 53 with BwPrincipal

use of org.bedework.calfacade.BwPrincipal in project bw-calendar-engine by Bedework.

the class OutboundSchedulingHandler method getInbox.

/* Return with deferred for external user.
   *
   * For an internal user - skips it if it's ourself - we don't want
   * our own message in our inbox. Otherwise checks that we have access
   * to send the message. If so sets the path of the inbox.
   */
private UserInbox getInbox(final ScheduleResult sr, final String recipient, final boolean freeBusyRequest) throws CalFacadeException {
    UserInbox ui = (UserInbox) sr.recipientResults.get(recipient);
    if (ui != null) {
        return ui;
    }
    ui = new UserInbox(recipient);
    sr.addRecipientResult(ui);
    final BwPrincipal principal = getSvc().getDirectories().caladdrToPrincipal(recipient);
    if (principal == null) {
        /* External to the system */
        ui.setHost(BwHosts.getHostForRecipient(recipient));
        final Host hi = ui.getHost();
        if (hi == null) {
            ui.setStatus(ScheduleStates.scheduleDeferred);
            return ui;
        }
        if (freeBusyRequest) {
            // All can handle that
            return ui;
        }
        if (!hi.getSupportsISchedule() && !hi.getSupportsCaldav() && !hi.getSupportsBedework()) {
            ui.setStatus(ScheduleStates.scheduleDeferred);
        }
        return ui;
    }
    try {
        if (principal.getPrincipalRef().equals(getPrincipal().getPrincipalRef())) {
            /* This is our own account. Let's not add it to our inbox.
         */
            ui.principal = getPrincipal();
            ui.setStatus(ScheduleStates.scheduleUnprocessed);
            return ui;
        }
        ui.principal = principal;
        final int priv;
        if (freeBusyRequest) {
            priv = PrivilegeDefs.privScheduleFreeBusy;
        } else {
            priv = PrivilegeDefs.privScheduleRequest;
        }
        final BwCalendar inbox = getSpecialCalendar(ui.principal, BwCalendar.calTypePendingInbox, true, priv);
        if (inbox == null) {
            ui.setStatus(ScheduleStates.scheduleNoAccess);
        } else {
            ui.inboxPath = inbox.getPath();
        }
    } catch (CalFacadeAccessException cae) {
        ui.setStatus(ScheduleStates.scheduleNoAccess);
    }
    return ui;
}
Also used : BwPrincipal(org.bedework.calfacade.BwPrincipal) Host(org.bedework.caldav.server.sysinterface.Host) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeAccessException(org.bedework.calfacade.exc.CalFacadeAccessException)

Example 54 with BwPrincipal

use of org.bedework.calfacade.BwPrincipal in project bw-calendar-engine by Bedework.

the class AbstractDirImpl method userToCaladdr.

/* (non-Javadoc)
   * @see org.bedework.calfacade.ifs.Directories#userToCaladdr(java.lang.String)
   */
@Override
public String userToCaladdr(final String val) throws CalFacadeException {
    /* Override this to do directory lookups or query vcard. The following
     * transforms may be insufficient
     */
    String ca = userToCalAddrMap.get(val);
    if (ca != null) {
        return ca;
    }
    if (isPrincipal(val)) {
        BwPrincipal p = getPrincipal(val);
        if (p.getKind() == WhoDefs.whoTypeUser) {
            ca = userToCaladdr(p.getAccount());
        } else {
            // Can't do anything with groups etc.
            ca = val;
        }
        userToCalAddrMap.put(val, ca);
        return ca;
    }
    // Ensure all set up
    getProps();
    try {
        int atPos = val.indexOf("@");
        boolean hasMailto = val.toLowerCase().startsWith("mailto:");
        if (atPos > 0) {
            if (hasMailto) {
                // ensure lower case (helps with some tests)
                return "mailto:" + val.substring(7);
            }
            ca = "mailto:" + val;
            userToCalAddrMap.put(val, ca);
            return ca;
        }
        StringBuilder sb = new StringBuilder();
        if (!hasMailto) {
            sb.append("mailto:");
        }
        sb.append(val);
        sb.append("@");
        sb.append(getDefaultDomain());
        ca = sb.toString();
        userToCalAddrMap.put(val, ca);
        return ca;
    } catch (CalFacadeException cfe) {
        throw cfe;
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : BwPrincipal(org.bedework.calfacade.BwPrincipal) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 55 with BwPrincipal

use of org.bedework.calfacade.BwPrincipal in project bw-calendar-engine by Bedework.

the class UserGroupsLdapImpl method getGroupMembers.

/* Find members for given group
   *
   */
private void getGroupMembers(final DirConfigProperties dirProps, final BwGroup group) throws CalFacadeException {
    LdapConfigProperties props = (LdapConfigProperties) dirProps;
    InitialLdapContext ctx = null;
    try {
        ctx = createLdapInitContext(props);
        BasicAttributes matchAttrs = new BasicAttributes(true);
        matchAttrs.put(props.getGroupIdAttr(), group.getAccount());
        String[] memberAttr = { props.getGroupMemberAttr() };
        ArrayList<String> mbrs = null;
        boolean beenHere = false;
        NamingEnumeration response = ctx.search(props.getGroupContextDn(), matchAttrs, memberAttr);
        while (response.hasMore()) {
            SearchResult sr = (SearchResult) response.next();
            Attributes attrs = sr.getAttributes();
            if (beenHere) {
                throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
            }
            beenHere = true;
            Attribute membersAttr = attrs.get(props.getGroupMemberAttr());
            mbrs = new ArrayList<String>();
            for (int m = 0; m < membersAttr.size(); m++) {
                mbrs.add(membersAttr.get(m).toString());
            }
        }
        // LDAP We need a way to search recursively for groups.
        /* Search for each user in the group */
        String memberContext = props.getGroupMemberContextDn();
        String memberSearchAttr = props.getGroupMemberSearchAttr();
        String[] idAttr = { props.getGroupMemberUserIdAttr(), props.getGroupMemberGroupIdAttr(), "objectclass" };
        for (String mbr : mbrs) {
            if (memberContext != null) {
                matchAttrs = new BasicAttributes(true);
                matchAttrs.put(memberSearchAttr, mbr);
                response = ctx.search(memberContext, matchAttrs, idAttr);
            } else {
                response = ctx.search(memberContext, null, idAttr);
            }
            if (response.hasMore()) {
                SearchResult sr = (SearchResult) response.next();
                Attributes attrs = sr.getAttributes();
                Attribute ocsAttr = attrs.get("objectclass");
                String userOc = props.getUserObjectClass();
                String groupOc = props.getGroupObjectClass();
                boolean isGroup = false;
                for (int oci = 0; oci < ocsAttr.size(); oci++) {
                    String oc = ocsAttr.get(oci).toString();
                    if (userOc.equals(oc)) {
                        break;
                    }
                    if (groupOc.equals(oc)) {
                        isGroup = true;
                        break;
                    }
                }
                BwPrincipal p = null;
                Attribute attr;
                if (isGroup) {
                    p = BwPrincipal.makeGroupPrincipal();
                    attr = attrs.get(props.getGroupMemberGroupIdAttr());
                } else {
                    p = BwPrincipal.makeUserPrincipal();
                    attr = attrs.get(props.getGroupMemberUserIdAttr());
                }
                if (attr.size() != 1) {
                    throw new CalFacadeException("org.bedework.ldap.groups.multiple.result");
                }
                p.setAccount(attr.get(0).toString());
                p.setPrincipalRef(makePrincipalUri(p.getAccount(), p.getKind()));
                group.addGroupMember(p);
            }
        }
    } catch (Throwable t) {
        if (debug) {
            error(t);
        }
        throw new CalFacadeException(t);
    } finally {
        // Close the context to release the connection
        if (ctx != null) {
            closeContext(ctx);
        }
    }
    for (BwGroup g : group.getGroups()) {
        getGroupMembers(props, g);
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) BwGroup(org.bedework.calfacade.BwGroup) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BwPrincipal(org.bedework.calfacade.BwPrincipal) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapConfigProperties(org.bedework.calfacade.configs.LdapConfigProperties)

Aggregations

BwPrincipal (org.bedework.calfacade.BwPrincipal)59 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)22 BwCalendar (org.bedework.calfacade.BwCalendar)16 BwAdminGroup (org.bedework.calfacade.svc.BwAdminGroup)10 EventInfo (org.bedework.calfacade.svc.EventInfo)9 BwEvent (org.bedework.calfacade.BwEvent)7 ArrayList (java.util.ArrayList)6 BwPreferences (org.bedework.calfacade.svc.BwPreferences)6 Acl (org.bedework.access.Acl)5 BwGroup (org.bedework.calfacade.BwGroup)4 Component (net.fortuna.ical4j.model.Component)3 AccessException (org.bedework.access.AccessException)3 Ace (org.bedework.access.Ace)3 Privilege (org.bedework.access.Privilege)3 BwOrganizer (org.bedework.calfacade.BwOrganizer)3 WebdavException (org.bedework.webdav.servlet.shared.WebdavException)3 File (java.io.File)2 Collection (java.util.Collection)2 NamingEnumeration (javax.naming.NamingEnumeration)2 Attribute (javax.naming.directory.Attribute)2