Search in sources :

Example 1 with Directories

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

the class CalSvc method getCal.

/* Currently this gets a local calintf only. Later we need to use a par to
   * get calintf from a table.
   */
Calintf getCal() throws CalFacadeException {
    if (cali != null) {
        return cali;
    }
    final long start = System.currentTimeMillis();
    try {
        final long beforeGetIntf = System.currentTimeMillis() - start;
        cali = CalintfFactory.getIntf(CalintfFactory.hibernateClass);
        final long afterGetIntf = System.currentTimeMillis() - start;
        cali.open(pars.getWebMode(), pars.getForRestore(), // Just for the user interactions
        pars.getIndexRebuild());
        postNotification(SysEvent.makeTimedEvent("Login: about to obtain calintf", beforeGetIntf));
        postNotification(SysEvent.makeTimedEvent("Login: calintf obtained", afterGetIntf));
        postNotification(SysEvent.makeTimedEvent("Login: intf opened", System.currentTimeMillis() - start));
        cali.beginTransaction();
        postNotification(SysEvent.makeTimedEvent("Login: transaction started", System.currentTimeMillis() - start));
        String runAsUser = pars.getUser();
        if (pars.getCalSuite() != null) {
            final BwCalSuite cs = cali.getCalSuite(pars.getCalSuite());
            if (cs == null) {
                error("******************************************************");
                error("Unable to fetch calendar suite " + pars.getCalSuite());
                error("Is the database correctly initialised?");
                error("******************************************************");
                throw new CalFacadeException(CalFacadeException.unknownCalsuite, pars.getCalSuite());
            }
            getCalSuitesHandler().set(new BwCalSuiteWrapper(cs));
            /* For administrative use we use the account of the admin group the user
         * is a direct member of
         *
         * For public clients we use the calendar suite owning group.
         */
            if (!pars.getPublicAdmin()) {
                runAsUser = cs.getGroup().getOwnerHref();
            }
        }
        postNotification(SysEvent.makeTimedEvent("Login: before get dirs", System.currentTimeMillis() - start));
        final Directories dir = getDirectories();
        /* Get ourselves a user object */
        String authenticatedUser = pars.getAuthUser();
        if (authenticatedUser != null) {
            final String sv = authenticatedUser;
            if (dir.isPrincipal(authenticatedUser)) {
                authenticatedUser = dir.accountFromPrincipal(authenticatedUser);
            }
            if (authenticatedUser == null) {
                error("Failed with Authenticated user " + sv);
                return null;
            }
            if (authenticatedUser.endsWith("/")) {
                getLogger().warn("Authenticated user " + authenticatedUser + " ends with \"/\"");
            }
        }
        postNotification(SysEvent.makeTimedEvent("Login: before user fetch", System.currentTimeMillis() - start));
        // synchronized (synchlock) {
        final Users users = (Users) getUsersHandler();
        if (runAsUser == null) {
            runAsUser = authenticatedUser;
        }
        BwPrincipal currentPrincipal;
        final BwPrincipal authPrincipal;
        PrivilegeSet maxAllowedPrivs = null;
        boolean subscriptionsOnly = getSystemProperties().getUserSubscriptionsOnly();
        boolean userMapHit = false;
        boolean addingUser = false;
        boolean addingRunAsUser = false;
        if (pars.getForRestore()) {
            authenticated = true;
            currentPrincipal = dir.caladdrToPrincipal(pars.getAuthUser());
            authPrincipal = currentPrincipal;
            subscriptionsOnly = false;
        } else if (authenticatedUser == null) {
            authenticated = false;
            // Unauthenticated use
            currentPrincipal = unauthUsers.get(runAsUser);
            if (currentPrincipal == null) {
                currentPrincipal = users.getUser(runAsUser);
            } else {
                userMapHit = true;
            }
            if (currentPrincipal == null) {
                // XXX Should we set this one up?
                currentPrincipal = BwPrincipal.makeUserPrincipal();
            }
            currentPrincipal.setUnauthenticated(true);
            if (!userMapHit) {
                unauthUsers.put(runAsUser, currentPrincipal);
            }
            authPrincipal = currentPrincipal;
            maxAllowedPrivs = PrivilegeSet.readOnlyPrivileges;
        } else {
            authenticated = true;
            currentPrincipal = unauthUsers.get(authenticatedUser);
            if (currentPrincipal == null) {
                currentPrincipal = users.getUser(authenticatedUser);
            } else {
                userMapHit = true;
            }
            if (currentPrincipal == null) {
                /* Add the user to the database. Presumably this is first logon
             */
                getLogger().debug("Add new user " + authenticatedUser);
                /*
            currentPrincipal = addUser(authenticatedUser);
            if (currentPrincipal == null) {
              error("Failed to find user after adding: " + authenticatedUser);
            }
            */
                currentPrincipal = getFakeUser(authenticatedUser);
                addingUser = true;
            }
            authPrincipal = currentPrincipal;
            if (authenticatedUser.equals(runAsUser)) {
                getLogger().debug("Authenticated user " + authenticatedUser + " logged on");
            } else {
                currentPrincipal = unauthUsers.get(runAsUser);
                if (currentPrincipal == null) {
                    currentPrincipal = users.getUser(runAsUser);
                } else {
                    userMapHit = true;
                }
                if (currentPrincipal == null) {
                    // throw new CalFacadeException("User " + runAsUser + " does not exist.");
                    /* Add the user to the database. Presumably this is first logon
               */
                    getLogger().debug("Add new run-as-user " + runAsUser);
                    // currentPrincipal = addUser(runAsUser);
                    currentPrincipal = getFakeUser(runAsUser);
                    addingRunAsUser = true;
                }
                getLogger().debug("Authenticated user " + authenticatedUser + " logged on - running as " + runAsUser);
            }
            if (!userMapHit && (currentPrincipal != null)) {
                currentPrincipal.setGroups(dir.getAllGroups(currentPrincipal));
                authUsers.put(currentPrincipal.getAccount(), currentPrincipal);
            }
            postNotification(SysEvent.makeTimedEvent("Login: after get Groups", System.currentTimeMillis() - start));
            if (pars.getService()) {
                subscriptionsOnly = false;
            } else {
                final BwPrincipalInfo bwpi = dir.getDirInfo(currentPrincipal);
                currentPrincipal.setPrincipalInfo(bwpi);
                if (pars.getPublicAdmin() || (bwpi != null && bwpi.getHasFullAccess())) {
                    subscriptionsOnly = false;
                }
                postNotification(SysEvent.makeTimedEvent("Login: got Dirinfo", System.currentTimeMillis() - start));
            }
        }
        principalInfo = new SvciPrincipalInfo(this, currentPrincipal, authPrincipal, maxAllowedPrivs, subscriptionsOnly);
        cali.init(pars.getLogId(), configs, principalInfo, null, pars.getPublicAdmin(), pars.getPublicSubmission(), pars.getSessionsless(), pars.getDontKill());
        if (addingUser) {
            // Do the real work of setting up user
            addUser(authenticatedUser);
        }
        if (addingRunAsUser) {
            // Do the real work of setting up user
            addUser(runAsUser);
        }
        if (!currentPrincipal.getUnauthenticated()) {
            if (pars.getService()) {
                postNotification(SysEvent.makePrincipalEvent(SysEvent.SysCode.SERVICE_USER_LOGIN, currentPrincipal, System.currentTimeMillis() - start));
            } else if (!creating) {
                users.logon(currentPrincipal);
                postNotification(SysEvent.makePrincipalEvent(SysEvent.SysCode.USER_LOGIN, currentPrincipal, System.currentTimeMillis() - start));
            }
        } else {
        // If we have a runAsUser it's a public client. Pretend we authenticated
        // WHY?          currentPrincipal.setUnauthenticated(runAsUser == null);
        }
        if (pars.getPublicAdmin() || pars.isGuest()) {
            if (debug) {
                trace("PublicAdmin: " + pars.getPublicAdmin() + " user: " + runAsUser);
            }
        /* We may be running as a different user. The preferences we want to see
           * are those of the user we are running as - i.e. the 'run.as' user
           * not those of the authenticated user.
           * /

          BwCalSuiteWrapper suite = getCalSuitesHandler().get();
          BwPrincipal user;

          if (suite != null) {
            // Use this user
            user = users.getPrincipal(suite.getGroup().getOwnerHref());
          } else if (runAsUser == null) {
            // Unauthenticated CalDAV for example?
            user = currentPrincipal;
          } else {
            // No calendar suite set up

            // XXX This is messy
            if (runAsUser.startsWith("/")) {
              user = users.getPrincipal(runAsUser);
            } else {
              user = users.getUser(runAsUser);
            }
          }

          if (!user.equals(principalInfo.getPrincipal())) {
            user.setGroups(getDirectories().getAllGroups(user));
            user.setPrincipalInfo(getDirectories().getDirInfo(user));
            ((SvciPrincipalInfo)principalInfo).setPrincipal(user);
          }

           */
        }
        return cali;
    // }
    } catch (final CalFacadeException cfe) {
        error(cfe);
        throw cfe;
    } catch (final Throwable t) {
        error(t);
        throw new CalFacadeException(t);
    } finally {
        if (cali != null) {
            cali.endTransaction();
            cali.close();
        // cali.flushAll();
        }
    }
}
Also used : BwCalSuite(org.bedework.calfacade.svc.BwCalSuite) PrivilegeSet(org.bedework.access.PrivilegeSet) BwString(org.bedework.calfacade.BwString) BwCalSuiteWrapper(org.bedework.calfacade.svc.wrappers.BwCalSuiteWrapper) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) Directories(org.bedework.calfacade.ifs.Directories) BwPrincipal(org.bedework.calfacade.BwPrincipal) BwPrincipalInfo(org.bedework.calfacade.BwPrincipalInfo)

