From 801c392e0c2c063c3ef5e84555fac242ef5e8bd9 Mon Sep 17 00:00:00 2001 From: Gusted Date: Wed, 29 Jan 2025 18:23:43 +0100 Subject: [PATCH] chore: remove deadcode in `models/user` - Remove `ErrUserInactive` introduced in f5fa22a49997cb423255e98da5abd67c2115f2c0 and removed in ef2a343e27d8af2de0bb696bd60d9a019e1e8b69 - Remove `GetUserEmailsByNames` introduced in a4cbe79567072befd96cf1b7eb319de1e2809ca and removed in 08ae6bb7edb9582c38edb8a0dba1b1be10fb00fc - Remove `GetUserNamesByIDs` introduced in 9ff4e1d2d9636ea8aa328427f1d31c962221263e and removed in b3a6596b54bdfa75b9f82c4d73185e676c32727f --- .deadcode-out | 4 ---- models/user/error.go | 21 --------------------- models/user/user.go | 27 --------------------------- models/user/user_test.go | 10 ---------- routers/web/auth/auth.go | 9 --------- routers/web/auth/linkaccount.go | 10 ---------- 6 files changed, 81 deletions(-) diff --git a/.deadcode-out b/.deadcode-out index a44599b6f1..20ccfc6473 100644 --- a/.deadcode-out +++ b/.deadcode-out @@ -81,16 +81,12 @@ code.gitea.io/gitea/models/repo WatchRepoMode code.gitea.io/gitea/models/user - ErrUserInactive.Error - ErrUserInactive.Unwrap IsErrExternalLoginUserAlreadyExist IsErrExternalLoginUserNotExist NewFederatedUser IsErrUserSettingIsNotExist GetUserAllSettings DeleteUserSetting - GetUserEmailsByNames - GetUserNamesByIDs code.gitea.io/gitea/modules/activitypub NewContext diff --git a/models/user/error.go b/models/user/error.go index cbf19998d1..5a956a2afe 100644 --- a/models/user/error.go +++ b/models/user/error.go @@ -71,27 +71,6 @@ func (err ErrUserProhibitLogin) Unwrap() error { return util.ErrPermissionDenied } -// ErrUserInactive represents a "ErrUserInactive" kind of error. -type ErrUserInactive struct { - UID int64 - Name string -} - -// IsErrUserInactive checks if an error is a ErrUserInactive -func IsErrUserInactive(err error) bool { - _, ok := err.(ErrUserInactive) - return ok -} - -func (err ErrUserInactive) Error() string { - return fmt.Sprintf("user is inactive [uid: %d, name: %s]", err.UID, err.Name) -} - -// Unwrap unwraps this error as a ErrPermission error -func (err ErrUserInactive) Unwrap() error { - return util.ErrPermissionDenied -} - // ErrUserIsNotLocal represents a "ErrUserIsNotLocal" kind of error. type ErrUserIsNotLocal struct { UID int64 diff --git a/models/user/user.go b/models/user/user.go index 423a26c8d3..f986ac5482 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -1043,22 +1043,6 @@ func GetUserByName(ctx context.Context, name string) (*User, error) { return u, nil } -// GetUserEmailsByNames returns a list of e-mails corresponds to names of users -// that have their email notifications set to enabled or onmention. -func GetUserEmailsByNames(ctx context.Context, names []string) []string { - mails := make([]string, 0, len(names)) - for _, name := range names { - u, err := GetUserByName(ctx, name) - if err != nil { - continue - } - if u.IsMailable() && u.EmailNotificationsPreference != EmailNotificationsDisabled { - mails = append(mails, u.Email) - } - } - return mails -} - // GetMaileableUsersByIDs gets users from ids, but only if they can receive mails func GetMaileableUsersByIDs(ctx context.Context, ids []int64, isMention bool) ([]*User, error) { if len(ids) == 0 { @@ -1085,17 +1069,6 @@ func GetMaileableUsersByIDs(ctx context.Context, ids []int64, isMention bool) ([ Find(&ous) } -// GetUserNamesByIDs returns usernames for all resolved users from a list of Ids. -func GetUserNamesByIDs(ctx context.Context, ids []int64) ([]string, error) { - unames := make([]string, 0, len(ids)) - err := db.GetEngine(ctx).In("id", ids). - Table("user"). - Asc("name"). - Cols("name"). - Find(&unames) - return unames, err -} - // GetUserNameByID returns username for the id func GetUserNameByID(ctx context.Context, id int64) (string, error) { var name string diff --git a/models/user/user_test.go b/models/user/user_test.go index 2c8c1609fd..bd1a462770 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -102,16 +102,6 @@ func TestGetUserByName(t *testing.T) { } } -func TestGetUserEmailsByNames(t *testing.T) { - require.NoError(t, unittest.PrepareTestDatabase()) - - // ignore none active user email - assert.ElementsMatch(t, []string{"user8@example.com"}, user_model.GetUserEmailsByNames(db.DefaultContext, []string{"user8", "user9"})) - assert.ElementsMatch(t, []string{"user8@example.com", "user5@example.com"}, user_model.GetUserEmailsByNames(db.DefaultContext, []string{"user8", "user5"})) - - assert.ElementsMatch(t, []string{"user8@example.com"}, user_model.GetUserEmailsByNames(db.DefaultContext, []string{"user8", "org7"})) -} - func TestCanCreateOrganization(t *testing.T) { require.NoError(t, unittest.PrepareTestDatabase()) diff --git a/routers/web/auth/auth.go b/routers/web/auth/auth.go index 5cb4ebb440..ca8f06bdb1 100644 --- a/routers/web/auth/auth.go +++ b/routers/web/auth/auth.go @@ -225,15 +225,6 @@ func SignInPost(ctx *context.Context) { log.Warn("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err) ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") ctx.HTML(http.StatusOK, "user/auth/prohibit_login") - } else if user_model.IsErrUserInactive(err) { - if setting.Service.RegisterEmailConfirm { - ctx.Data["Title"] = ctx.Tr("auth.active_your_account") - ctx.HTML(http.StatusOK, TplActivate) - } else { - log.Warn("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err) - ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") - ctx.HTML(http.StatusOK, "user/auth/prohibit_login") - } } else { ctx.ServerError("UserSignIn", err) } diff --git a/routers/web/auth/linkaccount.go b/routers/web/auth/linkaccount.go index 9b0141c14e..8dce0a30a4 100644 --- a/routers/web/auth/linkaccount.go +++ b/routers/web/auth/linkaccount.go @@ -99,16 +99,6 @@ func handleSignInError(ctx *context.Context, userName string, ptrForm any, tmpl log.Info("Failed authentication attempt for %s from %s: %v", userName, ctx.RemoteAddr(), err) ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") ctx.HTML(http.StatusOK, "user/auth/prohibit_login") - } else if user_model.IsErrUserInactive(err) { - ctx.Data["user_exists"] = true - if setting.Service.RegisterEmailConfirm { - ctx.Data["Title"] = ctx.Tr("auth.active_your_account") - ctx.HTML(http.StatusOK, TplActivate) - } else { - log.Info("Failed authentication attempt for %s from %s: %v", userName, ctx.RemoteAddr(), err) - ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") - ctx.HTML(http.StatusOK, "user/auth/prohibit_login") - } } else { ctx.ServerError(invoker, err) }