manager.go 16 KB

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