Example 2 with Directories

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

the class Events method setScheduleState.

/* Flag this as an attendee scheduling object or an organizer scheduling object
   */
private void setScheduleState(final BwEvent ev, final boolean adding, final boolean schedulingInbox) throws CalFacadeException {
    ev.setOrganizerSchedulingObject(false);
    ev.setAttendeeSchedulingObject(false);
    if ((ev.getEntityType() != IcalDefs.entityTypeEvent) && (ev.getEntityType() != IcalDefs.entityTypeTodo) && (ev.getEntityType() != IcalDefs.entityTypeVpoll)) {
        // Not a possible scheduling entity
        return;
    }
    final BwOrganizer org = ev.getOrganizer();
    final Set<BwAttendee> atts = ev.getAttendees();
    if (Util.isEmpty(atts) || (org == null)) {
        return;
    }
    final String curPrincipal = getSvc().getPrincipal().getPrincipalRef();
    final Directories dirs = getSvc().getDirectories();
    AccessPrincipal evPrincipal = dirs.caladdrToPrincipal(org.getOrganizerUri());
    if ((evPrincipal != null) && (evPrincipal.getPrincipalRef().equals(curPrincipal))) {
        ev.setOrganizerSchedulingObject(true);
        /* If we are expanding groups do so here */
        final ChangeTable chg = ev.getChangeset(getPrincipalHref());
        final Set<BwAttendee> groups = new TreeSet<>();
        if (!schedulingInbox) {
            final ChangeTableEntry cte = chg.getEntry(PropertyInfoIndex.ATTENDEE);
            checkAttendees: for (final BwAttendee att : atts) {
                if (CuType.GROUP.getValue().equals(att.getCuType())) {
                    groups.add(att);
                }
                final AccessPrincipal attPrincipal = getSvc().getDirectories().caladdrToPrincipal(att.getAttendeeUri());
                if ((attPrincipal != null) && (attPrincipal.getPrincipalRef().equals(curPrincipal))) {
                    // It's us
                    continue;
                }
                if (att.getPartstat().equals(IcalDefs.partstatValNeedsAction)) {
                    continue;
                }
                if (adding) {
                    // Can't add an event with attendees set to accepted
                    att.setPartstat(IcalDefs.partstatValNeedsAction);
                    continue;
                }
                // Not adding event. Did we add attendee?
                if ((cte != null) && !Util.isEmpty(cte.getAddedValues())) {
                    for (final Object o : cte.getAddedValues()) {
                        final BwAttendee chgAtt = (BwAttendee) o;
                        if (chgAtt.getCn().equals(att.getCn())) {
                            att.setPartstat(IcalDefs.partstatValNeedsAction);
                            continue checkAttendees;
                        }
                    }
                }
            }
        }
        try {
            /* If this is a vpoll we need the vvoters as we are going to
           have to remove the group vvoter entry and clone it for the
           attendees we add.

           I think this will work for any poll mode - if not we may
           have to rethink this approach.
         */
            Map<String, VVoter> voters = null;
            final boolean vpoll;
            if (ev.getEntityType() == IcalDefs.entityTypeVpoll) {
                voters = IcalUtil.parseVpollVvoters(ev);
                // We'll add them all back
                ev.clearVvoters();
                vpoll = true;
            } else {
                vpoll = false;
            }
            for (final BwAttendee att : groups) {
                /* If the group is in one of our domains we can try to expand it.
           * We should leave it if it's an external id.
           */
                final Holder<Boolean> trunc = new Holder<>();
                final List<BwPrincipalInfo> groupPis = dirs.find(att.getAttendeeUri(), att.getCuType(), // expand
                true, trunc);
                if ((groupPis == null) || (groupPis.size() != 1)) {
                    continue;
                }
                final BwPrincipalInfo pi = groupPis.get(0);
                if (pi.getMembers() == null) {
                    continue;
                }
                VVoter groupVvoter = null;
                Voter groupVoter = null;
                PropertyList pl = null;
                if (vpoll) {
                    groupVvoter = voters.get(att.getAttendeeUri());
                    if (groupVvoter == null) {
                        if (debug) {
                            warn("No vvoter found for " + att.getAttendeeUri());
                        }
                        continue;
                    }
                    voters.remove(att.getAttendeeUri());
                    groupVoter = groupVvoter.getVoter();
                    pl = groupVvoter.getProperties();
                }
                // Remove the group
                ev.removeAttendee(att);
                chg.changed(PropertyInfoIndex.ATTENDEE, att, null);
                for (final BwPrincipalInfo mbrPi : pi.getMembers()) {
                    if (mbrPi.getCaladruri() == null) {
                        continue;
                    }
                    final BwAttendee mbrAtt = new BwAttendee();
                    mbrAtt.setType(att.getType());
                    mbrAtt.setAttendeeUri(mbrPi.getCaladruri());
                    mbrAtt.setCn(mbrPi.getEmail());
                    mbrAtt.setCuType(mbrPi.getKind());
                    mbrAtt.setMember(att.getAttendeeUri());
                    ev.addAttendee(mbrAtt);
                    chg.addValue(PropertyInfoIndex.ATTENDEE, mbrAtt);
                    if (vpoll) {
                        pl.remove(groupVoter);
                        groupVoter = IcalUtil.setVoter(mbrAtt);
                        pl.add(groupVoter);
                        ev.addVvoter(groupVvoter.toString());
                    }
                }
            }
            if (vpoll) {
                // Add back any remaining vvoters
                for (VVoter vv : voters.values()) {
                    ev.addVvoter(vv.toString());
                }
            }
        } catch (final CalFacadeException cfe) {
            throw cfe;
        } catch (final Throwable t) {
            throw new CalFacadeException(t);
        }
        if (ev instanceof BwEventProxy) {
            // Only add x-property to master
            return;
        }
        if (CalFacadeDefs.jasigSchedulingAssistant.equals(getPars().getClientId())) {
            ev.addXproperty(new BwXproperty(BwXproperty.bedeworkSchedAssist, null, "true"));
        }
        return;
    }
    for (final BwAttendee att : atts) {
        /* See if at least one attendee is us */
        evPrincipal = getSvc().getDirectories().caladdrToPrincipal(att.getAttendeeUri());
        if ((evPrincipal != null) && (evPrincipal.getPrincipalRef().equals(curPrincipal))) {
            ev.setAttendeeSchedulingObject(true);
            break;
        }
    }
}
Also used : VVoter(net.fortuna.ical4j.model.component.VVoter) Holder(javax.xml.ws.Holder) BwEventProxy(org.bedework.calfacade.BwEventProxy) AccessPrincipal(org.bedework.access.AccessPrincipal) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) Directories(org.bedework.calfacade.ifs.Directories) PropertyList(net.fortuna.ical4j.model.PropertyList) BwXproperty(org.bedework.calfacade.BwXproperty) ChangeTable(org.bedework.calfacade.util.ChangeTable) TreeSet(java.util.TreeSet) Voter(net.fortuna.ical4j.model.property.Voter) VVoter(net.fortuna.ical4j.model.component.VVoter) ChangeTableEntry(org.bedework.calfacade.util.ChangeTableEntry) BwAttendee(org.bedework.calfacade.BwAttendee) BwPrincipalInfo(org.bedework.calfacade.BwPrincipalInfo) BwOrganizer(org.bedework.calfacade.BwOrganizer)

