controller.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package controllers
  2. // This helper is a hack to bypass this issue: https://github.com/revel/revel/issues/1239
  3. import (
  4. "devel.mephi.ru/dyokunev/cps-api/app/common"
  5. "devel.mephi.ru/dyokunev/cps-models"
  6. asuModels "devel.mephi.ru/dyokunev/go-asu-models"
  7. sdModels11 "devel.mephi.ru/dyokunev/go-sd-models/api11"
  8. "fmt"
  9. "github.com/go-sql-driver/mysql"
  10. "github.com/revel/revel"
  11. )
  12. type Controller struct {
  13. *revel.Controller
  14. EventSQL *models.EventScope
  15. PassSQL *models.PassScope
  16. PersonSQL *sdModels11.PersonScope
  17. UnitSQL *asuModels.UnitScope
  18. FormularSQL *asuModels.FormularScope
  19. TimesheetRowSQL *asuModels.TimesheetRowScope
  20. }
  21. func (c *Controller) init() (err error) {
  22. if c.Args["me"] == nil {
  23. return
  24. }
  25. me := c.Args["me"].(common.UserInfo)
  26. c.EventSQL = models.EventSQL.Scope()
  27. c.PassSQL = models.PassSQL.Scope()
  28. c.PersonSQL = sdModels11.PersonSQL.Scope()
  29. c.UnitSQL = asuModels.UnitSQL.Scope()
  30. c.FormularSQL = asuModels.FormularSQL.Scope()
  31. c.TimesheetRowSQL = asuModels.TimesheetRowSQL.Scope()
  32. if me.Permissions.IsFullAccess {
  33. return
  34. }
  35. allowedEmpGUIDs := me.Permissions.AllowedPeople.GetEmpGUIDs()
  36. allowedUnitIds := me.Permissions.AllowedUnits.GetUnitIds()
  37. if !me.Permissions.IsFixOk {
  38. c.PassSQL = models.PassSQL.Where("EmpGUID = ?", me.EmpGUID)
  39. }
  40. c.EventSQL = c.EventSQL.Where("EmpGUID IN (?)", allowedEmpGUIDs)
  41. c.PersonSQL = c.PersonSQL.Where("EmpGUID IN (?)", allowedEmpGUIDs)
  42. c.UnitSQL = c.UnitSQL.Where("id IN (?)", allowedUnitIds)
  43. c.FormularSQL = c.FormularSQL.Where("EmpGUID IN (?) OR OrgDiv IN (?)", allowedEmpGUIDs, allowedUnitIds)
  44. c.TimesheetRowSQL = c.TimesheetRowSQL.Where("`table_view`.`EmpGUID` IN (?)", allowedEmpGUIDs)
  45. return
  46. }
  47. func (c *Controller) Init() revel.Result {
  48. return c.ConsiderErr(c.init())
  49. }
  50. func (c Controller) ConsiderErr(err error) revel.Result {
  51. if err == nil {
  52. return nil
  53. }
  54. if err == mysql.ErrInvalidConn {
  55. return c.ErrorTryLater("Too long query execution time")
  56. }
  57. return c.Error(err)
  58. }
  59. func (c Controller) ErrorTryLater(v interface{}) revel.Result {
  60. c.ViewArgs["status"] = "ERROR"
  61. c.ViewArgs["error_action"] = "retry"
  62. c.ViewArgs["error_description"] = fmt.Sprintf("%v", v)
  63. return c.RenderJSON(c.ViewArgs)
  64. }
  65. func (c Controller) Error(v interface{}) revel.Result {
  66. if revel.DevMode {
  67. revel.ERROR.Panicf("Got error: %v", v)
  68. }
  69. c.ViewArgs["status"] = "ERROR"
  70. c.ViewArgs["error_description"] = fmt.Sprintf("%v", v)
  71. return c.RenderJSON(c.ViewArgs)
  72. }
  73. func (c *Controller) Redirect(url string) revel.Result {
  74. return c.Controller.Redirect(url)
  75. }
  76. func (c *Controller) Render(arg interface{}) revel.Result {
  77. c.ViewArgs["status"] = "OK"
  78. c.ViewArgs["arg"] = arg
  79. return c.RenderJSON(c.ViewArgs)
  80. }
  81. func (c Controller) GetMe() common.UserInfo {
  82. return c.Args["me"].(common.UserInfo)
  83. }