Search in sources :

Example 21 with BwCalendar

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

the class ResourcesImpl method copyMove.

@Override
public boolean copyMove(final BwResource val, final String to, final String name, final boolean copy, final boolean overwrite) throws CalFacadeException {
    try {
        setupSharableEntity(val, getPrincipal().getPrincipalRef());
        final BwCalendar collTo = getCols().get(to);
        if (collTo == null) {
            throw new CalFacadeException(CalFacadeException.collectionNotFound, to);
        }
        if (collTo.getCalType() != BwCalendar.calTypeFolder) {
            // Only allowed into a folder.
            throw new CalFacadeException(CalFacadeException.badRequest, to);
        }
        final int access;
        if (copy) {
            access = PrivilegeDefs.privWrite;
        } else {
            access = PrivilegeDefs.privBind;
        }
        checkAccess(collTo, access, false);
        BwResource r = getCal().getResource(val.getName(), collTo, access);
        boolean createdNew = false;
        getContent(val);
        if (r != null) {
            /* Update of the target from the source */
            if (!overwrite) {
                throw new CalFacadeException(CalFacadeException.targetExists, val.getName());
            }
            getContent(r);
            r.setContentType(val.getContentType());
            final BwResourceContent rc = r.getContent();
            final BwResourceContent toRc = val.getContent();
            r.setContentLength(toRc.getValue().length());
            r.updateLastmod(getCurrentTimestamp());
            rc.setValue(val.getContent().getValue());
            getCal().saveOrUpdate(r);
            getCal().saveOrUpdate(rc);
        } else {
            /* Create a new resource */
            r = new BwResource();
            setupSharableEntity(r, getPrincipal().getPrincipalRef());
            r.setName(name);
            r.setColPath(collTo.getPath());
            r.setContentType(val.getContentType());
            r.setContentLength(val.getContentLength());
            r.updateLastmod(getCurrentTimestamp());
            getCal().saveOrUpdate(r);
            final BwResourceContent fromRc = val.getContent();
            final BwResourceContent rc = new BwResourceContent();
            rc.setColPath(collTo.getPath());
            rc.setName(val.getName());
            rc.setValue(fromRc.getValue());
            getCal().saveOrUpdate(rc);
            createdNew = true;
        }
        if (!copy) {
            // Delete (tombstone) the old one
            final BwCalendar collFrom = getCols().get(val.getColPath());
            checkAccess(collFrom, PrivilegeDefs.privUnbind, false);
            final BwResourceContent rc = val.getContent();
            getCal().delete(rc);
            /* Remove any previous tombstoned version */
            final BwResource tr = getCal().getResource(val.getName() + BwResource.tombstonedSuffix, collFrom, PrivilegeDefs.privUnbind);
            if (tr != null) {
                getCal().delete(tr);
            }
            val.setContent(null);
            val.tombstone();
            val.updateLastmod(getCurrentTimestamp());
            getCal().saveOrUpdate(val);
            touchCalendar(collFrom);
        }
        touchCalendar(collTo);
        return createdNew;
    } catch (final CalFacadeException cfe) {
        getSvc().rollbackTransaction();
        throw cfe;
    } catch (final Throwable t) {
        getSvc().rollbackTransaction();
        throw new CalFacadeException(t);
    }
}
Also used : BwResource(org.bedework.calfacade.BwResource) BwResourceContent(org.bedework.calfacade.BwResourceContent) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 22 with BwCalendar

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

the class ResourcesImpl method save.

private boolean save(final String path, final BwResource val, final boolean forNotification, final boolean returnIfExists) throws CalFacadeException {
    try {
        final BwCalendar coll = getCols().get(path);
        if (coll == null) {
            throw new CalFacadeException(CalFacadeException.collectionNotFound, path);
        }
        if (forNotification) {
            // We allow this for subscription only
            if (coll.getCalType() != BwCalendar.calTypeNotifications) {
                throw new CalFacadeException(CalFacadeException.badRequest, path);
            }
        } else if (getPrincipalInfo().getSubscriptionsOnly()) {
            throw new CalFacadeForbidden("User has read only access");
        }
        final BwResource r = getCal().getResource(val.getName(), coll, PrivilegeDefs.privAny);
        if (r != null) {
            if (returnIfExists) {
                return false;
            }
            throw new CalFacadeException(CalFacadeException.duplicateResource, val.getName());
        }
        final BwResourceContent rc = val.getContent();
        if (rc == null) {
            throw new CalFacadeException(CalFacadeException.missingResourceContent);
        }
        setupSharableEntity(val, getPrincipal().getPrincipalRef());
        val.setColPath(path);
        if ((coll.getCalType() == BwCalendar.calTypeCalendarCollection) || (coll.getCalType() == BwCalendar.calTypeExtSub)) {
            throw new CalFacadeException(CalFacadeException.badRequest, path);
        }
        checkAccess(coll, PrivilegeDefs.privBind, false);
        val.updateLastmod(getCurrentTimestamp());
        getCal().add(val);
        rc.setColPath(val.getColPath());
        rc.setName(val.getName());
        getCal().add(rc);
        touchCalendar(coll);
        return true;
    } catch (final CalFacadeException cfe) {
        getSvc().rollbackTransaction();
        throw cfe;
    }
}
Also used : BwResource(org.bedework.calfacade.BwResource) CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) BwResourceContent(org.bedework.calfacade.BwResourceContent) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 23 with BwCalendar

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

