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

util.go 1.1 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package controllers
  2. type Response struct {
  3. Status string `json:"status"`
  4. Msg string `json:"msg"`
  5. Data interface{} `json:"data"`
  6. Data2 interface{} `json:"data2"`
  7. }
  8. func (c *ApiController) ResponseOk(data ...interface{}) {
  9. resp := Response{Status: "ok"}
  10. switch len(data) {
  11. case 2:
  12. resp.Data2 = data[1]
  13. fallthrough
  14. case 1:
  15. resp.Data = data[0]
  16. }
  17. c.Data["json"] = resp
  18. c.ServeJSON()
  19. }
  20. func (c *ApiController) ResponseError(error string, data ...interface{}) {
  21. resp := Response{Status: "error", Msg: error}
  22. switch len(data) {
  23. case 2:
  24. resp.Data2 = data[1]
  25. fallthrough
  26. case 1:
  27. resp.Data = data[0]
  28. }
  29. c.Data["json"] = resp
  30. c.ServeJSON()
  31. }
  32. func (c *ApiController) RequireSignedIn() (string, bool) {
  33. userId := c.GetSessionUsername()
  34. if userId == "" {
  35. c.ResponseError("Please sign in first")
  36. return "", false
  37. }
  38. return userId, true
  39. }
  40. func (c *ApiController) RequireAdmin() bool {
  41. user := c.GetSessionUser()
  42. if user == nil || !user.IsAdmin {
  43. c.ResponseError("this operation requires admin privilege")
  44. return true
  45. }
  46. return false
  47. }

基于Casbin的开源AI领域知识库平台

Contributors (1)