code.gitea.io/gitea@v1.21.7/services/context/user.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package context
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"strings"
    10  
    11  	user_model "code.gitea.io/gitea/models/user"
    12  	"code.gitea.io/gitea/modules/context"
    13  )
    14  
    15  // UserAssignmentWeb returns a middleware to handle context-user assignment for web routes
    16  func UserAssignmentWeb() func(ctx *context.Context) {
    17  	return func(ctx *context.Context) {
    18  		errorFn := func(status int, title string, obj any) {
    19  			err, ok := obj.(error)
    20  			if !ok {
    21  				err = fmt.Errorf("%s", obj)
    22  			}
    23  			if status == http.StatusNotFound {
    24  				ctx.NotFound(title, err)
    25  			} else {
    26  				ctx.ServerError(title, err)
    27  			}
    28  		}
    29  		ctx.ContextUser = userAssignment(ctx.Base, ctx.Doer, errorFn)
    30  		ctx.Data["ContextUser"] = ctx.ContextUser
    31  	}
    32  }
    33  
    34  // UserIDAssignmentAPI returns a middleware to handle context-user assignment for api routes
    35  func UserIDAssignmentAPI() func(ctx *context.APIContext) {
    36  	return func(ctx *context.APIContext) {
    37  		userID := ctx.ParamsInt64(":user-id")
    38  
    39  		if ctx.IsSigned && ctx.Doer.ID == userID {
    40  			ctx.ContextUser = ctx.Doer
    41  		} else {
    42  			var err error
    43  			ctx.ContextUser, err = user_model.GetUserByID(ctx, userID)
    44  			if err != nil {
    45  				if user_model.IsErrUserNotExist(err) {
    46  					ctx.Error(http.StatusNotFound, "GetUserByID", err)
    47  				} else {
    48  					ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
    49  				}
    50  			}
    51  		}
    52  	}
    53  }
    54  
    55  // UserAssignmentAPI returns a middleware to handle context-user assignment for api routes
    56  func UserAssignmentAPI() func(ctx *context.APIContext) {
    57  	return func(ctx *context.APIContext) {
    58  		ctx.ContextUser = userAssignment(ctx.Base, ctx.Doer, ctx.Error)
    59  	}
    60  }
    61  
    62  func userAssignment(ctx *context.Base, doer *user_model.User, errCb func(int, string, any)) (contextUser *user_model.User) {
    63  	username := ctx.Params(":username")
    64  
    65  	if doer != nil && doer.LowerName == strings.ToLower(username) {
    66  		contextUser = doer
    67  	} else {
    68  		var err error
    69  		contextUser, err = user_model.GetUserByName(ctx, username)
    70  		if err != nil {
    71  			if user_model.IsErrUserNotExist(err) {
    72  				if redirectUserID, err := user_model.LookupUserRedirect(ctx, username); err == nil {
    73  					context.RedirectToUser(ctx, username, redirectUserID)
    74  				} else if user_model.IsErrUserRedirectNotExist(err) {
    75  					errCb(http.StatusNotFound, "GetUserByName", err)
    76  				} else {
    77  					errCb(http.StatusInternalServerError, "LookupUserRedirect", err)
    78  				}
    79  			} else {
    80  				errCb(http.StatusInternalServerError, "GetUserByName", err)
    81  			}
    82  		}
    83  	}
    84  	return contextUser
    85  }