| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 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 admin implements HTTP routes for settings UI. | 5 // Package admin implements HTTP routes for settings UI. |
| 6 package admin | 6 package admin |
| 7 | 7 |
| 8 import ( | 8 import ( |
| 9 "html/template" | 9 "html/template" |
| 10 "net" | 10 "net" |
| 11 "net/http" | 11 "net/http" |
| 12 | 12 |
| 13 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 14 | 14 |
| 15 "github.com/luci/luci-go/common/errors" | 15 "github.com/luci/luci-go/common/errors" |
| 16 "github.com/luci/luci-go/common/logging" | 16 "github.com/luci/luci-go/common/logging" |
| 17 "github.com/luci/luci-go/common/retry" |
| 17 | 18 |
| 18 "github.com/luci/luci-go/server/auth" | 19 "github.com/luci/luci-go/server/auth" |
| 19 "github.com/luci/luci-go/server/auth/authdb" | 20 "github.com/luci/luci-go/server/auth/authdb" |
| 20 "github.com/luci/luci-go/server/auth/identity" | 21 "github.com/luci/luci-go/server/auth/identity" |
| 21 "github.com/luci/luci-go/server/auth/xsrf" | 22 "github.com/luci/luci-go/server/auth/xsrf" |
| 22 "github.com/luci/luci-go/server/router" | 23 "github.com/luci/luci-go/server/router" |
| 23 "github.com/luci/luci-go/server/templates" | 24 "github.com/luci/luci-go/server/templates" |
| 24 | 25 |
| 25 "github.com/luci/luci-go/server/settings/admin/internal/assets" | 26 "github.com/luci/luci-go/server/settings/admin/internal/assets" |
| 26 ) | 27 ) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 )) | 68 )) |
| 68 | 69 |
| 69 rr.GET("", router.MiddlewareChain{}, indexPage) | 70 rr.GET("", router.MiddlewareChain{}, indexPage) |
| 70 rr.GET("/:SettingsKey", router.MiddlewareChain{}, settingsPageGET) | 71 rr.GET("/:SettingsKey", router.MiddlewareChain{}, settingsPageGET) |
| 71 rr.POST("/:SettingsKey", router.NewMiddlewareChain(xsrf.WithTokenCheck),
settingsPagePOST) | 72 rr.POST("/:SettingsKey", router.NewMiddlewareChain(xsrf.WithTokenCheck),
settingsPagePOST) |
| 72 } | 73 } |
| 73 | 74 |
| 74 // replyError sends HTML error page with status 500 on transient errors or 400 | 75 // replyError sends HTML error page with status 500 on transient errors or 400 |
| 75 // on fatal ones. | 76 // on fatal ones. |
| 76 func replyError(c context.Context, rw http.ResponseWriter, err error) { | 77 func replyError(c context.Context, rw http.ResponseWriter, err error) { |
| 77 » if errors.IsTransient(err) { | 78 » if retry.Tag.In(err) { |
| 78 rw.WriteHeader(http.StatusInternalServerError) | 79 rw.WriteHeader(http.StatusInternalServerError) |
| 79 } else { | 80 } else { |
| 80 rw.WriteHeader(http.StatusBadRequest) | 81 rw.WriteHeader(http.StatusBadRequest) |
| 81 } | 82 } |
| 82 templates.MustRender(c, rw, "pages/error.html", templates.Args{ | 83 templates.MustRender(c, rw, "pages/error.html", templates.Args{ |
| 83 "Error": err.Error(), | 84 "Error": err.Error(), |
| 84 }) | 85 }) |
| 85 } | 86 } |
| 86 | 87 |
| 87 //////////////////////////////////////////////////////////////////////////////// | 88 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 // Redirect anonymous users to a login page that redirects back to the c
urrent | 129 // Redirect anonymous users to a login page that redirects back to the c
urrent |
| 129 // page. | 130 // page. |
| 130 if u.Identity == identity.AnonymousIdentity { | 131 if u.Identity == identity.AnonymousIdentity { |
| 131 // Make the current URL relative to the host. | 132 // Make the current URL relative to the host. |
| 132 destURL := *c.Request.URL | 133 destURL := *c.Request.URL |
| 133 destURL.Host = "" | 134 destURL.Host = "" |
| 134 destURL.Scheme = "" | 135 destURL.Scheme = "" |
| 135 url, err := auth.LoginURL(c.Context, destURL.String()) | 136 url, err := auth.LoginURL(c.Context, destURL.String()) |
| 136 if err != nil { | 137 if err != nil { |
| 137 logging.WithError(err).Errorf(c.Context, "Error when gen
erating login URL") | 138 logging.WithError(err).Errorf(c.Context, "Error when gen
erating login URL") |
| 138 » » » if errors.IsTransient(err) { | 139 » » » if retry.Tag.In(err) { |
| 139 http.Error(c.Writer, "Transient error when gener
ating login URL, see logs", 500) | 140 http.Error(c.Writer, "Transient error when gener
ating login URL, see logs", 500) |
| 140 } else { | 141 } else { |
| 141 http.Error(c.Writer, "Can't generate login URL,
see logs", 401) | 142 http.Error(c.Writer, "Can't generate login URL,
see logs", 401) |
| 142 } | 143 } |
| 143 return | 144 return |
| 144 } | 145 } |
| 145 http.Redirect(c.Writer, c.Request, url, 302) | 146 http.Redirect(c.Writer, c.Request, url, 302) |
| 146 return | 147 return |
| 147 } | 148 } |
| 148 | 149 |
| 149 // Non anonymous users must be admins to proceed. | 150 // Non anonymous users must be admins to proceed. |
| 150 if !u.Superuser { | 151 if !u.Superuser { |
| 151 c.Writer.WriteHeader(http.StatusForbidden) | 152 c.Writer.WriteHeader(http.StatusForbidden) |
| 152 templates.MustRender(c.Context, c.Writer, "pages/access_denied.h
tml", nil) | 153 templates.MustRender(c.Context, c.Writer, "pages/access_denied.h
tml", nil) |
| 153 return | 154 return |
| 154 } | 155 } |
| 155 | 156 |
| 156 next(c) | 157 next(c) |
| 157 } | 158 } |
| OLD | NEW |