mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-04 05:18:25 +00:00 
			
		
		
		
	Add ContextUser to http request context (#18798)
				
					
				
			This PR adds a middleware which sets a ContextUser (like GetUserByParams before) in a single place which can be used by other methods. For routes which represent a repo or org the respective middlewares set the field too. Also fix a bug in modules/context/org.go during refactoring.
This commit is contained in:
		@@ -87,6 +87,7 @@ import (
 | 
			
		||||
	"code.gitea.io/gitea/routers/api/v1/settings"
 | 
			
		||||
	"code.gitea.io/gitea/routers/api/v1/user"
 | 
			
		||||
	"code.gitea.io/gitea/services/auth"
 | 
			
		||||
	context_service "code.gitea.io/gitea/services/context"
 | 
			
		||||
	"code.gitea.io/gitea/services/forms"
 | 
			
		||||
 | 
			
		||||
	_ "code.gitea.io/gitea/routers/api/v1/swagger" // for swagger generation
 | 
			
		||||
@@ -156,6 +157,7 @@ func repoAssignment() func(ctx *context.APIContext) {
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		ctx.Repo.Owner = owner
 | 
			
		||||
		ctx.ContextUser = owner
 | 
			
		||||
 | 
			
		||||
		// Get repository.
 | 
			
		||||
		repo, err := repo_model.GetRepositoryByName(owner.ID, repoName)
 | 
			
		||||
@@ -441,6 +443,7 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) {
 | 
			
		||||
				}
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			ctx.ContextUser = ctx.Org.Organization.AsUser()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if assignTeam {
 | 
			
		||||
@@ -636,7 +639,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
 | 
			
		||||
						Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
 | 
			
		||||
					m.Combo("/{id}").Delete(user.DeleteAccessToken)
 | 
			
		||||
				}, reqBasicOrRevProxyAuth())
 | 
			
		||||
			})
 | 
			
		||||
			}, context_service.UserAssignmentAPI())
 | 
			
		||||
		})
 | 
			
		||||
 | 
			
		||||
		m.Group("/users", func() {
 | 
			
		||||
@@ -653,7 +656,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
 | 
			
		||||
				m.Get("/starred", user.GetStarredRepos)
 | 
			
		||||
 | 
			
		||||
				m.Get("/subscriptions", user.GetWatchedRepos)
 | 
			
		||||
			})
 | 
			
		||||
			}, context_service.UserAssignmentAPI())
 | 
			
		||||
		}, reqToken())
 | 
			
		||||
 | 
			
		||||
		m.Group("/user", func() {
 | 
			
		||||
@@ -669,7 +672,11 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
 | 
			
		||||
			m.Get("/followers", user.ListMyFollowers)
 | 
			
		||||
			m.Group("/following", func() {
 | 
			
		||||
				m.Get("", user.ListMyFollowing)
 | 
			
		||||
				m.Combo("/{username}").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
 | 
			
		||||
				m.Group("/{username}", func() {
 | 
			
		||||
					m.Get("", user.CheckMyFollowing)
 | 
			
		||||
					m.Put("", user.Follow)
 | 
			
		||||
					m.Delete("", user.Unfollow)
 | 
			
		||||
				}, context_service.UserAssignmentAPI())
 | 
			
		||||
			})
 | 
			
		||||
 | 
			
		||||
			m.Group("/keys", func() {
 | 
			
		||||
@@ -1005,7 +1012,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
 | 
			
		||||
		m.Group("/users/{username}/orgs", func() {
 | 
			
		||||
			m.Get("", org.ListUserOrgs)
 | 
			
		||||
			m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
 | 
			
		||||
		})
 | 
			
		||||
		}, context_service.UserAssignmentAPI())
 | 
			
		||||
		m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.Create)
 | 
			
		||||
		m.Get("/orgs", org.GetAll)
 | 
			
		||||
		m.Group("/orgs/{org}", func() {
 | 
			
		||||
@@ -1083,7 +1090,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
 | 
			
		||||
					m.Get("/orgs", org.ListUserOrgs)
 | 
			
		||||
					m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
 | 
			
		||||
					m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
 | 
			
		||||
				})
 | 
			
		||||
				}, context_service.UserAssignmentAPI())
 | 
			
		||||
			})
 | 
			
		||||
			m.Group("/unadopted", func() {
 | 
			
		||||
				m.Get("", admin.ListUnadoptedRepositories)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user