mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 11:52:10 +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:
parent
f36701c702
commit
59b867dc2d
23 changed files with 247 additions and 323 deletions
|
@ -82,11 +82,7 @@ func ListFollowers(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/UserList"
|
||||
|
||||
u := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
listUserFollowers(ctx, u)
|
||||
listUserFollowers(ctx, ctx.ContextUser)
|
||||
}
|
||||
|
||||
func listUserFollowing(ctx *context.APIContext, u *user_model.User) {
|
||||
|
@ -148,11 +144,7 @@ func ListFollowing(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/UserList"
|
||||
|
||||
u := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
listUserFollowing(ctx, u)
|
||||
listUserFollowing(ctx, ctx.ContextUser)
|
||||
}
|
||||
|
||||
func checkUserFollowing(ctx *context.APIContext, u *user_model.User, followID int64) {
|
||||
|
@ -180,25 +172,21 @@ func CheckMyFollowing(ctx *context.APIContext) {
|
|||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
target := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
checkUserFollowing(ctx, ctx.Doer, target.ID)
|
||||
checkUserFollowing(ctx, ctx.Doer, ctx.ContextUser.ID)
|
||||
}
|
||||
|
||||
// CheckFollowing check if one user is following another user
|
||||
func CheckFollowing(ctx *context.APIContext) {
|
||||
// swagger:operation GET /users/{follower}/following/{followee} user userCheckFollowing
|
||||
// swagger:operation GET /users/{username}/following/{target} user userCheckFollowing
|
||||
// ---
|
||||
// summary: Check if one user is following another user
|
||||
// parameters:
|
||||
// - name: follower
|
||||
// - name: username
|
||||
// in: path
|
||||
// description: username of following user
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: followee
|
||||
// - name: target
|
||||
// in: path
|
||||
// description: username of followed user
|
||||
// type: string
|
||||
|
@ -209,15 +197,11 @@ func CheckFollowing(ctx *context.APIContext) {
|
|||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
u := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
target := GetUserByParamsName(ctx, ":target")
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
checkUserFollowing(ctx, u, target.ID)
|
||||
checkUserFollowing(ctx, ctx.ContextUser, target.ID)
|
||||
}
|
||||
|
||||
// Follow follow a user
|
||||
|
@ -235,11 +219,7 @@ func Follow(ctx *context.APIContext) {
|
|||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
|
||||
target := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
if err := user_model.FollowUser(ctx.Doer.ID, target.ID); err != nil {
|
||||
if err := user_model.FollowUser(ctx.Doer.ID, ctx.ContextUser.ID); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "FollowUser", err)
|
||||
return
|
||||
}
|
||||
|
@ -261,11 +241,7 @@ func Unfollow(ctx *context.APIContext) {
|
|||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
|
||||
target := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
if err := user_model.UnfollowUser(ctx.Doer.ID, target.ID); err != nil {
|
||||
if err := user_model.UnfollowUser(ctx.Doer.ID, ctx.ContextUser.ID); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "UnfollowUser", err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -64,11 +64,7 @@ func ListGPGKeys(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/GPGKeyList"
|
||||
|
||||
user := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
listGPGKeys(ctx, user.ID, utils.GetListOptions(ctx))
|
||||
listGPGKeys(ctx, ctx.ContextUser.ID, utils.GetListOptions(ctx))
|
||||
}
|
||||
|
||||
// ListMyGPGKeys get the GPG key list of the authenticated user
|
||||
|
|
|
@ -151,11 +151,7 @@ func ListPublicKeys(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/PublicKeyList"
|
||||
|
||||
user := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
listPublicKeys(ctx, user)
|
||||
listPublicKeys(ctx, ctx.ContextUser)
|
||||
}
|
||||
|
||||
// GetPublicKey get a public key
|
||||
|
|
|
@ -78,12 +78,8 @@ func ListUserRepos(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
user := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
private := ctx.IsSigned
|
||||
listUserRepos(ctx, user, private)
|
||||
listUserRepos(ctx, ctx.ContextUser, private)
|
||||
}
|
||||
|
||||
// ListMyRepos - list the repositories you own or have access to.
|
||||
|
|
|
@ -62,15 +62,14 @@ func GetStarredRepos(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
user := GetUserByParams(ctx)
|
||||
private := user.ID == ctx.Doer.ID
|
||||
repos, err := getStarredRepos(user, private, utils.GetListOptions(ctx))
|
||||
private := ctx.ContextUser.ID == ctx.Doer.ID
|
||||
repos, err := getStarredRepos(ctx.ContextUser, private, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "getStarredRepos", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.SetTotalCountHeader(int64(user.NumStars))
|
||||
ctx.SetTotalCountHeader(int64(ctx.ContextUser.NumStars))
|
||||
ctx.JSON(http.StatusOK, &repos)
|
||||
}
|
||||
|
||||
|
|
|
@ -98,18 +98,12 @@ func GetInfo(ctx *context.APIContext) {
|
|||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
u := GetUserByParams(ctx)
|
||||
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
if !models.IsUserVisibleToViewer(u, ctx.Doer) {
|
||||
if !models.IsUserVisibleToViewer(ctx.ContextUser, ctx.Doer) {
|
||||
// fake ErrUserNotExist error message to not leak information about existence
|
||||
ctx.NotFound("GetUserByName", user_model.ErrUserNotExist{Name: ctx.Params(":username")})
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.Doer))
|
||||
ctx.JSON(http.StatusOK, convert.ToUser(ctx.ContextUser, ctx.Doer))
|
||||
}
|
||||
|
||||
// GetAuthenticatedUser get current user's information
|
||||
|
@ -145,12 +139,7 @@ func GetUserHeatmapData(ctx *context.APIContext) {
|
|||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
user := GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
heatmap, err := models.GetUserHeatmapDataByUser(user, ctx.Doer)
|
||||
heatmap, err := models.GetUserHeatmapDataByUser(ctx.ContextUser, ctx.Doer)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
|
||||
return
|
||||
|
|
|
@ -60,9 +60,8 @@ func GetWatchedRepos(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/RepositoryList"
|
||||
|
||||
user := GetUserByParams(ctx)
|
||||
private := user.ID == ctx.Doer.ID
|
||||
repos, total, err := getWatchedRepos(user, private, utils.GetListOptions(ctx))
|
||||
private := ctx.ContextUser.ID == ctx.Doer.ID
|
||||
repos, total, err := getWatchedRepos(ctx.ContextUser, private, utils.GetListOptions(ctx))
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue