manager.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. member.RealName = strings.TrimSpace(c.GetString("real_name",""))
  92. if phone != "" {
  93. member.Phone = phone
  94. }
  95. if err := member.Add(); err != nil {
  96. c.JsonResult(6006, err.Error())
  97. }
  98. c.JsonResult(0, "ok", member)
  99. }
  100. //更新用户状态.
  101. func (c *ManagerController) UpdateMemberStatus() {
  102. c.Prepare()
  103. member_id, _ := c.GetInt("member_id", 0)
  104. status, _ := c.GetInt("status", 0)
  105. if member_id <= 0 {
  106. c.JsonResult(6001, "参数错误")
  107. }
  108. if status != 0 && status != 1 {
  109. status = 0
  110. }
  111. member := models.NewMember()
  112. if _, err := member.Find(member_id); err != nil {
  113. c.JsonResult(6002, "用户不存在")
  114. }
  115. if member.MemberId == c.Member.MemberId {
  116. c.JsonResult(6004, "不能变更自己的状态")
  117. }
  118. if member.Role == conf.MemberSuperRole {
  119. c.JsonResult(6005, "不能变更超级管理员的状态")
  120. }
  121. member.Status = status
  122. if err := member.Update(); err != nil {
  123. logs.Error("", err)
  124. c.JsonResult(6003, "用户状态设置失败")
  125. }
  126. c.JsonResult(0, "ok", member)
  127. }
  128. //变更用户权限.
  129. func (c *ManagerController) ChangeMemberRole() {
  130. c.Prepare()
  131. member_id, _ := c.GetInt("member_id", 0)
  132. role, _ := c.GetInt("role", 0)
  133. if member_id <= 0 {
  134. c.JsonResult(6001, "参数错误")
  135. }
  136. if role != conf.MemberAdminRole && role != conf.MemberGeneralRole {
  137. c.JsonResult(6001, "用户权限不正确")
  138. }
  139. member := models.NewMember()
  140. if _, err := member.Find(member_id); err != nil {
  141. c.JsonResult(6002, "用户不存在")
  142. }
  143. if member.MemberId == c.Member.MemberId {
  144. c.JsonResult(6004, "不能变更自己的权限")
  145. }
  146. if member.Role == conf.MemberSuperRole {
  147. c.JsonResult(6005, "不能变更超级管理员的权限")
  148. }
  149. member.Role = role
  150. if err := member.Update(); err != nil {
  151. logs.Error("", err)
  152. c.JsonResult(6003, "用户权限设置失败")
  153. }
  154. member.ResolveRoleName()
  155. c.JsonResult(0, "ok", member)
  156. }
  157. //编辑用户信息.
  158. func (c *ManagerController) EditMember() {
  159. c.Prepare()
  160. c.TplName = "manager/edit_users.tpl"
  161. member_id, _ := c.GetInt(":id", 0)
  162. if member_id <= 0 {
  163. c.Abort("404")
  164. }
  165. member, err := models.NewMember().Find(member_id)
  166. if err != nil {
  167. beego.Error(err)
  168. c.Abort("404")
  169. }
  170. if c.Ctx.Input.IsPost() {
  171. password1 := c.GetString("password1")
  172. password2 := c.GetString("password2")
  173. email := c.GetString("email")
  174. phone := c.GetString("phone")
  175. description := c.GetString("description")
  176. member.Email = email
  177. member.Phone = phone
  178. member.Description = description
  179. member.RealName = c.GetString("real_name")
  180. if password1 != "" && password2 != password1 {
  181. c.JsonResult(6001, "确认密码不正确")
  182. }
  183. if password1 != "" && member.AuthMethod != conf.AuthMethodLDAP {
  184. member.Password = password1
  185. }
  186. if err := member.Valid(password1 == ""); err != nil {
  187. c.JsonResult(6002, err.Error())
  188. }
  189. if password1 != "" {
  190. password, err := utils.PasswordHash(password1)
  191. if err != nil {
  192. beego.Error(err)
  193. c.JsonResult(6003, "对用户密码加密时出错")
  194. }
  195. member.Password = password
  196. }
  197. if err := member.Update(); err != nil {
  198. beego.Error(err)
  199. c.JsonResult(6004, "保存失败")
  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)
  244. c.Data["PageHtml"] = pager.HtmlPages()
  245. } else {
  246. c.Data["PageHtml"] = ""
  247. }
  248. c.Data["Lists"] = books
  249. }
  250. //编辑项目.
  251. func (c *ManagerController) EditBook() {
  252. c.Prepare()
  253. c.TplName = "manager/edit_book.tpl"
  254. identify := c.GetString(":key")
  255. if identify == "" {
  256. c.Abort("404")
  257. }
  258. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  259. if err != nil {
  260. c.Abort("500")
  261. }
  262. if c.Ctx.Input.IsPost() {
  263. book_name := strings.TrimSpace(c.GetString("book_name"))
  264. description := strings.TrimSpace(c.GetString("description", ""))
  265. comment_status := c.GetString("comment_status")
  266. tag := strings.TrimSpace(c.GetString("label"))
  267. order_index, _ := c.GetInt("order_index", 0)
  268. if strings.Count(description, "") > 500 {
  269. c.JsonResult(6004, "项目描述不能大于500字")
  270. }
  271. if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
  272. comment_status = "closed"
  273. }
  274. if tag != "" {
  275. tags := strings.Split(tag, ";")
  276. if len(tags) > 10 {
  277. c.JsonResult(6005, "最多允许添加10个标签")
  278. }
  279. }
  280. book.BookName = book_name
  281. book.Description = description
  282. book.CommentStatus = comment_status
  283. book.Label = tag
  284. book.OrderIndex = order_index
  285. if err := book.Update(); err != nil {
  286. c.JsonResult(6006, "保存失败")
  287. }
  288. c.JsonResult(0, "ok")
  289. }
  290. if book.PrivateToken != "" {
  291. book.PrivateToken = c.BaseUrl() + beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  292. }
  293. c.Data["Model"] = book
  294. }
  295. // 删除项目.
  296. func (c *ManagerController) DeleteBook() {
  297. c.Prepare()
  298. book_id, _ := c.GetInt("book_id", 0)
  299. if book_id <= 0 {
  300. c.JsonResult(6001, "参数错误")
  301. }
  302. book := models.NewBook()
  303. err := book.ThoroughDeleteBook(book_id)
  304. if err == orm.ErrNoRows {
  305. c.JsonResult(6002, "项目不存在")
  306. }
  307. if err != nil {
  308. logs.Error("DeleteBook => ", err)
  309. c.JsonResult(6003, "删除失败")
  310. }
  311. c.JsonResult(0, "ok")
  312. }
  313. // CreateToken 创建访问来令牌.
  314. func (c *ManagerController) CreateToken() {
  315. c.Prepare()
  316. action := c.GetString("action")
  317. identify := c.GetString("identify")
  318. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  319. if err != nil {
  320. c.JsonResult(6001, "项目不存在")
  321. }
  322. if action == "create" {
  323. if book.PrivatelyOwned == 0 {
  324. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  325. }
  326. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  327. if err := book.Update(); err != nil {
  328. logs.Error("生成阅读令牌失败 => ", err)
  329. c.JsonResult(6003, "生成阅读令牌失败")
  330. }
  331. c.JsonResult(0, "ok", c.BaseUrl()+beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  332. } else {
  333. book.PrivateToken = ""
  334. if err := book.Update(); err != nil {
  335. logs.Error("CreateToken => ", err)
  336. c.JsonResult(6004, "删除令牌失败")
  337. }
  338. c.JsonResult(0, "ok", "")
  339. }
  340. }
  341. //项目设置.
  342. func (c *ManagerController) Setting() {
  343. c.Prepare()
  344. c.TplName = "manager/setting.tpl"
  345. options, err := models.NewOption().All()
  346. if c.Ctx.Input.IsPost() {
  347. for _, item := range options {
  348. item.OptionValue = c.GetString(item.OptionName)
  349. item.InsertOrUpdate()
  350. }
  351. c.JsonResult(0, "ok")
  352. }
  353. if err != nil {
  354. c.Abort("500")
  355. }
  356. c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
  357. for _, item := range options {
  358. c.Data[item.OptionName] = item
  359. }
  360. }
  361. // Transfer 转让项目.
  362. func (c *ManagerController) Transfer() {
  363. c.Prepare()
  364. account := c.GetString("account")
  365. if account == "" {
  366. c.JsonResult(6004, "接受者账号不能为空")
  367. }
  368. member, err := models.NewMember().FindByAccount(account)
  369. if err != nil {
  370. logs.Error("FindByAccount => ", err)
  371. c.JsonResult(6005, "接受用户不存在")
  372. }
  373. if member.Status != 0 {
  374. c.JsonResult(6006, "接受用户已被禁用")
  375. }
  376. if !c.Member.IsAdministrator() {
  377. c.Abort("403")
  378. }
  379. identify := c.GetString("identify")
  380. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  381. if err != nil {
  382. c.JsonResult(6001, err.Error())
  383. }
  384. rel, err := models.NewRelationship().FindFounder(book.BookId)
  385. if err != nil {
  386. beego.Error("FindFounder => ", err)
  387. c.JsonResult(6009, "查询项目创始人失败")
  388. }
  389. if member.MemberId == rel.MemberId {
  390. c.JsonResult(6007, "不能转让给自己")
  391. }
  392. err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
  393. if err != nil {
  394. logs.Error("Transfer => ", err)
  395. c.JsonResult(6008, err.Error())
  396. }
  397. c.JsonResult(0, "ok")
  398. }
  399. func (c *ManagerController) Comments() {
  400. c.Prepare()
  401. c.TplName = "manager/comments.tpl"
  402. if !c.Member.IsAdministrator() {
  403. c.Abort("403")
  404. }
  405. }
  406. //DeleteComment 标记评论为已删除
  407. func (c *ManagerController) DeleteComment() {
  408. c.Prepare()
  409. comment_id, _ := c.GetInt("comment_id", 0)
  410. if comment_id <= 0 {
  411. c.JsonResult(6001, "参数错误")
  412. }
  413. comment := models.NewComment()
  414. if _, err := comment.Find(comment_id); err != nil {
  415. c.JsonResult(6002, "评论不存在")
  416. }
  417. comment.Approved = 3
  418. if err := comment.Update("approved"); err != nil {
  419. c.JsonResult(6003, "删除评论失败")
  420. }
  421. c.JsonResult(0, "ok", comment)
  422. }
  423. //设置项目私有状态.
  424. func (c *ManagerController) PrivatelyOwned() {
  425. c.Prepare()
  426. status := c.GetString("status")
  427. identify := c.GetString("identify")
  428. if status != "open" && status != "close" {
  429. c.JsonResult(6003, "参数错误")
  430. }
  431. state := 0
  432. if status == "open" {
  433. state = 0
  434. } else {
  435. state = 1
  436. }
  437. if !c.Member.IsAdministrator() {
  438. c.Abort("403")
  439. }
  440. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  441. if err != nil {
  442. c.JsonResult(6001, err.Error())
  443. }
  444. book.PrivatelyOwned = state
  445. logs.Info("", state, status)
  446. err = book.Update()
  447. if err != nil {
  448. logs.Error("PrivatelyOwned => ", err)
  449. c.JsonResult(6004, "保存失败")
  450. }
  451. c.JsonResult(0, "ok")
  452. }
  453. //附件列表.
  454. func (c *ManagerController) AttachList() {
  455. c.Prepare()
  456. c.TplName = "manager/attach_list.tpl"
  457. pageIndex, _ := c.GetInt("page", 1)
  458. attachList, totalCount, err := models.NewAttachment().FindToPager(pageIndex, conf.PageSize)
  459. if err != nil {
  460. c.Abort("500")
  461. }
  462. if totalCount > 0 {
  463. pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize)
  464. c.Data["PageHtml"] = pager.HtmlPages()
  465. } else {
  466. c.Data["PageHtml"] = ""
  467. }
  468. for _, item := range attachList {
  469. p := filepath.Join(conf.WorkingDirectory, item.FilePath)
  470. item.IsExist = utils.FileExists(p)
  471. }
  472. c.Data["Lists"] = attachList
  473. }
  474. //附件详情.
  475. func (c *ManagerController) AttachDetailed() {
  476. c.Prepare()
  477. c.TplName = "manager/attach_detailed.tpl"
  478. attach_id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  479. if attach_id <= 0 {
  480. c.Abort("404")
  481. }
  482. attach, err := models.NewAttachmentResult().Find(attach_id)
  483. if err != nil {
  484. beego.Error("AttachDetailed => ", err)
  485. if err == orm.ErrNoRows {
  486. c.Abort("404")
  487. } else {
  488. c.Abort("500")
  489. }
  490. }
  491. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  492. attach.HttpPath = c.BaseUrl() + attach.HttpPath
  493. attach.IsExist = utils.FileExists(attach.FilePath)
  494. c.Data["Model"] = attach
  495. }
  496. //删除附件.
  497. func (c *ManagerController) AttachDelete() {
  498. c.Prepare()
  499. attach_id, _ := c.GetInt("attach_id")
  500. if attach_id <= 0 {
  501. c.Abort("404")
  502. }
  503. attach, err := models.NewAttachment().Find(attach_id)
  504. if err != nil {
  505. beego.Error("AttachDelete => ", err)
  506. c.JsonResult(6001, err.Error())
  507. }
  508. if err := attach.Delete(); err != nil {
  509. beego.Error("AttachDelete => ", err)
  510. c.JsonResult(6002, err.Error())
  511. }
  512. c.JsonResult(0, "ok")
  513. }