Search in sources :

Example 31 with Mountpoint

use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.

the class ZimbraMailAdapter method isMountpoint.

private static boolean isMountpoint(Mailbox mbox, String folderPath) throws ServiceException {
    Pair<Folder, String> pair = mbox.getFolderByPathLongestMatch(null, Mailbox.ID_FOLDER_USER_ROOT, folderPath);
    Folder f = pair.getFirst();
    return f != null && f instanceof Mountpoint;
}
Also used : Folder(com.zimbra.cs.mailbox.Folder) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Example 32 with Mountpoint

use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.

the class InQuery method compile.

@Override
public QueryOperation compile(Mailbox mbox, boolean bool) {
    if (specialTarget != null) {
        if (specialTarget == In.NONE) {
            return new NoResultsQueryOperation();
        } else if (specialTarget == In.ANY) {
            DBQueryOperation op = new DBQueryOperation();
            op.addAnyFolder(evalBool(bool));
            return op;
        } else {
            if (evalBool(bool)) {
                if (specialTarget == In.REMOTE) {
                    DBQueryOperation dbop = new DBQueryOperation();
                    dbop.addIsRemoteClause();
                    return dbop;
                } else {
                    assert (specialTarget == In.LOCAL);
                    DBQueryOperation dbop = new DBQueryOperation();
                    dbop.addIsLocalClause();
                    return dbop;
                }
            } else {
                if (specialTarget == In.REMOTE) {
                    DBQueryOperation dbop = new DBQueryOperation();
                    dbop.addIsLocalClause();
                    return dbop;
                } else {
                    assert (specialTarget == In.LOCAL);
                    DBQueryOperation dbop = new DBQueryOperation();
                    dbop.addIsRemoteClause();
                    return dbop;
                }
            }
        }
    }
    DBQueryOperation dbOp = new DBQueryOperation();
    if (folder != null) {
        if (includeSubfolders) {
            List<Folder> subFolders = folder.getSubfolderHierarchy();
            if (evalBool(bool)) {
                // (A or B or C)
                UnionQueryOperation union = new UnionQueryOperation();
                for (Folder sub : subFolders) {
                    DBQueryOperation dbop = new DBQueryOperation();
                    union.add(dbop);
                    if (sub instanceof Mountpoint) {
                        Mountpoint mpt = (Mountpoint) sub;
                        if (!mpt.isLocal()) {
                            dbop.addInRemoteFolder(mpt.getTarget(), "", includeSubfolders, evalBool(bool));
                        } else {
                        // TODO FIXME handle local mountpoints. Don't forget to check for infinite recursion!
                        }
                    } else {
                        dbop.addInFolder(sub, evalBool(bool));
                    }
                }
                return union;
            } else {
                // -(A or B or C) ==> -A and -B and -C
                IntersectionQueryOperation iop = new IntersectionQueryOperation();
                for (Folder f : subFolders) {
                    DBQueryOperation dbop = new DBQueryOperation();
                    iop.addQueryOp(dbop);
                    if (f instanceof Mountpoint) {
                        Mountpoint mpt = (Mountpoint) f;
                        if (!mpt.isLocal()) {
                            dbop.addInRemoteFolder(mpt.getTarget(), "", includeSubfolders, evalBool(bool));
                        } else {
                        // TODO FIXME handle local mountpoints.  Don't forget to check for infinite recursion!
                        }
                    } else {
                        dbop.addInFolder(f, evalBool(bool));
                    }
                }
                return iop;
            }
        } else {
            dbOp.addInFolder(folder, evalBool(bool));
        }
    } else if (remoteId != null) {
        dbOp.addInRemoteFolder(remoteId, subfolderPath, includeSubfolders, evalBool(bool));
    } else {
        assert (false);
    }
    return dbOp;
}
Also used : IntersectionQueryOperation(com.zimbra.cs.index.IntersectionQueryOperation) UnionQueryOperation(com.zimbra.cs.index.UnionQueryOperation) Folder(com.zimbra.cs.mailbox.Folder) DBQueryOperation(com.zimbra.cs.index.DBQueryOperation) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) NoResultsQueryOperation(com.zimbra.cs.index.NoResultsQueryOperation)

