diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2022-03-22 08:03:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-22 15:03:22 +0800 |
commit | 80fd25524e13dedbdc3527b32d008de152213a89 (patch) | |
tree | 63b2bfe4ffaf1dce12080cabdc2e845c8731a673 /modules | |
parent | 5495ba7660979973ba19e304786135da474e0e64 (diff) | |
download | gitea-80fd25524e13dedbdc3527b32d008de152213a89.tar.gz gitea-80fd25524e13dedbdc3527b32d008de152213a89.zip |
Renamed ctx.User to ctx.Doer. (#19161)
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/context/api.go | 20 | ||||
-rw-r--r-- | modules/context/auth.go | 26 | ||||
-rw-r--r-- | modules/context/context.go | 22 | ||||
-rw-r--r-- | modules/context/org.go | 12 | ||||
-rw-r--r-- | modules/context/permission.go | 4 | ||||
-rw-r--r-- | modules/context/repo.go | 24 | ||||
-rw-r--r-- | modules/test/context_tests.go | 4 |
7 files changed, 56 insertions, 56 deletions
diff --git a/modules/context/api.go b/modules/context/api.go index c825e48753..c1b31dcff9 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -100,7 +100,7 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) { if status == http.StatusInternalServerError { log.ErrorWithSkip(1, "%s: %s", title, message) - if setting.IsProd && !(ctx.User != nil && ctx.User.IsAdmin) { + if setting.IsProd && !(ctx.Doer != nil && ctx.Doer.IsAdmin) { message = "" } } @@ -117,7 +117,7 @@ func (ctx *APIContext) InternalServerError(err error) { log.ErrorWithSkip(1, "InternalServerError: %v", err) var message string - if !setting.IsProd || (ctx.User != nil && ctx.User.IsAdmin) { + if !setting.IsProd || (ctx.Doer != nil && ctx.Doer.IsAdmin) { message = err.Error() } @@ -225,7 +225,7 @@ func (ctx *APIContext) CheckForOTP() { } otpHeader := ctx.Req.Header.Get("X-Gitea-OTP") - twofa, err := auth.GetTwoFactorByUID(ctx.Context.User.ID) + twofa, err := auth.GetTwoFactorByUID(ctx.Context.Doer.ID) if err != nil { if auth.IsErrTwoFactorNotEnrolled(err) { return // No 2FA enrollment for this user @@ -248,18 +248,18 @@ func (ctx *APIContext) CheckForOTP() { func APIAuth(authMethod auth_service.Method) func(*APIContext) { return func(ctx *APIContext) { // Get user from session if logged in. - ctx.User = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session) - if ctx.User != nil { - if ctx.Locale.Language() != ctx.User.Language { + ctx.Doer = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session) + if ctx.Doer != nil { + if ctx.Locale.Language() != ctx.Doer.Language { ctx.Locale = middleware.Locale(ctx.Resp, ctx.Req) } ctx.IsBasicAuth = ctx.Data["AuthedMethod"].(string) == auth_service.BasicMethodName ctx.IsSigned = true ctx.Data["IsSigned"] = ctx.IsSigned - ctx.Data["SignedUser"] = ctx.User - ctx.Data["SignedUserID"] = ctx.User.ID - ctx.Data["SignedUserName"] = ctx.User.Name - ctx.Data["IsAdmin"] = ctx.User.IsAdmin + ctx.Data["SignedUser"] = ctx.Doer + ctx.Data["SignedUserID"] = ctx.Doer.ID + ctx.Data["SignedUserName"] = ctx.Doer.Name + ctx.Data["IsAdmin"] = ctx.Doer.IsAdmin } else { ctx.Data["SignedUserID"] = int64(0) ctx.Data["SignedUserName"] = "" diff --git a/modules/context/auth.go b/modules/context/auth.go index 7e7e8ab961..1a46ab586a 100644 --- a/modules/context/auth.go +++ b/modules/context/auth.go @@ -27,19 +27,19 @@ func Toggle(options *ToggleOptions) func(ctx *Context) { return func(ctx *Context) { // Check prohibit login users. if ctx.IsSigned { - if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { + if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") ctx.HTML(http.StatusOK, "user/auth/activate") return } - if !ctx.User.IsActive || ctx.User.ProhibitLogin { - log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr()) + if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin { + log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr()) ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") ctx.HTML(http.StatusOK, "user/auth/prohibit_login") return } - if ctx.User.MustChangePassword { + if ctx.Doer.MustChangePassword { if ctx.Req.URL.Path != "/user/settings/change_password" { ctx.Data["Title"] = ctx.Tr("auth.must_change_password") ctx.Data["ChangePasscodeLink"] = setting.AppSubURL + "/user/change_password" @@ -76,7 +76,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) { } ctx.Redirect(setting.AppSubURL + "/user/login") return - } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { + } else if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") ctx.HTML(http.StatusOK, "user/auth/activate") return @@ -94,7 +94,7 @@ func Toggle(options *ToggleOptions) func(ctx *Context) { } if options.AdminRequired { - if !ctx.User.IsAdmin { + if !ctx.Doer.IsAdmin { ctx.Error(http.StatusForbidden) return } @@ -108,15 +108,15 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { return func(ctx *APIContext) { // Check prohibit login users. if ctx.IsSigned { - if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { + if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") ctx.JSON(http.StatusForbidden, map[string]string{ "message": "This account is not activated.", }) return } - if !ctx.User.IsActive || ctx.User.ProhibitLogin { - log.Info("Failed authentication attempt for %s from %s", ctx.User.Name, ctx.RemoteAddr()) + if !ctx.Doer.IsActive || ctx.Doer.ProhibitLogin { + log.Info("Failed authentication attempt for %s from %s", ctx.Doer.Name, ctx.RemoteAddr()) ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") ctx.JSON(http.StatusForbidden, map[string]string{ "message": "This account is prohibited from signing in, please contact your site administrator.", @@ -124,7 +124,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { return } - if ctx.User.MustChangePassword { + if ctx.Doer.MustChangePassword { ctx.JSON(http.StatusForbidden, map[string]string{ "message": "You must change your password. Change it at: " + setting.AppURL + "/user/change_password", }) @@ -145,7 +145,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { "message": "Only signed in user is allowed to call APIs.", }) return - } else if !ctx.User.IsActive && setting.Service.RegisterEmailConfirm { + } else if !ctx.Doer.IsActive && setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") ctx.HTML(http.StatusOK, "user/auth/activate") return @@ -154,7 +154,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { if skip, ok := ctx.Data["SkipLocalTwoFA"]; ok && skip.(bool) { return // Skip 2FA } - twofa, err := auth.GetTwoFactorByUID(ctx.User.ID) + twofa, err := auth.GetTwoFactorByUID(ctx.Doer.ID) if err != nil { if auth.IsErrTwoFactorNotEnrolled(err) { return // No 2FA enrollment for this user @@ -178,7 +178,7 @@ func ToggleAPI(options *ToggleOptions) func(ctx *APIContext) { } if options.AdminRequired { - if !ctx.User.IsAdmin { + if !ctx.Doer.IsAdmin { ctx.JSON(http.StatusForbidden, map[string]string{ "message": "You have no permission to request for this.", }) diff --git a/modules/context/context.go b/modules/context/context.go index 8e50e154a1..57448907e2 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -63,7 +63,7 @@ type Context struct { Link string // current request URL EscapedLink string - User *user_model.User + Doer *user_model.User IsSigned bool IsBasicAuth bool @@ -88,7 +88,7 @@ func (ctx *Context) GetData() map[string]interface{} { // IsUserSiteAdmin returns true if current user is a site admin func (ctx *Context) IsUserSiteAdmin() bool { - return ctx.IsSigned && ctx.User.IsAdmin + return ctx.IsSigned && ctx.Doer.IsAdmin } // IsUserRepoOwner returns true if current user owns current repo @@ -574,10 +574,10 @@ func GetContext(req *http.Request) *Context { // GetContextUser returns context user func GetContextUser(req *http.Request) *user_model.User { if apiContext, ok := req.Context().Value(apiContextKey).(*APIContext); ok { - return apiContext.User + return apiContext.Doer } if ctx, ok := req.Context().Value(contextKey).(*Context); ok { - return ctx.User + return ctx.Doer } return nil } @@ -599,18 +599,18 @@ func getCsrfOpts() CsrfOptions { // Auth converts auth.Auth as a middleware func Auth(authMethod auth.Method) func(*Context) { return func(ctx *Context) { - ctx.User = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session) - if ctx.User != nil { - if ctx.Locale.Language() != ctx.User.Language { + ctx.Doer = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session) + if ctx.Doer != nil { + if ctx.Locale.Language() != ctx.Doer.Language { ctx.Locale = middleware.Locale(ctx.Resp, ctx.Req) } ctx.IsBasicAuth = ctx.Data["AuthedMethod"].(string) == auth.BasicMethodName ctx.IsSigned = true ctx.Data["IsSigned"] = ctx.IsSigned - ctx.Data["SignedUser"] = ctx.User - ctx.Data["SignedUserID"] = ctx.User.ID - ctx.Data["SignedUserName"] = ctx.User.Name - ctx.Data["IsAdmin"] = ctx.User.IsAdmin + ctx.Data["SignedUser"] = ctx.Doer + ctx.Data["SignedUserID"] = ctx.Doer.ID + ctx.Data["SignedUserName"] = ctx.Doer.Name + ctx.Data["IsAdmin"] = ctx.Doer.IsAdmin } else { ctx.Data["SignedUserID"] = int64(0) ctx.Data["SignedUserName"] = "" diff --git a/modules/context/org.go b/modules/context/org.go index 824f22b2f3..a1080fc0fb 100644 --- a/modules/context/org.go +++ b/modules/context/org.go @@ -77,14 +77,14 @@ func HandleOrgAssignment(ctx *Context, args ...bool) { ctx.Data["OrgTeams"] = teams // Admin has super access. - if ctx.IsSigned && ctx.User.IsAdmin { + if ctx.IsSigned && ctx.Doer.IsAdmin { ctx.Org.IsOwner = true ctx.Org.IsMember = true ctx.Org.IsTeamMember = true ctx.Org.IsTeamAdmin = true ctx.Org.CanCreateOrgRepo = true } else if ctx.IsSigned { - ctx.Org.IsOwner, err = org.IsOwnedBy(ctx.User.ID) + ctx.Org.IsOwner, err = org.IsOwnedBy(ctx.Doer.ID) if err != nil { ctx.ServerError("IsOwnedBy", err) return @@ -96,12 +96,12 @@ func HandleOrgAssignment(ctx *Context, args ...bool) { ctx.Org.IsTeamAdmin = true ctx.Org.CanCreateOrgRepo = true } else { - ctx.Org.IsMember, err = org.IsOrgMember(ctx.User.ID) + ctx.Org.IsMember, err = org.IsOrgMember(ctx.Doer.ID) if err != nil { ctx.ServerError("IsOrgMember", err) return } - ctx.Org.CanCreateOrgRepo, err = org.CanCreateOrgRepo(ctx.User.ID) + ctx.Org.CanCreateOrgRepo, err = org.CanCreateOrgRepo(ctx.Doer.ID) if err != nil { ctx.ServerError("CanCreateOrgRepo", err) return @@ -133,7 +133,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) { if ctx.Org.IsOwner { shouldSeeAllTeams = true } else { - teams, err := org.GetUserTeams(ctx.User.ID) + teams, err := org.GetUserTeams(ctx.Doer.ID) if err != nil { ctx.ServerError("GetUserTeams", err) return @@ -152,7 +152,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) { return } } else { - ctx.Org.Teams, err = org.GetUserTeams(ctx.User.ID) + ctx.Org.Teams, err = org.GetUserTeams(ctx.Doer.ID) if err != nil { ctx.ServerError("GetUserTeams", err) return diff --git a/modules/context/permission.go b/modules/context/permission.go index 2b87aa4591..142b86faea 100644 --- a/modules/context/permission.go +++ b/modules/context/permission.go @@ -49,7 +49,7 @@ func RequireRepoReader(unitType unit.Type) func(ctx *Context) { if ctx.IsSigned { log.Trace("Permission Denied: User %-v cannot read %-v in Repo %-v\n"+ "User in Repo has Permissions: %-+v", - ctx.User, + ctx.Doer, unitType, ctx.Repo.Repository, ctx.Repo.Permission) @@ -80,7 +80,7 @@ func RequireRepoReaderOr(unitTypes ...unit.Type) func(ctx *Context) { var args []interface{} if ctx.IsSigned { format = "Permission Denied: User %-v cannot read [" - args = append(args, ctx.User) + args = append(args, ctx.Doer) } else { format = "Permission Denied: Anonymous user cannot read [" } diff --git a/modules/context/repo.go b/modules/context/repo.go index e55c13f49c..d64380dd95 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -278,7 +278,7 @@ func RetrieveTemplateRepo(ctx *Context, repo *repo_model.Repository) { return } - perm, err := models.GetUserRepoPermission(templateRepo, ctx.User) + perm, err := models.GetUserRepoPermission(templateRepo, ctx.Doer) if err != nil { ctx.ServerError("GetUserRepoPermission", err) return @@ -346,7 +346,7 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) { return } - ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.User) + ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.Doer) if err != nil { ctx.ServerError("GetUserRepoPermission", err) return @@ -422,8 +422,8 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { repoName = strings.TrimSuffix(repoName, ".atom") // Check if the user is the same as the repository owner - if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) { - owner = ctx.User + if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) { + owner = ctx.Doer } else { owner, err = user_model.GetUserByName(userName) if err != nil { @@ -502,14 +502,14 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { ctx.Data["CanWriteIssues"] = ctx.Repo.CanWrite(unit_model.TypeIssues) ctx.Data["CanWritePulls"] = ctx.Repo.CanWrite(unit_model.TypePullRequests) - canSignedUserFork, err := models.CanUserForkRepo(ctx.User, ctx.Repo.Repository) + canSignedUserFork, err := models.CanUserForkRepo(ctx.Doer, ctx.Repo.Repository) if err != nil { ctx.ServerError("CanUserForkRepo", err) return } ctx.Data["CanSignedUserFork"] = canSignedUserFork - userAndOrgForks, err := models.GetForksByUserAndOrgs(ctx.User, ctx.Repo.Repository) + userAndOrgForks, err := models.GetForksByUserAndOrgs(ctx.Doer, ctx.Repo.Repository) if err != nil { ctx.ServerError("GetForksByUserAndOrgs", err) return @@ -532,8 +532,8 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { ctx.Data["WikiCloneLink"] = repo.WikiCloneLink() if ctx.IsSigned { - ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx.User.ID, repo.ID) - ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx.User.ID, repo.ID) + ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx.Doer.ID, repo.ID) + ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx.Doer.ID, repo.ID) } if repo.IsFork { @@ -633,7 +633,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { // People who have push access or have forked repository can propose a new pull request. canPush := ctx.Repo.CanWrite(unit_model.TypeCode) || - (ctx.IsSigned && repo_model.HasForkedRepo(ctx.User.ID, ctx.Repo.Repository.ID)) + (ctx.IsSigned && repo_model.HasForkedRepo(ctx.Doer.ID, ctx.Repo.Repository.ID)) canCompare := false // Pull request is allowed if this is a fork repository @@ -669,8 +669,8 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { } ctx.Data["RepoTransfer"] = repoTransfer - if ctx.User != nil { - ctx.Data["CanUserAcceptTransfer"] = repoTransfer.CanUserAcceptTransfer(ctx.User) + if ctx.Doer != nil { + ctx.Data["CanUserAcceptTransfer"] = repoTransfer.CanUserAcceptTransfer(ctx.Doer) } } @@ -950,7 +950,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context // GitHookService checks if repository Git hooks service has been enabled. func GitHookService() func(ctx *Context) { return func(ctx *Context) { - if !ctx.User.CanEditGitHook() { + if !ctx.Doer.CanEditGitHook() { ctx.NotFound("GitHookService", nil) return } diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index e6c78bcaa5..e5cea26eb9 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -61,7 +61,7 @@ func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) { ctx.Repo.Owner, err = user_model.GetUserByID(ctx.Repo.Repository.OwnerID) assert.NoError(t, err) ctx.Repo.RepoLink = ctx.Repo.Repository.Link() - ctx.Repo.Permission, err = models.GetUserRepoPermission(ctx.Repo.Repository, ctx.User) + ctx.Repo.Permission, err = models.GetUserRepoPermission(ctx.Repo.Repository, ctx.Doer) assert.NoError(t, err) } @@ -81,7 +81,7 @@ func LoadRepoCommit(t *testing.T, ctx *context.Context) { // LoadUser load a user into a test context. func LoadUser(t *testing.T, ctx *context.Context, userID int64) { - ctx.User = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID}).(*user_model.User) + ctx.Doer = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID}).(*user_model.User) } // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has |