the class RestoreImpl method fixSharee.

@Override
public FixAliasResult fixSharee(final BwCalendar col, final String shareeHref, final AccessType a) throws CalFacadeException {
    /* First ensure this alias is not circular */
    final Set<String> paths = new TreeSet<>();
    BwCalendar curCol = col;
    while (curCol.getInternalAlias()) {
        if (paths.contains(curCol.getPath())) {
            return FixAliasResult.circular;
        }
        paths.add(curCol.getPath());
        try {
            curCol = getCols().resolveAliasIdx(curCol, false, false);
        } catch (final CalFacadeAccessException ignored) {
            // but we can still check for circularity and broken aliases.
            break;
        }
        if (curCol == null) {
            return FixAliasResult.broken;
        }
    }
    // See if we are in the invite list
    final InviteType invite = getSvc().getSharingHandler().getInviteStatus(col);
    final String shareeCua = getSvc().getDirectories().userToCaladdr(shareeHref);
    UserType uentry = invite.finduser(shareeCua);
    if (uentry != null) {
        // Already in list of sharers
        return FixAliasResult.ok;
    }
    /* Now fix the sharing invite info */
    uentry = new UserType();
    uentry.setHref(shareeCua);
    uentry.setInviteStatus(AppleServerTags.inviteAccepted);
    // uentry.setCommonName(...);
    uentry.setAccess(a);
    // uentry.setSummary(s.getSummary());
    invite.getUsers().add(uentry);
    try {
        col.setQproperty(AppleServerTags.invite, invite.toXml());
        getCols().update(col);
    } catch (final CalFacadeException cfe) {
        throw cfe;
    } catch (final Throwable t) {
        throw new CalFacadeException(t);
    }
    return FixAliasResult.reshared;
}
Also used : TreeSet(java.util.TreeSet) InviteType(org.bedework.caldav.util.sharing.InviteType) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeAccessException(org.bedework.calfacade.exc.CalFacadeAccessException) UserType(org.bedework.caldav.util.sharing.UserType) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 24 with BwCalendar

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

the class Sharing method subscribe.

@Override
public SubscribeResult subscribe(final String colPath, final String subscribedName) throws CalFacadeException {
    /* We must have at least read access to the published collection */
    final BwCalendar publishedCol = getCols().get(colPath);
    final SubscribeResult sr = new SubscribeResult();
    if (publishedCol == null) {
        // Bad url?
        throw new CalFacadeForbidden("Bad url or no access");
    }
    if (!publishedCol.getPublick() && (publishedCol.getQproperty(AppleServerTags.publishUrl) == null)) {
        throw new CalFacadeForbidden("Not published");
    }
    /* We may already be subscribed. If so we're done.
     * Otherwise we need to create an alias in the calendar home using the
     * reply summary as the display name */
    final List<BwCalendar> aliases = findAlias(colPath);
    if (!Util.isEmpty(aliases)) {
        sr.path = aliases.get(0).getPath();
        sr.alreadySubscribed = true;
        return sr;
    }
    final BwCalendar alias = new BwCalendar();
    String summary = subscribedName;
    if ((summary == null) || (summary.length() == 0)) {
        summary = "Published Calendar";
    }
    alias.setName(getEncodedUuid());
    alias.setSummary(summary);
    alias.setCalType(BwCalendar.calTypeAlias);
    // alias.setPath(home.getPath() + "/" + UUID.randomUUID().toString());
    alias.setAliasUri("bwcal://" + colPath);
    alias.setShared(true);
    alias.setSharedWritable(false);
    sr.path = getCols().add(alias, getCols().getHome().getPath()).getPath();
    return sr;
}
Also used : CalFacadeForbidden(org.bedework.calfacade.exc.CalFacadeForbidden) BwCalendar(org.bedework.calfacade.BwCalendar)

