@@ -23,6 +23,7 @@ import (
23
23
"code.gitea.io/gitea/models"
24
24
"code.gitea.io/gitea/modules/auth/sso"
25
25
"code.gitea.io/gitea/modules/base"
26
+ mc "code.gitea.io/gitea/modules/cache"
26
27
"code.gitea.io/gitea/modules/log"
27
28
"code.gitea.io/gitea/modules/middlewares"
28
29
"code.gitea.io/gitea/modules/setting"
@@ -355,8 +356,8 @@ func (ctx *Context) Error(status int, contents ...string) {
355
356
356
357
// JSON render content as JSON
357
358
func (ctx * Context ) JSON (status int , content interface {}) {
358
- ctx .Resp .WriteHeader (status )
359
359
ctx .Resp .Header ().Set ("Content-Type" , "application/json;charset=utf8" )
360
+ ctx .Resp .WriteHeader (status )
360
361
if err := json .NewEncoder (ctx .Resp ).Encode (content ); err != nil {
361
362
ctx .ServerError ("Render JSON failed" , err )
362
363
}
@@ -484,6 +485,31 @@ func GetContext(req *http.Request) *Context {
484
485
return req .Context ().Value (contextKey ).(* Context )
485
486
}
486
487
488
+ // SignedUserName returns signed user's name via context
489
+ func SignedUserName (req * http.Request ) string {
490
+ if middlewares .IsInternalPath (req ) {
491
+ return ""
492
+ }
493
+ if middlewares .IsAPIPath (req ) {
494
+ ctx , ok := req .Context ().Value (apiContextKey ).(* APIContext )
495
+ if ok {
496
+ v := ctx .Data ["SignedUserName" ]
497
+ if res , ok := v .(string ); ok {
498
+ return res
499
+ }
500
+ }
501
+ } else {
502
+ ctx , ok := req .Context ().Value (contextKey ).(* Context )
503
+ if ok {
504
+ v := ctx .Data ["SignedUserName" ]
505
+ if res , ok := v .(string ); ok {
506
+ return res
507
+ }
508
+ }
509
+ }
510
+ return ""
511
+ }
512
+
487
513
func getCsrfOpts () CsrfOptions {
488
514
return CsrfOptions {
489
515
Secret : setting .SecretKey ,
@@ -499,23 +525,8 @@ func getCsrfOpts() CsrfOptions {
499
525
500
526
// Contexter initializes a classic context for a request.
501
527
func Contexter () func (next http.Handler ) http.Handler {
502
- rnd := templates .HTMLRenderer ()
503
-
504
- var c cache.Cache
505
- var err error
506
- if setting .CacheService .Enabled {
507
- c , err = cache .NewCacher (cache.Options {
508
- Adapter : setting .CacheService .Adapter ,
509
- AdapterConfig : setting .CacheService .Conn ,
510
- Interval : setting .CacheService .Interval ,
511
- })
512
- if err != nil {
513
- panic (err )
514
- }
515
- }
516
-
528
+ var rnd = templates .HTMLRenderer ()
517
529
var csrfOpts = getCsrfOpts ()
518
- //var flashEncryptionKey, _ = NewSecret()
519
530
520
531
return func (next http.Handler ) http.Handler {
521
532
return http .HandlerFunc (func (resp http.ResponseWriter , req * http.Request ) {
@@ -524,7 +535,7 @@ func Contexter() func(next http.Handler) http.Handler {
524
535
var link = setting .AppSubURL + strings .TrimSuffix (req .URL .EscapedPath (), "/" )
525
536
var ctx = Context {
526
537
Resp : NewResponse (resp ),
527
- Cache : c ,
538
+ Cache : mc . GetCache () ,
528
539
Locale : locale ,
529
540
Link : link ,
530
541
Render : rnd ,
@@ -571,16 +582,14 @@ func Contexter() func(next http.Handler) http.Handler {
571
582
}
572
583
ctx .Resp .Before (func (resp ResponseWriter ) {
573
584
if flash := f .Encode (); len (flash ) > 0 {
574
- if err == nil {
575
- middlewares .SetCookie (resp , "macaron_flash" , flash , 0 ,
576
- setting .SessionConfig .CookiePath ,
577
- middlewares .Domain (setting .SessionConfig .Domain ),
578
- middlewares .HTTPOnly (true ),
579
- middlewares .Secure (setting .SessionConfig .Secure ),
580
- //middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config
581
- )
582
- return
583
- }
585
+ middlewares .SetCookie (resp , "macaron_flash" , flash , 0 ,
586
+ setting .SessionConfig .CookiePath ,
587
+ middlewares .Domain (setting .SessionConfig .Domain ),
588
+ middlewares .HTTPOnly (true ),
589
+ middlewares .Secure (setting .SessionConfig .Secure ),
590
+ //middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config
591
+ )
592
+ return
584
593
}
585
594
586
595
ctx .SetCookie ("macaron_flash" , "" , - 1 ,
0 commit comments