Example 33 with Mountpoint

use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.

the class ExternalUserProvServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String param = req.getParameter("p");
    if (param == null) {
        throw new ServletException("request missing param");
    }
    Map<Object, Object> tokenMap = validatePrelimToken(param);
    Map<String, String> reqHeaders = new HashMap<String, String>();
    String ownerId = (String) tokenMap.get(AccountConstants.P_ACCOUNT_ID);
    String folderId = (String) tokenMap.get(AccountConstants.P_FOLDER_ID);
    String extUserEmail = (String) tokenMap.get(AccountConstants.P_EMAIL);
    String addressVerification = (String) tokenMap.get(AccountConstants.P_ADDRESS_VERIFICATION);
    if ("1".equals(addressVerification)) {
        Boolean expired = false;
        if (tokenMap.get(EXPIRED) != null) {
            expired = (Boolean) tokenMap.get(EXPIRED);
        }
        Map<String, String> attributes = handleAddressVerification(req, resp, ownerId, extUserEmail, expired);
        redirectRequest(req, resp, attributes, EXT_USER_PROV_ON_UI_NODE, PUBLIC_ADDRESS_VERIFICATION_JSP);
    } else {
        Provisioning prov = Provisioning.getInstance();
        Account grantee;
        try {
            Account owner = prov.getAccountById(ownerId);
            Domain domain = prov.getDomain(owner);
            grantee = prov.getAccountByName(mapExtEmailToAcctName(extUserEmail, domain));
            if (grantee == null) {
                // external virtual account not created yet
                if (prov.isOctopus() && DebugConfig.skipVirtualAccountRegistrationPage) {
                    // provision using 'null' password and display name
                    // UI will ask the user to set these post provisioning
                    provisionVirtualAccountAndRedirect(req, resp, null, null, ownerId, extUserEmail);
                } else {
                    resp.addCookie(new Cookie("ZM_PRELIM_AUTH_TOKEN", param));
                    Map<String, String> attrs = new HashMap<String, String>();
                    attrs.put("extuseremail", extUserEmail);
                    reqHeaders.put("ZM_PRELIM_AUTH_TOKEN", param);
                    redirectRequest(req, resp, attrs, reqHeaders, EXT_USER_PROV_ON_UI_NODE, PUBLIC_EXTUSERPROV_JSP);
                }
            } else {
                // create a new mountpoint in the external user's mailbox if not already created
                String[] sharedItems = owner.getSharedItem();
                int sharedFolderId = Integer.valueOf(folderId);
                String sharedFolderPath = null;
                MailItem.Type sharedFolderView = null;
                for (String sharedItem : sharedItems) {
                    ShareInfoData sid = AclPushSerializer.deserialize(sharedItem);
                    if (sid.getItemId() == sharedFolderId && extUserEmail.equalsIgnoreCase(sid.getGranteeId())) {
                        sharedFolderPath = sid.getPath();
                        sharedFolderView = sid.getFolderDefaultViewCode();
                        break;
                    }
                }
                if (sharedFolderPath == null) {
                    throw new ServletException("share not found");
                }
                String mountpointName = getMountpointName(owner, grantee, sharedFolderPath);
                ZMailbox.Options options = new ZMailbox.Options();
                options.setNoSession(true);
                options.setAuthToken(AuthProvider.getAuthToken(grantee).toZAuthToken());
                options.setUri(AccountUtil.getSoapUri(grantee));
                ZMailbox zMailbox = new ZMailbox(options);
                ZMountpoint zMtpt = null;
                try {
                    zMtpt = zMailbox.createMountpoint(String.valueOf(getMptParentFolderId(sharedFolderView, prov)), mountpointName, ZFolder.View.fromString(sharedFolderView.toString()), ZFolder.Color.DEFAULTCOLOR, null, ZMailbox.OwnerBy.BY_ID, ownerId, ZMailbox.SharedItemBy.BY_ID, folderId, false);
                } catch (ServiceException e) {
                    logger.debug("Error in attempting to create mountpoint. Probably it already exists.", e);
                }
                if (zMtpt != null) {
                    if (sharedFolderView == MailItem.Type.APPOINTMENT) {
                        // make sure that the mountpoint is checked in the UI by default
                        FolderActionSelector actionSelector = new FolderActionSelector(zMtpt.getId(), "check");
                        FolderActionRequest actionRequest = new FolderActionRequest(actionSelector);
                        try {
                            zMailbox.invokeJaxb(actionRequest);
                        } catch (ServiceException e) {
                            logger.warn("Error in invoking check action on calendar mountpoint", e);
                        }
                    }
                    HashSet<MailItem.Type> types = new HashSet<MailItem.Type>();
                    types.add(sharedFolderView);
                    enableAppFeatures(grantee, types);
                }
                // check if the external user is already logged-in
                String zAuthTokenCookie = null;
                javax.servlet.http.Cookie[] cookies = req.getCookies();
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        if (cookie.getName().equals("ZM_AUTH_TOKEN")) {
                            zAuthTokenCookie = cookie.getValue();
                            break;
                        }
                    }
                }
                AuthToken zAuthToken = null;
                if (zAuthTokenCookie != null) {
                    try {
                        zAuthToken = AuthProvider.getAuthToken(zAuthTokenCookie);
                    } catch (AuthTokenException ignored) {
                    // auth token is not valid
                    }
                }
                if (zAuthToken != null && !zAuthToken.isExpired() && zAuthToken.isRegistered() && grantee.getId().equals(zAuthToken.getAccountId())) {
                    // external virtual account already logged-in
                    resp.sendRedirect("/");
                } else if (prov.isOctopus() && !grantee.isVirtualAccountInitialPasswordSet() && DebugConfig.skipVirtualAccountRegistrationPage) {
                    // seems like the virtual user did not set his password during his last visit, after an account was
                    // provisioned for him
                    setCookieAndRedirect(req, resp, grantee);
                } else {
                    Map<String, String> attrs = new HashMap<String, String>();
                    attrs.put("virtualacctdomain", domain.getName());
                    redirectRequest(req, resp, attrs, PUBLIC_LOGIN_ON_UI_NODE, PUBLIC_LOGIN_JSP);
                }
            }
        } catch (ServiceException e) {
            Map<String, String> errorAttrs = new HashMap<String, String>();
            errorAttrs.put(ERROR_CODE, e.getCode());
            errorAttrs.put(ERROR_MESSAGE, e.getMessage());
            redirectRequest(req, resp, errorAttrs, EXT_USER_PROV_ON_UI_NODE, PUBLIC_EXTUSERPROV_JSP);
        } catch (Exception e) {
            Map<String, String> errorAttrs = new HashMap<String, String>();
            errorAttrs.put(ERROR_CODE, ServiceException.FAILURE);
            errorAttrs.put(ERROR_MESSAGE, e.getMessage());
            redirectRequest(req, resp, errorAttrs, EXT_USER_PROV_ON_UI_NODE, PUBLIC_EXTUSERPROV_JSP);
        }
    }
}
Also used : ZMountpoint(com.zimbra.client.ZMountpoint) GuestAccount(com.zimbra.cs.account.GuestAccount) Account(com.zimbra.cs.account.Account) SearchAccountsOptions(com.zimbra.cs.account.SearchAccountsOptions) HashMap(java.util.HashMap) Provisioning(com.zimbra.cs.account.Provisioning) ServletException(javax.servlet.ServletException) FolderActionSelector(com.zimbra.soap.mail.type.FolderActionSelector) ZMailbox(com.zimbra.client.ZMailbox) HashSet(java.util.HashSet) Cookie(javax.servlet.http.Cookie) ShareInfoData(com.zimbra.cs.account.ShareInfoData) ZMountpoint(com.zimbra.client.ZMountpoint) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) ServletException(javax.servlet.ServletException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) MailItem(com.zimbra.cs.mailbox.MailItem) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) FolderActionRequest(com.zimbra.soap.mail.message.FolderActionRequest) AuthTokenException(com.zimbra.cs.account.AuthTokenException) AuthToken(com.zimbra.cs.account.AuthToken) Domain(com.zimbra.cs.account.Domain) Map(java.util.Map) HashMap(java.util.HashMap)