Example 25 with BwCalendar

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

the class Sharing method setAccess.

private void setAccess(final BwCalendar col, final AddPrincipal ap) throws CalFacadeException {
    try {
        final String whoHref;
        final int whoKind;
        if (ap.pr != null) {
            whoHref = ap.pr.getPrincipalRef();
            whoKind = ap.pr.getKind();
        } else {
            // Read to all
            whoHref = null;
            whoKind = WhoDefs.whoTypeAll;
        }
        Acl acl = col.getCurrentAccess().getAcl();
        final AceWho who = AceWho.getAceWho(whoHref, whoKind, false);
        final Collection<Privilege> desiredPriv;
        if (ap.forRead) {
            desiredPriv = readPrivs;
        } else {
            desiredPriv = readWritePrivs;
        }
        /*
      boolean removeCurrentPrivs = false;

      for (Ace a: ainfo.acl.getAces()) {
        if (a.getWho().equals(who)) {
          if (a.getHow().equals(desiredPriv)) {
            // Already have that access
            return null;
          }

          removeCurrentPrivs = true;
        }
      }

      if (removeCurrentPrivs) {
        ainfo.acl = ainfo.acl.removeWho(who);
      }
      */
        Acl removed = acl.removeWho(who);
        if (removed != null) {
            acl = removed;
        }
        final BwPrincipal owner = getUsers().getPrincipal(col.getOwnerHref());
        final AceWho ownerWho = AceWho.getAceWho(owner.getAccount(), owner.getKind(), false);
        removed = acl.removeWho(ownerWho);
        if (removed != null) {
            acl = removed;
        }
        final Collection<Ace> aces = new ArrayList<>();
        aces.addAll(acl.getAces());
        aces.add(Ace.makeAce(who, desiredPriv, null));
        aces.add(Ace.makeAce(ownerWho, allPrivs, null));
        getSvc().changeAccess(col, aces, true);
        if (!col.getInternalAlias()) {
            return;
        }
        final BwCalendar target = getSvc().getCalendarsHandler().resolveAlias(col, false, false);
        if (target != null) {
            /* Switch identity to the sharee then reget the handler
         * and do the share
         */
            pushPrincipal(target.getOwnerHref());
            try {
                setAccess(target, ap);
            } catch (final CalFacadeException cfe) {
                throw cfe;
            } catch (final Throwable t) {
                throw new CalFacadeException(t);
            } finally {
                popPrincipal();
            }
        }
    } catch (final AccessException ae) {
        throw new CalFacadeException(ae);
    }
}
Also used : Ace(org.bedework.access.Ace) ArrayList(java.util.ArrayList) Acl(org.bedework.access.Acl) BwCalendar(org.bedework.calfacade.BwCalendar) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) BwPrincipal(org.bedework.calfacade.BwPrincipal) AccessException(org.bedework.access.AccessException) AceWho(org.bedework.access.AceWho) Privilege(org.bedework.access.Privilege)

Aggregations

BwCalendar (org.bedework.calfacade.BwCalendar)134 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)46 EventInfo (org.bedework.calfacade.svc.EventInfo)23 BwEvent (org.bedework.calfacade.BwEvent)19 ArrayList (java.util.ArrayList)18 CalFacadeAccessException (org.bedework.calfacade.exc.CalFacadeAccessException)18 BwPrincipal (org.bedework.calfacade.BwPrincipal)15 TreeSet (java.util.TreeSet)10 CoreEventInfo (org.bedework.calcorei.CoreEventInfo)10 BwCategory (org.bedework.calfacade.BwCategory)10 BwResource (org.bedework.calfacade.BwResource)10 WebdavException (org.bedework.webdav.servlet.shared.WebdavException)9 FilterBase (org.bedework.caldav.util.filter.FilterBase)8 BwPreferences (org.bedework.calfacade.svc.BwPreferences)8 BwEventProxy (org.bedework.calfacade.BwEventProxy)7 CalendarWrapper (org.bedework.calfacade.wrappers.CalendarWrapper)7 CurrentAccess (org.bedework.access.Acl.CurrentAccess)6 CalendarsI (org.bedework.calsvci.CalendarsI)6 Acl (org.bedework.access.Acl)5 BwEventAnnotation (org.bedework.calfacade.BwEventAnnotation)5