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.0 kB

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() bool {
  33. if c.GetSessionUser() == nil {
  34. c.ResponseError("please sign in first")
  35. return true
  36. }
  37. return false
  38. }
  39. func (c *ApiController) RequireAdmin() bool {
  40. user := c.GetSessionUser()
  41. if user == nil || !user.IsAdmin {
  42. c.ResponseError("this operation requires admin privilege")
  43. return true
  44. }
  45. return false
  46. }

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

Contributors (1)