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