manager.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. if password1 != "" && password2 != password1 {
  180. c.JsonResult(6001, "确认密码不正确")
  181. }
  182. if password1 != "" && member.AuthMethod != conf.AuthMethodLDAP {
  183. member.Password = password1
  184. }
  185. if err := member.Valid(password1 == ""); err != nil {
  186. c.JsonResult(6002, err.Error())
  187. }
  188. if password1 != "" {
  189. password, err := utils.PasswordHash(password1)
  190. if err != nil {
  191. beego.Error(err)
  192. c.JsonResult(6003, "对用户密码加密时出错")
  193. }
  194. member.Password = password
  195. }
  196. if err := member.Update(); err != nil {
  197. beego.Error(err)
  198. c.JsonResult(6004, "保存失败")
  199. }
  200. c.JsonResult(0, "ok")
  201. }
  202. c.Data["Model"] = member
  203. }
  204. //删除一个用户,并将该用户的所有信息转移到超级管理员上.
  205. func (c *ManagerController) DeleteMember() {
  206. c.Prepare()
  207. member_id, _ := c.GetInt("id", 0)
  208. if member_id <= 0 {
  209. c.JsonResult(404, "参数错误")
  210. }
  211. member, err := models.NewMember().Find(member_id)
  212. if err != nil {
  213. beego.Error(err)
  214. c.JsonResult(500, "用户不存在")
  215. }
  216. if member.Role == conf.MemberSuperRole {
  217. c.JsonResult(500, "不能删除超级管理员")
  218. }
  219. superMember, err := models.NewMember().FindByFieldFirst("role", 0)
  220. if err != nil {
  221. beego.Error(err)
  222. c.JsonResult(5001, "未能找到超级管理员")
  223. }
  224. err = models.NewMember().Delete(member_id, superMember.MemberId)
  225. if err != nil {
  226. beego.Error(err)
  227. c.JsonResult(5002, "删除失败")
  228. }
  229. c.JsonResult(0, "ok")
  230. }
  231. //项目列表.
  232. func (c *ManagerController) Books() {
  233. c.Prepare()
  234. c.TplName = "manager/books.tpl"
  235. pageIndex, _ := c.GetInt("page", 1)
  236. books, totalCount, err := models.NewBookResult().FindToPager(pageIndex, conf.PageSize)
  237. if err != nil {
  238. c.Abort("500")
  239. }
  240. if totalCount > 0 {
  241. //html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 8, totalCount)
  242. pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize)
  243. c.Data["PageHtml"] = pager.HtmlPages()
  244. } else {
  245. c.Data["PageHtml"] = ""
  246. }
  247. c.Data["Lists"] = books
  248. }
  249. //编辑项目.
  250. func (c *ManagerController) EditBook() {
  251. c.Prepare()
  252. c.TplName = "manager/edit_book.tpl"
  253. identify := c.GetString(":key")
  254. if identify == "" {
  255. c.Abort("404")
  256. }
  257. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  258. if err != nil {
  259. c.Abort("500")
  260. }
  261. if c.Ctx.Input.IsPost() {
  262. book_name := strings.TrimSpace(c.GetString("book_name"))
  263. description := strings.TrimSpace(c.GetString("description", ""))
  264. comment_status := c.GetString("comment_status")
  265. tag := strings.TrimSpace(c.GetString("label"))
  266. order_index, _ := c.GetInt("order_index", 0)
  267. if strings.Count(description, "") > 500 {
  268. c.JsonResult(6004, "项目描述不能大于500字")
  269. }
  270. if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
  271. comment_status = "closed"
  272. }
  273. if tag != "" {
  274. tags := strings.Split(tag, ";")
  275. if len(tags) > 10 {
  276. c.JsonResult(6005, "最多允许添加10个标签")
  277. }
  278. }
  279. book.BookName = book_name
  280. book.Description = description
  281. book.CommentStatus = comment_status
  282. book.Label = tag
  283. book.OrderIndex = order_index
  284. if err := book.Update(); err != nil {
  285. c.JsonResult(6006, "保存失败")
  286. }
  287. c.JsonResult(0, "ok")
  288. }
  289. if book.PrivateToken != "" {
  290. book.PrivateToken = c.BaseUrl() + beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  291. }
  292. c.Data["Model"] = book
  293. }
  294. // 删除项目.
  295. func (c *ManagerController) DeleteBook() {
  296. c.Prepare()
  297. book_id, _ := c.GetInt("book_id", 0)
  298. if book_id <= 0 {
  299. c.JsonResult(6001, "参数错误")
  300. }
  301. book := models.NewBook()
  302. err := book.ThoroughDeleteBook(book_id)
  303. if err == orm.ErrNoRows {
  304. c.JsonResult(6002, "项目不存在")
  305. }
  306. if err != nil {
  307. logs.Error("DeleteBook => ", err)
  308. c.JsonResult(6003, "删除失败")
  309. }
  310. c.JsonResult(0, "ok")
  311. }
  312. // CreateToken 创建访问来令牌.
  313. func (c *ManagerController) CreateToken() {
  314. c.Prepare()
  315. action := c.GetString("action")
  316. identify := c.GetString("identify")
  317. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  318. if err != nil {
  319. c.JsonResult(6001, "项目不存在")
  320. }
  321. if action == "create" {
  322. if book.PrivatelyOwned == 0 {
  323. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  324. }
  325. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  326. if err := book.Update(); err != nil {
  327. logs.Error("生成阅读令牌失败 => ", err)
  328. c.JsonResult(6003, "生成阅读令牌失败")
  329. }
  330. c.JsonResult(0, "ok", c.BaseUrl()+beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  331. } else {
  332. book.PrivateToken = ""
  333. if err := book.Update(); err != nil {
  334. logs.Error("CreateToken => ", err)
  335. c.JsonResult(6004, "删除令牌失败")
  336. }
  337. c.JsonResult(0, "ok", "")
  338. }
  339. }
  340. //项目设置.
  341. func (c *ManagerController) Setting() {
  342. c.Prepare()
  343. c.TplName = "manager/setting.tpl"
  344. options, err := models.NewOption().All()
  345. if c.Ctx.Input.IsPost() {
  346. for _, item := range options {
  347. item.OptionValue = c.GetString(item.OptionName)
  348. item.InsertOrUpdate()
  349. }
  350. c.JsonResult(0, "ok")
  351. }
  352. if err != nil {
  353. c.Abort("500")
  354. }
  355. c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
  356. for _, item := range options {
  357. c.Data[item.OptionName] = item
  358. }
  359. }
  360. // Transfer 转让项目.
  361. func (c *ManagerController) Transfer() {
  362. c.Prepare()
  363. account := c.GetString("account")
  364. if account == "" {
  365. c.JsonResult(6004, "接受者账号不能为空")
  366. }
  367. member, err := models.NewMember().FindByAccount(account)
  368. if err != nil {
  369. logs.Error("FindByAccount => ", err)
  370. c.JsonResult(6005, "接受用户不存在")
  371. }
  372. if member.Status != 0 {
  373. c.JsonResult(6006, "接受用户已被禁用")
  374. }
  375. if !c.Member.IsAdministrator() {
  376. c.Abort("403")
  377. }
  378. identify := c.GetString("identify")
  379. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  380. if err != nil {
  381. c.JsonResult(6001, err.Error())
  382. }
  383. rel, err := models.NewRelationship().FindFounder(book.BookId)
  384. if err != nil {
  385. beego.Error("FindFounder => ", err)
  386. c.JsonResult(6009, "查询项目创始人失败")
  387. }
  388. if member.MemberId == rel.MemberId {
  389. c.JsonResult(6007, "不能转让给自己")
  390. }
  391. err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
  392. if err != nil {
  393. logs.Error("Transfer => ", err)
  394. c.JsonResult(6008, err.Error())
  395. }
  396. c.JsonResult(0, "ok")
  397. }
  398. func (c *ManagerController) Comments() {
  399. c.Prepare()
  400. c.TplName = "manager/comments.tpl"
  401. if !c.Member.IsAdministrator() {
  402. c.Abort("403")
  403. }
  404. }
  405. //DeleteComment 标记评论为已删除
  406. func (c *ManagerController) DeleteComment() {
  407. c.Prepare()
  408. comment_id, _ := c.GetInt("comment_id", 0)
  409. if comment_id <= 0 {
  410. c.JsonResult(6001, "参数错误")
  411. }
  412. comment := models.NewComment()
  413. if _, err := comment.Find(comment_id); err != nil {
  414. c.JsonResult(6002, "评论不存在")
  415. }
  416. comment.Approved = 3
  417. if err := comment.Update("approved"); err != nil {
  418. c.JsonResult(6003, "删除评论失败")
  419. }
  420. c.JsonResult(0, "ok", comment)
  421. }
  422. //设置项目私有状态.
  423. func (c *ManagerController) PrivatelyOwned() {
  424. c.Prepare()
  425. status := c.GetString("status")
  426. identify := c.GetString("identify")
  427. if status != "open" && status != "close" {
  428. c.JsonResult(6003, "参数错误")
  429. }
  430. state := 0
  431. if status == "open" {
  432. state = 0
  433. } else {
  434. state = 1
  435. }
  436. if !c.Member.IsAdministrator() {
  437. c.Abort("403")
  438. }
  439. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  440. if err != nil {
  441. c.JsonResult(6001, err.Error())
  442. }
  443. book.PrivatelyOwned = state
  444. logs.Info("", state, status)
  445. err = book.Update()
  446. if err != nil {
  447. logs.Error("PrivatelyOwned => ", err)
  448. c.JsonResult(6004, "保存失败")
  449. }
  450. c.JsonResult(0, "ok")
  451. }
  452. //附件列表.
  453. func (c *ManagerController) AttachList() {
  454. c.Prepare()
  455. c.TplName = "manager/attach_list.tpl"
  456. pageIndex, _ := c.GetInt("page", 1)
  457. attachList, totalCount, err := models.NewAttachment().FindToPager(pageIndex, conf.PageSize)
  458. if err != nil {
  459. c.Abort("500")
  460. }
  461. if totalCount > 0 {
  462. pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize)
  463. c.Data["PageHtml"] = pager.HtmlPages()
  464. } else {
  465. c.Data["PageHtml"] = ""
  466. }
  467. for _, item := range attachList {
  468. p := filepath.Join(conf.WorkingDirectory, item.FilePath)
  469. item.IsExist = utils.FileExists(p)
  470. }
  471. c.Data["Lists"] = attachList
  472. }
  473. //附件详情.
  474. func (c *ManagerController) AttachDetailed() {
  475. c.Prepare()
  476. c.TplName = "manager/attach_detailed.tpl"
  477. attach_id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  478. if attach_id <= 0 {
  479. c.Abort("404")
  480. }
  481. attach, err := models.NewAttachmentResult().Find(attach_id)
  482. if err != nil {
  483. beego.Error("AttachDetailed => ", err)
  484. if err == orm.ErrNoRows {
  485. c.Abort("404")
  486. } else {
  487. c.Abort("500")
  488. }
  489. }
  490. attach.FilePath = filepath.Join(conf.WorkingDirectory, attach.FilePath)
  491. attach.HttpPath = c.BaseUrl() + attach.HttpPath
  492. attach.IsExist = utils.FileExists(attach.FilePath)
  493. c.Data["Model"] = attach
  494. }
  495. //删除附件.
  496. func (c *ManagerController) AttachDelete() {
  497. c.Prepare()
  498. attach_id, _ := c.GetInt("attach_id")
  499. if attach_id <= 0 {
  500. c.Abort("404")
  501. }
  502. attach, err := models.NewAttachment().Find(attach_id)
  503. if err != nil {
  504. beego.Error("AttachDelete => ", err)
  505. c.JsonResult(6001, err.Error())
  506. }
  507. if err := attach.Delete(); err != nil {
  508. beego.Error("AttachDelete => ", err)
  509. c.JsonResult(6002, err.Error())
  510. }
  511. c.JsonResult(0, "ok")
  512. }