创建文档Sitefinity后端非管理员用户

时间:2014-05-06 20:51:25

标签: c# asp.net telerik sitefinity

我遇到了telerik sitefinity api的问题。我正在尝试在后端创建pdf认证并将它们发布到文档库。以下是我创建和发布认证的代码。

   byte[] byteArray = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streamids, out warnings);

                var documentLibraryList = SitefinityHelper.GetDocumentLibraries();

                string certificationLibraryTitle = ConfigurationManager.AppSettings["CertificationLibrary"].ToString().ToUpper();

                DocumentLibrary certLibrary = null;

                //Make sure the parent library exists before doing anything else.
                foreach (DocumentLibrary library in documentLibraryList)
                {
                    if (library.Title.ToUpper() == certificationLibraryTitle.ToUpper())
                    {
                        certLibrary = library;
                        break;
                    }
                }
                //If not null, good to create the pdf cert, but need to check for previous ones.
                if (certLibrary != null)
                {
                    try
                    {


                        string pdfCertificationTitle = currentUser.UserName + "_"+ "Certification_" + moduleViewModel.ExamID.ToString();

                        UserCertifications previousCertifcationRecord = certBo.GetUserCertificationByTitle(pdfCertificationTitle);

                        if (previousCertifcationRecord == null)
                        {
                            //No previous record exists, create the new record. 

                            MemoryStream memStream = new MemoryStream(byteArray);

                            Guid documentID = Guid.NewGuid();

                            string format = ".pdf";

                            LibrariesManager librariesManager = LibrariesManager.GetManager();

                            Document pdfDocument = librariesManager.CreateDocument(documentID);

                            DocumentLibrary parentLibrary = librariesManager.GetDocumentLibraries().Where(d => d.Id == certLibrary.Id).SingleOrDefault();

                            pdfDocument.Parent = parentLibrary;

                            pdfDocument.Title = pdfCertificationTitle;
                            pdfDocument.DateCreated = DateTime.UtcNow;
                            pdfDocument.PublicationDate = DateTime.UtcNow;
                            pdfDocument.LastModified = DateTime.UtcNow;
                            pdfDocument.UrlName = Regex.Replace(pdfCertificationTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

                            librariesManager.RecompileAndValidateUrls(pdfDocument);
                            librariesManager.Upload(pdfDocument, memStream, format);

                            librariesManager.SaveChanges();

                            var bag = new Dictionary<string, string>();
                            bag.Add("ContentType", typeof(Document).FullName);

                            WorkflowManager.MessageWorkflow(documentID, typeof(Document), null, "Publish", false, bag);

                            string fullUrl = SitefinityHelper.GetDocumentUrl(pdfCertificationTitle);

                            //Save the certification to the DB.
                            certBo.InsertNewUserCertification(pdfCertificationTitle, fullUrl, currentUser.UserID);
                        }
                    }

                    catch (Exception ex)
                    {
                        ExceptionManager.LogException(this, ex);
                    }
                }

我遇到的异常消息是:

Telerik.Sitefinity.Libraries.Model.DocumentLibrary,Telerik.Sitefinity.Model未授予Document的ManageDocument for Principals with IDs

但是,此异常仅在我以非管理员/非后端用户身份登录时尝试创建和发布pdf文档时发生。当我以后端用户身份登录时,此代码执行得很好。我相信必须有一种方法来设置权限,以便允许非后端用户创建文档的适当权限,但我只是不知道如何和谷歌搜索没有透露太多。如果有人可以请求帮助我会非常感激。

这是使用Sitefinity版本6.2

1 个答案:

答案 0 :(得分:3)

Sitefinity内置了安全基础架构,允许您指定谁可以使用内置和自定义数据类型执行不同的操作。您获得的异常意味着您尝试创建文档的当前主体没有此操作的适当权限。您可以做的是检查文档库的权限并授予此类用户必要的权限,或者提升安全性上下文并跳过安全检查。要绕过安全检查,请使用以下代码(使用库管理器的部分):

        LibrariesManager librariesManager = LibrariesManager.GetManager();

        using(new ElevatedModeRegion(librariesManager))
        {

            Document pdfDocument = librariesManager.CreateDocument(documentID);

            DocumentLibrary parentLibrary = librariesManager.GetDocumentLibraries().Where(d => d.Id == certLibrary.Id).SingleOrDefault();

            pdfDocument.Parent = parentLibrary;

            pdfDocument.Title = pdfCertificationTitle;
            pdfDocument.DateCreated = DateTime.UtcNow;
            pdfDocument.PublicationDate = DateTime.UtcNow;
            pdfDocument.LastModified = DateTime.UtcNow;
            pdfDocument.UrlName = Regex.Replace(pdfCertificationTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

            librariesManager.RecompileAndValidateUrls(pdfDocument);
            librariesManager.Upload(pdfDocument, memStream, format);

            librariesManager.SaveChanges();

            var bag = new Dictionary<string, string>();
            bag.Add("ContentType", typeof(Document).FullName);

                            bool suppressSecurityChecks = WorkflowManager.GetManager().Provider.SuppressSecurityChecks;
            try
            {
                WorkflowManager.GetManager().Provider.SuppressSecurityChecks = true;
                WorkflowManager.MessageWorkflow(documentID, typeof(Document), null, "Publish", false, bag);

            }
            catch (Exception ex)
            {

            }
            finally 
            {
                WorkflowManager.GetManager().Provider.SuppressSecurityChecks = suppressSecurityChecks;
            }  
      }