(initConfig *resource.CmsConfig, cruds map[string]*resource.DbResource, configStore *resource.ConfigStore)
| 9 | ) |
| 10 | |
| 11 | func CreateConfigHandler(initConfig *resource.CmsConfig, cruds map[string]*resource.DbResource, configStore *resource.ConfigStore) func(*gin.Context) { |
| 12 | return func(c *gin.Context) { |
| 13 | |
| 14 | user := c.Request.Context().Value("user") |
| 15 | sessionUser := &auth.SessionUser{} |
| 16 | |
| 17 | if user != nil { |
| 18 | sessionUser = user.(*auth.SessionUser) |
| 19 | } |
| 20 | |
| 21 | userAccountTableCrud := cruds[resource.USER_ACCOUNT_TABLE_NAME] |
| 22 | transaction, err := userAccountTableCrud.Connection().Beginx() |
| 23 | if err != nil { |
| 24 | resource.CheckErr(err, "Failed to begin transaction [24]") |
| 25 | return |
| 26 | } |
| 27 | |
| 28 | defer transaction.Commit() |
| 29 | |
| 30 | if !resource.IsAdminWithTransaction(sessionUser, transaction) { |
| 31 | c.AbortWithError(403, fmt.Errorf("unauthorized")) |
| 32 | return |
| 33 | } |
| 34 | log.Tracef("User [%v] has access to config", sessionUser.UserReferenceId) |
| 35 | |
| 36 | if c.Request.Method == "GET" { |
| 37 | |
| 38 | key := c.Param("key") |
| 39 | |
| 40 | if key == "" { |
| 41 | c.AbortWithStatusJSON(200, configStore.GetAllConfig(transaction)) |
| 42 | } else { |
| 43 | end := c.Param("end") |
| 44 | val, err := configStore.GetConfigValueFor(key, end, transaction) |
| 45 | if err != nil { |
| 46 | c.AbortWithStatus(404) |
| 47 | return |
| 48 | } |
| 49 | c.String(200, "%v", val) |
| 50 | } |
| 51 | |
| 52 | } else if c.Request.Method == "POST" { |
| 53 | |
| 54 | key := c.Param("key") |
| 55 | end := c.Param("end") |
| 56 | |
| 57 | if key == "" || end == "" { |
| 58 | c.AbortWithStatus(400) |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | newVal, err := c.GetRawData() |
| 63 | if err != nil { |
| 64 | c.AbortWithStatus(400) |
| 65 | return |
| 66 | } |
| 67 | err = configStore.SetConfigValueFor(key, string(newVal), end, transaction) |
| 68 | if err != nil { |
no test coverage detected