manager.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "html/template"
  5. "regexp"
  6. "strings"
  7. "github.com/astaxie/beego"
  8. "github.com/astaxie/beego/logs"
  9. "github.com/astaxie/beego/orm"
  10. "github.com/lifei6671/mindoc/conf"
  11. "github.com/lifei6671/mindoc/models"
  12. "github.com/lifei6671/mindoc/utils"
  13. "path/filepath"
  14. "strconv"
  15. "github.com/lifei6671/mindoc/utils/pagination"
  16. "math"
  17. "gopkg.in/russross/blackfriday.v2"
  18. )
  19. type ManagerController struct {
  20. BaseController
  21. }
  22. func (c *ManagerController) Prepare() {
  23. c.BaseController.Prepare()
  24. if !c.Member.IsAdministrator() {
  25. c.Abort("403")
  26. }
  27. }
  28. func (c *ManagerController) Index() {
  29. c.TplName = "manager/index.tpl"
  30. c.Data["Model"] = models.NewDashboard().Query()
  31. }
  32. // 用户列表.
  33. func (c *ManagerController) Users() {
  34. c.Prepare()
  35. c.TplName = "manager/users.tpl"
  36. pageIndex, _ := c.GetInt("page", 0)
  37. members, totalCount, err := models.NewMember().FindToPager(pageIndex, conf.PageSize)
  38. if err != nil {
  39. c.Data["ErrorMessage"] = err.Error()
  40. return
  41. }
  42. if totalCount > 0 {
  43. pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize,c.BaseUrl())
  44. c.Data["PageHtml"] = pager.HtmlPages()
  45. } else {
  46. c.Data["PageHtml"] = ""
  47. }
  48. b, err := json.Marshal(members)
  49. if err != nil {
  50. c.Data["Result"] = template.JS("[]")
  51. } else {
  52. c.Data["Result"] = template.JS(string(b))
  53. }
  54. }
  55. // 添加用户.
  56. func (c *ManagerController) CreateMember() {
  57. c.Prepare()
  58. account := strings.TrimSpace(c.GetString("account"))
  59. password1 := strings.TrimSpace(c.GetString("password1"))
  60. password2 := strings.TrimSpace(c.GetString("password2"))
  61. email := strings.TrimSpace(c.GetString("email"))
  62. phone := strings.TrimSpace(c.GetString("phone"))
  63. role, _ := c.GetInt("role", 1)
  64. status, _ := c.GetInt("status", 0)
  65. if ok, err := regexp.MatchString(conf.RegexpAccount, account); account == "" || !ok || err != nil {
  66. c.JsonResult(6001, "账号只能由英文字母数字组成,且在3-50个字符")
  67. }
  68. if l := strings.Count(password1, ""); password1 == "" || l > 50 || l < 6 {
  69. c.JsonResult(6002, "密码必须在6-50个字符之间")
  70. }
  71. if password1 != password2 {
  72. c.JsonResult(6003, "确认密码不正确")
  73. }
  74. if ok, err := regexp.MatchString(conf.RegexpEmail, email); !ok || err != nil || email == "" {
  75. c.JsonResult(6004, "邮箱格式不正确")
  76. }
  77. if role != 0 && role != 1 && role != 2 {
  78. role = 1
  79. }
  80. if status != 0 && status != 1 {
  81. status = 0
  82. }
  83. member := models.NewMember()
  84. if _, err := member.FindByAccount(account); err == nil && member.MemberId > 0 {
  85. c.JsonResult(6005, "账号已存在")
  86. }
  87. member.Account = account
  88. member.Password = password1
  89. member.Role = role
  90. member.Avatar = conf.GetDefaultAvatar()
  91. member.CreateAt = c.Member.MemberId
  92. member.Email = email
  93. member.RealName = strings.TrimSpace(c.GetString("real_name",""))
  94. if phone != "" {
  95. member.Phone = phone
  96. }
  97. if err := member.Add(); err != nil {
  98. c.JsonResult(6006, err.Error())
  99. }
  100. c.JsonResult(0, "ok", member)
  101. }
  102. //更新用户状态.
  103. func (c *ManagerController) UpdateMemberStatus() {
  104. c.Prepare()
  105. member_id, _ := c.GetInt("member_id", 0)
  106. status, _ := c.GetInt("status", 0)
  107. if member_id <= 0 {
  108. c.JsonResult(6001, "参数错误")
  109. }
  110. if status != 0 && status != 1 {
  111. status = 0
  112. }
  113. member := models.NewMember()
  114. if _, err := member.Find(member_id); err != nil {
  115. c.JsonResult(6002, "用户不存在")
  116. }
  117. if member.MemberId == c.Member.MemberId {
  118. c.JsonResult(6004, "不能变更自己的状态")
  119. }
  120. if member.Role == conf.MemberSuperRole {
  121. c.JsonResult(6005, "不能变更超级管理员的状态")
  122. }
  123. member.Status = status
  124. if err := member.Update(); err != nil {
  125. logs.Error("", err)
  126. c.JsonResult(6003, "用户状态设置失败")
  127. }
  128. c.JsonResult(0, "ok", member)
  129. }
  130. //变更用户权限.
  131. func (c *ManagerController) ChangeMemberRole() {
  132. c.Prepare()
  133. member_id, _ := c.GetInt("member_id", 0)
  134. role, _ := c.GetInt("role", 0)
  135. if member_id <= 0 {
  136. c.JsonResult(6001, "参数错误")
  137. }
  138. if role != conf.MemberAdminRole && role != conf.MemberGeneralRole {
  139. c.JsonResult(6001, "用户权限不正确")
  140. }
  141. member := models.NewMember()
  142. if _, err := member.Find(member_id); err != nil {
  143. c.JsonResult(6002, "用户不存在")
  144. }
  145. if member.MemberId == c.Member.MemberId {
  146. c.JsonResult(6004, "不能变更自己的权限")
  147. }
  148. if member.Role == conf.MemberSuperRole {
  149. c.JsonResult(6005, "不能变更超级管理员的权限")
  150. }
  151. member.Role = role
  152. if err := member.Update(); err != nil {
  153. c.JsonResult(6003, "用户权限设置失败")
  154. }
  155. member.ResolveRoleName()
  156. c.JsonResult(0, "ok", member)
  157. }
  158. //编辑用户信息.
  159. func (c *ManagerController) EditMember() {
  160. c.Prepare()
  161. c.TplName = "manager/edit_users.tpl"
  162. member_id, _ := c.GetInt(":id", 0)
  163. if member_id <= 0 {
  164. c.Abort("404")
  165. }
  166. member, err := models.NewMember().Find(member_id)
  167. if err != nil {
  168. beego.Error(err)
  169. c.Abort("404")
  170. }
  171. if c.Ctx.Input.IsPost() {
  172. password1 := c.GetString("password1")
  173. password2 := c.GetString("password2")
  174. email := c.GetString("email")
  175. phone := c.GetString("phone")
  176. description := c.GetString("description")
  177. member.Email = email
  178. member.Phone = phone
  179. member.Description = description
  180. member.RealName = c.GetString("real_name")
  181. if password1 != "" && password2 != password1 {
  182. c.JsonResult(6001, "确认密码不正确")
  183. }
  184. if password1 != "" && member.AuthMethod != conf.AuthMethodLDAP {
  185. member.Password = password1
  186. }
  187. if err := member.Valid(password1 == ""); err != nil {
  188. c.JsonResult(6002, err.Error())
  189. }
  190. if password1 != "" {
  191. password, err := utils.PasswordHash(password1)
  192. if err != nil {
  193. beego.Error(err)
  194. c.JsonResult(6003, "对用户密码加密时出错")
  195. }
  196. member.Password = password
  197. }
  198. if err := member.Update(); err != nil {
  199. c.JsonResult(6004, err.Error())
  200. }
  201. c.JsonResult(0, "ok")
  202. }
  203. c.Data["Model"] = member
  204. }
  205. //删除一个用户,并将该用户的所有信息转移到超级管理员上.
  206. func (c *ManagerController) DeleteMember() {
  207. c.Prepare()
  208. member_id, _ := c.GetInt("id", 0)
  209. if member_id <= 0 {
  210. c.JsonResult(404, "参数错误")
  211. }
  212. member, err := models.NewMember().Find(member_id)
  213. if err != nil {
  214. beego.Error(err)
  215. c.JsonResult(500, "用户不存在")
  216. }
  217. if member.Role == conf.MemberSuperRole {
  218. c.JsonResult(500, "不能删除超级管理员")
  219. }
  220. superMember, err := models.NewMember().FindByFieldFirst("role", 0)
  221. if err != nil {
  222. beego.Error(err)
  223. c.JsonResult(5001, "未能找到超级管理员")
  224. }
  225. err = models.NewMember().Delete(member_id, superMember.MemberId)
  226. if err != nil {
  227. beego.Error(err)
  228. c.JsonResult(5002, "删除失败")
  229. }
  230. c.JsonResult(0, "ok")
  231. }
  232. //项目列表.
  233. func (c *ManagerController) Books() {
  234. c.Prepare()
  235. c.TplName = "manager/books.tpl"
  236. pageIndex, _ := c.GetInt("page", 1)
  237. books, totalCount, err := models.NewBookResult().FindToPager(pageIndex, conf.PageSize)
  238. if err != nil {
  239. c.Abort("500")
  240. }
  241. if totalCount > 0 {
  242. //html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 8, totalCount)
  243. pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize, c.BaseUrl())
  244. c.Data["PageHtml"] = pager.HtmlPages()
  245. } else {
  246. c.Data["PageHtml"] = ""
  247. }
  248. for i,book := range books {
  249. books[i].Description = utils.StripTags(string(blackfriday.Run([]byte(book.Description))))
  250. books[i].ModifyTime = book.ModifyTime.Local()
  251. books[i].CreateTime = book.CreateTime.Local()
  252. }
  253. c.Data["Lists"] = books
  254. }
  255. //编辑项目.
  256. func (c *ManagerController) EditBook() {
  257. c.Prepare()
  258. c.TplName = "manager/edit_book.tpl"
  259. identify := c.GetString(":key")
  260. if identify == "" {
  261. c.Abort("404")
  262. }
  263. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  264. if err != nil {
  265. c.Abort("500")
  266. }
  267. if c.Ctx.Input.IsPost() {
  268. bookName := strings.TrimSpace(c.GetString("book_name"))
  269. description := strings.TrimSpace(c.GetString("description", ""))
  270. commentStatus := c.GetString("comment_status")
  271. tag := strings.TrimSpace(c.GetString("label"))
  272. orderIndex, _ := c.GetInt("order_index", 0)
  273. if strings.Count(description, "") > 500 {
  274. c.JsonResult(6004, "项目描述不能大于500字")
  275. }
  276. if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
  277. commentStatus = "closed"
  278. }
  279. if tag != "" {
  280. tags := strings.Split(tag, ";")
  281. if len(tags) > 10 {
  282. c.JsonResult(6005, "最多允许添加10个标签")
  283. }
  284. }
  285. book.BookName = bookName
  286. book.Description = description
  287. book.CommentStatus = commentStatus
  288. book.Label = tag
  289. book.OrderIndex = orderIndex
  290. if err := book.Update(); err != nil {
  291. c.JsonResult(6006, "保存失败")
  292. }
  293. c.JsonResult(0, "ok")
  294. }
  295. if book.PrivateToken != "" {
  296. book.PrivateToken = conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  297. }
  298. c.Data["Model"] = book
  299. }
  300. // 删除项目.
  301. func (c *ManagerController) DeleteBook() {
  302. c.Prepare()
  303. bookId, _ := c.GetInt("book_id", 0)
  304. if bookId <= 0 {
  305. c.JsonResult(6001, "参数错误")
  306. }
  307. book := models.NewBook()
  308. err := book.ThoroughDeleteBook(bookId)
  309. if err == orm.ErrNoRows {
  310. c.JsonResult(6002, "项目不存在")
  311. }
  312. if err != nil {
  313. logs.Error("DeleteBook => ", err)
  314. c.JsonResult(6003, "删除失败")
  315. }
  316. c.JsonResult(0, "ok")
  317. }
  318. // CreateToken 创建访问来令牌.
  319. func (c *ManagerController) CreateToken() {
  320. c.Prepare()
  321. action := c.GetString("action")
  322. identify := c.GetString("identify")
  323. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  324. if err != nil {
  325. c.JsonResult(6001, "项目不存在")
  326. }
  327. if action == "create" {
  328. if book.PrivatelyOwned == 0 {
  329. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  330. }
  331. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  332. if err := book.Update(); err != nil {
  333. logs.Error("生成阅读令牌失败 => ", err)
  334. c.JsonResult(6003, "生成阅读令牌失败")
  335. }
  336. c.JsonResult(0, "ok", conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  337. } else {
  338. book.PrivateToken = ""
  339. if err := book.Update(); err != nil {
  340. logs.Error("CreateToken => ", err)
  341. c.JsonResult(6004, "删除令牌失败")
  342. }
  343. c.JsonResult(0, "ok", "")
  344. }
  345. }
  346. //项目设置.
  347. func (c *ManagerController) Setting() {
  348. c.Prepare()
  349. c.TplName = "manager/setting.tpl"
  350. options, err := models.NewOption().All()
  351. if c.Ctx.Input.IsPost() {
  352. for _, item := range options {
  353. item.OptionValue = c.GetString(item.OptionName)
  354. item.InsertOrUpdate()
  355. }
  356. c.JsonResult(0, "ok")
  357. }
  358. if err != nil {
  359. c.Abort("500")
  360. }
  361. c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
  362. for _, item := range options {
  363. c.Data[item.OptionName] = item.OptionValue
  364. }
  365. }
  366. // Transfer 转让项目.
  367. func (c *ManagerController) Transfer() {
  368. c.Prepare()
  369. account := c.GetString("account")
  370. if account == "" {
  371. c.JsonResult(6004, "接受者账号不能为空")
  372. }
  373. member, err := models.NewMember().FindByAccount(account)
  374. if err != nil {
  375. logs.Error("FindByAccount => ", err)
  376. c.JsonResult(6005, "接受用户不存在")
  377. }
  378. if member.Status != 0 {
  379. c.JsonResult(6006, "接受用户已被禁用")
  380. }
  381. if !c.Member.IsAdministrator() {
  382. c.Abort("403")
  383. }
  384. identify := c.GetString("identify")
  385. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  386. if err != nil {
  387. c.JsonResult(6001, err.Error())
  388. }
  389. rel, err := models.NewRelationship().FindFounder(book.BookId)
  390. if err != nil {
  391. beego.Error("FindFounder => ", err)
  392. c.JsonResult(6009, "查询项目创始人失败")
  393. }
  394. if member.MemberId == rel.MemberId {
  395. c.JsonResult(6007, "不能转让给自己")
  396. }
  397. err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
  398. if err != nil {
  399. logs.Error("Transfer => ", err)
  400. c.JsonResult(6008, err.Error())
  401. }
  402. c.JsonResult(0, "ok")
  403. }
  404. func (c *ManagerController) Comments() {
  405. c.Prepare()
  406. c.TplName = "manager/comments.tpl"
  407. if !c.Member.IsAdministrator() {
  408. c.Abort("403")
  409. }
  410. }
  411. //DeleteComment 标记评论为已删除
  412. func (c *ManagerController) DeleteComment() {
  413. c.Prepare()
  414. comment_id, _ := c.GetInt("comment_id", 0)
  415. if comment_id <= 0 {
  416. c.JsonResult(6001, "参数错误")
  417. }
  418. comment := models.NewComment()
  419. if _, err := comment.Find(comment_id); err != nil {
  420. c.JsonResult(6002, "评论不存在")
  421. }
  422. comment.Approved = 3
  423. if err := comment.Update("approved"); err != nil {
  424. c.JsonResult(6003, "删除评论失败")
  425. }
  426. c.JsonResult(0, "ok", comment)
  427. }
  428. //设置项目私有状态.
  429. func (c *ManagerController) PrivatelyOwned() {
  430. c.Prepare()
  431. status := c.GetString("status")
  432. identify := c.GetString("identify")
  433. if status != "open" && status != "close" {
  434. c.JsonResult(6003, "参数错误")
  435. }
  436. state := 0
  437. if status == "open" {
  438. state = 0
  439. } else {
  440. state = 1
  441. }
  442. if !c.Member.IsAdministrator() {
  443. c.Abort("403")
  444. }
  445. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  446. if err != nil {
  447. c.JsonResult(6001, err.Error())
  448. }
  449. book.PrivatelyOwned = state
  450. logs.Info("", state, status)
  451. err = book.Update()
  452. if err != nil {
  453. logs.Error("PrivatelyOwned => ", err)
  454. c.JsonResult(6004, "保存失败")
  455. }
  456. c.JsonResult(0, "ok")
  457. }
  458. //附件列表.
  459. func (c *ManagerController) AttachList() {
  460. c.Prepare()
  461. c.TplName = "manager/attach_list.tpl"
  462. pageIndex, _ := c.GetInt("page", 1)
  463. attachList, totalCount, err := models.NewAttachment().FindToPager(pageIndex, conf.PageSize)
  464. if err != nil {
  465. c.Abort("500")
  466. }
  467. if totalCount > 0 {
  468. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  469. c.Data["PageHtml"] = pager.HtmlPages()
  470. } else {
  471. c.Data["PageHtml"] = ""
  472. }
  473. for _, item := range attachList {
  474. p := filepath.Join(conf.WorkingDirectory, item.FilePath)
  475. item.IsExist = utils.FileExists(p)
  476. }
  477. c.Data["Lists"] = attachList
  478. }
  479. //附件详情.
  480. func (c *ManagerController) AttachDetailed() {
  481. c.Prepare()
  482. c.TplName = "manager/attach_detailed.tpl"
  483. attach_id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  484. if attach_id <= 0 {
  485. c.Abort("404")
  486. }
  487. attach, err := models.NewAttachmentResult().Find(attach_id)
  488. if err != nil {
  489. beego.Error("AttachDetailed => ", err)
  490. if err == orm.ErrNoRows {
  491. c.Abort("404")
  492. } else {
  493. c.Abort("500")
  494. }
  495. }
  496. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  497. attach.HttpPath = conf.URLForWithCdnImage(attach.HttpPath)
  498. attach.IsExist = utils.FileExists(attach.FilePath)
  499. c.Data["Model"] = attach
  500. }
  501. //删除附件.
  502. func (c *ManagerController) AttachDelete() {
  503. c.Prepare()
  504. attach_id, _ := c.GetInt("attach_id")
  505. if attach_id <= 0 {
  506. c.Abort("404")
  507. }
  508. attach, err := models.NewAttachment().Find(attach_id)
  509. if err != nil {
  510. beego.Error("AttachDelete => ", err)
  511. c.JsonResult(6001, err.Error())
  512. }
  513. if err := attach.Delete(); err != nil {
  514. beego.Error("AttachDelete => ", err)
  515. c.JsonResult(6002, err.Error())
  516. }
  517. c.JsonResult(0, "ok")
  518. }
  519. //标签列表
  520. func (c *ManagerController) LabelList() {
  521. c.Prepare()
  522. c.TplName = "manager/label_list.tpl"
  523. pageIndex, _ := c.GetInt("page", 1)
  524. labels, totalCount, err := models.NewLabel().FindToPager(pageIndex, conf.PageSize)
  525. if err != nil {
  526. c.ShowErrorPage(50001, err.Error())
  527. }
  528. if totalCount > 0 {
  529. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  530. c.Data["PageHtml"] = pager.HtmlPages()
  531. } else {
  532. c.Data["PageHtml"] = ""
  533. }
  534. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
  535. c.Data["Lists"] = labels
  536. }
  537. //删除标签
  538. func (c *ManagerController) LabelDelete(){
  539. labelId,err := strconv.Atoi(c.Ctx.Input.Param(":id"))
  540. if err != nil {
  541. beego.Error("获取删除标签参数时出错:",err)
  542. c.JsonResult(50001,"参数错误")
  543. }
  544. if labelId <= 0 {
  545. c.JsonResult(50001,"参数错误")
  546. }
  547. label,err := models.NewLabel().FindFirst("label_id",labelId)
  548. if err != nil {
  549. beego.Error("查询标签时出错:",err)
  550. c.JsonResult(50001,"查询标签时出错:" + err.Error())
  551. }
  552. if err := label.Delete();err != nil {
  553. c.JsonResult(50002,"删除失败:" + err.Error())
  554. }else{
  555. c.JsonResult(0,"ok")
  556. }
  557. }