manager.go 12 KB

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