From 095957eecfacbe57a812d98cc85c2459a1ba6a8e Mon Sep 17 00:00:00 2001 From: Minho Date: Fri, 12 May 2017 10:45:40 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=B7=BB=E5=8A=A0=E8=B6=85=E7=BA=A7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=99=A8=E7=BC=96=E8=BE=91=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E6=9D=83=E9=99=90=202=E3=80=81=E6=96=B0=E5=A2=9ECDN=E9=9D=99?= =?UTF-8?q?=E6=80=81=E6=96=87=E4=BB=B6=E5=8A=A0=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/command.go | 43 ++++++ conf/app.conf.example | 9 +- controllers/book.go | 41 ++++-- controllers/document.go | 158 +++++++++++++++------- models/book.go | 8 ++ static/css/kancloud.css | 6 +- utils/template_fun.go | 5 + views/book/index.tpl | 20 +-- views/document/kancloud_read_template.tpl | 14 +- views/manager/books.tpl | 25 ++-- 10 files changed, 236 insertions(+), 93 deletions(-) create mode 100644 utils/template_fun.go diff --git a/commands/command.go b/commands/command.go index 81a1b2a4..78c8dc92 100644 --- a/commands/command.go +++ b/commands/command.go @@ -13,6 +13,7 @@ import ( "github.com/lifei6671/gocaptcha" "github.com/lifei6671/godoc/conf" "github.com/lifei6671/godoc/models" + "strings" ) // RegisterDataBase 注册数据库 @@ -86,6 +87,48 @@ func RegisterCommand() { func RegisterFunction() { beego.AddFuncMap("config", models.GetOptionValue) + + beego.AddFuncMap("cdn", func(p string) string { + cdn := beego.AppConfig.DefaultString("cdn","") + if strings.HasPrefix(p,"/") && strings.HasSuffix(cdn,"/"){ + return cdn + string(p[1:]) + } + if !strings.HasPrefix(p,"/") && !strings.HasSuffix(cdn,"/"){ + return cdn + "/" + p + } + return cdn + p + }); + + beego.AddFuncMap("cdnjs", func(p string) string { + cdn := beego.AppConfig.DefaultString("cdnjs","") + if strings.HasPrefix(p,"/") && strings.HasSuffix(cdn,"/"){ + return cdn + string(p[1:]) + } + if !strings.HasPrefix(p,"/") && !strings.HasSuffix(cdn,"/"){ + return cdn + "/" + p + } + return cdn + p + }); + beego.AddFuncMap("cdncss", func(p string) string { + cdn := beego.AppConfig.DefaultString("cdncss","") + if strings.HasPrefix(p,"/") && strings.HasSuffix(cdn,"/"){ + return cdn + string(p[1:]) + } + if !strings.HasPrefix(p,"/") && !strings.HasSuffix(cdn,"/"){ + return cdn + "/" + p + } + return cdn + p + }); + beego.AddFuncMap("cdnimg", func(p string) string { + cdn := beego.AppConfig.DefaultString("cdnimg","") + if strings.HasPrefix(p,"/") && strings.HasSuffix(cdn,"/"){ + return cdn + string(p[1:]) + } + if !strings.HasPrefix(p,"/") && !strings.HasSuffix(cdn,"/"){ + return cdn + "/" + p + } + return cdn + p + }); } func init() { diff --git a/conf/app.conf.example b/conf/app.conf.example index 1e81563c..b2c64af8 100644 --- a/conf/app.conf.example +++ b/conf/app.conf.example @@ -64,4 +64,11 @@ mail_expired=30 ###############配置PDF生成工具地址################### -wkhtmltopdf=D:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe \ No newline at end of file +wkhtmltopdf=D:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe + +###############配置CDN加速################## +cdn= +cdnjs= +cdncss= +cdnimg= + diff --git a/controllers/book.go b/controllers/book.go index 80cf650e..6ad7b123 100644 --- a/controllers/book.go +++ b/controllers/book.go @@ -505,24 +505,35 @@ func (c *BookController) Release() { c.Prepare() identify := c.GetString("identify") - book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId) - if err != nil { - if err == models.ErrPermissionDenied { - c.JsonResult(6001,"权限不足") - } - if err == orm.ErrNoRows { - c.JsonResult(6002,"项目不存在") - } - beego.Error(err) - c.JsonResult(6003,"未知错误") - } - if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder && book.RoleId != conf.BookEditor{ - c.JsonResult(6003,"权限不足") - } + book_id := 0 + if c.Member.Role == conf.MemberSuperRole { + book,err := models.NewBook().FindByFieldFirst("identify",identify) + if err != nil { + + } + book_id = book.BookId + }else { + book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId) + + if err != nil { + if err == models.ErrPermissionDenied { + c.JsonResult(6001, "权限不足") + } + if err == orm.ErrNoRows { + c.JsonResult(6002, "项目不存在") + } + beego.Error(err) + c.JsonResult(6003, "未知错误") + } + if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder && book.RoleId != conf.BookEditor { + c.JsonResult(6003, "权限不足") + } + book_id = book.BookId + } go func(identify string) { - models.NewDocument().ReleaseContent(book.BookId) + models.NewDocument().ReleaseContent(book_id) pdfpath := "cache/" + identify + ".pdf" if _,err := os.Stat(pdfpath); os.IsExist(err){ diff --git a/controllers/document.go b/controllers/document.go index ba1093e6..29b4cba5 100644 --- a/controllers/document.go +++ b/controllers/document.go @@ -32,6 +32,10 @@ func isReadable (identify,token string,c *DocumentController) *models.BookResult beego.Error(err) c.Abort("500") } + if c.Member != nil && c.Member.Role == conf.MemberSuperRole { + bookResult := book.ToBookResult() + return bookResult + } //如果文档是私有的 if book.PrivatelyOwned == 1 { @@ -61,6 +65,7 @@ func isReadable (identify,token string,c *DocumentController) *models.BookResult bookResult := book.ToBookResult() if c.Member != nil { + rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, c.Member.MemberId) if err == nil { @@ -191,16 +196,27 @@ func (c *DocumentController) Edit() { c.Abort("404") } - bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId) + bookResult := models.NewBookResult() + //如果是超级管理者,则不判断权限 + if c.Member.Role == conf.MemberSuperRole { + book,err := models.NewBook().FindByFieldFirst("identify",identify) + if err != nil { + c.JsonResult(6002, "项目不存在或权限不足") + } + bookResult = book.ToBookResult() - if err != nil { - beego.Error("DocumentController.Edit => ",err) + }else { + bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId) - c.Abort("403") - } - if bookResult.RoleId == conf.BookObserver { + if err != nil { + beego.Error("DocumentController.Edit => ", err) - c.JsonResult(6002,"项目不存在或权限不足") + c.Abort("403") + } + if bookResult.RoleId == conf.BookObserver { + + c.JsonResult(6002, "项目不存在或权限不足") + } } //根据不同编辑器类型加载编辑器 @@ -260,16 +276,27 @@ func (c *DocumentController) Create() { c.JsonResult(6006,"文档标识已被使用") } } + book_id := 0 + //如果是超级管理员则不判断权限 + if c.Member.Role == conf.MemberSuperRole { + book,err := models.NewBook().FindByFieldFirst("identify",identify) + if err != nil { + beego.Error(err) + c.JsonResult(6002, "项目不存在或权限不足") + } + book_id = book.BookId + }else{ + bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId) - bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId) - - if err != nil || bookResult.RoleId == conf.BookObserver { - beego.Error("FindByIdentify => ",err) - c.JsonResult(6002,"项目不存在或权限不足") + if err != nil || bookResult.RoleId == conf.BookObserver { + beego.Error("FindByIdentify => ", err) + c.JsonResult(6002, "项目不存在或权限不足") + } + book_id = bookResult.BookId } if parent_id > 0 { doc,err := models.NewDocument().Find(parent_id) - if err != nil || doc.BookId != bookResult.BookId{ + if err != nil || doc.BookId != book_id { c.JsonResult(6003,"父分类不存在") } } @@ -277,7 +304,7 @@ func (c *DocumentController) Create() { document,_ := models.NewDocument().Find(doc_id) document.MemberId = c.Member.MemberId - document.BookId = bookResult.BookId + document.BookId = book_id if doc_identify != ""{ document.Identify = doc_identify } @@ -330,26 +357,39 @@ func (c *DocumentController) Upload() { if !conf.IsAllowUploadFileExt(ext) { c.JsonResult(6004,"不允许的文件类型") } + book_id := 0 + //如果是超级管理员,则不判断权限 + if c.Member.Role == conf.MemberSuperRole { + book,err := models.NewBook().FindByFieldFirst("identify",identify) - book,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId) - - if err != nil { - beego.Error("DocumentController.Edit => ",err) - if err == orm.ErrNoRows { - c.JsonResult(6006,"权限不足") + if err != nil { + c.JsonResult(6006, "文档不存在或权限不足") } - c.JsonResult(6001,err.Error()) - } - //如果没有编辑权限 - if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder { - c.JsonResult(6006,"权限不足") + book_id = book.BookId + + }else{ + book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId) + + if err != nil { + beego.Error("DocumentController.Edit => ", err) + if err == orm.ErrNoRows { + c.JsonResult(6006, "权限不足") + } + c.JsonResult(6001, err.Error()) + } + //如果没有编辑权限 + if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder { + c.JsonResult(6006, "权限不足") + } + book_id = book.BookId } + if doc_id > 0 { doc,err := models.NewDocument().Find(doc_id); if err != nil { c.JsonResult(6007,"文档不存在") } - if doc.BookId != book.BookId { + if doc.BookId != book_id { c.JsonResult(6008,"文档不属于指定的项目") } } @@ -369,7 +409,7 @@ func (c *DocumentController) Upload() { c.JsonResult(6005,"保存文件失败") } attachment := models.NewAttachment() - attachment.BookId = book.BookId + attachment.BookId = book_id attachment.FileName = moreFile.Filename attachment.CreateAt = c.Member.MemberId attachment.FileExt = ext @@ -431,19 +471,23 @@ func (c *DocumentController) DownloadAttachment() { if err != nil { //判断项目公开状态 - book,err := models.NewBook().FindByFieldFirst("identify",identify) + book, err := models.NewBook().FindByFieldFirst("identify", identify) if err != nil { c.Abort("404") } - //如果项目是私有的,并且token不正确 - if (book.PrivatelyOwned == 1 && token == "" ) || ( book.PrivatelyOwned == 1 && book.PrivateToken != token ){ - c.Abort("403") + //如果不是超级管理员则判断权限 + if c.Member == nil || c.Member.Role != conf.MemberSuperRole { + //如果项目是私有的,并且token不正确 + if (book.PrivatelyOwned == 1 && token == "" ) || ( book.PrivatelyOwned == 1 && book.PrivateToken != token ) { + c.Abort("403") + } } + book_id = book.BookId }else{ book_id = bookResult.BookId } - + //查找附件 attachment,err := models.NewAttachment().Find(attach_id) if err != nil { @@ -469,11 +513,23 @@ func (c *DocumentController) Delete() { identify := c.GetString("identify") doc_id,err := c.GetInt("doc_id",0) - bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId) + book_id := 0 + //如果是超级管理员则忽略权限判断 + if c.Member.Role == conf.MemberSuperRole { + book,err := models.NewBook().FindByFieldFirst("identify",identify) + if err != nil { + beego.Error("FindByIdentify => ", err) + c.JsonResult(6002, "项目不存在或权限不足") + } + book_id = book.BookId + }else { + bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId) - if err != nil || bookResult.RoleId == conf.BookObserver { - beego.Error("FindByIdentify => ",err) - c.JsonResult(6002,"项目不存在或权限不足") + if err != nil || bookResult.RoleId == conf.BookObserver { + beego.Error("FindByIdentify => ", err) + c.JsonResult(6002, "项目不存在或权限不足") + } + book_id = bookResult.BookId } if doc_id <= 0 { @@ -486,9 +542,11 @@ func (c *DocumentController) Delete() { beego.Error("Delete => ",err) c.JsonResult(6003,"删除失败") } - if doc.BookId != bookResult.BookId { + //如果文档所属项目错误 + if doc.BookId != book_id { c.JsonResult(6004,"参数错误") } + //递归删除项目下的文档以及子文档 err = doc.RecursiveDocument(doc.DocumentId) if err != nil { c.JsonResult(6005,"删除失败") @@ -508,12 +566,22 @@ func (c *DocumentController) Content() { if err != nil { doc_id,_ = strconv.Atoi(c.Ctx.Input.Param(":id")) } + book_id := 0 + //如果是超级管理员,则忽略权限 + if c.Member.Role == conf.MemberSuperRole { + book ,err := models.NewBook().FindByFieldFirst("identify",identify) + if err != nil { + c.JsonResult(6002, "项目不存在或权限不足") + } + book_id = book.BookId + }else { + bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId) - bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId) - - if err != nil || bookResult.RoleId == conf.BookObserver { - beego.Error("FindByIdentify => ",err) - c.JsonResult(6002,"项目不存在或权限不足") + if err != nil || bookResult.RoleId == conf.BookObserver { + beego.Error("FindByIdentify => ", err) + c.JsonResult(6002, "项目不存在或权限不足") + } + book_id = bookResult.BookId } if doc_id <= 0 { @@ -531,7 +599,7 @@ func (c *DocumentController) Content() { if err != nil { c.JsonResult(6003,"读取文档错误") } - if doc.BookId != bookResult.BookId { + if doc.BookId != book_id { c.JsonResult(6004,"保存的文档不属于指定项目") } if doc.Version != version && !strings.EqualFold(is_cover,"yes"){ @@ -581,10 +649,6 @@ func (c *DocumentController) Export() { } book := isReadable(identify,token,c) - if book.PrivatelyOwned == 1 { - - } - docs, err := models.NewDocument().FindListByBookId(book.BookId) if err != nil { diff --git a/models/book.go b/models/book.go index f3a1f8e9..5150e11d 100644 --- a/models/book.go +++ b/models/book.go @@ -134,6 +134,14 @@ func (m *Book) FindByFieldFirst(field string,value interface{})(*Book,error) { } +func (m *Book) FindByIdentify(identify string) (*Book,error) { + o := orm.NewOrm() + + err := o.QueryTable(m.TableNameWithPrefix()).Filter("identify",identify).One(m) + + return m,err +} + //分页查询指定用户的项目 func (m *Book) FindToPager(pageIndex, pageSize ,memberId int) (books []*BookResult,totalCount int,err error){ diff --git a/static/css/kancloud.css b/static/css/kancloud.css index 0798b941..6f0a5e83 100644 --- a/static/css/kancloud.css +++ b/static/css/kancloud.css @@ -340,7 +340,11 @@ h6 { margin-left: auto!important; margin-right: auto!important } - +@media screen and (max-width: 840px) { + .manual-article .article-content{ + min-width: inherit; + } +} .manual-article .article-content .article-body{ min-height: 90px; padding: 5px; diff --git a/utils/template_fun.go b/utils/template_fun.go new file mode 100644 index 00000000..5b9b062a --- /dev/null +++ b/utils/template_fun.go @@ -0,0 +1,5 @@ +package utils + +func Asset(p string,cdn string) string { + return cdn + p; +} diff --git a/views/book/index.tpl b/views/book/index.tpl index 48773686..cd20429c 100644 --- a/views/book/index.tpl +++ b/views/book/index.tpl @@ -5,13 +5,13 @@ - 我的文档 - Powered by MinDoc + 我的项目 - Powered by MinDoc - - + + - + - - - - - + + + + + - - - - + + + + + \ No newline at end of file