manager.go 14 KB

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