1
0

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