ManagerController.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "html/template"
  5. "regexp"
  6. "strings"
  7. "math"
  8. "path/filepath"
  9. "strconv"
  10. "io/ioutil"
  11. "os"
  12. "github.com/astaxie/beego"
  13. "github.com/astaxie/beego/logs"
  14. "github.com/astaxie/beego/orm"
  15. "github.com/mindoc-org/mindoc/conf"
  16. "github.com/mindoc-org/mindoc/models"
  17. "github.com/mindoc-org/mindoc/utils"
  18. "github.com/mindoc-org/mindoc/utils/filetil"
  19. "github.com/mindoc-org/mindoc/utils/pagination"
  20. "gopkg.in/russross/blackfriday.v2"
  21. )
  22. type ManagerController struct {
  23. BaseController
  24. }
  25. func (c *ManagerController) Prepare() {
  26. c.BaseController.Prepare()
  27. if !c.Member.IsAdministrator() {
  28. c.Abort("403")
  29. }
  30. }
  31. func (c *ManagerController) Index() {
  32. c.TplName = "manager/index.tpl"
  33. c.Data["Model"] = models.NewDashboard().Query()
  34. }
  35. // 用户列表.
  36. func (c *ManagerController) Users() {
  37. c.Prepare()
  38. c.TplName = "manager/users.tpl"
  39. pageIndex, _ := c.GetInt("page", 0)
  40. members, totalCount, err := models.NewMember().FindToPager(pageIndex, conf.PageSize)
  41. if err != nil {
  42. c.Data["ErrorMessage"] = err.Error()
  43. return
  44. }
  45. if totalCount > 0 {
  46. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  47. c.Data["PageHtml"] = pager.HtmlPages()
  48. for _, item := range members {
  49. item.Avatar = conf.URLForWithCdnImage(item.Avatar)
  50. }
  51. } else {
  52. c.Data["PageHtml"] = ""
  53. }
  54. b, err := json.Marshal(members)
  55. if err != nil {
  56. c.Data["Result"] = template.JS("[]")
  57. } else {
  58. c.Data["Result"] = template.JS(string(b))
  59. }
  60. }
  61. // 添加用户.
  62. func (c *ManagerController) CreateMember() {
  63. c.Prepare()
  64. account := strings.TrimSpace(c.GetString("account"))
  65. password1 := strings.TrimSpace(c.GetString("password1"))
  66. password2 := strings.TrimSpace(c.GetString("password2"))
  67. email := strings.TrimSpace(c.GetString("email"))
  68. phone := strings.TrimSpace(c.GetString("phone"))
  69. role, _ := c.GetInt("role", 1)
  70. status, _ := c.GetInt("status", 0)
  71. if ok, err := regexp.MatchString(conf.RegexpAccount, account); account == "" || !ok || err != nil {
  72. c.JsonResult(6001, "账号只能由英文字母数字组成,且在3-50个字符")
  73. }
  74. if l := strings.Count(password1, ""); password1 == "" || l > 50 || l < 6 {
  75. c.JsonResult(6002, "密码必须在6-50个字符之间")
  76. }
  77. if password1 != password2 {
  78. c.JsonResult(6003, "确认密码不正确")
  79. }
  80. if ok, err := regexp.MatchString(conf.RegexpEmail, email); !ok || err != nil || email == "" {
  81. c.JsonResult(6004, "邮箱格式不正确")
  82. }
  83. if role != 0 && role != 1 && role != 2 {
  84. role = 1
  85. }
  86. if status != 0 && status != 1 {
  87. status = 0
  88. }
  89. member := models.NewMember()
  90. if _, err := member.FindByAccount(account); err == nil && member.MemberId > 0 {
  91. c.JsonResult(6005, "账号已存在")
  92. }
  93. member.Account = account
  94. member.Password = password1
  95. member.Role = conf.SystemRole(role)
  96. member.Avatar = conf.GetDefaultAvatar()
  97. member.CreateAt = c.Member.MemberId
  98. member.Email = email
  99. member.RealName = strings.TrimSpace(c.GetString("real_name", ""))
  100. if phone != "" {
  101. member.Phone = phone
  102. }
  103. if err := member.Add(); err != nil {
  104. c.JsonResult(6006, err.Error())
  105. }
  106. c.JsonResult(0, "ok", member)
  107. }
  108. //更新用户状态.
  109. func (c *ManagerController) UpdateMemberStatus() {
  110. c.Prepare()
  111. member_id, _ := c.GetInt("member_id", 0)
  112. status, _ := c.GetInt("status", 0)
  113. if member_id <= 0 {
  114. c.JsonResult(6001, "参数错误")
  115. }
  116. if status != 0 && status != 1 {
  117. status = 0
  118. }
  119. member := models.NewMember()
  120. if _, err := member.Find(member_id); err != nil {
  121. c.JsonResult(6002, "用户不存在")
  122. }
  123. if member.MemberId == c.Member.MemberId {
  124. c.JsonResult(6004, "不能变更自己的状态")
  125. }
  126. if member.Role == conf.MemberSuperRole {
  127. c.JsonResult(6005, "不能变更超级管理员的状态")
  128. }
  129. member.Status = status
  130. if err := member.Update(); err != nil {
  131. logs.Error("", err)
  132. c.JsonResult(6003, "用户状态设置失败")
  133. }
  134. c.JsonResult(0, "ok", member)
  135. }
  136. //变更用户权限.
  137. func (c *ManagerController) ChangeMemberRole() {
  138. c.Prepare()
  139. memberId, _ := c.GetInt("member_id", 0)
  140. role, _ := c.GetInt("role", 0)
  141. if memberId <= 0 {
  142. c.JsonResult(6001, "参数错误")
  143. }
  144. if role != int(conf.MemberAdminRole) && role != int(conf.MemberGeneralRole) {
  145. c.JsonResult(6001, "用户权限不正确")
  146. }
  147. member := models.NewMember()
  148. if _, err := member.Find(memberId); err != nil {
  149. c.JsonResult(6002, "用户不存在")
  150. }
  151. if member.MemberId == c.Member.MemberId {
  152. c.JsonResult(6004, "不能变更自己的权限")
  153. }
  154. if member.Role == conf.MemberSuperRole {
  155. c.JsonResult(6005, "不能变更超级管理员的权限")
  156. }
  157. member.Role = conf.SystemRole(role)
  158. if err := member.Update(); err != nil {
  159. c.JsonResult(6003, "用户权限设置失败")
  160. }
  161. member.Lang = c.Lang
  162. member.ResolveRoleName()
  163. c.JsonResult(0, "ok", member)
  164. }
  165. //编辑用户信息.
  166. func (c *ManagerController) EditMember() {
  167. c.Prepare()
  168. c.TplName = "manager/edit_users.tpl"
  169. member_id, _ := c.GetInt(":id", 0)
  170. if member_id <= 0 {
  171. c.Abort("404")
  172. }
  173. member, err := models.NewMember().Find(member_id)
  174. if err != nil {
  175. logs.Error(err)
  176. c.Abort("404")
  177. }
  178. if c.Ctx.Input.IsPost() {
  179. password1 := c.GetString("password1")
  180. password2 := c.GetString("password2")
  181. email := c.GetString("email")
  182. phone := c.GetString("phone")
  183. description := c.GetString("description")
  184. member.Email = email
  185. member.Phone = phone
  186. member.Description = description
  187. member.RealName = c.GetString("real_name")
  188. if password1 != "" && password2 != password1 {
  189. c.JsonResult(6001, "确认密码不正确")
  190. }
  191. if password1 != "" && member.AuthMethod != conf.AuthMethodLDAP {
  192. member.Password = password1
  193. }
  194. if err := member.Valid(password1 == ""); err != nil {
  195. c.JsonResult(6002, err.Error())
  196. }
  197. if password1 != "" {
  198. password, err := utils.PasswordHash(password1)
  199. if err != nil {
  200. logs.Error(err)
  201. c.JsonResult(6003, "对用户密码加密时出错")
  202. }
  203. member.Password = password
  204. }
  205. if err := member.Update(); err != nil {
  206. c.JsonResult(6004, err.Error())
  207. }
  208. c.JsonResult(0, "ok")
  209. }
  210. c.Data["Model"] = member
  211. }
  212. //删除一个用户,并将该用户的所有信息转移到超级管理员上.
  213. func (c *ManagerController) DeleteMember() {
  214. c.Prepare()
  215. member_id, _ := c.GetInt("id", 0)
  216. if member_id <= 0 {
  217. c.JsonResult(404, "参数错误")
  218. }
  219. member, err := models.NewMember().Find(member_id)
  220. if err != nil {
  221. logs.Error(err)
  222. c.JsonResult(500, "用户不存在")
  223. }
  224. if member.Role == conf.MemberSuperRole {
  225. c.JsonResult(500, "不能删除超级管理员")
  226. }
  227. superMember, err := models.NewMember().FindByFieldFirst("role", 0)
  228. if err != nil {
  229. logs.Error(err)
  230. c.JsonResult(5001, "未能找到超级管理员")
  231. }
  232. err = models.NewMember().Delete(member_id, superMember.MemberId)
  233. if err != nil {
  234. logs.Error(err)
  235. c.JsonResult(5002, "删除失败")
  236. }
  237. c.JsonResult(0, "ok")
  238. }
  239. //项目列表.
  240. func (c *ManagerController) Books() {
  241. c.Prepare()
  242. c.TplName = "manager/books.tpl"
  243. pageIndex, _ := c.GetInt("page", 1)
  244. books, totalCount, err := models.NewBookResult().FindToPager(pageIndex, conf.PageSize)
  245. if err != nil {
  246. c.Abort("500")
  247. }
  248. if totalCount > 0 {
  249. //html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 8, totalCount)
  250. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  251. c.Data["PageHtml"] = pager.HtmlPages()
  252. } else {
  253. c.Data["PageHtml"] = ""
  254. }
  255. for i, book := range books {
  256. books[i].Description = utils.StripTags(string(blackfriday.Run([]byte(book.Description))))
  257. books[i].ModifyTime = book.ModifyTime.Local()
  258. books[i].CreateTime = book.CreateTime.Local()
  259. }
  260. c.Data["Lists"] = books
  261. }
  262. //编辑项目.
  263. func (c *ManagerController) EditBook() {
  264. c.Prepare()
  265. c.TplName = "manager/edit_book.tpl"
  266. identify := c.GetString(":key")
  267. if identify == "" {
  268. c.Abort("404")
  269. }
  270. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  271. if err != nil {
  272. c.Abort("500")
  273. }
  274. if c.Ctx.Input.IsPost() {
  275. bookName := strings.TrimSpace(c.GetString("book_name"))
  276. description := strings.TrimSpace(c.GetString("description", ""))
  277. commentStatus := c.GetString("comment_status")
  278. tag := strings.TrimSpace(c.GetString("label"))
  279. orderIndex, _ := c.GetInt("order_index", 0)
  280. isDownload := strings.TrimSpace(c.GetString("is_download")) == "on"
  281. enableShare := strings.TrimSpace(c.GetString("enable_share")) == "on"
  282. isUseFirstDocument := strings.TrimSpace(c.GetString("is_use_first_document")) == "on"
  283. autoRelease := strings.TrimSpace(c.GetString("auto_release")) == "on"
  284. publisher := strings.TrimSpace(c.GetString("publisher"))
  285. historyCount, _ := c.GetInt("history_count", 0)
  286. itemId, _ := c.GetInt("itemId")
  287. if strings.Count(description, "") > 500 {
  288. c.JsonResult(6004, "项目描述不能大于500字")
  289. }
  290. if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
  291. commentStatus = "closed"
  292. }
  293. if tag != "" {
  294. tags := strings.Split(tag, ";")
  295. if len(tags) > 10 {
  296. c.JsonResult(6005, "最多允许添加10个标签")
  297. }
  298. }
  299. if !models.NewItemsets().Exist(itemId) {
  300. c.JsonResult(6006, "项目空间不存在")
  301. }
  302. book.Publisher = publisher
  303. book.HistoryCount = historyCount
  304. book.BookName = bookName
  305. book.Description = description
  306. book.CommentStatus = commentStatus
  307. book.Label = tag
  308. book.OrderIndex = orderIndex
  309. book.ItemId = itemId
  310. book.BookPassword = strings.TrimSpace(c.GetString("bPassword"))
  311. if autoRelease {
  312. book.AutoRelease = 1
  313. } else {
  314. book.AutoRelease = 0
  315. }
  316. if isDownload {
  317. book.IsDownload = 0
  318. } else {
  319. book.IsDownload = 1
  320. }
  321. if enableShare {
  322. book.IsEnableShare = 0
  323. } else {
  324. book.IsEnableShare = 1
  325. }
  326. if isUseFirstDocument {
  327. book.IsUseFirstDocument = 1
  328. } else {
  329. book.IsUseFirstDocument = 0
  330. }
  331. if err := book.Update(); err != nil {
  332. c.JsonResult(6006, "保存失败")
  333. }
  334. c.JsonResult(0, "ok")
  335. }
  336. if book.PrivateToken != "" {
  337. book.PrivateToken = conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  338. }
  339. bookResult := models.NewBookResult()
  340. bookResult.ToBookResult(*book)
  341. c.Data["Model"] = bookResult
  342. }
  343. // 删除项目.
  344. func (c *ManagerController) DeleteBook() {
  345. c.Prepare()
  346. bookId, _ := c.GetInt("book_id", 0)
  347. if bookId <= 0 {
  348. c.JsonResult(6001, "参数错误")
  349. }
  350. book := models.NewBook()
  351. err := book.ThoroughDeleteBook(bookId)
  352. if err == orm.ErrNoRows {
  353. c.JsonResult(6002, "项目不存在")
  354. }
  355. if err != nil {
  356. logs.Error("删除失败 -> ", err)
  357. c.JsonResult(6003, "删除失败")
  358. }
  359. c.JsonResult(0, "ok")
  360. }
  361. // CreateToken 创建访问来令牌.
  362. func (c *ManagerController) CreateToken() {
  363. c.Prepare()
  364. action := c.GetString("action")
  365. identify := c.GetString("identify")
  366. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  367. if err != nil {
  368. c.JsonResult(6001, "项目不存在")
  369. }
  370. if action == "create" {
  371. if book.PrivatelyOwned == 0 {
  372. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  373. }
  374. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  375. if err := book.Update(); err != nil {
  376. logs.Error("生成阅读令牌失败 => ", err)
  377. c.JsonResult(6003, "生成阅读令牌失败")
  378. }
  379. c.JsonResult(0, "ok", conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  380. } else {
  381. book.PrivateToken = ""
  382. if err := book.Update(); err != nil {
  383. logs.Error("CreateToken => ", err)
  384. c.JsonResult(6004, "删除令牌失败")
  385. }
  386. c.JsonResult(0, "ok", "")
  387. }
  388. }
  389. //项目设置.
  390. func (c *ManagerController) Setting() {
  391. c.Prepare()
  392. c.TplName = "manager/setting.tpl"
  393. options, err := models.NewOption().All()
  394. if c.Ctx.Input.IsPost() {
  395. for _, item := range options {
  396. item.OptionValue = c.GetString(item.OptionName)
  397. item.InsertOrUpdate()
  398. }
  399. c.JsonResult(0, "ok")
  400. }
  401. if err != nil {
  402. c.Abort("500")
  403. }
  404. c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
  405. for _, item := range options {
  406. c.Data[item.OptionName] = item.OptionValue
  407. }
  408. }
  409. // Transfer 转让项目.
  410. func (c *ManagerController) Transfer() {
  411. c.Prepare()
  412. account := c.GetString("account")
  413. if account == "" {
  414. c.JsonResult(6004, "接受者账号不能为空")
  415. }
  416. member, err := models.NewMember().FindByAccount(account)
  417. if err != nil {
  418. logs.Error("FindByAccount => ", err)
  419. c.JsonResult(6005, "接受用户不存在")
  420. }
  421. if member.Status != 0 {
  422. c.JsonResult(6006, "接受用户已被禁用")
  423. }
  424. if !c.Member.IsAdministrator() {
  425. c.Abort("403")
  426. }
  427. identify := c.GetString("identify")
  428. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  429. if err != nil {
  430. c.JsonResult(6001, err.Error())
  431. }
  432. rel, err := models.NewRelationship().FindFounder(book.BookId)
  433. if err != nil {
  434. logs.Error("FindFounder => ", err)
  435. c.JsonResult(6009, "查询项目创始人失败")
  436. }
  437. if member.MemberId == rel.MemberId {
  438. c.JsonResult(6007, "不能转让给自己")
  439. }
  440. err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
  441. if err != nil {
  442. logs.Error("Transfer => ", err)
  443. c.JsonResult(6008, err.Error())
  444. }
  445. c.JsonResult(0, "ok")
  446. }
  447. func (c *ManagerController) Comments() {
  448. c.Prepare()
  449. c.TplName = "manager/comments.tpl"
  450. if !c.Member.IsAdministrator() {
  451. c.Abort("403")
  452. }
  453. }
  454. //DeleteComment 标记评论为已删除
  455. func (c *ManagerController) DeleteComment() {
  456. c.Prepare()
  457. comment_id, _ := c.GetInt("comment_id", 0)
  458. if comment_id <= 0 {
  459. c.JsonResult(6001, "参数错误")
  460. }
  461. comment := models.NewComment()
  462. if _, err := comment.Find(comment_id); err != nil {
  463. c.JsonResult(6002, "评论不存在")
  464. }
  465. comment.Approved = 3
  466. if err := comment.Update("approved"); err != nil {
  467. c.JsonResult(6003, "删除评论失败")
  468. }
  469. c.JsonResult(0, "ok", comment)
  470. }
  471. //设置项目私有状态.
  472. func (c *ManagerController) PrivatelyOwned() {
  473. c.Prepare()
  474. status := c.GetString("status")
  475. identify := c.GetString("identify")
  476. if status != "open" && status != "close" {
  477. c.JsonResult(6003, "参数错误")
  478. }
  479. state := 0
  480. if status == "open" {
  481. state = 0
  482. } else {
  483. state = 1
  484. }
  485. if !c.Member.IsAdministrator() {
  486. c.Abort("403")
  487. }
  488. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  489. if err != nil {
  490. c.JsonResult(6001, err.Error())
  491. }
  492. book.PrivatelyOwned = state
  493. logs.Info("", state, status)
  494. err = book.Update()
  495. if err != nil {
  496. logs.Error("PrivatelyOwned => ", err)
  497. c.JsonResult(6004, "保存失败")
  498. }
  499. c.JsonResult(0, "ok")
  500. }
  501. //附件列表.
  502. func (c *ManagerController) AttachList() {
  503. c.Prepare()
  504. c.TplName = "manager/attach_list.tpl"
  505. pageIndex, _ := c.GetInt("page", 1)
  506. attachList, totalCount, err := models.NewAttachment().FindToPager(pageIndex, conf.PageSize)
  507. if err != nil {
  508. c.Abort("500")
  509. }
  510. if totalCount > 0 {
  511. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  512. c.Data["PageHtml"] = pager.HtmlPages()
  513. } else {
  514. c.Data["PageHtml"] = ""
  515. }
  516. for _, item := range attachList {
  517. p := filepath.Join(conf.WorkingDirectory, item.FilePath)
  518. item.IsExist = filetil.FileExists(p)
  519. }
  520. c.Data["Lists"] = attachList
  521. }
  522. //附件详情.
  523. func (c *ManagerController) AttachDetailed() {
  524. c.Prepare()
  525. c.TplName = "manager/attach_detailed.tpl"
  526. attach_id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  527. if attach_id <= 0 {
  528. c.Abort("404")
  529. }
  530. attach, err := models.NewAttachmentResult().Find(attach_id)
  531. if err != nil {
  532. logs.Error("AttachDetailed => ", err)
  533. if err == orm.ErrNoRows {
  534. c.Abort("404")
  535. } else {
  536. c.Abort("500")
  537. }
  538. }
  539. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  540. attach.HttpPath = conf.URLForWithCdnImage(attach.HttpPath)
  541. attach.IsExist = filetil.FileExists(attach.FilePath)
  542. c.Data["Model"] = attach
  543. }
  544. //删除附件.
  545. func (c *ManagerController) AttachDelete() {
  546. c.Prepare()
  547. attachId, _ := c.GetInt("attach_id")
  548. if attachId <= 0 {
  549. c.Abort("404")
  550. }
  551. attach, err := models.NewAttachment().Find(attachId)
  552. if err != nil {
  553. logs.Error("AttachDelete => ", err)
  554. c.JsonResult(6001, err.Error())
  555. }
  556. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  557. if err := attach.Delete(); err != nil {
  558. logs.Error("AttachDelete => ", err)
  559. c.JsonResult(6002, err.Error())
  560. }
  561. c.JsonResult(0, "ok")
  562. }
  563. //标签列表
  564. func (c *ManagerController) LabelList() {
  565. c.Prepare()
  566. c.TplName = "manager/label_list.tpl"
  567. pageIndex, _ := c.GetInt("page", 1)
  568. labels, totalCount, err := models.NewLabel().FindToPager(pageIndex, conf.PageSize)
  569. if err != nil {
  570. c.ShowErrorPage(50001, err.Error())
  571. }
  572. if totalCount > 0 {
  573. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  574. c.Data["PageHtml"] = pager.HtmlPages()
  575. } else {
  576. c.Data["PageHtml"] = ""
  577. }
  578. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
  579. c.Data["Lists"] = labels
  580. }
  581. //删除标签
  582. func (c *ManagerController) LabelDelete() {
  583. labelId, err := strconv.Atoi(c.Ctx.Input.Param(":id"))
  584. if err != nil {
  585. logs.Error("获取删除标签参数时出错:", err)
  586. c.JsonResult(50001, "参数错误")
  587. }
  588. if labelId <= 0 {
  589. c.JsonResult(50001, "参数错误")
  590. }
  591. label, err := models.NewLabel().FindFirst("label_id", labelId)
  592. if err != nil {
  593. logs.Error("查询标签时出错:", err)
  594. c.JsonResult(50001, "查询标签时出错:"+err.Error())
  595. }
  596. if err := label.Delete(); err != nil {
  597. c.JsonResult(50002, "删除失败:"+err.Error())
  598. } else {
  599. c.JsonResult(0, "ok")
  600. }
  601. }
  602. func (c *ManagerController) Config() {
  603. c.Prepare()
  604. c.TplName = "manager/config.tpl"
  605. if c.Ctx.Input.IsPost() {
  606. content := strings.TrimSpace(c.GetString("configFileTextArea"))
  607. if content == "" {
  608. c.JsonResult(500, "配置文件不能为空")
  609. }
  610. tf, err := ioutil.TempFile(os.TempDir(), "mindoc")
  611. if err != nil {
  612. logs.Error("创建临时文件失败 ->", err)
  613. c.JsonResult(5001, "创建临时文件失败")
  614. }
  615. defer tf.Close()
  616. tf.WriteString(content)
  617. err = beego.LoadAppConfig("ini", tf.Name())
  618. if err != nil {
  619. logs.Error("加载配置文件失败 ->", err)
  620. c.JsonResult(5002, "加载配置文件失败")
  621. }
  622. err = filetil.CopyFile(tf.Name(), conf.ConfigurationFile)
  623. if err != nil {
  624. logs.Error("保存配置文件失败 ->", err)
  625. c.JsonResult(5003, "保存配置文件失败")
  626. }
  627. c.JsonResult(0, "保存成功")
  628. }
  629. c.Data["ConfigContent"] = ""
  630. if b, err := ioutil.ReadFile(conf.ConfigurationFile); err == nil {
  631. c.Data["ConfigContent"] = string(b)
  632. }
  633. }
  634. func (c *ManagerController) Team() {
  635. c.Prepare()
  636. c.TplName = "manager/team.tpl"
  637. pageIndex, _ := c.GetInt("page", 0)
  638. teams, totalCount, err := models.NewTeam().FindToPager(pageIndex, conf.PageSize)
  639. if err != nil && err != orm.ErrNoRows {
  640. c.ShowErrorPage(500, err.Error())
  641. }
  642. if err == orm.ErrNoRows || len(teams) <= 0 {
  643. c.Data["Result"] = template.JS("[]")
  644. c.Data["PageHtml"] = ""
  645. return
  646. }
  647. if totalCount > 0 {
  648. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  649. c.Data["PageHtml"] = pager.HtmlPages()
  650. } else {
  651. c.Data["PageHtml"] = ""
  652. }
  653. b, err := json.Marshal(teams)
  654. if err != nil {
  655. c.Data["Result"] = template.JS("[]")
  656. } else {
  657. c.Data["Result"] = template.JS(string(b))
  658. }
  659. }
  660. func (c *ManagerController) TeamCreate() {
  661. c.Prepare()
  662. teamName := c.GetString("teamName")
  663. if teamName == "" {
  664. c.JsonResult(5001, "团队名称不能为空")
  665. }
  666. team := models.NewTeam()
  667. team.MemberId = c.Member.MemberId
  668. team.TeamName = teamName
  669. if err := team.Save(); err == nil {
  670. c.JsonResult(0, "OK", team)
  671. } else {
  672. c.JsonResult(5002, err.Error())
  673. }
  674. }
  675. func (c *ManagerController) TeamEdit() {
  676. c.Prepare()
  677. teamName := c.GetString("teamName")
  678. teamId, _ := c.GetInt("teamId")
  679. if teamName == "" {
  680. c.JsonResult(5001, "团队名称不能为空")
  681. }
  682. if teamId <= 0 {
  683. c.JsonResult(5002, "团队标识不能为空")
  684. }
  685. team, err := models.NewTeam().First(teamId)
  686. c.CheckJsonError(5003, err)
  687. team.TeamName = teamName
  688. err = team.Save()
  689. c.CheckJsonError(5004, err)
  690. c.JsonResult(0, "OK", team)
  691. }
  692. func (c *ManagerController) TeamDelete() {
  693. c.Prepare()
  694. teamId, _ := c.GetInt("teamId")
  695. if teamId <= 0 {
  696. c.JsonResult(5002, "团队标识不能为空")
  697. }
  698. err := models.NewTeam().Delete(teamId)
  699. c.CheckJsonError(5001, err)
  700. c.JsonResult(0, "OK")
  701. }
  702. func (c *ManagerController) TeamMemberList() {
  703. c.Prepare()
  704. c.TplName = "manager/team_member_list.tpl"
  705. teamId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  706. pageIndex, _ := c.GetInt("page", 0)
  707. if teamId <= 0 {
  708. c.ShowErrorPage(500, "参数错误")
  709. }
  710. team, err := models.NewTeam().First(teamId)
  711. if err == orm.ErrNoRows {
  712. c.ShowErrorPage(404, "团队不存在")
  713. }
  714. c.CheckErrorResult(500, err)
  715. c.Data["Model"] = team
  716. teams, totalCount, err := models.NewTeamMember().FindToPager(teamId, pageIndex, conf.PageSize)
  717. if err != nil && err != orm.ErrNoRows {
  718. c.ShowErrorPage(500, err.Error())
  719. }
  720. if err == orm.ErrNoRows || len(teams) <= 0 {
  721. c.Data["Result"] = template.JS("[]")
  722. c.Data["PageHtml"] = ""
  723. return
  724. }
  725. if totalCount > 0 {
  726. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  727. c.Data["PageHtml"] = pager.HtmlPages()
  728. } else {
  729. c.Data["PageHtml"] = ""
  730. }
  731. b, err := json.Marshal(teams)
  732. if err != nil {
  733. logs.Error("编码 JSON 结果失败 ->", err)
  734. c.Data["Result"] = template.JS("[]")
  735. } else {
  736. c.Data["Result"] = template.JS(string(b))
  737. }
  738. }
  739. //搜索团队用户.
  740. func (c *ManagerController) TeamSearchMember() {
  741. c.Prepare()
  742. teamId, _ := c.GetInt("teamId")
  743. keyword := strings.TrimSpace(c.GetString("q"))
  744. if teamId <= 0 {
  745. c.JsonResult(500, "参数错误")
  746. }
  747. searchResult, err := models.NewTeamMember().FindNotJoinMemberByAccount(teamId, keyword, 10)
  748. if err != nil {
  749. c.JsonResult(500, err.Error())
  750. }
  751. c.JsonResult(0, "OK", searchResult)
  752. }
  753. func (c *ManagerController) TeamMemberAdd() {
  754. c.Prepare()
  755. teamId, _ := c.GetInt("teamId")
  756. memberId, _ := c.GetInt("memberId")
  757. roleId, _ := c.GetInt("roleId")
  758. if teamId <= 0 || memberId <= 0 || roleId <= 0 || roleId > int(conf.BookObserver) {
  759. c.JsonResult(5001, "参数不正确")
  760. }
  761. teamMember := models.NewTeamMember()
  762. teamMember.MemberId = memberId
  763. teamMember.TeamId = teamId
  764. teamMember.RoleId = conf.BookRole(roleId)
  765. if err := teamMember.Save(); err != nil {
  766. c.CheckJsonError(5001, err)
  767. }
  768. teamMember.Include()
  769. c.JsonResult(0, "OK", teamMember)
  770. }
  771. func (c *ManagerController) TeamMemberDelete() {
  772. c.Prepare()
  773. memberId, _ := c.GetInt("memberId")
  774. teamId, _ := c.GetInt("teamId")
  775. teamMember, err := models.NewTeamMember().FindFirst(teamId, memberId)
  776. if err != nil {
  777. c.JsonResult(5001, "用户不存在或已禁用")
  778. }
  779. err = teamMember.Delete(teamMember.TeamMemberId)
  780. if err != nil {
  781. c.JsonResult(5002, "删除失败")
  782. }
  783. c.JsonResult(0, "ok")
  784. }
  785. func (c *ManagerController) TeamChangeMemberRole() {
  786. c.Prepare()
  787. memberId, _ := c.GetInt("memberId")
  788. roleId, _ := c.GetInt("roleId")
  789. teamId, _ := c.GetInt("teamId")
  790. if memberId <= 0 || roleId <= 0 || teamId <= 0 || roleId > int(conf.BookObserver) {
  791. c.JsonResult(5001, "参数错误")
  792. }
  793. teamMember, err := models.NewTeamMember().ChangeRoleId(teamId, memberId, conf.BookRole(roleId))
  794. if err != nil {
  795. c.JsonResult(5002, err.Error())
  796. } else {
  797. c.JsonResult(0, "OK", teamMember)
  798. }
  799. }
  800. //团队项目列表.
  801. func (c *ManagerController) TeamBookList() {
  802. c.Prepare()
  803. c.TplName = "manager/team_book_list.tpl"
  804. teamId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  805. pageIndex, _ := c.GetInt("page", 0)
  806. if teamId <= 0 {
  807. c.JsonResult(5002, "团队标识不能为空")
  808. }
  809. team, err := models.NewTeam().First(teamId)
  810. if err == orm.ErrNoRows {
  811. c.ShowErrorPage(404, "团队不存在")
  812. }
  813. c.CheckErrorResult(500, err)
  814. c.Data["Model"] = team
  815. teams, totalCount, err := models.NewTeamRelationship().FindToPager(teamId, pageIndex, conf.PageSize)
  816. if err != nil && err != orm.ErrNoRows {
  817. c.ShowErrorPage(500, err.Error())
  818. }
  819. if err == orm.ErrNoRows || len(teams) <= 0 {
  820. c.Data["Result"] = template.JS("[]")
  821. c.Data["PageHtml"] = ""
  822. return
  823. }
  824. if totalCount > 0 {
  825. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  826. c.Data["PageHtml"] = pager.HtmlPages()
  827. } else {
  828. c.Data["PageHtml"] = ""
  829. }
  830. b, err := json.Marshal(teams)
  831. if err != nil {
  832. logs.Error("编码 JSON 结果失败 ->", err)
  833. c.Data["Result"] = template.JS("[]")
  834. } else {
  835. c.Data["Result"] = template.JS(string(b))
  836. }
  837. }
  838. //给团队增加项目.
  839. func (c *ManagerController) TeamBookAdd() {
  840. c.Prepare()
  841. teamId, _ := c.GetInt("teamId")
  842. bookId, _ := c.GetInt("bookId")
  843. if teamId <= 0 || bookId <= 0 {
  844. c.JsonResult(500, "参数错误")
  845. }
  846. teamRel := models.NewTeamRelationship()
  847. teamRel.BookId = bookId
  848. teamRel.TeamId = teamId
  849. err := teamRel.Save()
  850. if err != nil {
  851. c.JsonResult(5001, err.Error())
  852. } else {
  853. teamRel.Include()
  854. c.JsonResult(0, "OK", teamRel)
  855. }
  856. }
  857. //搜索未参与的项目.
  858. func (c *ManagerController) TeamSearchBook() {
  859. c.Prepare()
  860. teamId, _ := c.GetInt("teamId")
  861. keyword := strings.TrimSpace(c.GetString("q"))
  862. if teamId <= 0 {
  863. c.JsonResult(500, "参数错误")
  864. }
  865. searchResult, err := models.NewTeamRelationship().FindNotJoinBookByName(teamId, keyword, 10)
  866. if err != nil {
  867. c.JsonResult(500, err.Error())
  868. }
  869. c.JsonResult(0, "OK", searchResult)
  870. }
  871. //删除团队项目.
  872. func (c *ManagerController) TeamBookDelete() {
  873. c.Prepare()
  874. teamRelationshipId, _ := c.GetInt("teamRelId")
  875. if teamRelationshipId <= 0 {
  876. c.JsonResult(500, "参数错误")
  877. }
  878. err := models.NewTeamRelationship().Delete(teamRelationshipId)
  879. if err != nil {
  880. c.JsonResult(5001, "删除失败")
  881. }
  882. c.JsonResult(0, "OK")
  883. }
  884. //项目空间列表.
  885. func (c *ManagerController) Itemsets() {
  886. c.Prepare()
  887. c.TplName = "manager/itemsets.tpl"
  888. pageIndex, _ := c.GetInt("page", 0)
  889. items, totalCount, err := models.NewItemsets().FindToPager(pageIndex, conf.PageSize)
  890. if err != nil && err != orm.ErrNoRows {
  891. c.ShowErrorPage(500, err.Error())
  892. }
  893. if err == orm.ErrNoRows || len(items) <= 0 {
  894. c.Data["Lists"] = items
  895. c.Data["PageHtml"] = ""
  896. return
  897. }
  898. if totalCount > 0 {
  899. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  900. c.Data["PageHtml"] = pager.HtmlPages()
  901. } else {
  902. c.Data["PageHtml"] = ""
  903. }
  904. c.Data["Lists"] = items
  905. }
  906. //编辑或添加项目空间.
  907. func (c *ManagerController) ItemsetsEdit() {
  908. c.Prepare()
  909. itemId, _ := c.GetInt("itemId")
  910. itemName := strings.TrimSpace(c.GetString("itemName"))
  911. itemKey := strings.TrimSpace(c.GetString("itemKey"))
  912. if itemName == "" || itemKey == "" {
  913. c.JsonResult(5001, "参数错误")
  914. }
  915. var item *models.Itemsets
  916. var err error
  917. if itemId > 0 {
  918. if item, err = models.NewItemsets().First(itemId); err != nil {
  919. if err == orm.ErrNoRows {
  920. c.JsonResult(5002, "项目空间不存在")
  921. } else {
  922. c.JsonResult(5003, "查询项目空间出错")
  923. }
  924. }
  925. } else {
  926. item = models.NewItemsets()
  927. }
  928. item.ItemKey = itemKey
  929. item.ItemName = itemName
  930. item.MemberId = c.Member.MemberId
  931. item.ModifyAt = c.Member.MemberId
  932. if err := item.Save(); err != nil {
  933. c.JsonResult(5004, err.Error())
  934. }
  935. c.JsonResult(0, "OK")
  936. }
  937. //删除项目空间.
  938. func (c *ManagerController) ItemsetsDelete() {
  939. c.Prepare()
  940. itemId, _ := c.GetInt("itemId")
  941. if err := models.NewItemsets().Delete(itemId); err != nil {
  942. c.JsonResult(5001, err.Error())
  943. }
  944. c.JsonResult(0, "OK")
  945. }