manager.go 14 KB

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