1
0

DocumentController.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "image/png"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/astaxie/beego"
  16. "github.com/astaxie/beego/logs"
  17. "github.com/astaxie/beego/orm"
  18. "github.com/beego/i18n"
  19. "github.com/boombuler/barcode"
  20. "github.com/boombuler/barcode/qr"
  21. "github.com/mindoc-org/mindoc/conf"
  22. "github.com/mindoc-org/mindoc/models"
  23. "github.com/mindoc-org/mindoc/utils"
  24. "github.com/mindoc-org/mindoc/utils/cryptil"
  25. "github.com/mindoc-org/mindoc/utils/filetil"
  26. "github.com/mindoc-org/mindoc/utils/gopool"
  27. "github.com/mindoc-org/mindoc/utils/pagination"
  28. "gopkg.in/russross/blackfriday.v2"
  29. )
  30. // DocumentController struct
  31. type DocumentController struct {
  32. BaseController
  33. }
  34. // 文档首页
  35. func (c *DocumentController) Index() {
  36. c.Prepare()
  37. identify := c.Ctx.Input.Param(":key")
  38. token := c.GetString("token")
  39. if identify == "" {
  40. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.item_not_exist"))
  41. }
  42. // 如果没有开启匿名访问则跳转到登录
  43. if !c.EnableAnonymous && !c.isUserLoggedIn() {
  44. promptUserToLogIn(c)
  45. return
  46. }
  47. bookResult := c.isReadable(identify, token)
  48. c.TplName = "document/" + bookResult.Theme + "_read.tpl"
  49. selected := 0
  50. if bookResult.IsUseFirstDocument {
  51. doc, err := bookResult.FindFirstDocumentByBookId(bookResult.BookId)
  52. if err == nil {
  53. selected = doc.DocumentId
  54. c.Data["Title"] = doc.DocumentName
  55. c.Data["Content"] = template.HTML(doc.Release)
  56. c.Data["Description"] = utils.AutoSummary(doc.Release, 120)
  57. }
  58. } else {
  59. c.Data["Title"] = i18n.Tr(c.Lang, "blog.summary")
  60. c.Data["Content"] = template.HTML(blackfriday.Run([]byte(bookResult.Description)))
  61. }
  62. tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, selected)
  63. if err != nil {
  64. if err == orm.ErrNoRows {
  65. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.no_doc_in_cur_proj"))
  66. } else {
  67. logs.Error("生成项目文档树时出错 -> ", err)
  68. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.build_doc_tree_error"))
  69. }
  70. }
  71. c.Data["Model"] = bookResult
  72. c.Data["Result"] = template.HTML(tree)
  73. }
  74. // 阅读文档
  75. func (c *DocumentController) Read() {
  76. c.Prepare()
  77. identify := c.Ctx.Input.Param(":key")
  78. token := c.GetString("token")
  79. id := c.GetString(":id")
  80. c.Data["DocumentId"] = id
  81. if identify == "" || id == "" {
  82. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.item_not_exist"))
  83. }
  84. // 如果没有开启匿名访问则跳转到登录
  85. if !c.EnableAnonymous && !c.isUserLoggedIn() {
  86. promptUserToLogIn(c)
  87. return
  88. }
  89. bookResult := c.isReadable(identify, token)
  90. c.TplName = fmt.Sprintf("document/%s_read.tpl", bookResult.Theme)
  91. doc := models.NewDocument()
  92. if docId, err := strconv.Atoi(id); err == nil {
  93. doc, err = doc.FromCacheById(docId)
  94. if err != nil || doc == nil {
  95. logs.Error("从缓存中读取文档时失败 ->", err)
  96. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.doc_not_exist"))
  97. return
  98. }
  99. } else {
  100. doc, err = doc.FromCacheByIdentify(id, bookResult.BookId)
  101. if err != nil || doc == nil {
  102. if err == orm.ErrNoRows {
  103. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.doc_not_exist"))
  104. } else {
  105. logs.Error("从数据库查询文档时出错 ->", err)
  106. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.unknown_exception"))
  107. }
  108. return
  109. }
  110. }
  111. if doc.BookId != bookResult.BookId {
  112. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.doc_not_exist"))
  113. }
  114. doc.Lang = c.Lang
  115. doc.Processor()
  116. attach, err := models.NewAttachment().FindListByDocumentId(doc.DocumentId)
  117. if err == nil {
  118. doc.AttachList = attach
  119. }
  120. if c.IsAjax() {
  121. var data struct {
  122. DocTitle string `json:"doc_title"`
  123. Body string `json:"body"`
  124. Title string `json:"title"`
  125. Version int64 `json:"version"`
  126. }
  127. data.DocTitle = doc.DocumentName
  128. data.Body = doc.Release
  129. data.Title = doc.DocumentName + " - Powered by MinDoc"
  130. data.Version = doc.Version
  131. c.JsonResult(0, "ok", data)
  132. }
  133. tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, doc.DocumentId)
  134. if err != nil && err != orm.ErrNoRows {
  135. logs.Error("生成项目文档树时出错 ->", err)
  136. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.build_doc_tree_error"))
  137. }
  138. c.Data["Description"] = utils.AutoSummary(doc.Release, 120)
  139. c.Data["Model"] = bookResult
  140. c.Data["Result"] = template.HTML(tree)
  141. c.Data["Title"] = doc.DocumentName
  142. c.Data["Content"] = template.HTML(doc.Release)
  143. }
  144. // 编辑文档
  145. func (c *DocumentController) Edit() {
  146. c.Prepare()
  147. identify := c.Ctx.Input.Param(":key")
  148. if identify == "" {
  149. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.project_id_error"))
  150. }
  151. bookResult := models.NewBookResult()
  152. var err error
  153. // 如果是管理者,则不判断权限
  154. if c.Member.IsAdministrator() {
  155. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  156. if err != nil {
  157. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  158. }
  159. bookResult = models.NewBookResult().ToBookResult(*book)
  160. } else {
  161. bookResult, err = models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  162. if err != nil {
  163. if err == orm.ErrNoRows || err == models.ErrPermissionDenied {
  164. c.ShowErrorPage(403, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  165. } else {
  166. logs.Error("查询项目时出错 -> ", err)
  167. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.system_error"))
  168. }
  169. return
  170. }
  171. if bookResult.RoleId == conf.BookObserver {
  172. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  173. }
  174. }
  175. // 根据不同编辑器类型加载编辑器
  176. if bookResult.Editor == "markdown" {
  177. c.TplName = "document/markdown_edit_template.tpl"
  178. } else if bookResult.Editor == "html" {
  179. c.TplName = "document/new_html_edit_template.tpl"
  180. } else {
  181. c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl"
  182. }
  183. c.Data["Model"] = bookResult
  184. r, _ := json.Marshal(bookResult)
  185. c.Data["ModelResult"] = template.JS(string(r))
  186. c.Data["Result"] = template.JS("[]")
  187. trees, err := models.NewDocument().FindDocumentTree(bookResult.BookId)
  188. if err != nil {
  189. logs.Error("FindDocumentTree => ", err)
  190. } else {
  191. if len(trees) > 0 {
  192. if jtree, err := json.Marshal(trees); err == nil {
  193. c.Data["Result"] = template.JS(string(jtree))
  194. }
  195. } else {
  196. c.Data["Result"] = template.JS("[]")
  197. }
  198. }
  199. c.Data["BaiDuMapKey"] = beego.AppConfig.DefaultString("baidumapkey", "")
  200. if conf.GetUploadFileSize() > 0 {
  201. c.Data["UploadFileSize"] = conf.GetUploadFileSize()
  202. } else {
  203. c.Data["UploadFileSize"] = "undefined"
  204. }
  205. }
  206. // 创建一个文档
  207. func (c *DocumentController) Create() {
  208. identify := c.GetString("identify")
  209. docIdentify := c.GetString("doc_identify")
  210. docName := c.GetString("doc_name")
  211. parentId, _ := c.GetInt("parent_id", 0)
  212. docId, _ := c.GetInt("doc_id", 0)
  213. isOpen, _ := c.GetInt("is_open", 0)
  214. if identify == "" {
  215. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  216. }
  217. if docName == "" {
  218. c.JsonResult(6004, i18n.Tr(c.Lang, "message.doc_name_empty"))
  219. }
  220. bookId := 0
  221. // 如果是超级管理员则不判断权限
  222. if c.Member.IsAdministrator() {
  223. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  224. if err != nil {
  225. logs.Error(err)
  226. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_existed_or_no_permit"))
  227. }
  228. bookId = book.BookId
  229. } else {
  230. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  231. if err != nil || bookResult.RoleId == conf.BookObserver {
  232. logs.Error("FindByIdentify => ", err)
  233. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_existed_or_no_permit"))
  234. }
  235. bookId = bookResult.BookId
  236. }
  237. if docIdentify != "" {
  238. if ok, err := regexp.MatchString(`[a-z]+[a-zA-Z0-9_.\-]*$`, docIdentify); !ok || err != nil {
  239. c.JsonResult(6003, i18n.Tr(c.Lang, "message.project_id_tips"))
  240. }
  241. d, _ := models.NewDocument().FindByIdentityFirst(docIdentify, bookId)
  242. if d.DocumentId > 0 && d.DocumentId != docId {
  243. c.JsonResult(6006, i18n.Tr(c.Lang, "message.project_id_existed"))
  244. }
  245. }
  246. if parentId > 0 {
  247. doc, err := models.NewDocument().Find(parentId)
  248. if err != nil || doc.BookId != bookId {
  249. c.JsonResult(6003, i18n.Tr(c.Lang, "message.parent_id_not_existed"))
  250. }
  251. }
  252. document, _ := models.NewDocument().Find(docId)
  253. document.MemberId = c.Member.MemberId
  254. document.BookId = bookId
  255. document.Identify = docIdentify
  256. document.Version = time.Now().Unix()
  257. document.DocumentName = docName
  258. document.ParentId = parentId
  259. if isOpen == 1 {
  260. document.IsOpen = 1
  261. } else if isOpen == 2 {
  262. document.IsOpen = 2
  263. } else {
  264. document.IsOpen = 0
  265. }
  266. if err := document.InsertOrUpdate(); err != nil {
  267. logs.Error("添加或更新文档时出错 -> ", err)
  268. c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
  269. } else {
  270. c.JsonResult(0, "ok", document)
  271. }
  272. }
  273. // 上传附件或图片
  274. func (c *DocumentController) Upload() {
  275. identify := c.GetString("identify")
  276. docId, _ := c.GetInt("doc_id")
  277. isAttach := true
  278. if identify == "" {
  279. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  280. }
  281. name := "editormd-file-file"
  282. file, moreFile, err := c.GetFile(name)
  283. if err == http.ErrMissingFile || moreFile == nil {
  284. name = "editormd-image-file"
  285. file, moreFile, err = c.GetFile(name)
  286. if err == http.ErrMissingFile || moreFile == nil {
  287. c.JsonResult(6003, i18n.Tr(c.Lang, "message.upload_file_empty"))
  288. return
  289. }
  290. }
  291. if err != nil {
  292. c.JsonResult(6002, err.Error())
  293. }
  294. defer file.Close()
  295. type Size interface {
  296. Size() int64
  297. }
  298. if conf.GetUploadFileSize() > 0 && moreFile.Size > conf.GetUploadFileSize() {
  299. c.JsonResult(6009, i18n.Tr(c.Lang, "message.upload_file_size_limit"))
  300. }
  301. ext := filepath.Ext(moreFile.Filename)
  302. //文件必须带有后缀名
  303. if ext == "" {
  304. c.JsonResult(6003, i18n.Tr(c.Lang, "message.upload_file_type_error"))
  305. }
  306. //如果文件类型设置为 * 标识不限制文件类型
  307. if conf.IsAllowUploadFileExt(ext) == false {
  308. c.JsonResult(6004, i18n.Tr(c.Lang, "message.upload_file_type_error"))
  309. }
  310. bookId := 0
  311. // 如果是超级管理员,则不判断权限
  312. if c.Member.IsAdministrator() {
  313. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  314. if err != nil {
  315. c.JsonResult(6006, i18n.Tr(c.Lang, "message.doc_not_exist_or_no_permit"))
  316. }
  317. bookId = book.BookId
  318. } else {
  319. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  320. if err != nil {
  321. logs.Error("DocumentController.Edit => ", err)
  322. if err == orm.ErrNoRows {
  323. c.JsonResult(6006, i18n.Tr(c.Lang, "message.no_permission"))
  324. }
  325. c.JsonResult(6001, err.Error())
  326. }
  327. // 如果没有编辑权限
  328. if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  329. c.JsonResult(6006, i18n.Tr(c.Lang, "message.no_permission"))
  330. }
  331. bookId = book.BookId
  332. }
  333. if docId > 0 {
  334. doc, err := models.NewDocument().Find(docId)
  335. if err != nil {
  336. c.JsonResult(6007, i18n.Tr(c.Lang, "message.doc_not_exist"))
  337. }
  338. if doc.BookId != bookId {
  339. c.JsonResult(6008, i18n.Tr(c.Lang, "message.doc_not_belong_project"))
  340. }
  341. }
  342. fileName := "m_" + cryptil.UniqueId() + "_r"
  343. filePath := filepath.Join(conf.WorkingDirectory, "uploads", identify)
  344. //将图片和文件分开存放
  345. if filetil.IsImageExt(moreFile.Filename) {
  346. filePath = filepath.Join(filePath, "images", fileName+ext)
  347. } else {
  348. filePath = filepath.Join(filePath, "files", fileName+ext)
  349. }
  350. path := filepath.Dir(filePath)
  351. _ = os.MkdirAll(path, os.ModePerm)
  352. err = c.SaveToFile(name, filePath)
  353. if err != nil {
  354. logs.Error("保存文件失败 -> ", err)
  355. c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
  356. }
  357. attachment := models.NewAttachment()
  358. attachment.BookId = bookId
  359. attachment.FileName = moreFile.Filename
  360. attachment.CreateAt = c.Member.MemberId
  361. attachment.FileExt = ext
  362. attachment.FilePath = strings.TrimPrefix(filePath, conf.WorkingDirectory)
  363. attachment.DocumentId = docId
  364. if fileInfo, err := os.Stat(filePath); err == nil {
  365. attachment.FileSize = float64(fileInfo.Size())
  366. }
  367. if docId > 0 {
  368. attachment.DocumentId = docId
  369. }
  370. if filetil.IsImageExt(moreFile.Filename) {
  371. attachment.HttpPath = "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  372. if strings.HasPrefix(attachment.HttpPath, "//") {
  373. attachment.HttpPath = conf.URLForWithCdnImage(string(attachment.HttpPath[1:]))
  374. }
  375. isAttach = false
  376. }
  377. err = attachment.Insert()
  378. if err != nil {
  379. os.Remove(filePath)
  380. logs.Error("文件保存失败 ->", err)
  381. c.JsonResult(6006, i18n.Tr(c.Lang, "message.failed"))
  382. }
  383. if attachment.HttpPath == "" {
  384. attachment.HttpPath = conf.URLForNotHost("DocumentController.DownloadAttachment", ":key", identify, ":attach_id", attachment.AttachmentId)
  385. if err := attachment.Update(); err != nil {
  386. logs.Error("保存文件失败 ->", err)
  387. c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
  388. }
  389. }
  390. result := map[string]interface{}{
  391. "errcode": 0,
  392. "success": 1,
  393. "message": "ok",
  394. "url": attachment.HttpPath,
  395. "alt": attachment.FileName,
  396. "is_attach": isAttach,
  397. "attach": attachment,
  398. }
  399. c.Ctx.Output.JSON(result, true, false)
  400. c.StopRun()
  401. }
  402. // 下载附件
  403. func (c *DocumentController) DownloadAttachment() {
  404. c.Prepare()
  405. identify := c.Ctx.Input.Param(":key")
  406. attachId, _ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
  407. token := c.GetString("token")
  408. memberId := 0
  409. if c.Member != nil {
  410. memberId = c.Member.MemberId
  411. }
  412. bookId := 0
  413. // 判断用户是否参与了项目
  414. bookResult, err := models.NewBookResult().FindByIdentify(identify, memberId)
  415. if err != nil {
  416. // 判断项目公开状态
  417. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  418. if err != nil {
  419. if err == orm.ErrNoRows {
  420. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.item_not_exist"))
  421. } else {
  422. logs.Error("查找项目时出错 ->", err)
  423. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.system_error"))
  424. }
  425. }
  426. // 如果不是超级管理员则判断权限
  427. if c.Member == nil || c.Member.Role != conf.MemberSuperRole {
  428. // 如果项目是私有的,并且 token 不正确
  429. if (book.PrivatelyOwned == 1 && token == "") || (book.PrivatelyOwned == 1 && book.PrivateToken != token) {
  430. c.ShowErrorPage(403, i18n.Tr(c.Lang, "message.no_permission"))
  431. }
  432. }
  433. bookId = book.BookId
  434. } else {
  435. bookId = bookResult.BookId
  436. }
  437. // 查找附件
  438. attachment, err := models.NewAttachment().Find(attachId)
  439. if err != nil {
  440. logs.Error("查找附件时出错 -> ", err)
  441. if err == orm.ErrNoRows {
  442. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.attachment_not_exist"))
  443. } else {
  444. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.system_error"))
  445. }
  446. }
  447. if attachment.BookId != bookId {
  448. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.attachment_not_exist"))
  449. }
  450. c.Ctx.Output.Download(filepath.Join(conf.WorkingDirectory, attachment.FilePath), attachment.FileName)
  451. c.StopRun()
  452. }
  453. // 删除附件
  454. func (c *DocumentController) RemoveAttachment() {
  455. c.Prepare()
  456. attachId, _ := c.GetInt("attach_id")
  457. if attachId <= 0 {
  458. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  459. }
  460. attach, err := models.NewAttachment().Find(attachId)
  461. if err != nil {
  462. logs.Error(err)
  463. c.JsonResult(6002, i18n.Tr(c.Lang, "message.attachment_not_exist"))
  464. }
  465. document, err := models.NewDocument().Find(attach.DocumentId)
  466. if err != nil {
  467. logs.Error(err)
  468. c.JsonResult(6003, i18n.Tr(c.Lang, "message.doc_not_exist"))
  469. }
  470. if c.Member.Role != conf.MemberSuperRole {
  471. rel, err := models.NewRelationship().FindByBookIdAndMemberId(document.BookId, c.Member.MemberId)
  472. if err != nil {
  473. logs.Error(err)
  474. c.JsonResult(6004, i18n.Tr(c.Lang, "message.no_permission"))
  475. }
  476. if rel.RoleId == conf.BookObserver {
  477. c.JsonResult(6004, i18n.Tr(c.Lang, "message.no_permission"))
  478. }
  479. }
  480. err = attach.Delete()
  481. if err != nil {
  482. logs.Error(err)
  483. c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
  484. }
  485. os.Remove(filepath.Join(conf.WorkingDirectory, attach.FilePath))
  486. c.JsonResult(0, "ok", attach)
  487. }
  488. // 删除文档
  489. func (c *DocumentController) Delete() {
  490. c.Prepare()
  491. identify := c.GetString("identify")
  492. docId, err := c.GetInt("doc_id", 0)
  493. bookId := 0
  494. // 如果是超级管理员则忽略权限判断
  495. if c.Member.IsAdministrator() {
  496. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  497. if err != nil {
  498. logs.Error("FindByIdentify => ", err)
  499. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  500. }
  501. bookId = book.BookId
  502. } else {
  503. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  504. if err != nil || bookResult.RoleId == conf.BookObserver {
  505. logs.Error("FindByIdentify => ", err)
  506. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  507. }
  508. bookId = bookResult.BookId
  509. }
  510. if docId <= 0 {
  511. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  512. }
  513. doc, err := models.NewDocument().Find(docId)
  514. if err != nil {
  515. logs.Error("Delete => ", err)
  516. c.JsonResult(6003, i18n.Tr(c.Lang, "message.failed"))
  517. }
  518. // 如果文档所属项目错误
  519. if doc.BookId != bookId {
  520. c.JsonResult(6004, i18n.Tr(c.Lang, "message.param_error"))
  521. }
  522. // 递归删除项目下的文档以及子文档
  523. err = doc.RecursiveDocument(doc.DocumentId)
  524. if err != nil {
  525. c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
  526. }
  527. // 重置文档数量统计
  528. models.NewBook().ResetDocumentNumber(doc.BookId)
  529. c.JsonResult(0, "ok")
  530. }
  531. // 获取文档内容
  532. func (c *DocumentController) Content() {
  533. c.Prepare()
  534. identify := c.Ctx.Input.Param(":key")
  535. docId, err := c.GetInt("doc_id")
  536. if err != nil {
  537. docId, _ = strconv.Atoi(c.Ctx.Input.Param(":id"))
  538. }
  539. bookId := 0
  540. autoRelease := false
  541. // 如果是超级管理员,则忽略权限
  542. if c.Member.IsAdministrator() {
  543. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  544. if err != nil || book == nil {
  545. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  546. return
  547. }
  548. bookId = book.BookId
  549. autoRelease = book.AutoRelease == 1
  550. } else {
  551. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  552. if err != nil || bookResult.RoleId == conf.BookObserver {
  553. logs.Error("项目不存在或权限不足 -> ", err)
  554. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  555. }
  556. bookId = bookResult.BookId
  557. autoRelease = bookResult.AutoRelease
  558. }
  559. if docId <= 0 {
  560. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  561. }
  562. if c.Ctx.Input.IsPost() {
  563. markdown := strings.TrimSpace(c.GetString("markdown", ""))
  564. content := c.GetString("html")
  565. version, _ := c.GetInt64("version", 0)
  566. isCover := c.GetString("cover")
  567. doc, err := models.NewDocument().Find(docId)
  568. if err != nil || doc == nil {
  569. c.JsonResult(6003, i18n.Tr(c.Lang, "message.read_file_error"))
  570. return
  571. }
  572. if doc.BookId != bookId {
  573. c.JsonResult(6004, i18n.Tr(c.Lang, "message.dock_not_belong_project"))
  574. }
  575. if doc.Version != version && !strings.EqualFold(isCover, "yes") {
  576. logs.Info("%d|", version, doc.Version)
  577. c.JsonResult(6005, i18n.Tr(c.Lang, "message.confirm_override_doc"))
  578. }
  579. history := models.NewDocumentHistory()
  580. history.DocumentId = docId
  581. history.Content = doc.Content
  582. history.Markdown = doc.Markdown
  583. history.DocumentName = doc.DocumentName
  584. history.ModifyAt = c.Member.MemberId
  585. history.MemberId = doc.MemberId
  586. history.ParentId = doc.ParentId
  587. history.Version = time.Now().Unix()
  588. history.Action = "modify"
  589. history.ActionName = i18n.Tr(c.Lang, "doc.modify_doc")
  590. if markdown == "" && content != "" {
  591. doc.Markdown = content
  592. } else {
  593. doc.Markdown = markdown
  594. }
  595. doc.Version = time.Now().Unix()
  596. doc.Content = content
  597. doc.ModifyAt = c.Member.MemberId
  598. if err := doc.InsertOrUpdate(); err != nil {
  599. logs.Error("InsertOrUpdate => ", err)
  600. c.JsonResult(6006, i18n.Tr(c.Lang, "message.failed"))
  601. }
  602. // 如果启用了文档历史,则添加历史文档
  603. ///如果两次保存的MD5值不同则保存为历史,否则忽略
  604. go func(history *models.DocumentHistory) {
  605. if c.EnableDocumentHistory && cryptil.Md5Crypt(history.Markdown) != cryptil.Md5Crypt(doc.Markdown) {
  606. _, err = history.InsertOrUpdate()
  607. if err != nil {
  608. logs.Error("DocumentHistory InsertOrUpdate => ", err)
  609. }
  610. }
  611. }(history)
  612. //如果启用了自动发布
  613. if autoRelease {
  614. go func() {
  615. doc.Lang = c.Lang
  616. err := doc.ReleaseContent()
  617. if err == nil {
  618. logs.Informational(i18n.Tr(c.Lang, "message.doc_auto_published")+"-> document_id=%d;document_name=%s", doc.DocumentId, doc.DocumentName)
  619. }
  620. }()
  621. }
  622. c.JsonResult(0, "ok", doc)
  623. }
  624. doc, err := models.NewDocument().Find(docId)
  625. if err != nil {
  626. c.JsonResult(6003, i18n.Tr(c.Lang, "message.doc_not_exist"))
  627. return
  628. }
  629. attach, err := models.NewAttachment().FindListByDocumentId(doc.DocumentId)
  630. if err == nil {
  631. doc.AttachList = attach
  632. }
  633. c.JsonResult(0, "ok", doc)
  634. }
  635. // Export 导出
  636. func (c *DocumentController) Export() {
  637. c.Prepare()
  638. identify := c.Ctx.Input.Param(":key")
  639. if identify == "" {
  640. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.param_error"))
  641. }
  642. output := c.GetString("output")
  643. token := c.GetString("token")
  644. // 如果没有开启匿名访问则跳转到登录
  645. if !c.EnableAnonymous && !c.isUserLoggedIn() {
  646. promptUserToLogIn(c)
  647. return
  648. }
  649. if !conf.GetEnableExport() {
  650. c.ShowErrorPage(500, i18n.Tr(c.Lang, "export_func_disable"))
  651. }
  652. bookResult := models.NewBookResult()
  653. if c.Member != nil && c.Member.IsAdministrator() {
  654. book, err := models.NewBook().FindByIdentify(identify)
  655. if err != nil {
  656. if err == orm.ErrNoRows {
  657. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.item_not_exist"))
  658. } else {
  659. logs.Error("查找项目时出错 ->", err)
  660. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.system_error"))
  661. }
  662. }
  663. bookResult = models.NewBookResult().ToBookResult(*book)
  664. } else {
  665. bookResult = c.isReadable(identify, token)
  666. }
  667. if !bookResult.IsDownload {
  668. c.ShowErrorPage(200, i18n.Tr(c.Lang, "message.cur_project_export_func_disable"))
  669. }
  670. if !strings.HasPrefix(bookResult.Cover, "http:://") && !strings.HasPrefix(bookResult.Cover, "https:://") {
  671. bookResult.Cover = conf.URLForWithCdnImage(bookResult.Cover)
  672. }
  673. if output == "markdown" {
  674. if bookResult.Editor != "markdown" {
  675. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.cur_project_not_support_md"))
  676. }
  677. p, err := bookResult.ExportMarkdown(c.CruSession.SessionID())
  678. if err != nil {
  679. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.failed"))
  680. }
  681. c.Ctx.Output.Download(p, bookResult.BookName+".zip")
  682. c.StopRun()
  683. return
  684. }
  685. outputPath := filepath.Join(conf.GetExportOutputPath(), strconv.Itoa(bookResult.BookId))
  686. pdfpath := filepath.Join(outputPath, "book.pdf")
  687. epubpath := filepath.Join(outputPath, "book.epub")
  688. mobipath := filepath.Join(outputPath, "book.mobi")
  689. docxpath := filepath.Join(outputPath, "book.docx")
  690. if output == "pdf" && filetil.FileExists(pdfpath) {
  691. c.Ctx.Output.Download(pdfpath, bookResult.BookName+".pdf")
  692. c.Abort("200")
  693. } else if output == "epub" && filetil.FileExists(epubpath) {
  694. c.Ctx.Output.Download(epubpath, bookResult.BookName+".epub")
  695. c.Abort("200")
  696. } else if output == "mobi" && filetil.FileExists(mobipath) {
  697. c.Ctx.Output.Download(mobipath, bookResult.BookName+".mobi")
  698. c.Abort("200")
  699. } else if output == "docx" && filetil.FileExists(docxpath) {
  700. c.Ctx.Output.Download(docxpath, bookResult.BookName+".docx")
  701. c.Abort("200")
  702. } else if output == "pdf" || output == "epub" || output == "docx" || output == "mobi" {
  703. if err := models.BackgroundConvert(c.CruSession.SessionID(), bookResult); err != nil && err != gopool.ErrHandlerIsExist {
  704. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.export_failed"))
  705. }
  706. c.ShowErrorPage(200, i18n.Tr(c.Lang, "message.file_converting"))
  707. } else {
  708. c.ShowErrorPage(200, i18n.Tr(c.Lang, "message.unsupport_file_type"))
  709. }
  710. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.no_exportable_file"))
  711. }
  712. // 生成项目访问的二维码
  713. func (c *DocumentController) QrCode() {
  714. c.Prepare()
  715. identify := c.GetString(":key")
  716. book, err := models.NewBook().FindByIdentify(identify)
  717. if err != nil || book.BookId <= 0 {
  718. c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.item_not_exist"))
  719. }
  720. uri := conf.URLFor("DocumentController.Index", ":key", identify)
  721. code, err := qr.Encode(uri, qr.L, qr.Unicode)
  722. if err != nil {
  723. logs.Error("生成二维码失败 ->", err)
  724. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.gen_qrcode_failed"))
  725. }
  726. code, err = barcode.Scale(code, 150, 150)
  727. if err != nil {
  728. logs.Error("生成二维码失败 ->", err)
  729. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.gen_qrcode_failed"))
  730. }
  731. c.Ctx.ResponseWriter.Header().Set("Content-Type", "image/png")
  732. // imgpath := filepath.Join("cache","qrcode",identify + ".png")
  733. err = png.Encode(c.Ctx.ResponseWriter, code)
  734. if err != nil {
  735. logs.Error("生成二维码失败 ->", err)
  736. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.gen_qrcode_failed"))
  737. }
  738. }
  739. // 项目内搜索
  740. func (c *DocumentController) Search() {
  741. c.Prepare()
  742. identify := c.Ctx.Input.Param(":key")
  743. token := c.GetString("token")
  744. keyword := strings.TrimSpace(c.GetString("keyword"))
  745. if identify == "" {
  746. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  747. }
  748. if !c.EnableAnonymous && !c.isUserLoggedIn() {
  749. promptUserToLogIn(c)
  750. return
  751. }
  752. bookResult := c.isReadable(identify, token)
  753. docs, err := models.NewDocumentSearchResult().SearchDocument(keyword, bookResult.BookId)
  754. if err != nil {
  755. logs.Error(err)
  756. c.JsonResult(6002, i18n.Tr(c.Lang, "message.search_result_error"))
  757. }
  758. if len(docs) < 0 {
  759. c.JsonResult(404, i18n.Tr(c.Lang, "message.no_data"))
  760. }
  761. for _, doc := range docs {
  762. doc.BookId = bookResult.BookId
  763. doc.BookName = bookResult.BookName
  764. doc.Description = bookResult.Description
  765. doc.BookIdentify = bookResult.Identify
  766. }
  767. c.JsonResult(0, "ok", docs)
  768. }
  769. // 文档历史列表
  770. func (c *DocumentController) History() {
  771. c.Prepare()
  772. c.TplName = "document/history.tpl"
  773. identify := c.GetString("identify")
  774. docId, err := c.GetInt("doc_id", 0)
  775. pageIndex, _ := c.GetInt("page", 1)
  776. bookId := 0
  777. // 如果是超级管理员则忽略权限判断
  778. if c.Member.IsAdministrator() {
  779. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  780. if err != nil {
  781. logs.Error("查找项目失败 ->", err)
  782. c.Data["ErrorMessage"] = i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit")
  783. return
  784. }
  785. bookId = book.BookId
  786. c.Data["Model"] = book
  787. } else {
  788. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  789. if err != nil || bookResult.RoleId == conf.BookObserver {
  790. logs.Error("查找项目失败 ->", err)
  791. c.Data["ErrorMessage"] = i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit")
  792. return
  793. }
  794. bookId = bookResult.BookId
  795. c.Data["Model"] = bookResult
  796. }
  797. if docId <= 0 {
  798. c.Data["ErrorMessage"] = i18n.Tr(c.Lang, "message.param_error")
  799. return
  800. }
  801. doc, err := models.NewDocument().Find(docId)
  802. if err != nil {
  803. logs.Error("Delete => ", err)
  804. c.Data["ErrorMessage"] = i18n.Tr(c.Lang, "message.get_doc_his_failed")
  805. return
  806. }
  807. // 如果文档所属项目错误
  808. if doc.BookId != bookId {
  809. c.Data["ErrorMessage"] = i18n.Tr(c.Lang, "message.param_error")
  810. return
  811. }
  812. histories, totalCount, err := models.NewDocumentHistory().FindToPager(docId, pageIndex, conf.PageSize)
  813. if err != nil {
  814. logs.Error("分页查找文档历史失败 ->", err)
  815. c.Data["ErrorMessage"] = i18n.Tr(c.Lang, "message.get_doc_his_failed")
  816. return
  817. }
  818. c.Data["List"] = histories
  819. c.Data["PageHtml"] = ""
  820. c.Data["Document"] = doc
  821. if totalCount > 0 {
  822. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  823. c.Data["PageHtml"] = pager.HtmlPages()
  824. }
  825. }
  826. func (c *DocumentController) DeleteHistory() {
  827. c.Prepare()
  828. c.TplName = "document/history.tpl"
  829. identify := c.GetString("identify")
  830. docId, err := c.GetInt("doc_id", 0)
  831. historyId, _ := c.GetInt("history_id", 0)
  832. if historyId <= 0 {
  833. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  834. }
  835. bookId := 0
  836. // 如果是超级管理员则忽略权限判断
  837. if c.Member.IsAdministrator() {
  838. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  839. if err != nil {
  840. logs.Error("查找项目失败 ->", err)
  841. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  842. }
  843. bookId = book.BookId
  844. } else {
  845. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  846. if err != nil || bookResult.RoleId == conf.BookObserver {
  847. logs.Error("查找项目失败 ->", err)
  848. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  849. }
  850. bookId = bookResult.BookId
  851. }
  852. if docId <= 0 {
  853. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  854. }
  855. doc, err := models.NewDocument().Find(docId)
  856. if err != nil {
  857. logs.Error("Delete => ", err)
  858. c.JsonResult(6001, i18n.Tr(c.Lang, "message.get_doc_his_failed"))
  859. }
  860. // 如果文档所属项目错误
  861. if doc.BookId != bookId {
  862. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  863. }
  864. err = models.NewDocumentHistory().Delete(historyId, docId)
  865. if err != nil {
  866. logs.Error(err)
  867. c.JsonResult(6002, i18n.Tr(c.Lang, "message.failed"))
  868. }
  869. c.JsonResult(0, "ok")
  870. }
  871. //通过文档历史恢复文档
  872. func (c *DocumentController) RestoreHistory() {
  873. c.Prepare()
  874. c.TplName = "document/history.tpl"
  875. identify := c.GetString("identify")
  876. docId, err := c.GetInt("doc_id", 0)
  877. historyId, _ := c.GetInt("history_id", 0)
  878. if historyId <= 0 {
  879. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  880. }
  881. bookId := 0
  882. // 如果是超级管理员则忽略权限判断
  883. if c.Member.IsAdministrator() {
  884. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  885. if err != nil {
  886. logs.Error("FindByIdentify => ", err)
  887. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  888. }
  889. bookId = book.BookId
  890. } else {
  891. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  892. if err != nil || bookResult.RoleId == conf.BookObserver {
  893. logs.Error("FindByIdentify => ", err)
  894. c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
  895. }
  896. bookId = bookResult.BookId
  897. }
  898. if docId <= 0 {
  899. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  900. }
  901. doc, err := models.NewDocument().Find(docId)
  902. if err != nil {
  903. logs.Error("Delete => ", err)
  904. c.JsonResult(6001, i18n.Tr(c.Lang, "message.get_doc_his_failed"))
  905. }
  906. // 如果文档所属项目错误
  907. if doc.BookId != bookId {
  908. c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
  909. }
  910. err = models.NewDocumentHistory().Restore(historyId, docId, c.Member.MemberId)
  911. if err != nil {
  912. logs.Error(err)
  913. c.JsonResult(6002, i18n.Tr(c.Lang, "message.failed"))
  914. }
  915. c.JsonResult(0, "ok", doc)
  916. }
  917. func (c *DocumentController) Compare() {
  918. c.Prepare()
  919. c.TplName = "document/compare.tpl"
  920. historyId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  921. identify := c.Ctx.Input.Param(":key")
  922. bookId := 0
  923. editor := "markdown"
  924. // 如果是超级管理员则忽略权限判断
  925. if c.Member.IsAdministrator() {
  926. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  927. if err != nil {
  928. logs.Error("DocumentController.Compare => ", err)
  929. c.ShowErrorPage(403, i18n.Tr(c.Lang, "message.no_permission"))
  930. return
  931. }
  932. bookId = book.BookId
  933. c.Data["Model"] = book
  934. editor = book.Editor
  935. } else {
  936. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  937. if err != nil || bookResult.RoleId == conf.BookObserver {
  938. logs.Error("FindByIdentify => ", err)
  939. c.ShowErrorPage(403, i18n.Tr(c.Lang, "message.no_permission"))
  940. return
  941. }
  942. bookId = bookResult.BookId
  943. c.Data["Model"] = bookResult
  944. editor = bookResult.Editor
  945. }
  946. if historyId <= 0 {
  947. c.ShowErrorPage(60002, i18n.Tr(c.Lang, "message.param_error"))
  948. }
  949. history, err := models.NewDocumentHistory().Find(historyId)
  950. if err != nil {
  951. logs.Error("DocumentController.Compare => ", err)
  952. c.ShowErrorPage(60003, err.Error())
  953. }
  954. doc, err := models.NewDocument().Find(history.DocumentId)
  955. if err != nil || doc == nil || doc.BookId != bookId {
  956. c.ShowErrorPage(60002, i18n.Tr(c.Lang, "message.doc_not_exist"))
  957. return
  958. }
  959. c.Data["HistoryId"] = historyId
  960. c.Data["DocumentId"] = doc.DocumentId
  961. if editor == "markdown" {
  962. c.Data["HistoryContent"] = history.Markdown
  963. c.Data["Content"] = doc.Markdown
  964. } else {
  965. c.Data["HistoryContent"] = template.HTML(history.Content)
  966. c.Data["Content"] = template.HTML(doc.Content)
  967. }
  968. }
  969. // 判断用户是否可以阅读文档
  970. func (c *DocumentController) isReadable(identify, token string) *models.BookResult {
  971. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  972. if err != nil {
  973. logs.Error(err)
  974. c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.item_not_exist"))
  975. }
  976. bookResult := models.NewBookResult().ToBookResult(*book)
  977. isOk := false
  978. if c.isUserLoggedIn() {
  979. roleId, err := models.NewBook().FindForRoleId(book.BookId, c.Member.MemberId)
  980. if err == nil {
  981. isOk = true
  982. bookResult.MemberId = c.Member.MemberId
  983. bookResult.RoleId = roleId
  984. }
  985. }
  986. // 如果文档是私有的
  987. if book.PrivatelyOwned == 1 && (!c.isUserLoggedIn() || !c.Member.IsAdministrator()) {
  988. if s, ok := c.GetSession(identify).(string); !ok || (!strings.EqualFold(s, book.PrivateToken) && !strings.EqualFold(s, book.BookPassword)) {
  989. if book.PrivateToken != "" && !isOk && token != "" {
  990. // 如果有访问的 Token,并且该项目设置了访问 Token,并且和用户提供的相匹配,则记录到 Session 中。
  991. // 如果用户未提供 Token 且用户登录了,则判断用户是否参与了该项目。
  992. // 如果用户未登录,则从 Session 中读取 Token。
  993. if token != "" && strings.EqualFold(token, book.PrivateToken) {
  994. c.SetSession(identify, token)
  995. } else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
  996. logs.Info("尝试访问文档但权限不足 ->", identify, token)
  997. c.ShowErrorPage(403, i18n.Tr(c.Lang, "message.no_permission"))
  998. }
  999. } else if password := c.GetString("bPassword", ""); !isOk && book.BookPassword != "" && password != "" {
  1000. //如果设置了密码,则判断密码是否正确
  1001. if book.BookPassword != password {
  1002. c.JsonResult(5001, i18n.Tr(c.Lang, "message.wrong_password"))
  1003. } else {
  1004. c.SetSession(identify, password)
  1005. c.JsonResult(0, "OK")
  1006. }
  1007. } else if !isOk {
  1008. //如果设置了密码,则显示密码输入页面
  1009. if book.BookPassword != "" {
  1010. //判断已存在的密码是否正确
  1011. if password, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(password, book.BookPassword) {
  1012. body, err := c.ExecuteViewPathTemplate("document/document_password.tpl", map[string]string{"Identify": book.Identify})
  1013. if err != nil {
  1014. logs.Error("显示密码页面失败 ->", err)
  1015. }
  1016. c.CustomAbort(200, body)
  1017. }
  1018. } else {
  1019. logs.Info("尝试访问文档但权限不足 ->", identify, token)
  1020. c.ShowErrorPage(403, i18n.Tr(c.Lang, "message.no_permission"))
  1021. }
  1022. }
  1023. }
  1024. }
  1025. return bookResult
  1026. }
  1027. func promptUserToLogIn(c *DocumentController) {
  1028. logs.Info("Access " + c.Ctx.Request.URL.RequestURI() + " not permitted.")
  1029. logs.Info(" Access will be redirected to login page(SessionId: " + c.CruSession.SessionID() + ").")
  1030. if c.IsAjax() {
  1031. c.JsonResult(6000, i18n.Tr(c.Lang, "message.need_relogin"))
  1032. } else {
  1033. c.Redirect(conf.URLFor("AccountController.Login")+"?url="+url.PathEscape(conf.BaseUrl+c.Ctx.Request.URL.RequestURI()), 302)
  1034. }
  1035. }