The new Ripple frontend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

simple.go 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/url"
  6. "github.com/gin-gonic/gin"
  7. )
  8. func loadSimplePages(r *gin.Engine) {
  9. for _, el := range simplePages {
  10. if el.Handler == "" {
  11. continue
  12. }
  13. r.GET(el.Handler, simplePageFunc(el))
  14. }
  15. }
  16. func simplePageFunc(p templateConfig) gin.HandlerFunc {
  17. return func(c *gin.Context) {
  18. s := getContext(c)
  19. if s.User.Privileges&p.mp() != p.mp() {
  20. resp403(c)
  21. return
  22. }
  23. simple(c, p, nil, nil)
  24. }
  25. }
  26. func resp403(c *gin.Context) {
  27. if getContext(c).User.ID == 0 {
  28. ru := c.Request.URL
  29. addMessage(c, warningMessage{T(c, "You need to login first.")})
  30. getSession(c).Save()
  31. c.Redirect(302, "/login?redir="+url.QueryEscape(ru.Path+"?"+ru.RawQuery))
  32. return
  33. }
  34. respEmpty(c, "Forbidden", warningMessage{T(c, "You should not be 'round here.")})
  35. }
  36. func simpleReply(c *gin.Context, errs ...message) error {
  37. t := getSimple(c.Request.URL.Path)
  38. if t.Handler == "" {
  39. return errors.New("simpleReply: simplepage not found")
  40. }
  41. simple(c, t, errs, nil)
  42. return nil
  43. }
  44. func getSimple(h string) templateConfig {
  45. for _, s := range simplePages {
  46. if s.Handler == h {
  47. return s
  48. }
  49. }
  50. fmt.Println("oh handler shit not found", h)
  51. return templateConfig{}
  52. }
  53. func getSimpleByFilename(f string) templateConfig {
  54. for _, s := range simplePages {
  55. if s.Template == f {
  56. return s
  57. }
  58. }
  59. fmt.Println("oh shit not found", f)
  60. return templateConfig{}
  61. }
  62. func simple(c *gin.Context, p templateConfig, errs []message, requestInfo map[string]interface{}) {
  63. resp(c, 200, p.Template, &baseTemplateData{
  64. TitleBar: p.TitleBar,
  65. KyutGrill: p.KyutGrill,
  66. Scripts: p.additionalJS(),
  67. HeadingOnRight: p.HugeHeadingRight,
  68. FormData: normaliseURLValues(c.Request.PostForm),
  69. RequestInfo: requestInfo,
  70. Messages: errs,
  71. })
  72. }
  73. func normaliseURLValues(uv url.Values) map[string]string {
  74. m := make(map[string]string, len(uv))
  75. for k, v := range uv {
  76. if len(v) > 0 {
  77. m[k] = v[0]
  78. }
  79. }
  80. return m
  81. }