Example 34 with Mountpoint

use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.

the class ExternalUserProvServlet method provisionVirtualAccountAndRedirect.

private static void provisionVirtualAccountAndRedirect(HttpServletRequest req, HttpServletResponse resp, String displayName, String password, String grantorId, String extUserEmail) throws ServiceException {
    Provisioning prov = Provisioning.getInstance();
    try {
        Account owner = prov.getAccountById(grantorId);
        Domain domain = prov.getDomain(owner);
        Account grantee = prov.getAccountByName(mapExtEmailToAcctName(extUserEmail, domain));
        if (grantee != null) {
            throw AccountServiceException.ACCOUNT_EXISTS(extUserEmail);
        }
        // search all shares accessible to the external user
        SearchAccountsOptions searchOpts = new SearchAccountsOptions(domain, new String[] { Provisioning.A_zimbraId, Provisioning.A_displayName, Provisioning.A_zimbraSharedItem });
        // get all groups extUserEmail belongs to
        GuestAccount guestAcct = new GuestAccount(extUserEmail, null);
        List<String> groupIds = prov.getGroupMembership(guestAcct, false).groupIds();
        List<String> grantees = Lists.newArrayList(extUserEmail);
        grantees.addAll(groupIds);
        searchOpts.setFilter(ZLdapFilterFactory.getInstance().accountsByGrants(grantees, false, false));
        List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
        if (accounts.isEmpty()) {
            throw AccountServiceException.NO_SHARE_EXISTS();
        }
        // create external account
        Map<String, Object> attrs = new HashMap<String, Object>();
        attrs.put(Provisioning.A_zimbraIsExternalVirtualAccount, ProvisioningConstants.TRUE);
        attrs.put(Provisioning.A_zimbraExternalUserMailAddress, extUserEmail);
        attrs.put(Provisioning.A_zimbraMailHost, prov.getLocalServer().getServiceHostname());
        if (!StringUtil.isNullOrEmpty(displayName)) {
            attrs.put(Provisioning.A_displayName, displayName);
        }
        attrs.put(Provisioning.A_zimbraHideInGal, ProvisioningConstants.TRUE);
        attrs.put(Provisioning.A_zimbraMailStatus, Provisioning.MailStatus.disabled.toString());
        if (!StringUtil.isNullOrEmpty(password)) {
            attrs.put(Provisioning.A_zimbraVirtualAccountInitialPasswordSet, ProvisioningConstants.TRUE);
        }
        // create external account mailbox
        Mailbox granteeMbox;
        try {
            grantee = prov.createAccount(mapExtEmailToAcctName(extUserEmail, domain), password, attrs);
            granteeMbox = MailboxManager.getInstance().getMailboxByAccount(grantee);
        } catch (ServiceException e) {
            // the next attempt
            if (grantee != null) {
                prov.deleteAccount(grantee.getId());
            }
            throw e;
        }
        // create mountpoints
        Set<MailItem.Type> viewTypes = new HashSet<MailItem.Type>();
        for (NamedEntry ne : accounts) {
            Account account = (Account) ne;
            String[] sharedItems = account.getSharedItem();
            for (String sharedItem : sharedItems) {
                ShareInfoData shareData = AclPushSerializer.deserialize(sharedItem);
                if (!granteeMatchesShare(shareData, grantee)) {
                    continue;
                }
                String sharedFolderPath = shareData.getPath();
                String mountpointName = getMountpointName(account, grantee, sharedFolderPath);
                MailItem.Type viewType = shareData.getFolderDefaultViewCode();
                Mountpoint mtpt = granteeMbox.createMountpoint(null, getMptParentFolderId(viewType, prov), mountpointName, account.getId(), shareData.getItemId(), shareData.getItemUuid(), viewType, 0, MailItem.DEFAULT_COLOR, false);
                if (viewType == MailItem.Type.APPOINTMENT) {
                    // make sure that the mountpoint is checked in the UI by default
                    granteeMbox.alterTag(null, mtpt.getId(), mtpt.getType(), Flag.FlagInfo.CHECKED, true, null);
                }
                viewTypes.add(viewType);
            }
        }
        enableAppFeatures(grantee, viewTypes);
        setCookieAndRedirect(req, resp, grantee);
    } catch (ServiceException e) {
        ZimbraLog.account.debug("Exception while creating virtual account for %s", extUserEmail, e);
        throw e;
    } catch (Exception e) {
        ZimbraLog.account.debug("Exception while creating virtual account for %s", extUserEmail, e);
        throw ServiceException.TEMPORARILY_UNAVAILABLE();
    }
}
Also used : GuestAccount(com.zimbra.cs.account.GuestAccount) Account(com.zimbra.cs.account.Account) SearchAccountsOptions(com.zimbra.cs.account.SearchAccountsOptions) GuestAccount(com.zimbra.cs.account.GuestAccount) HashMap(java.util.HashMap) ShareInfoData(com.zimbra.cs.account.ShareInfoData) Provisioning(com.zimbra.cs.account.Provisioning) ServletException(javax.servlet.ServletException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) NamedEntry(com.zimbra.cs.account.NamedEntry) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) Domain(com.zimbra.cs.account.Domain) ZMountpoint(com.zimbra.client.ZMountpoint) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) HashSet(java.util.HashSet)

