MCPcopy
hub / github.com/writefreely/writefreely / AdminApper

Method AdminApper

handle.go:214–249  ·  view source on GitHub ↗

AdminApper handles requests on /admin routes that require an Apper.

(f userApperHandlerFunc)

Source from the content-addressed store, hash-verified

212
213// AdminApper handles requests on /admin routes that require an Apper.
214func (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
251func apiAuth(app *App, r *http.Request) (*User, error) {
252 // Authorize user from Authorization header

Callers 1

InitRoutesFunction · 0.80

Calls 7

handleHTTPErrorMethod · 0.95
pageForReqFunction · 0.85
getUserSessionFunction · 0.85
IsAdminMethod · 0.80
AppMethod · 0.65
ReqLogMethod · 0.65
fFunction · 0.50

Tested by

no test coverage detected