| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package settings | 5 package settings |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/base64" | 8 "encoding/base64" |
| 9 "encoding/json" | 9 "encoding/json" |
| 10 "fmt" | 10 "fmt" |
| 11 "net/http" | 11 "net/http" |
| 12 | 12 |
| 13 "github.com/julienschmidt/httprouter" | |
| 14 "github.com/luci/gae/service/datastore" | 13 "github.com/luci/gae/service/datastore" |
| 15 "github.com/luci/luci-go/appengine/cmd/milo/model" | 14 "github.com/luci/luci-go/appengine/cmd/milo/model" |
| 16 "github.com/luci/luci-go/appengine/cmd/milo/resp" | 15 "github.com/luci/luci-go/appengine/cmd/milo/resp" |
| 17 "github.com/luci/luci-go/server/auth" | 16 "github.com/luci/luci-go/server/auth" |
| 18 "github.com/luci/luci-go/server/auth/identity" | 17 "github.com/luci/luci-go/server/auth/identity" |
| 19 "github.com/luci/luci-go/server/auth/xsrf" | 18 "github.com/luci/luci-go/server/auth/xsrf" |
| 19 "github.com/luci/luci-go/server/router" |
| 20 "golang.org/x/net/context" | 20 "golang.org/x/net/context" |
| 21 ) | 21 ) |
| 22 | 22 |
| 23 type updateReq struct { | 23 type updateReq struct { |
| 24 Theme string | 24 Theme string |
| 25 } | 25 } |
| 26 | 26 |
| 27 // GetTheme returns the chosen theme based on the current user. | 27 // GetTheme returns the chosen theme based on the current user. |
| 28 func GetTheme(c context.Context, r *http.Request) Theme { | 28 func GetTheme(c context.Context, r *http.Request) Theme { |
| 29 cfg := getUserSettings(c) | 29 cfg := getUserSettings(c) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 } | 68 } |
| 69 err = json.Unmarshal([]byte(s), &config) | 69 err = json.Unmarshal([]byte(s), &config) |
| 70 if err != nil { | 70 if err != nil { |
| 71 panic(err) | 71 panic(err) |
| 72 } | 72 } |
| 73 return &config | 73 return &config |
| 74 } | 74 } |
| 75 | 75 |
| 76 // ChangeSettings is invoked in a POST request to settings and changes either | 76 // ChangeSettings is invoked in a POST request to settings and changes either |
| 77 // the user settings in the datastore, or the cookies if user is anon. | 77 // the user settings in the datastore, or the cookies if user is anon. |
| 78 func ChangeSettings(c context.Context, h http.ResponseWriter, r *http.Request, p
httprouter.Params) { | 78 func ChangeSettings(c *router.Context) { |
| 79 // First, check XSRF token. | 79 // First, check XSRF token. |
| 80 » err := xsrf.Check(c, r.FormValue("xsrf_token")) | 80 » err := xsrf.Check(c.Context, c.Request.FormValue("xsrf_token")) |
| 81 if err != nil { | 81 if err != nil { |
| 82 » » h.WriteHeader(http.StatusUnauthorized) | 82 » » c.Writer.WriteHeader(http.StatusUnauthorized) |
| 83 » » h.Write([]byte("Failed XSRF check.")) | 83 » » c.Writer.Write([]byte("Failed XSRF check.")) |
| 84 return | 84 return |
| 85 } | 85 } |
| 86 | 86 |
| 87 u := &updateReq{ | 87 u := &updateReq{ |
| 88 » » Theme: r.FormValue("theme"), | 88 » » Theme: c.Request.FormValue("theme"), |
| 89 } | 89 } |
| 90 validateUpdate(u) | 90 validateUpdate(u) |
| 91 » s := getUserSettings(c) | 91 » s := getUserSettings(c.Context) |
| 92 if s == nil { | 92 if s == nil { |
| 93 // User doesn't exist, just respond with a cookie. | 93 // User doesn't exist, just respond with a cookie. |
| 94 » » s = getCookieSettings(c, r) | 94 » » s = getCookieSettings(c.Context, c.Request) |
| 95 s.Theme = u.Theme | 95 s.Theme = u.Theme |
| 96 » » setCookieSettings(h, s) | 96 » » setCookieSettings(c.Writer, s) |
| 97 } else { | 97 } else { |
| 98 » » changeUserSettings(c, u) | 98 » » changeUserSettings(c.Context, u) |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Redirect to the GET endpoint. | 101 // Redirect to the GET endpoint. |
| 102 » http.Redirect(h, r, r.URL.String(), http.StatusSeeOther) | 102 » http.Redirect(c.Writer, c.Request, c.Request.URL.String(), http.StatusSe
eOther) |
| 103 } | 103 } |
| 104 | 104 |
| 105 // setCookieSettings sets the cfg object as a base64 json serialized string. | 105 // setCookieSettings sets the cfg object as a base64 json serialized string. |
| 106 func setCookieSettings(h http.ResponseWriter, cfg *model.UserConfig) { | 106 func setCookieSettings(h http.ResponseWriter, cfg *model.UserConfig) { |
| 107 s, err := json.Marshal(cfg) | 107 s, err := json.Marshal(cfg) |
| 108 if err != nil { | 108 if err != nil { |
| 109 panic(err) | 109 panic(err) |
| 110 } | 110 } |
| 111 bs := base64.StdEncoding.EncodeToString(s) | 111 bs := base64.StdEncoding.EncodeToString(s) |
| 112 cookie := http.Cookie{ | 112 cookie := http.Cookie{ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 141 | 141 |
| 142 result := &resp.Settings{} | 142 result := &resp.Settings{} |
| 143 result.ActionURL = r.URL.String() | 143 result.ActionURL = r.URL.String() |
| 144 result.Theme = &resp.Choices{ | 144 result.Theme = &resp.Choices{ |
| 145 Choices: GetAllThemes(), | 145 Choices: GetAllThemes(), |
| 146 Selected: userSettings.Theme, | 146 Selected: userSettings.Theme, |
| 147 } | 147 } |
| 148 | 148 |
| 149 return result, nil | 149 return result, nil |
| 150 } | 150 } |
| OLD | NEW |