manager.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. }
  251. c.Data["Lists"] = books
  252. }
  253. //编辑项目.
  254. func (c *ManagerController) EditBook() {
  255. c.Prepare()
  256. c.TplName = "manager/edit_book.tpl"
  257. identify := c.GetString(":key")
  258. if identify == "" {
  259. c.Abort("404")
  260. }
  261. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  262. if err != nil {
  263. c.Abort("500")
  264. }
  265. if c.Ctx.Input.IsPost() {
  266. book_name := strings.TrimSpace(c.GetString("book_name"))
  267. description := strings.TrimSpace(c.GetString("description", ""))
  268. comment_status := c.GetString("comment_status")
  269. tag := strings.TrimSpace(c.GetString("label"))
  270. order_index, _ := c.GetInt("order_index", 0)
  271. if strings.Count(description, "") > 500 {
  272. c.JsonResult(6004, "项目描述不能大于500字")
  273. }
  274. if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
  275. comment_status = "closed"
  276. }
  277. if tag != "" {
  278. tags := strings.Split(tag, ";")
  279. if len(tags) > 10 {
  280. c.JsonResult(6005, "最多允许添加10个标签")
  281. }
  282. }
  283. book.BookName = book_name
  284. book.Description = description
  285. book.CommentStatus = comment_status
  286. book.Label = tag
  287. book.OrderIndex = order_index
  288. if err := book.Update(); err != nil {
  289. c.JsonResult(6006, "保存失败")
  290. }
  291. c.JsonResult(0, "ok")
  292. }
  293. if book.PrivateToken != "" {
  294. book.PrivateToken = conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  295. }
  296. c.Data["Model"] = book
  297. }
  298. // 删除项目.
  299. func (c *ManagerController) DeleteBook() {
  300. c.Prepare()
  301. bookId, _ := c.GetInt("book_id", 0)
  302. if bookId <= 0 {
  303. c.JsonResult(6001, "参数错误")
  304. }
  305. book := models.NewBook()
  306. err := book.ThoroughDeleteBook(bookId)
  307. if err == orm.ErrNoRows {
  308. c.JsonResult(6002, "项目不存在")
  309. }
  310. if err != nil {
  311. logs.Error("DeleteBook => ", err)
  312. c.JsonResult(6003, "删除失败")
  313. }
  314. c.JsonResult(0, "ok")
  315. }
  316. // CreateToken 创建访问来令牌.
  317. func (c *ManagerController) CreateToken() {
  318. c.Prepare()
  319. action := c.GetString("action")
  320. identify := c.GetString("identify")
  321. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  322. if err != nil {
  323. c.JsonResult(6001, "项目不存在")
  324. }
  325. if action == "create" {
  326. if book.PrivatelyOwned == 0 {
  327. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  328. }
  329. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  330. if err := book.Update(); err != nil {
  331. logs.Error("生成阅读令牌失败 => ", err)
  332. c.JsonResult(6003, "生成阅读令牌失败")
  333. }
  334. c.JsonResult(0, "ok", conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  335. } else {
  336. book.PrivateToken = ""
  337. if err := book.Update(); err != nil {
  338. logs.Error("CreateToken => ", err)
  339. c.JsonResult(6004, "删除令牌失败")
  340. }
  341. c.JsonResult(0, "ok", "")
  342. }
  343. }
  344. //项目设置.
  345. func (c *ManagerController) Setting() {
  346. c.Prepare()
  347. c.TplName = "manager/setting.tpl"
  348. options, err := models.NewOption().All()
  349. if c.Ctx.Input.IsPost() {
  350. for _, item := range options {
  351. item.OptionValue = c.GetString(item.OptionName)
  352. item.InsertOrUpdate()
  353. }
  354. c.JsonResult(0, "ok")
  355. }
  356. if err != nil {
  357. c.Abort("500")
  358. }
  359. c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
  360. for _, item := range options {
  361. c.Data[item.OptionName] = item.OptionValue
  362. }
  363. }
  364. // Transfer 转让项目.
  365. func (c *ManagerController) Transfer() {
  366. c.Prepare()
  367. account := c.GetString("account")
  368. if account == "" {
  369. c.JsonResult(6004, "接受者账号不能为空")
  370. }
  371. member, err := models.NewMember().FindByAccount(account)
  372. if err != nil {
  373. logs.Error("FindByAccount => ", err)
  374. c.JsonResult(6005, "接受用户不存在")
  375. }
  376. if member.Status != 0 {
  377. c.JsonResult(6006, "接受用户已被禁用")
  378. }
  379. if !c.Member.IsAdministrator() {
  380. c.Abort("403")
  381. }
  382. identify := c.GetString("identify")
  383. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  384. if err != nil {
  385. c.JsonResult(6001, err.Error())
  386. }
  387. rel, err := models.NewRelationship().FindFounder(book.BookId)
  388. if err != nil {
  389. beego.Error("FindFounder => ", err)
  390. c.JsonResult(6009, "查询项目创始人失败")
  391. }
  392. if member.MemberId == rel.MemberId {
  393. c.JsonResult(6007, "不能转让给自己")
  394. }
  395. err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
  396. if err != nil {
  397. logs.Error("Transfer => ", err)
  398. c.JsonResult(6008, err.Error())
  399. }
  400. c.JsonResult(0, "ok")
  401. }
  402. func (c *ManagerController) Comments() {
  403. c.Prepare()
  404. c.TplName = "manager/comments.tpl"
  405. if !c.Member.IsAdministrator() {
  406. c.Abort("403")
  407. }
  408. }
  409. //DeleteComment 标记评论为已删除
  410. func (c *ManagerController) DeleteComment() {
  411. c.Prepare()
  412. comment_id, _ := c.GetInt("comment_id", 0)
  413. if comment_id <= 0 {
  414. c.JsonResult(6001, "参数错误")
  415. }
  416. comment := models.NewComment()
  417. if _, err := comment.Find(comment_id); err != nil {
  418. c.JsonResult(6002, "评论不存在")
  419. }
  420. comment.Approved = 3
  421. if err := comment.Update("approved"); err != nil {
  422. c.JsonResult(6003, "删除评论失败")
  423. }
  424. c.JsonResult(0, "ok", comment)
  425. }
  426. //设置项目私有状态.
  427. func (c *ManagerController) PrivatelyOwned() {
  428. c.Prepare()
  429. status := c.GetString("status")
  430. identify := c.GetString("identify")
  431. if status != "open" && status != "close" {
  432. c.JsonResult(6003, "参数错误")
  433. }
  434. state := 0
  435. if status == "open" {
  436. state = 0
  437. } else {
  438. state = 1
  439. }
  440. if !c.Member.IsAdministrator() {
  441. c.Abort("403")
  442. }
  443. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  444. if err != nil {
  445. c.JsonResult(6001, err.Error())
  446. }
  447. book.PrivatelyOwned = state
  448. logs.Info("", state, status)
  449. err = book.Update()
  450. if err != nil {
  451. logs.Error("PrivatelyOwned => ", err)
  452. c.JsonResult(6004, "保存失败")
  453. }
  454. c.JsonResult(0, "ok")
  455. }
  456. //附件列表.
  457. func (c *ManagerController) AttachList() {
  458. c.Prepare()
  459. c.TplName = "manager/attach_list.tpl"
  460. pageIndex, _ := c.GetInt("page", 1)
  461. attachList, totalCount, err := models.NewAttachment().FindToPager(pageIndex, conf.PageSize)
  462. if err != nil {
  463. c.Abort("500")
  464. }
  465. if totalCount > 0 {
  466. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  467. c.Data["PageHtml"] = pager.HtmlPages()
  468. } else {
  469. c.Data["PageHtml"] = ""
  470. }
  471. for _, item := range attachList {
  472. p := filepath.Join(conf.WorkingDirectory, item.FilePath)
  473. item.IsExist = utils.FileExists(p)
  474. }
  475. c.Data["Lists"] = attachList
  476. }
  477. //附件详情.
  478. func (c *ManagerController) AttachDetailed() {
  479. c.Prepare()
  480. c.TplName = "manager/attach_detailed.tpl"
  481. attach_id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  482. if attach_id <= 0 {
  483. c.Abort("404")
  484. }
  485. attach, err := models.NewAttachmentResult().Find(attach_id)
  486. if err != nil {
  487. beego.Error("AttachDetailed => ", err)
  488. if err == orm.ErrNoRows {
  489. c.Abort("404")
  490. } else {
  491. c.Abort("500")
  492. }
  493. }
  494. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  495. attach.HttpPath = conf.URLForWithCdnImage(attach.HttpPath)
  496. attach.IsExist = utils.FileExists(attach.FilePath)
  497. c.Data["Model"] = attach
  498. }
  499. //删除附件.
  500. func (c *ManagerController) AttachDelete() {
  501. c.Prepare()
  502. attach_id, _ := c.GetInt("attach_id")
  503. if attach_id <= 0 {
  504. c.Abort("404")
  505. }
  506. attach, err := models.NewAttachment().Find(attach_id)
  507. if err != nil {
  508. beego.Error("AttachDelete => ", err)
  509. c.JsonResult(6001, err.Error())
  510. }
  511. if err := attach.Delete(); err != nil {
  512. beego.Error("AttachDelete => ", err)
  513. c.JsonResult(6002, err.Error())
  514. }
  515. c.JsonResult(0, "ok")
  516. }
  517. //标签列表
  518. func (c *ManagerController) LabelList() {
  519. c.Prepare()
  520. c.TplName = "manager/label_list.tpl"
  521. pageIndex, _ := c.GetInt("page", 1)
  522. labels, totalCount, err := models.NewLabel().FindToPager(pageIndex, conf.PageSize)
  523. if err != nil {
  524. c.ShowErrorPage(50001, err.Error())
  525. }
  526. if totalCount > 0 {
  527. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  528. c.Data["PageHtml"] = pager.HtmlPages()
  529. } else {
  530. c.Data["PageHtml"] = ""
  531. }
  532. c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
  533. c.Data["Lists"] = labels
  534. }
  535. //删除标签
  536. func (c *ManagerController) LabelDelete(){
  537. labelId,err := strconv.Atoi(c.Ctx.Input.Param(":id"))
  538. if err != nil {
  539. beego.Error("获取删除标签参数时出错:",err)
  540. c.JsonResult(50001,"参数错误")
  541. }
  542. if labelId <= 0 {
  543. c.JsonResult(50001,"参数错误")
  544. }
  545. label,err := models.NewLabel().FindFirst("label_id",labelId)
  546. if err != nil {
  547. beego.Error("查询标签时出错:",err)
  548. c.JsonResult(50001,"查询标签时出错:" + err.Error())
  549. }
  550. if err := label.Delete();err != nil {
  551. c.JsonResult(50002,"删除失败:" + err.Error())
  552. }else{
  553. c.JsonResult(0,"ok")
  554. }
  555. }