manager.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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/godoc/conf"
  11. "github.com/lifei6671/godoc/models"
  12. "github.com/lifei6671/godoc/utils"
  13. )
  14. type ManagerController struct {
  15. BaseController
  16. }
  17. func (c *ManagerController) Prepare (){
  18. c.BaseController.Prepare()
  19. if !c.Member.IsAdministrator() {
  20. c.Abort("403")
  21. }
  22. }
  23. func (c *ManagerController) Index() {
  24. c.TplName = "manager/index.tpl"
  25. if !c.Member.IsAdministrator() {
  26. c.Abort("403")
  27. }
  28. c.Data["Model"] = models.NewDashboard().Query()
  29. }
  30. // 用户列表.
  31. func (c *ManagerController) Users() {
  32. c.Prepare()
  33. c.TplName = "manager/users.tpl"
  34. if !c.Member.IsAdministrator() {
  35. c.Abort("403")
  36. }
  37. pageIndex, _ := c.GetInt("page", 0)
  38. members, totalCount, err := models.NewMember().FindToPager(pageIndex, 15)
  39. if err != nil {
  40. c.Data["ErrorMessage"] = err.Error()
  41. return
  42. }
  43. if totalCount > 0 {
  44. html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 10, int(totalCount))
  45. c.Data["PageHtml"] = html
  46. } else {
  47. c.Data["PageHtml"] = ""
  48. }
  49. b, err := json.Marshal(members)
  50. if err != nil {
  51. c.Data["Result"] = template.JS("[]")
  52. } else {
  53. c.Data["Result"] = template.JS(string(b))
  54. }
  55. }
  56. // 添加用户.
  57. func (c *ManagerController) CreateMember() {
  58. c.Prepare()
  59. if !c.Member.IsAdministrator() {
  60. c.Abort("403")
  61. }
  62. account := strings.TrimSpace(c.GetString("account"))
  63. password1 := strings.TrimSpace(c.GetString("password1"))
  64. password2 := strings.TrimSpace(c.GetString("password2"))
  65. email := strings.TrimSpace(c.GetString("email"))
  66. phone := strings.TrimSpace(c.GetString("phone"))
  67. role, _ := c.GetInt("role", 1)
  68. status, _ := c.GetInt("status", 0)
  69. if ok, err := regexp.MatchString(conf.RegexpAccount, account); account == "" || !ok || err != nil {
  70. c.JsonResult(6001, "账号只能由英文字母数字组成,且在3-50个字符")
  71. }
  72. if l := strings.Count(password1, ""); password1 == "" || l > 50 || l < 6 {
  73. c.JsonResult(6002, "密码必须在6-50个字符之间")
  74. }
  75. if password1 != password2 {
  76. c.JsonResult(6003, "确认密码不正确")
  77. }
  78. if ok, err := regexp.MatchString(conf.RegexpEmail, email); !ok || err != nil || email == "" {
  79. c.JsonResult(6004, "邮箱格式不正确")
  80. }
  81. if role != 0 && role != 1 && role != 2 {
  82. role = 1
  83. }
  84. if status != 0 && status != 1 {
  85. status = 0
  86. }
  87. member := models.NewMember()
  88. if _, err := member.FindByAccount(account); err == nil && member.MemberId > 0 {
  89. c.JsonResult(6005, "账号已存在")
  90. }
  91. member.Account = account
  92. member.Password = password1
  93. member.Role = role
  94. member.Avatar = conf.GetDefaultAvatar()
  95. member.CreateAt = c.Member.MemberId
  96. member.Email = email
  97. if phone != "" {
  98. member.Phone = phone
  99. }
  100. if err := member.Add(); err != nil {
  101. c.JsonResult(6006, err.Error())
  102. }
  103. c.JsonResult(0, "ok", member)
  104. }
  105. //更新用户状态.
  106. func (c *ManagerController) UpdateMemberStatus() {
  107. c.Prepare()
  108. if !c.Member.IsAdministrator() {
  109. c.Abort("403")
  110. }
  111. member_id, _ := c.GetInt("member_id", 0)
  112. status, _ := c.GetInt("status", 0)
  113. if member_id <= 0 {
  114. c.JsonResult(6001, "参数错误")
  115. }
  116. if status != 0 && status != 1 {
  117. status = 0
  118. }
  119. member := models.NewMember()
  120. if _, err := member.Find(member_id); err != nil {
  121. c.JsonResult(6002, "用户不存在")
  122. }
  123. if member.MemberId == c.Member.MemberId {
  124. c.JsonResult(6004,"不能变更自己的状态")
  125. }
  126. member.Status = status
  127. if err := member.Update(); err != nil {
  128. logs.Error("", err)
  129. c.JsonResult(6003, "用户状态设置失败")
  130. }
  131. c.JsonResult(0, "ok", member)
  132. }
  133. //变更用户权限.
  134. func (c *ManagerController) ChangeMemberRole() {
  135. c.Prepare()
  136. if !c.Member.IsAdministrator() {
  137. c.Abort("403")
  138. }
  139. member_id, _ := c.GetInt("member_id", 0)
  140. role, _ := c.GetInt("role", 0)
  141. if member_id <= 0 {
  142. c.JsonResult(6001, "参数错误")
  143. }
  144. if role != conf.MemberAdminRole && role != conf.MemberGeneralRole {
  145. c.JsonResult(6001, "用户权限不正确")
  146. }
  147. member := models.NewMember()
  148. if _, err := member.Find(member_id); err != nil {
  149. c.JsonResult(6002, "用户不存在")
  150. }
  151. if member.MemberId == c.Member.MemberId {
  152. c.JsonResult(6004,"不能变更自己的权限")
  153. }
  154. member.Role = role
  155. if err := member.Update(); err != nil {
  156. logs.Error("", err)
  157. c.JsonResult(6003, "用户权限设置失败")
  158. }
  159. member.ResolveRoleName()
  160. c.JsonResult(0, "ok", member)
  161. }
  162. func (c *ManagerController) EditMember() {
  163. c.Prepare()
  164. c.TplName = "manager/edit_users.tpl"
  165. if !c.Member.IsAdministrator() {
  166. c.Abort("403")
  167. }
  168. member_id,_ := c.GetInt(":id",0)
  169. if member_id <= 0 {
  170. c.Abort("404")
  171. }
  172. member ,err := models.NewMember().Find(member_id)
  173. if err != nil {
  174. beego.Error(err)
  175. c.Abort("404")
  176. }
  177. if c.Ctx.Input.IsPost() {
  178. password1 := c.GetString("password1")
  179. password2 := c.GetString("password2")
  180. email := c.GetString("email")
  181. phone := c.GetString("phone")
  182. description := c.GetString("description")
  183. member.Email = email
  184. member.Phone = phone
  185. member.Description = description
  186. if password1 != "" && password2 != password1 {
  187. c.JsonResult(6001,"确认密码不正确")
  188. }
  189. if password1 != "" {
  190. member.Password = password1
  191. }
  192. if err := member.Valid(password1 == "");err != nil {
  193. c.JsonResult(6002,err.Error())
  194. }
  195. if password1 != "" {
  196. password,err := utils.PasswordHash(password1)
  197. if err != nil {
  198. beego.Error(err)
  199. c.JsonResult(6003,"对用户密码加密时出错")
  200. }
  201. member.Password = password
  202. }
  203. if err := member.Update();err != nil {
  204. beego.Error(err)
  205. c.JsonResult(6004,"保存失败")
  206. }
  207. c.JsonResult(0,"ok")
  208. }
  209. c.Data["Model"] = member
  210. }
  211. func (c *ManagerController) Books() {
  212. c.Prepare()
  213. c.TplName = "manager/books.tpl"
  214. pageIndex, _ := c.GetInt("page", 1)
  215. books, totalCount, err := models.NewBookResult().FindToPager(pageIndex, conf.PageSize)
  216. if err != nil {
  217. c.Abort("500")
  218. }
  219. if totalCount > 0 {
  220. html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount)
  221. c.Data["PageHtml"] = html
  222. } else {
  223. c.Data["PageHtml"] = ""
  224. }
  225. c.Data["Lists"] = books
  226. }
  227. //编辑项目
  228. func (c *ManagerController) EditBook() {
  229. c.TplName = "manager/edit_book.tpl"
  230. if !c.Member.IsAdministrator() {
  231. c.Abort("403")
  232. }
  233. identify := c.GetString(":key")
  234. if identify == "" {
  235. c.Abort("404")
  236. }
  237. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  238. if err != nil {
  239. c.Abort("500")
  240. }
  241. if c.Ctx.Input.IsPost() {
  242. book_name := strings.TrimSpace(c.GetString("book_name"))
  243. description := strings.TrimSpace(c.GetString("description", ""))
  244. comment_status := c.GetString("comment_status")
  245. tag := strings.TrimSpace(c.GetString("label"))
  246. order_index, _ := c.GetInt("order_index", 0)
  247. if strings.Count(description, "") > 500 {
  248. c.JsonResult(6004, "项目描述不能大于500字")
  249. }
  250. if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
  251. comment_status = "closed"
  252. }
  253. if tag != "" {
  254. tags := strings.Split(tag, ";")
  255. if len(tags) > 10 {
  256. c.JsonResult(6005, "最多允许添加10个标签")
  257. }
  258. }
  259. book.BookName = book_name
  260. book.Description = description
  261. book.CommentStatus = comment_status
  262. book.Label = tag
  263. book.OrderIndex = order_index
  264. if err := book.Update(); err != nil {
  265. c.JsonResult(6006, "保存失败")
  266. }
  267. c.JsonResult(0, "ok")
  268. }
  269. if book.PrivateToken != "" {
  270. book.PrivateToken = c.BaseUrl() + beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken)
  271. }
  272. c.Data["Model"] = book
  273. }
  274. // 删除项目.
  275. func (c *ManagerController) DeleteBook() {
  276. c.Prepare()
  277. if !c.Member.IsAdministrator() {
  278. c.Abort("403")
  279. }
  280. book_id, _ := c.GetInt("book_id", 0)
  281. if book_id <= 0 {
  282. c.JsonResult(6001, "参数错误")
  283. }
  284. book := models.NewBook()
  285. err := book.ThoroughDeleteBook(book_id)
  286. if err == orm.ErrNoRows {
  287. c.JsonResult(6002, "项目不存在")
  288. }
  289. if err != nil {
  290. logs.Error("DeleteBook => ", err)
  291. c.JsonResult(6003, "删除失败")
  292. }
  293. c.JsonResult(0, "ok")
  294. }
  295. // CreateToken 创建访问来令牌.
  296. func (c *ManagerController) CreateToken() {
  297. action := c.GetString("action")
  298. identify := c.GetString("identify")
  299. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  300. if err != nil {
  301. c.JsonResult(6001, "项目不存在")
  302. }
  303. if action == "create" {
  304. if book.PrivatelyOwned == 0 {
  305. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  306. }
  307. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  308. if err := book.Update(); err != nil {
  309. logs.Error("生成阅读令牌失败 => ", err)
  310. c.JsonResult(6003, "生成阅读令牌失败")
  311. }
  312. c.JsonResult(0, "ok", c.BaseUrl()+beego.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
  313. } else {
  314. book.PrivateToken = ""
  315. if err := book.Update(); err != nil {
  316. logs.Error("CreateToken => ", err)
  317. c.JsonResult(6004, "删除令牌失败")
  318. }
  319. c.JsonResult(0, "ok", "")
  320. }
  321. }
  322. //项目设置.
  323. func (c *ManagerController) Setting() {
  324. c.Prepare()
  325. c.TplName = "manager/setting.tpl"
  326. if !c.Member.IsAdministrator() {
  327. c.Abort("403")
  328. }
  329. options, err := models.NewOption().All()
  330. if c.Ctx.Input.IsPost() {
  331. for _, item := range options {
  332. item.OptionValue = c.GetString(item.OptionName)
  333. item.InsertOrUpdate()
  334. }
  335. c.JsonResult(0, "ok")
  336. }
  337. if err != nil {
  338. c.Abort("500")
  339. }
  340. c.Data["SITE_TITLE"] = c.Option["SITE_NAME"]
  341. for _, item := range options {
  342. c.Data[item.OptionName] = item
  343. }
  344. }
  345. // Transfer 转让项目.
  346. func (c *ManagerController) Transfer() {
  347. c.Prepare()
  348. account := c.GetString("account")
  349. if account == "" {
  350. c.JsonResult(6004, "接受者账号不能为空")
  351. }
  352. member, err := models.NewMember().FindByAccount(account)
  353. if err != nil {
  354. logs.Error("FindByAccount => ", err)
  355. c.JsonResult(6005, "接受用户不存在")
  356. }
  357. if member.Status != 0 {
  358. c.JsonResult(6006, "接受用户已被禁用")
  359. }
  360. if !c.Member.IsAdministrator() {
  361. c.Abort("403")
  362. }
  363. identify := c.GetString("identify")
  364. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  365. if err != nil {
  366. c.JsonResult(6001, err.Error())
  367. }
  368. rel, err := models.NewRelationship().FindFounder(book.BookId)
  369. if err != nil {
  370. beego.Error("FindFounder => ", err)
  371. c.JsonResult(6009, "查询项目创始人失败")
  372. }
  373. if member.MemberId == rel.MemberId {
  374. c.JsonResult(6007, "不能转让给自己")
  375. }
  376. err = models.NewRelationship().Transfer(book.BookId, rel.MemberId, member.MemberId)
  377. if err != nil {
  378. logs.Error("Transfer => ", err)
  379. c.JsonResult(6008, err.Error())
  380. }
  381. c.JsonResult(0, "ok")
  382. }
  383. func (c *ManagerController) Comments() {
  384. c.Prepare()
  385. c.TplName = "manager/comments.tpl"
  386. if !c.Member.IsAdministrator() {
  387. c.Abort("403")
  388. }
  389. }
  390. //DeleteComment 标记评论为已删除
  391. func (c *ManagerController) DeleteComment() {
  392. c.Prepare()
  393. if !c.Member.IsAdministrator() {
  394. c.Abort("403")
  395. }
  396. comment_id, _ := c.GetInt("comment_id", 0)
  397. if comment_id <= 0 {
  398. c.JsonResult(6001, "参数错误")
  399. }
  400. comment := models.NewComment()
  401. if _, err := comment.Find(comment_id); err != nil {
  402. c.JsonResult(6002, "评论不存在")
  403. }
  404. comment.Approved = 3
  405. if err := comment.Update("approved"); err != nil {
  406. c.JsonResult(6003, "删除评论失败")
  407. }
  408. c.JsonResult(0, "ok", comment)
  409. }
  410. //设置项目私有状态.
  411. func (c *ManagerController) PrivatelyOwned() {
  412. status := c.GetString("status")
  413. identify := c.GetString("identify")
  414. if status != "open" && status != "close" {
  415. c.JsonResult(6003, "参数错误")
  416. }
  417. state := 0
  418. if status == "open" {
  419. state = 0
  420. } else {
  421. state = 1
  422. }
  423. if !c.Member.IsAdministrator() {
  424. c.Abort("403")
  425. }
  426. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  427. if err != nil {
  428. c.JsonResult(6001, err.Error())
  429. }
  430. book.PrivatelyOwned = state
  431. logs.Info("", state, status)
  432. err = book.Update()
  433. if err != nil {
  434. logs.Error("PrivatelyOwned => ", err)
  435. c.JsonResult(6004, "保存失败")
  436. }
  437. c.JsonResult(0, "ok")
  438. }