package controllers // This helper is a hack to bypass this issue: https://github.com/revel/revel/issues/1239 import ( "devel.mephi.ru/dyokunev/cps-api/app/common" "devel.mephi.ru/dyokunev/cps-models" asuModels "devel.mephi.ru/dyokunev/go-asu-models" sdModels11 "devel.mephi.ru/dyokunev/go-sd-models/api11" "fmt" "github.com/go-sql-driver/mysql" "github.com/revel/revel" ) type Controller struct { *revel.Controller EventSQL *models.EventScope PassSQL *models.PassScope PersonSQL *sdModels11.PersonScope UnitSQL *asuModels.UnitScope FormularSQL *asuModels.FormularScope TimesheetRowSQL *asuModels.TimesheetRowScope } func (c *Controller) init() (err error) { if c.Args["me"] == nil { return } me := c.Args["me"].(common.UserInfo) c.EventSQL = models.EventSQL.Scope() c.PassSQL = models.PassSQL.Scope() c.PersonSQL = sdModels11.PersonSQL.Scope() c.UnitSQL = asuModels.UnitSQL.Scope() c.FormularSQL = asuModels.FormularSQL.Scope() c.TimesheetRowSQL = asuModels.TimesheetRowSQL.Scope() if me.Permissions.IsFullAccess { return } allowedEmpGUIDs := me.Permissions.AllowedPeople.GetEmpGUIDs() allowedUnitIds := me.Permissions.AllowedUnits.GetUnitIds() if !me.Permissions.IsFixOk { c.PassSQL = models.PassSQL.Where("EmpGUID = ?", me.EmpGUID) } c.EventSQL = c.EventSQL.Where("EmpGUID IN (?)", allowedEmpGUIDs) c.PersonSQL = c.PersonSQL.Where("EmpGUID IN (?)", allowedEmpGUIDs) c.UnitSQL = c.UnitSQL.Where("id IN (?)", allowedUnitIds) c.FormularSQL = c.FormularSQL.Where("EmpGUID IN (?) OR OrgDiv IN (?)", allowedEmpGUIDs, allowedUnitIds) c.TimesheetRowSQL = c.TimesheetRowSQL.Where("`table_view`.`EmpGUID` IN (?)", allowedEmpGUIDs) return } func (c *Controller) Init() revel.Result { return c.ConsiderErr(c.init()) } func (c Controller) ConsiderErr(err error) revel.Result { if err == nil { return nil } if err == mysql.ErrInvalidConn { return c.ErrorTryLater("Too long query execution time") } return c.Error(err) } func (c Controller) ErrorTryLater(v interface{}) revel.Result { c.ViewArgs["status"] = "ERROR" c.ViewArgs["error_action"] = "retry" c.ViewArgs["error_description"] = fmt.Sprintf("%v", v) return c.RenderJSON(c.ViewArgs) } func (c Controller) Error(v interface{}) revel.Result { if revel.DevMode { revel.ERROR.Panicf("Got error: %v", v) } c.ViewArgs["status"] = "ERROR" c.ViewArgs["error_description"] = fmt.Sprintf("%v", v) return c.RenderJSON(c.ViewArgs) } func (c *Controller) Redirect(url string) revel.Result { return c.Controller.Redirect(url) } func (c *Controller) Render(arg interface{}) revel.Result { c.ViewArgs["status"] = "OK" c.ViewArgs["arg"] = arg return c.RenderJSON(c.ViewArgs) } func (c Controller) GetMe() common.UserInfo { return c.Args["me"].(common.UserInfo) }