/// <summary>
        /// <see cref="MS.Internal.Documents.Application.IDocumentController"/>
        /// </summary>
        bool IDocumentController.Rebind(Document document)
        {
            RightsDocument doc      = (RightsDocument)document; // see class remarks on why this is ok
            Stream         ciphered = doc.Dependency.Source;
            Stream         clear    = ciphered;

            if (doc.IsRebindNeeded)
            {
                if (doc.SourcePackage != null)
                {
                    CloseEnvelope(doc.SourcePackage);
                    doc.SourcePackage = null;
                }

                EncryptedPackageEnvelope envelope   = null;
                PackageProperties        properties = null;
                bool isSourceProtected = doc.IsSourceProtected();

                if (isSourceProtected)
                {
                    envelope          = OpenEnvelopeOnStream(ciphered);
                    doc.SourcePackage = envelope;

                    properties = new SuppressedProperties(envelope);
                }

                DocumentProperties.Current.SetRightsManagedProperties(properties);
                DocumentRightsManagementManager.Current.SetEncryptedPackage(envelope);

                if (isSourceProtected)
                {
                    clear = DocumentRightsManagementManager.Current.DecryptPackage();

                    if (clear != null)
                    {
                        clear = new RightsManagementSuppressedStream(
                            clear,
                            DocumentRightsManagementManager.Current.HasPermissionToEdit);

                        // Reset the position of the stream since GetPackageStream will
                        // create a package and move the stream pointer somewhere else
                        clear.Position = 0;
                    }
                    else
                    {
                        Trace.SafeWrite(
                            Trace.Rights,
                            "You do not have rights for the current document.");

                        return(false);
                    }
                }

                doc.SourceProxy.Target = clear;
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// <see cref="MS.Internal.Documents.Application.IDocumentController"/>
        /// </summary>
        /// <remarks>
        /// This method severely breaks encapsulating the chain, however it does
        /// so because the requirement breaks encapsulation and the original design
        /// of the class only consider streams not properties.  The requirement is
        /// that as we transition to/from a protected document we copy properties
        /// between the layers.  It was least risk to compromise the design here
        /// then extend it.
        /// </remarks>
        bool IDocumentController.SavePreperation(Document document)
        {
            // we don't have to check packageDoc for because we are only responsible
            // for PackageDocuments
            PackageDocument packageDoc = (PackageDocument)document;
            RightsDocument  rightsDoc  = document.Dependency as RightsDocument;

            if (rightsDoc != null)
            {
                bool isDestinationProtected = rightsDoc.IsDestinationProtected();
                bool isSourceProtected      = rightsDoc.IsSourceProtected();

                // the only time we don't need to copy properties is when
                // neither source nor destination is protected as OPC properties
                // are copied as parts

                // if the source was protected and the destination is not
                // then we need to copy properties to the package
                if (isSourceProtected && !isDestinationProtected)
                {
                    DocumentProperties.Copy(
                        new SuppressedProperties(rightsDoc.SourcePackage),
                        packageDoc.Package.PackageProperties);
                }

                // if the source was not protected and the destination is
                // then we need to copy properties from the package to the envelope
                if (!isSourceProtected && isDestinationProtected)
                {
                    DocumentProperties.Copy(
                        packageDoc.Package.PackageProperties,
                        new SuppressedProperties(rightsDoc.DestinationPackage));
                }

                // if they were both protected we need to copy properties
                // from the old envelope to the new
                if (isDestinationProtected && isSourceProtected)
                {
                    DocumentProperties.Copy(
                        new SuppressedProperties(rightsDoc.SourcePackage),
                        new SuppressedProperties(rightsDoc.DestinationPackage));
                }
            }
            return(true);
        }
        /// <summary>
        /// <see cref="MS.Internal.Documents.Application.IDocumentController"/>
        /// </summary>
        bool IDocumentController.Open(Document document)
        {
            RightsDocument doc               = (RightsDocument)document; // see class remarks on why this is ok
            Stream         ciphered          = doc.Dependency.Source;
            Stream         clear             = ciphered;
            bool           isSourceProtected = doc.IsSourceProtected();

            if (isSourceProtected)
            {
                // Do not catch exceptions here - there can be no mitigation
                EncryptedPackageEnvelope envelope   = OpenEnvelopeOnStream(ciphered);
                PackageProperties        properties = new SuppressedProperties(envelope);

                doc.SourcePackage = envelope;
                DocumentProperties.Current.SetRightsManagedProperties(properties);
            }

            RightsManagementProvider provider =
                new RightsManagementProvider(doc.SourcePackage);

            _provider.Value = provider;

            try
            {
                DocumentRightsManagementManager.Initialize(provider);

                DocumentRightsManagementManager.Current.PublishLicenseChange +=
                    new EventHandler(delegate(object sender, EventArgs args)
                {
                    Trace.SafeWrite(
                        Trace.Rights,
                        "Disabling file copy for current document.");
                    doc.IsFileCopySafe = false;

                    DocumentManager.CreateDefault().EnableEdit(null);
                });

                if (isSourceProtected)
                {
                    clear = DocumentRightsManagementManager.Current.DecryptPackage();

                    if (clear != null)
                    {
                        clear = new RightsManagementSuppressedStream(
                            clear,
                            DocumentRightsManagementManager.Current.HasPermissionToEdit);

                        // Reset the position of the stream since GetPackageStream will
                        // create a package and move the stream pointer somewhere else
                        clear.Position = 0;
                    }
                    else
                    {
                        Trace.SafeWrite(
                            Trace.Rights,
                            "You do not have rights for the current document.");

                        return(false);
                    }
                }
            }
            catch
            {
                // If anything failed here, we cannot use the provider any longer,
                // so we can dispose it
                provider.Dispose();
                _provider.Value = null;
                throw;
            }

            if (clear != null)
            {
                doc.SourceProxy = new StreamProxy(clear);
            }
            else
            {
                // If decryption failed, we can no longer do anything with the
                // provider instance or the current RM manager
                provider.Dispose();
                _provider.Value = null;
            }

            return(true);
        }