manager.go 14 KB

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