创建文档Sitefinity后端非admin用户

本文关键字:admin 用户 后端 文档 Sitefinity 创建 | 更新日期: 2023-09-27 18:02:14

我在使用teleerik站点完整性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。对于id为

的主体,DocumentLibrary, teleruk . sitefinity . model未在Document中授予ManageDocument

然而,这个例外,只发生在我试图创建和发布pdf文档时,我作为一个非管理员/非后台用户登录。当我作为后端用户登录时,这段代码执行得很好。我相信一定有一种方法来设置权限,以便允许非后端用户有适当的权限来创建文档,但我只是不知道如何和谷歌没有透露太多。如果有人能帮忙,我将非常感激。

这是使用Sitefinity 6.2版

创建文档Sitefinity后端非admin用户

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