Example 35 with Mountpoint

use of com.zimbra.cs.mailbox.Mountpoint in project zm-mailbox by Zimbra.

the class UserServlet method proxyIfMountpoint.

protected boolean proxyIfMountpoint(HttpServletRequest req, HttpServletResponse resp, UserServletContext context, MailItem item) throws IOException, ServiceException, UserServletException {
    if (!(item instanceof Mountpoint))
        return false;
    if (context.format != null && context.format.equals("html"))
        return false;
    Mountpoint mpt = (Mountpoint) item;
    String uri = SERVLET_PATH + "/~/?" + QP_ID + '=' + HttpUtil.urlEscape(mpt.getOwnerId()) + "%3A" + mpt.getRemoteId();
    if (context.format != null)
        uri += '&' + QP_FMT + '=' + HttpUtil.urlEscape(context.format.toString());
    if (context.extraPath != null)
        uri += '&' + QP_NAME + '=' + HttpUtil.urlEscape(context.extraPath);
    for (Map.Entry<String, String> entry : HttpUtil.getURIParams(req).entrySet()) {
        String qp = entry.getKey();
        if (!qp.equals(QP_ID) && !qp.equals(QP_FMT))
            uri += '&' + HttpUtil.urlEscape(qp) + '=' + HttpUtil.urlEscape(entry.getValue());
    }
    Provisioning prov = Provisioning.getInstance();
    Account targetAccount = prov.get(AccountBy.id, mpt.getOwnerId());
    if (targetAccount == null)
        throw new UserServletException(HttpServletResponse.SC_BAD_REQUEST, L10nUtil.getMessage(MsgKey.errNoSuchAccount, req));
    try {
        proxyServletRequest(req, resp, prov.getServer(targetAccount), uri, getProxyAuthToken(context));
    } catch (HttpException e) {
        throw new IOException("Unknown error", e);
    }
    return true;
}
Also used : GuestAccount(com.zimbra.cs.account.GuestAccount) Account(com.zimbra.cs.account.Account) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) Map(java.util.Map) HashMap(java.util.HashMap) Provisioning(com.zimbra.cs.account.Provisioning)

Aggregations

Mountpoint (com.zimbra.cs.mailbox.Mountpoint)43 Folder (com.zimbra.cs.mailbox.Folder)30 ServiceException (com.zimbra.common.service.ServiceException)23 Mailbox (com.zimbra.cs.mailbox.Mailbox)18 Account (com.zimbra.cs.account.Account)17 MailItem (com.zimbra.cs.mailbox.MailItem)14 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)14 ZFolder (com.zimbra.client.ZFolder)11 ItemId (com.zimbra.cs.service.util.ItemId)11 ZMailbox (com.zimbra.client.ZMailbox)8 Element (com.zimbra.common.soap.Element)8 OperationContext (com.zimbra.cs.mailbox.OperationContext)8 IOException (java.io.IOException)8 HashMap (java.util.HashMap)8 Provisioning (com.zimbra.cs.account.Provisioning)6 SearchFolder (com.zimbra.cs.mailbox.SearchFolder)6 ZMountpoint (com.zimbra.client.ZMountpoint)5 Pair (com.zimbra.common.util.Pair)5 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)5 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)5