private ExternalUser ApplyPermission(MailboxSession mailboxSession, SmtpAddress smtpAddress, SharingContext context) { ExternalUser externalUser = null; ExternalUser externalUser2; using (ExternalUserCollection externalUsers = mailboxSession.GetExternalUsers()) { externalUser2 = externalUsers.FindReachUserWithOriginalSmtpAddress(smtpAddress); externalUser = externalUsers.FindFederatedUserWithOriginalSmtpAddress(smtpAddress); if (externalUser2 == null) { externalUser2 = externalUsers.AddReachUser(smtpAddress); externalUsers.Save(); } } using (FolderPermissionContext current = FolderPermissionContext.GetCurrent(mailboxSession, context)) { FreeBusyAccess freeBusy = this.GetFreeBusy(context); PermissionSecurityPrincipal principal = new PermissionSecurityPrincipal(externalUser2); PermissionLevel permissionLevel = (context.SharingDetail == SharingContextDetailLevel.FullDetails) ? PermissionLevel.Reviewer : PermissionLevel.None; current.AddOrChangePermission(principal, permissionLevel, new FreeBusyAccess?(freeBusy)); if (externalUser != null) { current.RemovePermission(new PermissionSecurityPrincipal(externalUser)); } } return(externalUser2); }
protected override void InternalPerformRevocation(MailboxSession mailboxSession, SharingContext context) { using (FolderPermissionContext current = FolderPermissionContext.GetCurrent(mailboxSession, context)) { ValidRecipient recipient = new ValidRecipient(context.InitiatorSmtpAddress, null); PermissionSecurityPrincipal principal = this.CreatePermissionSecurityPrincipal(recipient, mailboxSession.MailboxOwner); current.RemovePermission(principal); } }
protected override void InternalPerformRevocation(MailboxSession mailboxSession, SharingContext context) { using (FolderPermissionContext current = FolderPermissionContext.GetCurrent(mailboxSession, context)) { using (ExternalUserCollection externalUsers = mailboxSession.GetExternalUsers()) { PermissionSecurityPrincipal permissionSecurityPrincipal = this.CreatePermissionSecurityPrincipal(context.InitiatorSmtpAddress, externalUsers); if (permissionSecurityPrincipal != null) { current.RemovePermission(permissionSecurityPrincipal); } } } }
protected override PerformInvitationResults InternalPerformInvitation(MailboxSession mailboxSession, SharingContext context, ValidRecipient[] recipients, IFrontEndLocator frontEndLocator) { PermissionLevel permissionLevel = this.GetPermissionLevel(context); FreeBusyAccess? freeBusy = this.GetFreeBusy(context); using (FolderPermissionContext current = FolderPermissionContext.GetCurrent(mailboxSession, context)) { foreach (ValidRecipient recipient in recipients) { PermissionSecurityPrincipal principal = this.CreatePermissionSecurityPrincipal(recipient, mailboxSession.MailboxOwner); current.AddOrChangePermission(principal, permissionLevel, freeBusy); } } return(new PerformInvitationResults(recipients)); }
internal static FolderPermissionContext GetCurrent(MailboxSession mailboxSession, SharingContext sharingContext) { FolderPermissionContext folderPermissionContext; if (!FolderPermissionContext.activeContexts.ContainsKey(sharingContext)) { ExTraceGlobals.SharingTracer.TraceDebug <IExchangePrincipal, SharingContext>(0L, "{0}: Create new FolderPermissionContext for SharingContext {1}.", mailboxSession.MailboxOwner, sharingContext); folderPermissionContext = new FolderPermissionContext(mailboxSession, sharingContext); } else { ExTraceGlobals.SharingTracer.TraceDebug <IExchangePrincipal, SharingContext>(0L, "{0}: Get existing FolderPermissionContext for SharingContext {1}.", mailboxSession.MailboxOwner, sharingContext); folderPermissionContext = FolderPermissionContext.activeContexts[sharingContext]; } folderPermissionContext.refCount++; folderPermissionContext.enabled = true; return(folderPermissionContext); }
private void PerformInvitation() { Dictionary <SharingProvider, ValidRecipient[]> dictionary = new Dictionary <SharingProvider, ValidRecipient[]>(); List <ValidRecipient> list = new List <ValidRecipient>(); SharingProvider fallbackSharingProvider = this.SharingContext.FallbackSharingProvider; using (FolderPermissionContext current = FolderPermissionContext.GetCurrent(this.MailboxSession, this.SharingContext)) { bool flag = false; try { foreach (KeyValuePair <SharingProvider, CheckRecipientsResults> keyValuePair in this.SharingContext.AvailableSharingProviders) { SharingProvider key = keyValuePair.Key; CheckRecipientsResults value = keyValuePair.Value; if (this.FallbackEnabled && key == fallbackSharingProvider) { if (value != null) { list.AddRange(value.ValidRecipients); } } else if (value != null) { PerformInvitationResults performInvitationResults = key.PerformInvitation(this.MailboxSession, this.SharingContext, value.ValidRecipients, this.FrontEndLocator); ExTraceGlobals.SharingTracer.TraceDebug <IExchangePrincipal, SharingProvider, PerformInvitationResults>((long)this.GetHashCode(), "{0}: Performed invitation by provider {1}. Result = {2}", this.MailboxSession.MailboxOwner, key, performInvitationResults); if (performInvitationResults.Result == PerformInvitationResultType.Failed || performInvitationResults.Result == PerformInvitationResultType.PartiallySuccess) { if (!this.FallbackEnabled) { StoreObjectId folderId = this.SharingContext.FolderId; InvalidSharingRecipientsResolution invalidSharingRecipientsResolution; if (!this.CanUseFallback) { invalidSharingRecipientsResolution = new InvalidSharingRecipientsResolution(folderId); } else { using (Folder folder = Folder.Bind(this.MailboxSession, folderId)) { this.SharingContext.PopulateUrls(folder); } invalidSharingRecipientsResolution = new InvalidSharingRecipientsResolution(this.BrowseUrl, this.ICalUrl); } ExTraceGlobals.SharingTracer.TraceError <IExchangePrincipal, InvalidSharingRecipientsResolution>((long)this.GetHashCode(), "{0}: No fall back for these invalid recipients. Resolution = {1}", this.MailboxSession.MailboxOwner, invalidSharingRecipientsResolution); throw new InvalidSharingRecipientsException(performInvitationResults.FailedRecipients, invalidSharingRecipientsResolution); } ValidRecipient[] array = Array.ConvertAll <InvalidRecipient, ValidRecipient>(performInvitationResults.FailedRecipients, (InvalidRecipient invalidRecipient) => new ValidRecipient(invalidRecipient.SmtpAddress, null)); ExTraceGlobals.SharingTracer.TraceDebug <IExchangePrincipal, SharingProvider, int>((long)this.GetHashCode(), "{0}: Fall back on provider {1} for these {2} failed recipients.", this.MailboxSession.MailboxOwner, fallbackSharingProvider, array.Length); list.AddRange(array); dictionary.Add(key, performInvitationResults.SucceededRecipients); } } } if (this.FallbackEnabled) { foreach (KeyValuePair <SharingProvider, ValidRecipient[]> keyValuePair2 in dictionary) { SharingProvider key2 = keyValuePair2.Key; ValidRecipient[] value2 = keyValuePair2.Value; this.SharingContext.AvailableSharingProviders[key2] = new CheckRecipientsResults(value2); } this.SharingContext.AvailableSharingProviders[fallbackSharingProvider] = new CheckRecipientsResults(list.ToArray()); PerformInvitationResults performInvitationResults2 = fallbackSharingProvider.PerformInvitation(this.MailboxSession, this.SharingContext, list.ToArray(), this.FrontEndLocator); ExTraceGlobals.SharingTracer.TraceDebug <IExchangePrincipal, SharingProvider, PerformInvitationResults>((long)this.GetHashCode(), "{0}: Performed invitation by fallback provider {1}. Result = {2}", this.MailboxSession.MailboxOwner, fallbackSharingProvider, performInvitationResults2); if (performInvitationResults2.Result == PerformInvitationResultType.Failed || performInvitationResults2.Result == PerformInvitationResultType.PartiallySuccess) { throw new InvalidOperationException("The fallback provider should never fail."); } } flag = true; } finally { if (!flag) { current.Disable(); } } } }
protected override PerformInvitationResults InternalPerformInvitation(MailboxSession mailboxSession, SharingContext context, ValidRecipient[] recipients, IFrontEndLocator frontEndLocator) { ExternalAuthentication current = ExternalAuthentication.GetCurrent(); if (!current.Enabled) { ExTraceGlobals.SharingTracer.TraceError <string>((long)this.GetHashCode(), "{0}: The organization is not federated for external sharing.", context.InitiatorSmtpAddress); return(new PerformInvitationResults(new InvalidSharingRecipientsException(ValidRecipient.ConvertToStringArray(recipients), new OrganizationNotFederatedException()))); } SharedFolderDataEncryption sharedFolderDataEncryption = new SharedFolderDataEncryption(current); string text = StoreId.StoreIdToEwsId(mailboxSession.MailboxOwner.MailboxInfo.MailboxGuid, context.FolderId); PerformInvitationResults result; using (ExternalUserCollection externalUsers = mailboxSession.GetExternalUsers()) { PerformInvitationResults performInvitationResults = null; EncryptionResults encryptionResults = null; Exception ex = null; try { encryptionResults = sharedFolderDataEncryption.Encrypt(mailboxSession.MailboxOwner, mailboxSession.GetADRecipientSession(true, ConsistencyMode.IgnoreInvalid), externalUsers, recipients, context.InitiatorSmtpAddress, context.FolderClass, text, frontEndLocator); } catch (UserWithoutFederatedProxyAddressException ex2) { ex = ex2; } catch (InvalidFederatedOrganizationIdException ex3) { ex = ex3; } catch (StoragePermanentException ex4) { ex = ex4; } if (ex != null) { ExTraceGlobals.SharingTracer.TraceError <string, Exception>((long)this.GetHashCode(), "{0}: Error occurred when trying to encrypt. Exception = {1}", context.InitiatorSmtpAddress, ex); result = new PerformInvitationResults(new InvalidSharingRecipientsException(ValidRecipient.ConvertToStringArray(recipients), ex)); } else { if (encryptionResults.InvalidRecipients != null && encryptionResults.InvalidRecipients.Length > 0) { InvalidSharingRecipientsException exception = new InvalidSharingRecipientsException(encryptionResults.InvalidRecipients); if (encryptionResults.InvalidRecipients.Length == recipients.Length) { return(new PerformInvitationResults(exception)); } performInvitationResults = new PerformInvitationResults(recipients, exception); recipients = performInvitationResults.SucceededRecipients; } else { performInvitationResults = new PerformInvitationResults(recipients); } PermissionLevel permissionLevel = this.GetPermissionLevel(context); FreeBusyAccess? freeBusy = this.GetFreeBusy(context); using (FolderPermissionContext current2 = FolderPermissionContext.GetCurrent(mailboxSession, context)) { foreach (ValidRecipient validRecipient in recipients) { PermissionSecurityPrincipal principal = this.CreatePermissionSecurityPrincipal(validRecipient.SmtpAddress, externalUsers); current2.AddOrChangePermission(principal, permissionLevel, freeBusy); ExternalUser externalUser = externalUsers.FindReachUserWithOriginalSmtpAddress(new SmtpAddress(validRecipient.SmtpAddress)); if (externalUser != null) { current2.RemovePermission(new PermissionSecurityPrincipal(externalUser)); } } } context.FolderEwsId = text; context.EncryptedSharedFolderDataCollection = encryptionResults.EncryptedSharedFolderDataCollection; result = performInvitationResults; } } return(result); }