AdminApper handles requests on /admin routes that require an Apper.
(f userApperHandlerFunc)
| 212 | |
| 213 | // AdminApper handles requests on /admin routes that require an Apper. |
| 214 | func (h *Handler) AdminApper(f userApperHandlerFunc) http.HandlerFunc { |
| 215 | return func(w http.ResponseWriter, r *http.Request) { |
| 216 | h.handleHTTPError(w, r, func() error { |
| 217 | var status int |
| 218 | start := time.Now() |
| 219 | |
| 220 | defer func() { |
| 221 | if e := recover(); e != nil { |
| 222 | log.Error("%s: %s", e, debug.Stack()) |
| 223 | h.errors.InternalServerError.ExecuteTemplate(w, "base", pageForReq(h.app.App(), r)) |
| 224 | status = http.StatusInternalServerError |
| 225 | } |
| 226 | |
| 227 | log.Info(h.app.ReqLog(r, status, time.Since(start))) |
| 228 | }() |
| 229 | |
| 230 | u := getUserSession(h.app.App(), r) |
| 231 | if u == nil || !u.IsAdmin() { |
| 232 | err := impart.HTTPError{http.StatusNotFound, ""} |
| 233 | status = err.Status |
| 234 | return err |
| 235 | } |
| 236 | |
| 237 | err := f(h.app, u, w, r) |
| 238 | if err == nil { |
| 239 | status = http.StatusOK |
| 240 | } else if err, ok := err.(impart.HTTPError); ok { |
| 241 | status = err.Status |
| 242 | } else { |
| 243 | status = http.StatusInternalServerError |
| 244 | } |
| 245 | |
| 246 | return err |
| 247 | }()) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | func apiAuth(app *App, r *http.Request) (*User, error) { |
| 252 | // Authorize user from Authorization header |
no test coverage detected