check.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package controllers
  2. import (
  3. "errors"
  4. "github.com/astaxie/beego/logs"
  5. "github.com/astaxie/beego/orm"
  6. "nginx-ui/server/middleware"
  7. "nginx-ui/server/models"
  8. "strconv"
  9. )
  10. // CheckNginxPermission 从path中获取nginx的参数
  11. func (c *BaseController) CheckNginxPermission() (*models.Nginx, error) {
  12. idStr := c.getParam(":id")
  13. id, err := strconv.Atoi(idStr)
  14. if err != nil {
  15. logs.Warn("strconv.Atoi(idStr) fail", idStr)
  16. c.setCode(-1).setMsg("请传递正确的参数!").json()
  17. return nil, err
  18. }
  19. return c.CheckNginxPermissionById(id)
  20. }
  21. // CheckNginxPermissionById 验证权限,如果无权操作该nginx,返回nil,否则返回
  22. func (c *BaseController) CheckNginxPermissionById(nginxId int) (*models.Nginx, error) {
  23. current := c.RequiredUser()
  24. if current == nil {
  25. middleware.WriteForbidden(c.Ctx.ResponseWriter)
  26. return nil, errors.New("当前未登录,无法操作")
  27. }
  28. if nginxId < 1 {
  29. c.setCode(-1).setMsg("Nginx ID must gt 0!").json()
  30. return nil, errors.New("nginx ID must gt 0!")
  31. }
  32. nginx := models.Nginx{Id: nginxId}
  33. o := orm.NewOrm()
  34. err := o.Read(&nginx)
  35. if err != nil {
  36. c.ErrorJson(err)
  37. return nil, err
  38. }
  39. if !current.IsAdmin() && current.Account != nginx.Uid {
  40. c.Forbidden()
  41. return nil, errors.New("forbidden")
  42. }
  43. return &nginx, nil
  44. }