manager.go 13 KB

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