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);
}
}
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;
}
}
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;
}
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;
}
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);
}
}
Aggregations