Example 3 with Directories

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

the class SchedulingBase method findUserAttendee.

/**
 * Find the attendee in this event which corresponds to the current user
 *
 * @param ev
 * @return attendee or null.
 * @throws CalFacadeException
 */
protected BwAttendee findUserAttendee(final BwEvent ev) throws CalFacadeException {
    Directories dir = getSvc().getDirectories();
    String thisPref = getPrincipal().getPrincipalRef();
    for (BwAttendee att : ev.getAttendees()) {
        BwPrincipal p = dir.caladdrToPrincipal(att.getAttendeeUri());
        if (p == null) {
            continue;
        }
        if (thisPref.equals(p.getPrincipalRef())) {
            return att;
        }
    }
    return null;
}
Also used : Directories(org.bedework.calfacade.ifs.Directories) BwPrincipal(org.bedework.calfacade.BwPrincipal) BwAttendee(org.bedework.calfacade.BwAttendee)

Aggregations

Directories (org.bedework.calfacade.ifs.Directories)3 BwAttendee (org.bedework.calfacade.BwAttendee)2 BwPrincipal (org.bedework.calfacade.BwPrincipal)2 BwPrincipalInfo (org.bedework.calfacade.BwPrincipalInfo)2 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 TreeSet (java.util.TreeSet)1 Holder (javax.xml.ws.Holder)1 PropertyList (net.fortuna.ical4j.model.PropertyList)1 VVoter (net.fortuna.ical4j.model.component.VVoter)1 Voter (net.fortuna.ical4j.model.property.Voter)1 AccessPrincipal (org.bedework.access.AccessPrincipal)1 PrivilegeSet (org.bedework.access.PrivilegeSet)1 BwEventProxy (org.bedework.calfacade.BwEventProxy)1 BwOrganizer (org.bedework.calfacade.BwOrganizer)1 BwString (org.bedework.calfacade.BwString)1 BwXproperty (org.bedework.calfacade.BwXproperty)1 BwCalSuite (org.bedework.calfacade.svc.BwCalSuite)1 BwCalSuiteWrapper (org.bedework.calfacade.svc.wrappers.BwCalSuiteWrapper)1 ChangeTable (org.bedework.calfacade.util.ChangeTable)1 ChangeTableEntry (org.bedework.calfacade.util.ChangeTableEntry)1