private void assertBelongsToUser(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxNotOwnedException { if (!mailboxPath.belongsTo(mailboxSession)) { throw new MailboxNotOwnedException(); } }
private boolean belongsToCurrentUser(Mailbox mailbox, MailboxSession session) { return mailbox.generateAssociatedPath().belongsTo(session); }
private void assertOwned(MailboxSession mailboxSession, MessageManager parent) throws MailboxException { if (!parent.getMailboxPath().belongsTo(mailboxSession)) { throw new MailboxNotOwnedException(); } }
private boolean allMailboxOwned(List<MailboxId> mailboxIds, MailboxSession session) { FunctionChainer<MailboxId, MessageManager> findMailbox = Throwing.function(mailboxId -> mailboxManager.getMailbox(mailboxId, session)); return mailboxIds.stream() .map(findMailbox.sneakyThrow()) .map(Throwing.function(MessageManager::getMailboxPath)) .allMatch(path -> path.belongsTo(session)); }
private void assertIsOwner(MailboxSession mailboxSession, MailboxPath mailboxPath) throws MailboxNotFoundException { if (!mailboxPath.belongsTo(mailboxSession)) { LOGGER.info("Mailbox {} does not belong to {}", mailboxPath.asString(), mailboxSession.getUser().getUserName()); throw new MailboxNotFoundException(mailboxPath.asString()); } }
/** * ACL is sensible information and as such we should expose as few information as possible * to users. This method allows to filter a {@link MailboxACL} in order to present it to * the connected user. */ @VisibleForTesting static MailboxACL filteredForSession(Mailbox mailbox, MailboxACL acl, MailboxSession mailboxSession) throws UnsupportedRightException { if (mailbox.generateAssociatedPath().belongsTo(mailboxSession)) { return acl; } MailboxACL.EntryKey userAsKey = MailboxACL.EntryKey.createUserEntryKey(mailboxSession.getUser().getUserName()); Rfc4314Rights rights = acl.getEntries().getOrDefault(userAsKey, new Rfc4314Rights()); if (rights.contains(MailboxACL.Right.Administer)) { return acl; } return new MailboxACL(ImmutableMap.of(userAsKey, rights)); } }
private Mailbox fromMessageManager(MessageManager messageManager, Optional<List<MailboxMetaData>> userMailboxesMetadata, MailboxSession mailboxSession) throws MailboxException { MailboxPath mailboxPath = messageManager.getMailboxPath(); boolean isOwner = mailboxPath.belongsTo(mailboxSession); Optional<Role> role = Role.from(mailboxPath.getName()); MailboxCounters mailboxCounters = messageManager.getMailboxCounters(mailboxSession); MessageManager.MetaData metaData = messageManager.getMetaData(NO_RESET_RECENT, mailboxSession, MessageManager.MetaData.FetchGroup.NO_COUNT); Rights rights = Rights.fromACL(metaData.getACL()) .removeEntriesFor(Username.forMailboxPath(mailboxPath)); Username username = Username.fromSession(mailboxSession); Quotas quotas = getQuotas(mailboxPath); return Mailbox.builder() .id(messageManager.getId()) .name(getName(mailboxPath, mailboxSession)) .parentId(getParentIdFromMailboxPath(mailboxPath, userMailboxesMetadata, mailboxSession).orElse(null)) .role(role) .unreadMessages(mailboxCounters.getUnseen()) .totalMessages(mailboxCounters.getCount()) .sortOrder(SortOrder.getSortOrder(role)) .sharedWith(rights) .mayAddItems(rights.mayAddItems(username).orElse(isOwner)) .mayCreateChild(rights.mayCreateChild(username).orElse(isOwner)) .mayDelete(rights.mayDelete(username).orElse(isOwner)) .mayReadItems(rights.mayReadItems(username).orElse(isOwner)) .mayRemoveItems(rights.mayRemoveItems(username).orElse(isOwner)) .mayRename(rights.mayRename(username).orElse(isOwner)) .namespace(getNamespace(mailboxPath, isOwner)) .quotas(quotas) .build(); }