diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-24 17:49:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-24 17:49:20 +0800 |
commit | a666829a37be6f9fd98f9e7dd1767c420f7f3b32 (patch) | |
tree | 9ab1434b759a8a2cb275a83149903a823851e309 /routers | |
parent | 4e7ca946da2a2642a62f114825129bf5d7ed9196 (diff) | |
download | gitea-a666829a37be6f9fd98f9e7dd1767c420f7f3b32.tar.gz gitea-a666829a37be6f9fd98f9e7dd1767c420f7f3b32.zip |
Move user related model into models/user (#17781)
* Move user related model into models/user
* Fix lint for windows
* Fix windows lint
* Fix windows lint
* Move some tests in models
* Merge
Diffstat (limited to 'routers')
67 files changed, 537 insertions, 486 deletions
diff --git a/routers/api/v1/admin/adopt.go b/routers/api/v1/admin/adopt.go index 184a2941a8..85262bc836 100644 --- a/routers/api/v1/admin/adopt.go +++ b/routers/api/v1/admin/adopt.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/api/v1/utils" @@ -79,9 +80,9 @@ func AdoptRepository(ctx *context.APIContext) { ownerName := ctx.Params(":username") repoName := ctx.Params(":reponame") - ctxUser, err := models.GetUserByName(ownerName) + ctxUser, err := user_model.GetUserByName(ownerName) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound() return } @@ -141,9 +142,9 @@ func DeleteUnadoptedRepository(ctx *context.APIContext) { ownerName := ctx.Params(":username") repoName := ctx.Params(":reponame") - ctxUser, err := models.GetUserByName(ownerName) + ctxUser, err := user_model.GetUserByName(ownerName) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound() return } diff --git a/routers/api/v1/admin/org.go b/routers/api/v1/admin/org.go index 2633865d2c..bdfe87fd4e 100644 --- a/routers/api/v1/admin/org.go +++ b/routers/api/v1/admin/org.go @@ -9,6 +9,8 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -61,15 +63,15 @@ func CreateOrg(ctx *context.APIContext) { Website: form.Website, Location: form.Location, IsActive: true, - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, Visibility: visibility, } if err := models.CreateOrganization(org, u); err != nil { - if models.IsErrUserAlreadyExist(err) || - models.IsErrNameReserved(err) || - models.IsErrNameCharsNotAllowed(err) || - models.IsErrNamePatternNotAllowed(err) { + if user_model.IsErrUserAlreadyExist(err) || + db.IsErrNameReserved(err) || + db.IsErrNameCharsNotAllowed(err) || + db.IsErrNamePatternNotAllowed(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "CreateOrganization", err) @@ -104,10 +106,10 @@ func GetAllOrgs(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - users, maxResults, err := models.SearchUsers(&models.SearchUserOptions{ + users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{ Actor: ctx.User, - Type: models.UserTypeOrganization, - OrderBy: models.SearchOrderByAlphabetically, + Type: user_model.UserTypeOrganization, + OrderBy: db.SearchOrderByAlphabetically, ListOptions: listOptions, Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate}, }) diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index 95060e7a1c..e50abb5937 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -11,6 +11,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -25,7 +26,7 @@ import ( user_service "code.gitea.io/gitea/services/user" ) -func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, loginName string) { +func parseLoginSource(ctx *context.APIContext, u *user_model.User, sourceID int64, loginName string) { if sourceID == 0 { return } @@ -70,7 +71,7 @@ func CreateUser(ctx *context.APIContext) { // "$ref": "#/responses/validationError" form := web.GetForm(ctx).(*api.CreateUserOption) - u := &models.User{ + u := &user_model.User{ Name: form.Username, FullName: form.FullName, Email: form.Email, @@ -102,20 +103,20 @@ func CreateUser(ctx *context.APIContext) { return } - var overwriteDefault *models.CreateUserOverwriteOptions + var overwriteDefault *user_model.CreateUserOverwriteOptions if form.Visibility != "" { - overwriteDefault = &models.CreateUserOverwriteOptions{ + overwriteDefault = &user_model.CreateUserOverwriteOptions{ Visibility: api.VisibilityModes[form.Visibility], } } - if err := models.CreateUser(u, overwriteDefault); err != nil { - if models.IsErrUserAlreadyExist(err) || + if err := user_model.CreateUser(u, overwriteDefault); err != nil { + if user_model.IsErrUserAlreadyExist(err) || user_model.IsErrEmailAlreadyUsed(err) || - models.IsErrNameReserved(err) || - models.IsErrNameCharsNotAllowed(err) || + db.IsErrNameReserved(err) || + db.IsErrNameCharsNotAllowed(err) || user_model.IsErrEmailInvalid(err) || - models.IsErrNamePatternNotAllowed(err) { + db.IsErrNamePatternNotAllowed(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "CreateUser", err) @@ -183,7 +184,7 @@ func EditUser(ctx *context.APIContext) { ctx.Error(http.StatusBadRequest, "PasswordPwned", errors.New("PasswordPwned")) return } - if u.Salt, err = models.GetUserSalt(); err != nil { + if u.Salt, err = user_model.GetUserSalt(); err != nil { ctx.Error(http.StatusInternalServerError, "UpdateUser", err) return } @@ -246,7 +247,7 @@ func EditUser(ctx *context.APIContext) { u.IsRestricted = *form.Restricted } - if err := models.UpdateUser(u); err != nil { + if err := user_model.UpdateUser(u); err != nil { if user_model.IsErrEmailAlreadyUsed(err) || user_model.IsErrEmailInvalid(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { @@ -409,10 +410,10 @@ func GetAllUsers(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - users, maxResults, err := models.SearchUsers(&models.SearchUserOptions{ + users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{ Actor: ctx.User, - Type: models.UserTypeIndividual, - OrderBy: models.SearchOrderByAlphabetically, + Type: user_model.UserTypeIndividual, + OrderBy: db.SearchOrderByAlphabetically, ListOptions: listOptions, }) if err != nil { diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index ea653b9677..867803f8dd 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -103,9 +103,9 @@ func sudo() func(ctx *context.APIContext) { if len(sudo) > 0 { if ctx.IsSigned && ctx.User.IsAdmin { - user, err := models.GetUserByName(sudo) + user, err := user_model.GetUserByName(sudo) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) @@ -130,7 +130,7 @@ func repoAssignment() func(ctx *context.APIContext) { repoName := ctx.Params("reponame") var ( - owner *models.User + owner *user_model.User err error ) @@ -138,9 +138,9 @@ func repoAssignment() func(ctx *context.APIContext) { if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) { owner = ctx.User } else { - owner, err = models.GetUserByName(userName) + owner, err = user_model.GetUserByName(userName) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { if redirectUserID, err := user_model.LookupUserRedirect(userName); err == nil { context.RedirectToUser(ctx.Context, userName, redirectUserID) } else if user_model.IsErrUserRedirectNotExist(err) { diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index 952e29fba9..84065363f0 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -19,7 +20,7 @@ import ( "code.gitea.io/gitea/services/org" ) -func listUserOrgs(ctx *context.APIContext, u *models.User) { +func listUserOrgs(ctx *context.APIContext, u *user_model.User) { listOptions := utils.GetListOptions(ctx) showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == u.ID) @@ -130,12 +131,12 @@ func GetUserOrgsPermissions(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - var u *models.User + var u *user_model.User if u = user.GetUserByParams(ctx); u == nil { return } - var o *models.User + var o *user_model.User if o = user.GetUserByParamsName(ctx, ":org"); o == nil { return } @@ -206,11 +207,11 @@ func GetAll(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - publicOrgs, maxResults, err := models.SearchUsers(&models.SearchUserOptions{ + publicOrgs, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{ Actor: ctx.User, ListOptions: listOptions, - Type: models.UserTypeOrganization, - OrderBy: models.SearchOrderByAlphabetically, + Type: user_model.UserTypeOrganization, + OrderBy: db.SearchOrderByAlphabetically, Visible: vMode, }) if err != nil { @@ -266,15 +267,15 @@ func Create(ctx *context.APIContext) { Website: form.Website, Location: form.Location, IsActive: true, - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, Visibility: visibility, RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess, } if err := models.CreateOrganization(org, ctx.User); err != nil { - if models.IsErrUserAlreadyExist(err) || - models.IsErrNameReserved(err) || - models.IsErrNameCharsNotAllowed(err) || - models.IsErrNamePatternNotAllowed(err) { + if user_model.IsErrUserAlreadyExist(err) || + db.IsErrNameReserved(err) || + db.IsErrNameCharsNotAllowed(err) || + db.IsErrNamePatternNotAllowed(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "CreateOrganization", err) @@ -344,7 +345,7 @@ func Edit(ctx *context.APIContext) { if form.RepoAdminChangeTeamAccess != nil { org.RepoAdminChangeTeamAccess = *form.RepoAdminChangeTeamAccess } - if err := models.UpdateUserCols(db.DefaultContext, org.AsUser(), + if err := user_model.UpdateUserCols(db.DefaultContext, org.AsUser(), "full_name", "description", "website", "location", "visibility", "repo_admin_change_team_access", ); err != nil { diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 04da901239..70e5b3e2b8 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -11,6 +11,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" @@ -423,27 +424,27 @@ func CreateBranchProtection(ctx *context.APIContext) { requiredApprovals = form.RequiredApprovals } - whitelistUsers, err := models.GetUserIDsByNames(form.PushWhitelistUsernames, false) + whitelistUsers, err := user_model.GetUserIDsByNames(form.PushWhitelistUsernames, false) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err) return } ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err) return } - mergeWhitelistUsers, err := models.GetUserIDsByNames(form.MergeWhitelistUsernames, false) + mergeWhitelistUsers, err := user_model.GetUserIDsByNames(form.MergeWhitelistUsernames, false) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err) return } ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err) return } - approvalsWhitelistUsers, err := models.GetUserIDsByNames(form.ApprovalsWhitelistUsernames, false) + approvalsWhitelistUsers, err := user_model.GetUserIDsByNames(form.ApprovalsWhitelistUsernames, false) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err) return } @@ -653,9 +654,9 @@ func EditBranchProtection(ctx *context.APIContext) { var whitelistUsers []int64 if form.PushWhitelistUsernames != nil { - whitelistUsers, err = models.GetUserIDsByNames(form.PushWhitelistUsernames, false) + whitelistUsers, err = user_model.GetUserIDsByNames(form.PushWhitelistUsernames, false) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err) return } @@ -667,9 +668,9 @@ func EditBranchProtection(ctx *context.APIContext) { } var mergeWhitelistUsers []int64 if form.MergeWhitelistUsernames != nil { - mergeWhitelistUsers, err = models.GetUserIDsByNames(form.MergeWhitelistUsernames, false) + mergeWhitelistUsers, err = user_model.GetUserIDsByNames(form.MergeWhitelistUsernames, false) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err) return } @@ -681,9 +682,9 @@ func EditBranchProtection(ctx *context.APIContext) { } var approvalsWhitelistUsers []int64 if form.ApprovalsWhitelistUsernames != nil { - approvalsWhitelistUsers, err = models.GetUserIDsByNames(form.ApprovalsWhitelistUsernames, false) + approvalsWhitelistUsers, err = user_model.GetUserIDsByNames(form.ApprovalsWhitelistUsernames, false) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err) return } diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index d636220f62..fcc8f9a90f 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -10,6 +10,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -99,9 +100,9 @@ func IsCollaborator(ctx *context.APIContext) { // "422": // "$ref": "#/responses/validationError" - user, err := models.GetUserByName(ctx.Params(":collaborator")) + user, err := user_model.GetUserByName(ctx.Params(":collaborator")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) @@ -155,9 +156,9 @@ func AddCollaborator(ctx *context.APIContext) { form := web.GetForm(ctx).(*api.AddCollaboratorOption) - collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) + collaborator, err := user_model.GetUserByName(ctx.Params(":collaborator")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) @@ -214,9 +215,9 @@ func DeleteCollaborator(ctx *context.APIContext) { // "422": // "$ref": "#/responses/validationError" - collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) + collaborator, err := user_model.GetUserByName(ctx.Params(":collaborator")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 639100757b..9907054b83 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -12,6 +12,7 @@ import ( "strconv" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" @@ -188,7 +189,7 @@ func GetAllCommits(ctx *context.APIContext) { return } - userCache := make(map[string]*models.User) + userCache := make(map[string]*user_model.User) apiCommits := make([]*api.Commit, len(commits)) for i, commit := range commits { diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 45d3c815a7..528b8fdecc 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -10,6 +10,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -99,7 +100,7 @@ func CreateFork(ctx *context.APIContext) { form := web.GetForm(ctx).(*api.CreateForkOption) repo := ctx.Repo.Repository - var forker *models.User // user/org that will own the fork + var forker *user_model.User // user/org that will own the fork if form.Organization == nil { forker = ctx.User } else { diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index a2454b8618..5137c7246e 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" issue_indexer "code.gitea.io/gitea/modules/indexer/issues" @@ -133,7 +134,7 @@ func SearchIssues(ctx *context.APIContext) { Collaborate: util.OptionalBoolNone, // This needs to be a column that is not nil in fixtures or // MySQL will return different results when sorting by null in some cases - OrderBy: models.SearchOrderByAlphabetically, + OrderBy: db.SearchOrderByAlphabetically, Actor: ctx.User, } if ctx.IsSigned { @@ -141,9 +142,9 @@ func SearchIssues(ctx *context.APIContext) { opts.AllLimited = true } if ctx.FormString("owner") != "" { - owner, err := models.GetUserByName(ctx.FormString("owner")) + owner, err := user_model.GetUserByName(ctx.FormString("owner")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusBadRequest, "Owner not found", err) } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) @@ -492,8 +493,8 @@ func getUserIDForFilter(ctx *context.APIContext, queryName string) int64 { return 0 } - user, err := models.GetUserByName(userName) - if models.IsErrUserNotExist(err) { + user, err := user_model.GetUserByName(userName) + if user_model.IsErrUserNotExist(err) { ctx.NotFound(err) return 0 } @@ -604,7 +605,7 @@ func CreateIssue(ctx *context.APIContext) { issue.MilestoneID = form.Milestone assigneeIDs, err = models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err)) } else { ctx.Error(http.StatusInternalServerError, "AddAssigneeByName", err) @@ -614,7 +615,7 @@ func CreateIssue(ctx *context.APIContext) { // Check if the passed assignees is assignable for _, aID := range assigneeIDs { - assignee, err := models.GetUserByID(aID) + assignee, err := user_model.GetUserByID(aID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserByID", err) return diff --git a/routers/api/v1/repo/issue_subscription.go b/routers/api/v1/repo/issue_subscription.go index 8acd378cc5..09be3cc193 100644 --- a/routers/api/v1/repo/issue_subscription.go +++ b/routers/api/v1/repo/issue_subscription.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -114,9 +115,9 @@ func setIssueSubscription(ctx *context.APIContext, watch bool) { return } - user, err := models.GetUserByName(ctx.Params(":user")) + user, err := user_model.GetUserByName(ctx.Params(":user")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) diff --git a/routers/api/v1/repo/issue_tracked_time.go b/routers/api/v1/repo/issue_tracked_time.go index f73e63547b..00f8e77feb 100644 --- a/routers/api/v1/repo/issue_tracked_time.go +++ b/routers/api/v1/repo/issue_tracked_time.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -92,8 +93,8 @@ func ListTrackedTimes(ctx *context.APIContext) { qUser := ctx.FormTrim("user") if qUser != "" { - user, err := models.GetUserByName(qUser) - if models.IsErrUserNotExist(err) { + user, err := user_model.GetUserByName(qUser) + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusNotFound, "User does not exist", err) } else if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) @@ -201,7 +202,7 @@ func AddTime(ctx *context.APIContext) { if form.User != "" { if (ctx.IsUserRepoAdmin() && ctx.User.Name != form.User) || ctx.User.IsAdmin { //allow only RepoAdmin, Admin and User to add time - user, err = models.GetUserByName(form.User) + user, err = user_model.GetUserByName(form.User) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) } @@ -413,9 +414,9 @@ func ListTrackedTimesByUser(ctx *context.APIContext) { ctx.Error(http.StatusBadRequest, "", "time tracking disabled") return } - user, err := models.GetUserByName(ctx.Params(":timetrackingusername")) + user, err := user_model.GetUserByName(ctx.Params(":timetrackingusername")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound(err) } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) @@ -510,8 +511,8 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) { // Filters qUser := ctx.FormTrim("user") if qUser != "" { - user, err := models.GetUserByName(qUser) - if models.IsErrUserNotExist(err) { + user, err := user_model.GetUserByName(qUser) + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusNotFound, "User does not exist", err) } else if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) diff --git a/routers/api/v1/repo/migrate.go b/routers/api/v1/repo/migrate.go index 767972ee98..bc594419f7 100644 --- a/routers/api/v1/repo/migrate.go +++ b/routers/api/v1/repo/migrate.go @@ -12,6 +12,8 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/graceful" @@ -54,18 +56,18 @@ func Migrate(ctx *context.APIContext) { //get repoOwner var ( - repoOwner *models.User + repoOwner *user_model.User err error ) if len(form.RepoOwner) != 0 { - repoOwner, err = models.GetUserByName(form.RepoOwner) + repoOwner, err = user_model.GetUserByName(form.RepoOwner) } else if form.RepoOwnerID != 0 { - repoOwner, err = models.GetUserByID(form.RepoOwnerID) + repoOwner, err = user_model.GetUserByID(form.RepoOwnerID) } else { repoOwner = ctx.User } if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "GetUser", err) @@ -208,7 +210,7 @@ func Migrate(ctx *context.APIContext) { ctx.JSON(http.StatusCreated, convert.ToRepo(repo, models.AccessModeAdmin)) } -func handleMigrateError(ctx *context.APIContext, repoOwner *models.User, remoteAddr string, err error) { +func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, remoteAddr string, err error) { switch { case models.IsErrRepoAlreadyExist(err): ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.") @@ -220,12 +222,12 @@ func handleMigrateError(ctx *context.APIContext, repoOwner *models.User, remoteA ctx.Error(http.StatusUnprocessableEntity, "", "Remote visit required two factors authentication.") case models.IsErrReachLimitOfRepo(err): ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("You have already reached your limit of %d repositories.", repoOwner.MaxCreationLimit())) - case models.IsErrNameReserved(err): - ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' is reserved.", err.(models.ErrNameReserved).Name)) - case models.IsErrNameCharsNotAllowed(err): - ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' contains invalid characters.", err.(models.ErrNameCharsNotAllowed).Name)) - case models.IsErrNamePatternNotAllowed(err): - ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The pattern '%s' is not allowed in a username.", err.(models.ErrNamePatternNotAllowed).Pattern)) + case db.IsErrNameReserved(err): + ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' is reserved.", err.(db.ErrNameReserved).Name)) + case db.IsErrNameCharsNotAllowed(err): + ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The username '%s' contains invalid characters.", err.(db.ErrNameCharsNotAllowed).Name)) + case db.IsErrNamePatternNotAllowed(err): + ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("The pattern '%s' is not allowed in a username.", err.(db.ErrNamePatternNotAllowed).Pattern)) case models.IsErrInvalidCloneAddr(err): ctx.Error(http.StatusUnprocessableEntity, "", err) case base.IsErrNotSupported(err): diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index efe72e1502..dd415a34af 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" @@ -382,7 +383,7 @@ func CreatePullRequest(ctx *context.APIContext) { // Get all assignee IDs assigneeIDs, err := models.MakeIDsFromAPIAssigneesToAdd(form.Assignee, form.Assignees) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err)) } else { ctx.Error(http.StatusInternalServerError, "AddAssigneeByName", err) @@ -391,7 +392,7 @@ func CreatePullRequest(ctx *context.APIContext) { } // Check if the passed assignees is assignable for _, aID := range assigneeIDs { - assignee, err := models.GetUserByID(aID) + assignee, err := user_model.GetUserByID(aID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserByID", err) return @@ -522,7 +523,7 @@ func EditPullRequest(ctx *context.APIContext) { if ctx.Repo.CanWrite(unit.TypePullRequests) && (form.Assignees != nil || len(form.Assignee) > 0) { err = issue_service.UpdateAssignees(issue, form.Assignee, form.Assignees, ctx.User) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Assignee does not exist: [name: %s]", err)) } else { ctx.Error(http.StatusInternalServerError, "UpdateAssignees", err) @@ -900,7 +901,7 @@ func MergePullRequest(ctx *context.APIContext) { ctx.Status(http.StatusOK) } -func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*models.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) { +func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*user_model.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) { baseRepo := ctx.Repo.Repository // Get compared branches information @@ -913,7 +914,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) baseBranch := form.Base var ( - headUser *models.User + headUser *user_model.User headBranch string isSameRepo bool err error @@ -927,9 +928,9 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) headBranch = headInfos[0] } else if len(headInfos) == 2 { - headUser, err = models.GetUserByName(headInfos[0]) + headUser, err = user_model.GetUserByName(headInfos[0]) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound("GetUserByName") } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) @@ -1209,7 +1210,7 @@ func GetPullRequestCommits(ctx *context.APIContext) { totalNumberOfCommits := len(commits) totalNumberOfPages := int(math.Ceil(float64(totalNumberOfCommits) / float64(listOptions.PageSize))) - userCache := make(map[string]*models.User) + userCache := make(map[string]*user_model.User) start, end := listOptions.GetStartEnd() diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index 55b5178305..ec5f045647 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -10,6 +10,7 @@ import ( "strings" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" @@ -554,7 +555,7 @@ func prepareSingleReview(ctx *context.APIContext) (*models.Review, *models.PullR return nil, nil, true } - if err := review.LoadAttributes(); err != nil && !models.IsErrUserNotExist(err) { + if err := review.LoadAttributes(); err != nil && !user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusInternalServerError, "ReviewLoadAttributes", err) return nil, nil, true } @@ -659,7 +660,7 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions return } - reviewers := make([]*models.User, 0, len(opts.Reviewers)) + reviewers := make([]*user_model.User, 0, len(opts.Reviewers)) permDoer, err := models.GetUserRepoPermission(pr.Issue.Repo, ctx.User) if err != nil { @@ -668,15 +669,15 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions } for _, r := range opts.Reviewers { - var reviewer *models.User + var reviewer *user_model.User if strings.Contains(r, "@") { - reviewer, err = models.GetUserByEmail(r) + reviewer, err = user_model.GetUserByEmail(r) } else { - reviewer, err = models.GetUserByName(r) + reviewer, err = user_model.GetUserByName(r) } if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound("UserNotExist", fmt.Sprintf("User '%s' not exist", r)) return } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 872ce22871..a74e13d3ab 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -12,7 +12,9 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" unit_model "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" @@ -26,20 +28,20 @@ import ( repo_service "code.gitea.io/gitea/services/repository" ) -var searchOrderByMap = map[string]map[string]models.SearchOrderBy{ +var searchOrderByMap = map[string]map[string]db.SearchOrderBy{ "asc": { - "alpha": models.SearchOrderByAlphabetically, - "created": models.SearchOrderByOldest, - "updated": models.SearchOrderByLeastUpdated, - "size": models.SearchOrderBySize, - "id": models.SearchOrderByID, + "alpha": db.SearchOrderByAlphabetically, + "created": db.SearchOrderByOldest, + "updated": db.SearchOrderByLeastUpdated, + "size": db.SearchOrderBySize, + "id": db.SearchOrderByID, }, "desc": { - "alpha": models.SearchOrderByAlphabeticallyReverse, - "created": models.SearchOrderByNewest, - "updated": models.SearchOrderByRecentUpdated, - "size": models.SearchOrderBySizeReverse, - "id": models.SearchOrderByIDReverse, + "alpha": db.SearchOrderByAlphabeticallyReverse, + "created": db.SearchOrderByNewest, + "updated": db.SearchOrderByRecentUpdated, + "size": db.SearchOrderBySizeReverse, + "id": db.SearchOrderByIDReverse, }, } @@ -239,7 +241,7 @@ func Search(ctx *context.APIContext) { } // CreateUserRepo create a repository for a user -func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) { +func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.CreateRepoOption) { if opt.AutoInit && opt.Readme == "" { opt.Readme = "Default" } @@ -259,8 +261,8 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR if err != nil { if models.IsErrRepoAlreadyExist(err) { ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.") - } else if models.IsErrNameReserved(err) || - models.IsErrNamePatternNotAllowed(err) { + } else if db.IsErrNameReserved(err) || + db.IsErrNamePatternNotAllowed(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "CreateRepository", err) @@ -374,9 +376,9 @@ func Generate(ctx *context.APIContext) { ctxUser := ctx.User var err error if form.Owner != ctxUser.Name { - ctxUser, err = models.GetUserByName(form.Owner) + ctxUser, err = user_model.GetUserByName(form.Owner) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.JSON(http.StatusNotFound, map[string]interface{}{ "error": "request owner `" + form.Owner + "` does not exist", }) @@ -408,8 +410,8 @@ func Generate(ctx *context.APIContext) { if err != nil { if models.IsErrRepoAlreadyExist(err) { ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.") - } else if models.IsErrNameReserved(err) || - models.IsErrNamePatternNotAllowed(err) { + } else if db.IsErrNameReserved(err) || + db.IsErrNamePatternNotAllowed(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "CreateRepository", err) @@ -648,10 +650,10 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err switch { case models.IsErrRepoAlreadyExist(err): ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is already taken [name: %s]", newRepoName), err) - case models.IsErrNameReserved(err): + case db.IsErrNameReserved(err): ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is reserved [name: %s]", newRepoName), err) - case models.IsErrNamePatternNotAllowed(err): - ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name's pattern is not allowed [name: %s, pattern: %s]", newRepoName, err.(models.ErrNamePatternNotAllowed).Pattern), err) + case db.IsErrNamePatternNotAllowed(err): + ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name's pattern is not allowed [name: %s, pattern: %s]", newRepoName, err.(db.ErrNamePatternNotAllowed).Pattern), err) default: ctx.Error(http.StatusUnprocessableEntity, "ChangeRepositoryName", err) } diff --git a/routers/api/v1/repo/transfer.go b/routers/api/v1/repo/transfer.go index f16e2bb081..b3913bcd34 100644 --- a/routers/api/v1/repo/transfer.go +++ b/routers/api/v1/repo/transfer.go @@ -9,6 +9,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/log" @@ -53,9 +54,9 @@ func Transfer(ctx *context.APIContext) { opts := web.GetForm(ctx).(*api.TransferRepoOption) - newOwner, err := models.GetUserByName(opts.NewOwner) + newOwner, err := user_model.GetUserByName(opts.NewOwner) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusNotFound, "", "The new owner does not exist or cannot be found") return } @@ -63,7 +64,7 @@ func Transfer(ctx *context.APIContext) { return } - if newOwner.Type == models.UserTypeOrganization { + if newOwner.Type == user_model.UserTypeOrganization { if !ctx.User.IsAdmin && newOwner.Visibility == api.VisibleTypePrivate && !models.OrgFromUser(newOwner).HasMemberWithUserID(ctx.User.ID) { // The user shouldn't know about this organization ctx.Error(http.StatusNotFound, "", "The new owner does not exist or cannot be found") diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go index edf3cea3ea..1eacb89db2 100644 --- a/routers/api/v1/user/follower.go +++ b/routers/api/v1/user/follower.go @@ -8,7 +8,6 @@ package user import ( "net/http" - "code.gitea.io/gitea/models" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -16,7 +15,7 @@ import ( "code.gitea.io/gitea/routers/api/v1/utils" ) -func responseAPIUsers(ctx *context.APIContext, users []*models.User) { +func responseAPIUsers(ctx *context.APIContext, users []*user_model.User) { apiUsers := make([]*api.User, len(users)) for i := range users { apiUsers[i] = convert.ToUser(users[i], ctx.User) @@ -24,8 +23,8 @@ func responseAPIUsers(ctx *context.APIContext, users []*models.User) { ctx.JSON(http.StatusOK, &apiUsers) } -func listUserFollowers(ctx *context.APIContext, u *models.User) { - users, err := models.GetUserFollowers(u, utils.GetListOptions(ctx)) +func listUserFollowers(ctx *context.APIContext, u *user_model.User) { + users, err := user_model.GetUserFollowers(u, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err) return @@ -90,8 +89,8 @@ func ListFollowers(ctx *context.APIContext) { listUserFollowers(ctx, u) } -func listUserFollowing(ctx *context.APIContext, u *models.User) { - users, err := models.GetUserFollowing(u, utils.GetListOptions(ctx)) +func listUserFollowing(ctx *context.APIContext, u *user_model.User) { + users, err := user_model.GetUserFollowing(u, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserFollowing", err) return @@ -156,7 +155,7 @@ func ListFollowing(ctx *context.APIContext) { listUserFollowing(ctx, u) } -func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) { +func checkUserFollowing(ctx *context.APIContext, u *user_model.User, followID int64) { if user_model.IsFollowing(u.ID, followID) { ctx.Status(http.StatusNoContent) } else { diff --git a/routers/api/v1/user/helper.go b/routers/api/v1/user/helper.go index a7c2e61405..fab3ce2ae5 100644 --- a/routers/api/v1/user/helper.go +++ b/routers/api/v1/user/helper.go @@ -7,17 +7,16 @@ package user import ( "net/http" - "code.gitea.io/gitea/models" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" ) // GetUserByParamsName get user by name -func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { +func GetUserByParamsName(ctx *context.APIContext, name string) *user_model.User { username := ctx.Params(name) - user, err := models.GetUserByName(username) + user, err := user_model.GetUserByName(username) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { if redirectUserID, err2 := user_model.LookupUserRedirect(username); err2 == nil { context.RedirectToUser(ctx.Context, username, redirectUserID) } else { @@ -32,6 +31,6 @@ func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { } // GetUserByParams returns user whose name is presented in URL (":username"). -func GetUserByParams(ctx *context.APIContext) *models.User { +func GetUserByParams(ctx *context.APIContext) *user_model.User { return GetUserByParamsName(ctx, ":username") } diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index 36b1c17b89..719e1c0bdc 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/setting" @@ -18,7 +19,7 @@ import ( ) // appendPrivateInformation appends the owner and key type information to api.PublicKey -func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defaultUser *models.User) (*api.PublicKey, error) { +func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defaultUser *user_model.User) (*api.PublicKey, error) { if key.Type == models.KeyTypeDeploy { apiKey.KeyType = "deploy" } else if key.Type == models.KeyTypeUser { @@ -27,7 +28,7 @@ func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defa if defaultUser.ID == key.OwnerID { apiKey.Owner = convert.ToUser(defaultUser, defaultUser) } else { - user, err := models.GetUserByID(key.OwnerID) + user, err := user_model.GetUserByID(key.OwnerID) if err != nil { return apiKey, err } @@ -44,7 +45,7 @@ func composePublicKeysAPILink() string { return setting.AppURL + "api/v1/user/keys/" } -func listPublicKeys(ctx *context.APIContext, user *models.User) { +func listPublicKeys(ctx *context.APIContext, user *user_model.User) { var keys []*models.PublicKey var err error var count int diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 3c9a17a0be..0b0b522c15 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -8,6 +8,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -15,7 +16,7 @@ import ( ) // listUserRepos - List the repositories owned by the given user. -func listUserRepos(ctx *context.APIContext, u *models.User, private bool) { +func listUserRepos(ctx *context.APIContext, u *user_model.User, private bool) { opts := utils.GetListOptions(ctx) repos, count, err := models.GetUserRepositories(&models.SearchRepoOptions{ diff --git a/routers/api/v1/user/settings.go b/routers/api/v1/user/settings.go index b4548e7443..40bee56681 100644 --- a/routers/api/v1/user/settings.go +++ b/routers/api/v1/user/settings.go @@ -7,7 +7,7 @@ package user import ( "net/http" - "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -74,7 +74,7 @@ func UpdateUserSettings(ctx *context.APIContext) { ctx.User.KeepActivityPrivate = *form.HideActivity } - if err := models.UpdateUser(ctx.User); err != nil { + if err := user_model.UpdateUser(ctx.User); err != nil { ctx.InternalServerError(err) return } diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index f067722bfa..946ae738e9 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -18,7 +19,7 @@ import ( // getStarredRepos returns the repos that the user with the specified userID has // starred -func getStarredRepos(user *models.User, private bool, listOptions db.ListOptions) ([]*api.Repository, error) { +func getStarredRepos(user *user_model.User, private bool, listOptions db.ListOptions) ([]*api.Repository, error) { starredRepos, err := models.GetStarredRepos(user.ID, private, listOptions) if err != nil { return nil, err diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 8c57b1f099..bba7b7a5d1 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -9,6 +9,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/routers/api/v1/utils" @@ -54,11 +55,11 @@ func Search(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - users, maxResults, err := models.SearchUsers(&models.SearchUserOptions{ + users, maxResults, err := user_model.SearchUsers(&user_model.SearchUserOptions{ Actor: ctx.User, Keyword: ctx.FormTrim("q"), UID: ctx.FormInt64("uid"), - Type: models.UserTypeIndividual, + Type: user_model.UserTypeIndividual, ListOptions: listOptions, }) if err != nil { @@ -105,7 +106,7 @@ func GetInfo(ctx *context.APIContext) { if !models.IsUserVisibleToViewer(u, ctx.User) { // fake ErrUserNotExist error message to not leak information about existence - ctx.NotFound("GetUserByName", models.ErrUserNotExist{Name: ctx.Params(":username")}) + ctx.NotFound("GetUserByName", user_model.ErrUserNotExist{Name: ctx.Params(":username")}) return } ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.User)) diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index 3c6f8b3070..54d5e74bc0 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -16,7 +17,7 @@ import ( ) // getWatchedRepos returns the repos that the user with the specified userID is watching -func getWatchedRepos(user *models.User, private bool, listOptions db.ListOptions) ([]*api.Repository, int64, error) { +func getWatchedRepos(user *user_model.User, private bool, listOptions db.ListOptions) ([]*api.Repository, int64, error) { watchedRepos, total, err := models.GetWatchedRepos(user.ID, private, listOptions) if err != nil { return nil, 0, err diff --git a/routers/install/install.go b/routers/install/install.go index 837467056d..bd19ab5eb6 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -14,9 +14,9 @@ import ( "strings" "time" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/migrations" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/generate" @@ -72,7 +72,7 @@ func Init(next http.Handler) http.Handler { "TmplLoadTimes": func() string { return time.Since(startTime).String() }, - "PasswordHashAlgorithms": models.AvailableHashAlgorithms, + "PasswordHashAlgorithms": user_model.AvailableHashAlgorithms, }, } for _, lang := range translation.AllLangs() { @@ -264,13 +264,13 @@ func SubmitInstall(ctx *context.Context) { // Check admin user creation if len(form.AdminName) > 0 { // Ensure AdminName is valid - if err := models.IsUsableUsername(form.AdminName); err != nil { + if err := user_model.IsUsableUsername(form.AdminName); err != nil { ctx.Data["Err_Admin"] = true ctx.Data["Err_AdminName"] = true - if models.IsErrNameReserved(err) { + if db.IsErrNameReserved(err) { ctx.RenderWithErr(ctx.Tr("install.err_admin_name_is_reserved"), tplInstall, form) return - } else if models.IsErrNamePatternNotAllowed(err) { + } else if db.IsErrNamePatternNotAllowed(err) { ctx.RenderWithErr(ctx.Tr("install.err_admin_name_pattern_not_allowed"), tplInstall, form) return } @@ -416,15 +416,15 @@ func SubmitInstall(ctx *context.Context) { // Create admin account if len(form.AdminName) > 0 { - u := &models.User{ + u := &user_model.User{ Name: form.AdminName, Email: form.AdminEmail, Passwd: form.AdminPasswd, IsAdmin: true, IsActive: true, } - if err = models.CreateUser(u); err != nil { - if !models.IsErrUserAlreadyExist(err) { + if err = user_model.CreateUser(u); err != nil { + if !user_model.IsErrUserAlreadyExist(err) { setting.InstallLock = false ctx.Data["Err_AdminName"] = true ctx.Data["Err_AdminEmail"] = true @@ -432,7 +432,7 @@ func SubmitInstall(ctx *context.Context) { return } log.Info("Admin account already exist") - u, _ = models.GetUserByName(u.Name) + u, _ = user_model.GetUserByName(u.Name) } days := 86400 * setting.LogInRememberDays diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index 04c334b38f..5ab21d525b 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" gitea_context "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -23,7 +24,7 @@ import ( type preReceiveContext struct { *gitea_context.PrivateContext - user *models.User + user *user_model.User perm models.Permission canCreatePullRequest bool @@ -41,7 +42,7 @@ type preReceiveContext struct { } // User gets or loads User -func (ctx *preReceiveContext) User() *models.User { +func (ctx *preReceiveContext) User() *user_model.User { if ctx.user == nil { ctx.user, ctx.perm = loadUserAndPermission(ctx.PrivateContext, ctx.opts.UserID) } @@ -449,8 +450,8 @@ func generateGitEnv(opts *private.HookOptions) (env []string) { return env } -func loadUserAndPermission(ctx *gitea_context.PrivateContext, id int64) (user *models.User, perm models.Permission) { - user, err := models.GetUserByID(id) +func loadUserAndPermission(ctx *gitea_context.PrivateContext, id int64) (user *user_model.User, perm models.Permission) { + user, err := user_model.GetUserByID(id) if err != nil { log.Error("Unable to get User id %d Error: %v", id, err) ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/routers/private/mail.go b/routers/private/mail.go index 6b9100159a..5218cd34a1 100644 --- a/routers/private/mail.go +++ b/routers/private/mail.go @@ -9,7 +9,7 @@ import ( "net/http" "strconv" - "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -44,7 +44,7 @@ func SendEmail(ctx *context.PrivateContext) { var emails []string if len(mail.To) > 0 { for _, uname := range mail.To { - user, err := models.GetUserByName(uname) + user, err := user_model.GetUserByName(uname) if err != nil { err := fmt.Sprintf("Failed to get user information: %v", err) log.Error(err) @@ -59,7 +59,7 @@ func SendEmail(ctx *context.PrivateContext) { } } } else { - err := models.IterateUser(func(user *models.User) error { + err := user_model.IterateUser(func(user *user_model.User) error { if len(user.Email) > 0 { emails = append(emails, user.Email) } diff --git a/routers/private/serv.go b/routers/private/serv.go index 329d80476a..18057ededb 100644 --- a/routers/private/serv.go +++ b/routers/private/serv.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" @@ -48,9 +49,9 @@ func ServNoCommand(ctx *context.PrivateContext) { results.Key = key if key.Type == models.KeyTypeUser || key.Type == models.KeyTypePrincipal { - user, err := models.GetUserByID(key.OwnerID) + user, err := user_model.GetUserByID(key.OwnerID) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.JSON(http.StatusUnauthorized, private.Response{ Err: fmt.Sprintf("Cannot find owner with id: %d for key: %d", key.OwnerID, keyID), }) @@ -105,7 +106,7 @@ func ServCommand(ctx *context.PrivateContext) { results.RepoName = repoName[:len(repoName)-5] } - owner, err := models.GetUserByName(results.OwnerName) + owner, err := user_model.GetUserByName(results.OwnerName) if err != nil { log.Error("Unable to get repository owner: %s/%s Error: %v", results.OwnerName, results.RepoName, err) ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{ @@ -214,7 +215,7 @@ func ServCommand(ctx *context.PrivateContext) { // So now we need to check if the key is a deploy key // We'll keep hold of the deploy key here for permissions checking var deployKey *models.DeployKey - var user *models.User + var user *user_model.User if key.Type == models.KeyTypeDeploy { results.IsDeployKey = true @@ -248,9 +249,9 @@ func ServCommand(ctx *context.PrivateContext) { } else { // Get the user represented by the Key var err error - user, err = models.GetUserByID(key.OwnerID) + user, err = user_model.GetUserByID(key.OwnerID) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.JSON(http.StatusUnauthorized, private.ErrServCommand{ Results: results, Err: fmt.Sprintf("Public Key: %d:%s owner %d does not exist.", key.ID, key.Name, key.OwnerID), @@ -333,7 +334,7 @@ func ServCommand(ctx *context.PrivateContext) { // We already know we aren't using a deploy key if !repoExist { - owner, err := models.GetUserByName(ownerName) + owner, err := user_model.GetUserByName(ownerName) if err != nil { ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{ Results: results, diff --git a/routers/web/admin/orgs.go b/routers/web/admin/orgs.go index df3118b60f..3d440d83cb 100644 --- a/routers/web/admin/orgs.go +++ b/routers/web/admin/orgs.go @@ -6,8 +6,8 @@ package admin import ( - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" @@ -25,9 +25,9 @@ func Organizations(ctx *context.Context) { ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminOrganizations"] = true - explore.RenderUserSearch(ctx, &models.SearchUserOptions{ + explore.RenderUserSearch(ctx, &user_model.SearchUserOptions{ Actor: ctx.User, - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, ListOptions: db.ListOptions{ PageSize: setting.UI.Admin.OrgPagingNum, }, diff --git a/routers/web/admin/repos.go b/routers/web/admin/repos.go index 432dd2f6ae..69e522ef7e 100644 --- a/routers/web/admin/repos.go +++ b/routers/web/admin/repos.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -119,9 +120,9 @@ func AdoptOrDeleteRepository(ctx *context.Context) { return } - ctxUser, err := models.GetUserByName(dirSplit[0]) + ctxUser, err := user_model.GetUserByName(dirSplit[0]) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { log.Debug("User does not exist: %s", dirSplit[0]) ctx.Redirect(setting.AppSubURL + "/admin/repos") return diff --git a/routers/web/admin/users.go b/routers/web/admin/users.go index 077cf02f15..b92c5cf01a 100644 --- a/routers/web/admin/users.go +++ b/routers/web/admin/users.go @@ -56,9 +56,9 @@ func Users(ctx *context.Context) { "SortType": sortType, } - explore.RenderUserSearch(ctx, &models.SearchUserOptions{ + explore.RenderUserSearch(ctx, &user_model.SearchUserOptions{ Actor: ctx.User, - Type: models.UserTypeIndividual, + Type: user_model.UserTypeIndividual, ListOptions: db.ListOptions{ PageSize: setting.UI.Admin.UserPagingNum, }, @@ -114,7 +114,7 @@ func NewUserPost(ctx *context.Context) { return } - u := &models.User{ + u := &user_model.User{ Name: form.UserName, Email: form.Email, Passwd: form.Password, @@ -156,9 +156,9 @@ func NewUserPost(ctx *context.Context) { u.MustChangePassword = form.MustChangePassword } - if err := models.CreateUser(u, &models.CreateUserOverwriteOptions{Visibility: form.Visibility}); err != nil { + if err := user_model.CreateUser(u, &user_model.CreateUserOverwriteOptions{Visibility: form.Visibility}); err != nil { switch { - case models.IsErrUserAlreadyExist(err): + case user_model.IsErrUserAlreadyExist(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplUserNew, &form) case user_model.IsErrEmailAlreadyUsed(err): @@ -167,15 +167,15 @@ func NewUserPost(ctx *context.Context) { case user_model.IsErrEmailInvalid(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplUserNew, &form) - case models.IsErrNameReserved(err): + case db.IsErrNameReserved(err): ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplUserNew, &form) - case models.IsErrNamePatternNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(db.ErrNameReserved).Name), tplUserNew, &form) + case db.IsErrNamePatternNotAllowed(err): ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplUserNew, &form) - case models.IsErrNameCharsNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplUserNew, &form) + case db.IsErrNameCharsNotAllowed(err): ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(ctx.Tr("user.form.name_chars_not_allowed", err.(models.ErrNameCharsNotAllowed).Name), tplUserNew, &form) + ctx.RenderWithErr(ctx.Tr("user.form.name_chars_not_allowed", err.(db.ErrNameCharsNotAllowed).Name), tplUserNew, &form) default: ctx.ServerError("CreateUser", err) } @@ -192,8 +192,8 @@ func NewUserPost(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/admin/users/" + strconv.FormatInt(u.ID, 10)) } -func prepareUserInfo(ctx *context.Context) *models.User { - u, err := models.GetUserByID(ctx.ParamsInt64(":userid")) +func prepareUserInfo(ctx *context.Context) *user_model.User { + u, err := user_model.GetUserByID(ctx.ParamsInt64(":userid")) if err != nil { ctx.ServerError("GetUserByID", err) return nil @@ -298,7 +298,7 @@ func EditUserPost(ctx *context.Context) { ctx.RenderWithErr(errMsg, tplUserNew, &form) return } - if u.Salt, err = models.GetUserSalt(); err != nil { + if u.Salt, err = user_model.GetUserSalt(); err != nil { ctx.ServerError("UpdateUser", err) return } @@ -352,7 +352,7 @@ func EditUserPost(ctx *context.Context) { u.ProhibitLogin = form.ProhibitLogin } - if err := models.UpdateUser(u); err != nil { + if err := user_model.UpdateUser(u); err != nil { if user_model.IsErrEmailAlreadyUsed(err) { ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserEdit, &form) @@ -372,7 +372,7 @@ func EditUserPost(ctx *context.Context) { // DeleteUser response for deleting a user func DeleteUser(ctx *context.Context) { - u, err := models.GetUserByID(ctx.ParamsInt64(":userid")) + u, err := user_model.GetUserByID(ctx.ParamsInt64(":userid")) if err != nil { ctx.ServerError("GetUserByID", err) return diff --git a/routers/web/admin/users_test.go b/routers/web/admin/users_test.go index 607ef2ea66..1849781bc4 100644 --- a/routers/web/admin/users_test.go +++ b/routers/web/admin/users_test.go @@ -7,8 +7,8 @@ package admin import ( "testing" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -23,10 +23,10 @@ func TestNewUserPost_MustChangePassword(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := unittest.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ IsAdmin: true, ID: 2, - }).(*models.User) + }).(*user_model.User) ctx.User = u @@ -48,7 +48,7 @@ func TestNewUserPost_MustChangePassword(t *testing.T) { assert.NotEmpty(t, ctx.Flash.SuccessMsg) - u, err := models.GetUserByName(username) + u, err := user_model.GetUserByName(username) assert.NoError(t, err) assert.Equal(t, username, u.Name) @@ -60,10 +60,10 @@ func TestNewUserPost_MustChangePasswordFalse(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := unittest.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ IsAdmin: true, ID: 2, - }).(*models.User) + }).(*user_model.User) ctx.User = u @@ -85,7 +85,7 @@ func TestNewUserPost_MustChangePasswordFalse(t *testing.T) { assert.NotEmpty(t, ctx.Flash.SuccessMsg) - u, err := models.GetUserByName(username) + u, err := user_model.GetUserByName(username) assert.NoError(t, err) assert.Equal(t, username, u.Name) @@ -97,10 +97,10 @@ func TestNewUserPost_InvalidEmail(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := unittest.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ IsAdmin: true, ID: 2, - }).(*models.User) + }).(*user_model.User) ctx.User = u @@ -127,10 +127,10 @@ func TestNewUserPost_VisibilityDefaultPublic(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := unittest.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ IsAdmin: true, ID: 2, - }).(*models.User) + }).(*user_model.User) ctx.User = u @@ -152,7 +152,7 @@ func TestNewUserPost_VisibilityDefaultPublic(t *testing.T) { assert.NotEmpty(t, ctx.Flash.SuccessMsg) - u, err := models.GetUserByName(username) + u, err := user_model.GetUserByName(username) assert.NoError(t, err) assert.Equal(t, username, u.Name) @@ -165,10 +165,10 @@ func TestNewUserPost_VisibilityPrivate(t *testing.T) { unittest.PrepareTestEnv(t) ctx := test.MockContext(t, "admin/users/new") - u := unittest.AssertExistsAndLoadBean(t, &models.User{ + u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ IsAdmin: true, ID: 2, - }).(*models.User) + }).(*user_model.User) ctx.User = u @@ -191,7 +191,7 @@ func TestNewUserPost_VisibilityPrivate(t *testing.T) { assert.NotEmpty(t, ctx.Flash.SuccessMsg) - u, err := models.GetUserByName(username) + u, err := user_model.GetUserByName(username) assert.NoError(t, err) assert.Equal(t, username, u.Name) diff --git a/routers/web/dev/template.go b/routers/web/dev/template.go index de334c4f8b..29d6033a7a 100644 --- a/routers/web/dev/template.go +++ b/routers/web/dev/template.go @@ -7,7 +7,7 @@ package dev import ( "net/http" - "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" @@ -16,7 +16,7 @@ import ( // TemplatePreview render for previewing the indicated template func TemplatePreview(ctx *context.Context) { - ctx.Data["User"] = models.User{Name: "Unknown"} + ctx.Data["User"] = user_model.User{Name: "Unknown"} ctx.Data["AppName"] = setting.AppName ctx.Data["AppVer"] = setting.AppVer ctx.Data["AppUrl"] = setting.AppURL diff --git a/routers/web/explore/org.go b/routers/web/explore/org.go index d005cfa503..41c0a0c83c 100644 --- a/routers/web/explore/org.go +++ b/routers/web/explore/org.go @@ -5,8 +5,8 @@ package explore import ( - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" @@ -31,9 +31,9 @@ func Organizations(ctx *context.Context) { visibleTypes = append(visibleTypes, structs.VisibleTypeLimited, structs.VisibleTypePrivate) } - RenderUserSearch(ctx, &models.SearchUserOptions{ + RenderUserSearch(ctx, &user_model.SearchUserOptions{ Actor: ctx.User, - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum}, Visible: visibleTypes, }, tplExploreOrganizations) diff --git a/routers/web/explore/repo.go b/routers/web/explore/repo.go index 78035037e5..a81386b5fd 100644 --- a/routers/web/explore/repo.go +++ b/routers/web/explore/repo.go @@ -39,38 +39,38 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { repos []*models.Repository count int64 err error - orderBy models.SearchOrderBy + orderBy db.SearchOrderBy ) ctx.Data["SortType"] = ctx.FormString("sort") switch ctx.FormString("sort") { case "newest": - orderBy = models.SearchOrderByNewest + orderBy = db.SearchOrderByNewest case "oldest": - orderBy = models.SearchOrderByOldest + orderBy = db.SearchOrderByOldest case "recentupdate": - orderBy = models.SearchOrderByRecentUpdated + orderBy = db.SearchOrderByRecentUpdated case "leastupdate": - orderBy = models.SearchOrderByLeastUpdated + orderBy = db.SearchOrderByLeastUpdated case "reversealphabetically": - orderBy = models.SearchOrderByAlphabeticallyReverse + orderBy = db.SearchOrderByAlphabeticallyReverse case "alphabetically": - orderBy = models.SearchOrderByAlphabetically + orderBy = db.SearchOrderByAlphabetically case "reversesize": - orderBy = models.SearchOrderBySizeReverse + orderBy = db.SearchOrderBySizeReverse case "size": - orderBy = models.SearchOrderBySize + orderBy = db.SearchOrderBySize case "moststars": - orderBy = models.SearchOrderByStarsReverse + orderBy = db.SearchOrderByStarsReverse case "feweststars": - orderBy = models.SearchOrderByStars + orderBy = db.SearchOrderByStars case "mostforks": - orderBy = models.SearchOrderByForksReverse + orderBy = db.SearchOrderByForksReverse case "fewestforks": - orderBy = models.SearchOrderByForks + orderBy = db.SearchOrderByForks default: ctx.Data["SortType"] = "recentupdate" - orderBy = models.SearchOrderByRecentUpdated + orderBy = db.SearchOrderByRecentUpdated } keyword := ctx.FormTrim("q") diff --git a/routers/web/explore/user.go b/routers/web/explore/user.go index 1fe45ed585..8560378447 100644 --- a/routers/web/explore/user.go +++ b/routers/web/explore/user.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" @@ -34,17 +35,17 @@ func isKeywordValid(keyword string) bool { } // RenderUserSearch render user search page -func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplName base.TplName) { +func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions, tplName base.TplName) { opts.Page = ctx.FormInt("page") if opts.Page <= 1 { opts.Page = 1 } var ( - users []*models.User + users []*user_model.User count int64 err error - orderBy models.SearchOrderBy + orderBy db.SearchOrderBy ) // we can not set orderBy to `models.SearchOrderByXxx`, because there may be a JOIN in the statement, different tables may have the same name columns @@ -69,7 +70,7 @@ func RenderUserSearch(ctx *context.Context, opts *models.SearchUserOptions, tplN opts.Keyword = ctx.FormTrim("q") opts.OrderBy = orderBy if len(opts.Keyword) == 0 || isKeywordValid(opts.Keyword) { - users, count, err = models.SearchUsers(opts) + users, count, err = user_model.SearchUsers(opts) if err != nil { ctx.ServerError("SearchUsers", err) return @@ -100,9 +101,9 @@ func Users(ctx *context.Context) { ctx.Data["PageIsExploreUsers"] = true ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled - RenderUserSearch(ctx, &models.SearchUserOptions{ + RenderUserSearch(ctx, &user_model.SearchUserOptions{ Actor: ctx.User, - Type: models.UserTypeIndividual, + Type: user_model.UserTypeIndividual, ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum}, IsActive: util.OptionalBoolTrue, Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate}, diff --git a/routers/web/feed/profile.go b/routers/web/feed/profile.go index 8bd0cb7c29..1a7f4ad24b 100644 --- a/routers/web/feed/profile.go +++ b/routers/web/feed/profile.go @@ -9,6 +9,7 @@ import ( "time" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "github.com/gorilla/feeds" @@ -22,7 +23,7 @@ func RetrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) []*mode return nil } - userCache := map[int64]*models.User{options.RequestedUser.ID: options.RequestedUser} + userCache := map[int64]*user_model.User{options.RequestedUser.ID: options.RequestedUser} if ctx.User != nil { userCache[ctx.User.ID] = ctx.User } @@ -35,9 +36,9 @@ func RetrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) []*mode for _, act := range actions { repoOwner, ok := userCache[act.Repo.OwnerID] if !ok { - repoOwner, err = models.GetUserByID(act.Repo.OwnerID) + repoOwner, err = user_model.GetUserByID(act.Repo.OwnerID) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { continue } ctx.ServerError("GetUserByID", err) @@ -51,7 +52,7 @@ func RetrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) []*mode } // ShowUserFeed show user activity as RSS / Atom feed -func ShowUserFeed(ctx *context.Context, ctxUser *models.User, formatType string) { +func ShowUserFeed(ctx *context.Context, ctxUser *user_model.User, formatType string) { actions := RetrieveFeeds(ctx, models.GetFeedsOptions{ RequestedUser: ctxUser, Actor: ctx.User, diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 6e81257135..0dc441fe84 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -50,32 +50,32 @@ func Home(ctx *context.Context) { ctx.Data["RenderedDescription"] = desc } - var orderBy models.SearchOrderBy + var orderBy db.SearchOrderBy ctx.Data["SortType"] = ctx.FormString("sort") switch ctx.FormString("sort") { case "newest": - orderBy = models.SearchOrderByNewest + orderBy = db.SearchOrderByNewest case "oldest": - orderBy = models.SearchOrderByOldest + orderBy = db.SearchOrderByOldest case "recentupdate": - orderBy = models.SearchOrderByRecentUpdated + orderBy = db.SearchOrderByRecentUpdated case "leastupdate": - orderBy = models.SearchOrderByLeastUpdated + orderBy = db.SearchOrderByLeastUpdated case "reversealphabetically": - orderBy = models.SearchOrderByAlphabeticallyReverse + orderBy = db.SearchOrderByAlphabeticallyReverse case "alphabetically": - orderBy = models.SearchOrderByAlphabetically + orderBy = db.SearchOrderByAlphabetically case "moststars": - orderBy = models.SearchOrderByStarsReverse + orderBy = db.SearchOrderByStarsReverse case "feweststars": - orderBy = models.SearchOrderByStars + orderBy = db.SearchOrderByStars case "mostforks": - orderBy = models.SearchOrderByForksReverse + orderBy = db.SearchOrderByForksReverse case "fewestforks": - orderBy = models.SearchOrderByForks + orderBy = db.SearchOrderByForks default: ctx.Data["SortType"] = "recentupdate" - orderBy = models.SearchOrderByRecentUpdated + orderBy = db.SearchOrderByRecentUpdated } keyword := ctx.FormTrim("q") diff --git a/routers/web/org/org.go b/routers/web/org/org.go index 6722f8d104..c66a0cd4c8 100644 --- a/routers/web/org/org.go +++ b/routers/web/org/org.go @@ -10,6 +10,8 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -52,7 +54,7 @@ func CreatePost(ctx *context.Context) { org := &models.Organization{ Name: form.OrgName, IsActive: true, - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, Visibility: form.Visibility, RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess, } @@ -60,12 +62,12 @@ func CreatePost(ctx *context.Context) { if err := models.CreateOrganization(org, ctx.User); err != nil { ctx.Data["Err_OrgName"] = true switch { - case models.IsErrUserAlreadyExist(err): + case user_model.IsErrUserAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), tplCreateOrg, &form) - case models.IsErrNameReserved(err): - ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), tplCreateOrg, &form) - case models.IsErrNamePatternNotAllowed(err): - ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form) + case db.IsErrNameReserved(err): + ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(db.ErrNameReserved).Name), tplCreateOrg, &form) + case db.IsErrNamePatternNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form) case models.IsErrUserNotAllowedCreateOrg(err): ctx.RenderWithErr(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form) default: diff --git a/routers/web/org/setting.go b/routers/web/org/setting.go index 7a6f4fec60..f0e1db14d0 100644 --- a/routers/web/org/setting.go +++ b/routers/web/org/setting.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -61,7 +62,7 @@ func SettingsPost(ctx *context.Context) { // Check if organization name has been changed. if org.LowerName != strings.ToLower(form.Name) { - isExist, err := models.IsUserExist(org.ID, form.Name) + isExist, err := user_model.IsUserExist(org.ID, form.Name) if err != nil { ctx.ServerError("IsUserExist", err) return @@ -69,8 +70,8 @@ func SettingsPost(ctx *context.Context) { ctx.Data["OrgName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSettingsOptions, &form) return - } else if err = models.ChangeUserName(org.AsUser(), form.Name); err != nil { - if err == models.ErrUserNameIllegal { + } else if err = user_model.ChangeUserName(org.AsUser(), form.Name); err != nil { + if err == user_model.ErrUserNameIllegal { ctx.Data["OrgName"] = true ctx.RenderWithErr(ctx.Tr("form.illegal_username"), tplSettingsOptions, &form) } else { @@ -101,7 +102,7 @@ func SettingsPost(ctx *context.Context) { visibilityChanged := form.Visibility != org.Visibility org.Visibility = form.Visibility - if err := models.UpdateUser(org.AsUser()); err != nil { + if err := user_model.UpdateUser(org.AsUser()); err != nil { ctx.ServerError("UpdateUser", err) return } diff --git a/routers/web/org/teams.go b/routers/web/org/teams.go index ac6a42904a..da0830aad9 100644 --- a/routers/web/org/teams.go +++ b/routers/web/org/teams.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" unit_model "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -114,10 +115,10 @@ func TeamsAction(ctx *context.Context) { return } uname := utils.RemoveUsernameParameterSuffix(strings.ToLower(ctx.FormString("uname"))) - var u *models.User - u, err = models.GetUserByName(uname) + var u *user_model.User + u, err = user_model.GetUserByName(uname) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Flash.Error(ctx.Tr("form.user_not_exist")) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName)) } else { diff --git a/routers/web/repo/blame.go b/routers/web/repo/blame.go index 2fd72d81ab..5786aa1eb2 100644 --- a/routers/web/repo/blame.go +++ b/routers/web/repo/blame.go @@ -12,6 +12,7 @@ import ( "strings" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" @@ -139,9 +140,9 @@ func RefBlame(ctx *context.Context) { ctx.HTML(http.StatusOK, tplBlame) } -func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[string]*models.UserCommit, map[string]string) { +func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[string]*user_model.UserCommit, map[string]string) { // store commit data by SHA to look up avatar info etc - commitNames := make(map[string]*models.UserCommit) + commitNames := make(map[string]*user_model.UserCommit) // previousCommits contains links from SHA to parent SHA, // if parent also contains the current TreePath. previousCommits := make(map[string]string) @@ -195,14 +196,14 @@ func processBlameParts(ctx *context.Context, blameParts []git.BlamePart) (map[st } // populate commit email addresses to later look up avatars. - for _, c := range models.ValidateCommitsWithEmails(commits) { + for _, c := range user_model.ValidateCommitsWithEmails(commits) { commitNames[c.ID.String()] = c } return commitNames, previousCommits } -func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]*models.UserCommit, previousCommits map[string]string) { +func renderBlame(ctx *context.Context, blameParts []git.BlamePart, commitNames map[string]*user_model.UserCommit, previousCommits map[string]string) { repoLink := ctx.Repo.RepoLink language := "" diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index ecb5107a3d..48386f7935 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/context" @@ -344,7 +345,7 @@ func Diff(ctx *context.Context) { verification := models.ParseCommitWithSignature(commit) ctx.Data["Verification"] = verification - ctx.Data["Author"] = models.ValidateCommitWithEmail(commit) + ctx.Data["Author"] = user_model.ValidateCommitWithEmail(commit) ctx.Data["Parents"] = parents ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0 @@ -358,7 +359,7 @@ func Diff(ctx *context.Context) { if err == nil { ctx.Data["Note"] = string(charset.ToUTF8WithFallback(note.Message)) ctx.Data["NoteCommit"] = note.Commit - ctx.Data["NoteAuthor"] = models.ValidateCommitWithEmail(note.Commit) + ctx.Data["NoteAuthor"] = user_model.ValidateCommitWithEmail(note.Commit) } ctx.Data["BranchName"], err = commit.GetBranchName() diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 706009820a..865a734caf 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/context" @@ -163,7 +164,7 @@ func setCsvCompareContext(ctx *context.Context) { // CompareInfo represents the collected results from ParseCompareInfo type CompareInfo struct { - HeadUser *models.User + HeadUser *user_model.User HeadRepo *models.Repository HeadGitRepo *git.Repository CompareInfo *git.CompareInfo @@ -237,9 +238,9 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { } else if len(headInfos) == 2 { headInfosSplit := strings.Split(headInfos[0], "/") if len(headInfosSplit) == 1 { - ci.HeadUser, err = models.GetUserByName(headInfos[0]) + ci.HeadUser, err = user_model.GetUserByName(headInfos[0]) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound("GetUserByName", nil) } else { ctx.ServerError("GetUserByName", err) @@ -262,7 +263,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { return nil } if err := ci.HeadRepo.GetOwner(); err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound("GetUserByName", nil) } else { ctx.ServerError("GetUserByName", err) diff --git a/routers/web/repo/http.go b/routers/web/repo/http.go index 0626d08a7c..ce2d977518 100644 --- a/routers/web/repo/http.go +++ b/routers/web/repo/http.go @@ -110,9 +110,9 @@ func httpBase(ctx *context.Context) (h *serviceHandler) { reponame = reponame[:len(reponame)-5] } - owner, err := models.GetUserByName(username) + owner, err := user_model.GetUserByName(username) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { if redirectUserID, err := user_model.LookupUserRedirect(username); err == nil { context.RedirectToUser(ctx, username, redirectUserID) } else { diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index ac6240e282..1ff126c7d1 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -470,7 +471,7 @@ func retrieveProjects(ctx *context.Context, repo *models.Repository) { type repoReviewerSelection struct { IsTeam bool Team *models.Team - User *models.User + User *user_model.User Review *models.Review CanChange bool Checked bool @@ -503,7 +504,7 @@ func RetrieveRepoReviewers(ctx *context.Context, repo *models.Repository, issue reviewersResult []*repoReviewerSelection teamReviewersResult []*repoReviewerSelection teamReviewers []*models.Team - reviewers []*models.User + reviewers []*user_model.User ) if canChooseReviewer { @@ -575,7 +576,7 @@ func RetrieveRepoReviewers(ctx *context.Context, repo *models.Repository, issue for _, item := range pullReviews { if item.Review.ReviewerID > 0 { if err = item.Review.LoadReviewer(); err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { continue } ctx.ServerError("LoadReviewer", err) @@ -919,7 +920,7 @@ func ValidateRepoMetas(ctx *context.Context, form forms.CreateIssueForm, isPull // Check if the passed assignees actually exists and is assignable for _, aID := range assigneeIDs { - assignee, err := models.GetUserByID(aID) + assignee, err := user_model.GetUserByID(aID) if err != nil { ctx.ServerError("GetUserByID", err) return nil, nil, 0, 0 @@ -1019,7 +1020,7 @@ func NewIssuePost(ctx *context.Context) { } // roleDescriptor returns the Role Decriptor for a comment in/with the given repo, poster and issue -func roleDescriptor(repo *models.Repository, poster *models.User, issue *models.Issue) (models.RoleDescriptor, error) { +func roleDescriptor(repo *models.Repository, poster *user_model.User, issue *models.Issue) (models.RoleDescriptor, error) { perm, err := models.GetUserRepoPermission(repo, poster) if err != nil { return models.RoleDescriptorNone, err @@ -1267,7 +1268,7 @@ func ViewIssue(ctx *context.Context) { ok bool marked = make(map[int64]models.RoleDescriptor) comment *models.Comment - participants = make([]*models.User, 1, 10) + participants = make([]*user_model.User, 1, 10) ) if ctx.Repo.Repository.IsTimetrackerEnabled() { if ctx.IsSigned { @@ -1430,11 +1431,11 @@ func ViewIssue(ctx *context.Context) { continue } if err = comment.Review.LoadAttributes(); err != nil { - if !models.IsErrUserNotExist(err) { + if !user_model.IsErrUserNotExist(err) { ctx.ServerError("Review.LoadAttributes", err) return } - comment.Review.Reviewer = models.NewGhostUser() + comment.Review.Reviewer = user_model.NewGhostUser() } if err = comment.Review.LoadCodeComments(); err != nil { ctx.ServerError("Review.LoadCodeComments", err) @@ -1833,12 +1834,12 @@ func UpdateIssueAssignee(ctx *context.Context) { for _, issue := range issues { switch action { case "clear": - if err := issue_service.DeleteNotPassedAssignee(issue, ctx.User, []*models.User{}); err != nil { + if err := issue_service.DeleteNotPassedAssignee(issue, ctx.User, []*user_model.User{}); err != nil { ctx.ServerError("ClearAssignees", err) return } default: - assignee, err := models.GetUserByID(assigneeID) + assignee, err := user_model.GetUserByID(assigneeID) if err != nil { ctx.ServerError("GetUserByID", err) return @@ -1949,9 +1950,9 @@ func UpdatePullReviewRequest(ctx *context.Context) { continue } - reviewer, err := models.GetUserByID(reviewID) + reviewer, err := user_model.GetUserByID(reviewID) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { log.Warn( "UpdatePullReviewRequest: requested reviewer [%d] for %-v to %-v#%d is not exist: Error: %v", reviewID, issue.Repo, issue.Index, @@ -2452,7 +2453,7 @@ func ChangeCommentReaction(ctx *context.Context) { }) } -func addParticipant(poster *models.User, participants []*models.User) []*models.User { +func addParticipant(poster *user_model.User, participants []*user_model.User) []*user_model.User { for _, part := range participants { if poster.ID == part.ID { return participants diff --git a/routers/web/repo/lfs.go b/routers/web/repo/lfs.go index b15c7628db..b1bc06f08b 100644 --- a/routers/web/repo/lfs.go +++ b/routers/web/repo/lfs.go @@ -216,9 +216,9 @@ func LFSLockFile(ctx *context.Context) { } _, err := models.CreateLFSLock(&models.LFSLock{ - Repo: ctx.Repo.Repository, - Path: lockPath, - Owner: ctx.User, + Repo: ctx.Repo.Repository, + Path: lockPath, + OwnerID: ctx.User.ID, }) if err != nil { if models.IsErrLFSLockAlreadyExist(err) { diff --git a/routers/web/repo/middlewares.go b/routers/web/repo/middlewares.go index 0dc6e1cb72..3ee9d1a3e9 100644 --- a/routers/web/repo/middlewares.go +++ b/routers/web/repo/middlewares.go @@ -7,8 +7,8 @@ package repo import ( "fmt" - "code.gitea.io/gitea/models" admin_model "code.gitea.io/gitea/models/admin" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" ) @@ -56,7 +56,7 @@ func SetDiffViewStyle(ctx *context.Context) { } ctx.Data["IsSplitStyle"] = style == "split" - if err := models.UpdateUserDiffViewStyle(ctx.User, style); err != nil { + if err := user_model.UpdateUserDiffViewStyle(ctx.User, style); err != nil { ctx.ServerError("ErrUpdateDiffViewStyle", err) } } diff --git a/routers/web/repo/migrate.go b/routers/web/repo/migrate.go index 1940d89628..5eba4690d0 100644 --- a/routers/web/repo/migrate.go +++ b/routers/web/repo/migrate.go @@ -11,6 +11,8 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/lfs" @@ -66,7 +68,7 @@ func Migrate(ctx *context.Context) { ctx.HTML(http.StatusOK, base.TplName("repo/migrate/"+serviceType.Name())) } -func handleMigrateError(ctx *context.Context, owner *models.User, err error, name string, tpl base.TplName, form *forms.MigrateRepoForm) { +func handleMigrateError(ctx *context.Context, owner *user_model.User, err error, name string, tpl base.TplName, form *forms.MigrateRepoForm) { if setting.Repository.DisableMigrations { ctx.Error(http.StatusForbidden, "MigrateError: the site administrator has disabled migrations") return @@ -94,12 +96,12 @@ func handleMigrateError(ctx *context.Context, owner *models.User, err error, nam default: ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist"), tpl, form) } - case models.IsErrNameReserved(err): + case db.IsErrNameReserved(err): ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form) - case models.IsErrNamePatternNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tpl, form) + case db.IsErrNamePatternNotAllowed(err): ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form) + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tpl, form) default: remoteAddr, _ := forms.ParseRemoteAddr(form.CloneAddr, form.AuthUsername, form.AuthPassword) err = util.NewStringURLSanitizedError(err, remoteAddr, true) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 83e353833f..06aa86206d 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" @@ -237,10 +238,10 @@ func ForkPost(ctx *context.Context) { switch { case models.IsErrRepoAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form) - case models.IsErrNameReserved(err): - ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplFork, &form) - case models.IsErrNamePatternNotAllowed(err): - ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplFork, &form) + case db.IsErrNameReserved(err): + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tplFork, &form) + case db.IsErrNamePatternNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplFork, &form) default: ctx.ServerError("ForkPost", err) } @@ -1024,7 +1025,7 @@ func MergePullRequest(ctx *context.Context) { ctx.Redirect(issue.Link()) } -func stopTimerIfAvailable(user *models.User, issue *models.Issue) error { +func stopTimerIfAvailable(user *user_model.User, issue *models.Issue) error { if models.StopwatchExists(user.ID, issue.ID) { if err := models.CreateOrStopIssueStopwatch(user, issue); err != nil { @@ -1185,9 +1186,9 @@ func TriggerTask(ctx *context.Context) { return } - pusher, err := models.GetUserByID(pusherID) + pusher, err := user_model.GetUserByID(pusherID) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Error(http.StatusNotFound) } else { ctx.ServerError("GetUserByID", err) diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 3f12ee72bc..9bca4e6897 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -132,7 +133,7 @@ func releasesOrTags(ctx *context.Context, isTagList bool) { // Temporary cache commits count of used branches to speed up. countCache := make(map[string]int64) - cacheUsers := make(map[int64]*models.User) + cacheUsers := make(map[int64]*user_model.User) if ctx.User != nil { cacheUsers[ctx.User.ID] = ctx.User } @@ -140,10 +141,10 @@ func releasesOrTags(ctx *context.Context, isTagList bool) { for _, r := range releases { if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok { - r.Publisher, err = models.GetUserByID(r.PublisherID) + r.Publisher, err = user_model.GetUserByID(r.PublisherID) if err != nil { - if models.IsErrUserNotExist(err) { - r.Publisher = models.NewGhostUser() + if user_model.IsErrUserNotExist(err) { + r.Publisher = user_model.NewGhostUser() } else { ctx.ServerError("GetUserByID", err) return @@ -207,10 +208,10 @@ func SingleRelease(ctx *context.Context) { return } - release.Publisher, err = models.GetUserByID(release.PublisherID) + release.Publisher, err = user_model.GetUserByID(release.PublisherID) if err != nil { - if models.IsErrUserNotExist(err) { - release.Publisher = models.NewGhostUser() + if user_model.IsErrUserNotExist(err) { + release.Publisher = user_model.NewGhostUser() } else { ctx.ServerError("GetUserByID", err) return diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 9463fc4c5f..bacfa549b5 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/graceful" @@ -55,7 +56,7 @@ func MustBeAbleToUpload(ctx *context.Context) { } } -func checkContextUser(ctx *context.Context, uid int64) *models.User { +func checkContextUser(ctx *context.Context, uid int64) *user_model.User { orgs, err := models.GetOrgsCanCreateRepoByUserID(ctx.User.ID) if err != nil { ctx.ServerError("GetOrgsCanCreateRepoByUserID", err) @@ -79,8 +80,8 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User { return ctx.User } - org, err := models.GetUserByID(uid) - if models.IsErrUserNotExist(err) { + org, err := user_model.GetUserByID(uid) + if user_model.IsErrUserNotExist(err) { return ctx.User } @@ -158,7 +159,7 @@ func Create(ctx *context.Context) { ctx.HTML(http.StatusOK, tplCreate) } -func handleCreateError(ctx *context.Context, owner *models.User, err error, name string, tpl base.TplName, form interface{}) { +func handleCreateError(ctx *context.Context, owner *user_model.User, err error, name string, tpl base.TplName, form interface{}) { switch { case models.IsErrReachLimitOfRepo(err): ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form) @@ -177,12 +178,12 @@ func handleCreateError(ctx *context.Context, owner *models.User, err error, name default: ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist"), tpl, form) } - case models.IsErrNameReserved(err): + case db.IsErrNameReserved(err): ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form) - case models.IsErrNamePatternNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tpl, form) + case db.IsErrNamePatternNotAllowed(err): ctx.Data["Err_RepoName"] = true - ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form) + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tpl, form) default: ctx.ServerError(name, err) } diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index 6753ce1172..20fa9ea785 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" unit_model "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" @@ -94,8 +95,8 @@ func SettingsPost(ctx *context.Context) { switch { case models.IsErrRepoAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tplSettingsOptions, &form) - case models.IsErrNameReserved(err): - ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplSettingsOptions, &form) + case db.IsErrNameReserved(err): + ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tplSettingsOptions, &form) case models.IsErrRepoFilesAlreadyExist(err): ctx.Data["Err_RepoName"] = true switch { @@ -108,8 +109,8 @@ func SettingsPost(ctx *context.Context) { default: ctx.RenderWithErr(ctx.Tr("form.repository_files_already_exist"), tplSettingsOptions, form) } - case models.IsErrNamePatternNotAllowed(err): - ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSettingsOptions, &form) + case db.IsErrNamePatternNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplSettingsOptions, &form) default: ctx.ServerError("ChangeRepositoryName", err) } @@ -577,9 +578,9 @@ func SettingsPost(ctx *context.Context) { return } - newOwner, err := models.GetUserByName(ctx.FormString("new_owner_name")) + newOwner, err := user_model.GetUserByName(ctx.FormString("new_owner_name")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), tplSettingsOptions, nil) return } @@ -587,7 +588,7 @@ func SettingsPost(ctx *context.Context) { return } - if newOwner.Type == models.UserTypeOrganization { + if newOwner.Type == user_model.UserTypeOrganization { if !ctx.User.IsAdmin && newOwner.Visibility == structs.VisibleTypePrivate && !models.OrgFromUser(newOwner).HasMemberWithUserID(ctx.User.ID) { // The user shouldn't know about this organization ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), tplSettingsOptions, nil) @@ -798,9 +799,9 @@ func CollaborationPost(ctx *context.Context) { return } - u, err := models.GetUserByName(name) + u, err := user_model.GetUserByName(name) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Flash.Error(ctx.Tr("form.user_not_exist")) ctx.Redirect(setting.AppSubURL + ctx.Req.URL.EscapedPath()) } else { @@ -935,10 +936,10 @@ func DeleteTeam(ctx *context.Context) { } // parseOwnerAndRepo get repos by owner -func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) { - owner, err := models.GetUserByName(ctx.Params(":username")) +func parseOwnerAndRepo(ctx *context.Context) (*user_model.User, *models.Repository) { + owner, err := user_model.GetUserByName(ctx.Params(":username")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound("GetUserByName", err) } else { ctx.ServerError("GetUserByName", err) diff --git a/routers/web/repo/settings_test.go b/routers/web/repo/settings_test.go index 3e8ae2b07e..8e53babe7c 100644 --- a/routers/web/repo/settings_test.go +++ b/routers/web/repo/settings_test.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" @@ -105,9 +106,9 @@ func TestCollaborationPost(t *testing.T) { ctx.Req.Form.Set("collaborator", "user4") - u := &models.User{ + u := &user_model.User{ LowerName: "user2", - Type: models.UserTypeIndividual, + Type: user_model.UserTypeIndividual, } re := &models.Repository{ @@ -142,7 +143,7 @@ func TestCollaborationPost_InactiveUser(t *testing.T) { ctx.Req.Form.Set("collaborator", "user9") repo := &context.Repository{ - Owner: &models.User{ + Owner: &user_model.User{ LowerName: "user2", }, } @@ -165,9 +166,9 @@ func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) { ctx.Req.Form.Set("collaborator", "user4") - u := &models.User{ + u := &user_model.User{ LowerName: "user2", - Type: models.UserTypeIndividual, + Type: user_model.UserTypeIndividual, } re := &models.Repository{ @@ -207,7 +208,7 @@ func TestCollaborationPost_NonExistentUser(t *testing.T) { ctx.Req.Form.Set("collaborator", "user34") repo := &context.Repository{ - Owner: &models.User{ + Owner: &user_model.User{ LowerName: "user2", }, } @@ -226,9 +227,9 @@ func TestAddTeamPost(t *testing.T) { ctx.Req.Form.Set("team", "team11") - org := &models.User{ + org := &user_model.User{ LowerName: "org26", - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, } team := &models.Team{ @@ -243,7 +244,7 @@ func TestAddTeamPost(t *testing.T) { } repo := &context.Repository{ - Owner: &models.User{ + Owner: &user_model.User{ ID: 26, LowerName: "org26", RepoAdminChangeTeamAccess: true, @@ -266,9 +267,9 @@ func TestAddTeamPost_NotAllowed(t *testing.T) { ctx.Req.Form.Set("team", "team11") - org := &models.User{ + org := &user_model.User{ LowerName: "org26", - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, } team := &models.Team{ @@ -283,7 +284,7 @@ func TestAddTeamPost_NotAllowed(t *testing.T) { } repo := &context.Repository{ - Owner: &models.User{ + Owner: &user_model.User{ ID: 26, LowerName: "org26", RepoAdminChangeTeamAccess: false, @@ -307,9 +308,9 @@ func TestAddTeamPost_AddTeamTwice(t *testing.T) { ctx.Req.Form.Set("team", "team11") - org := &models.User{ + org := &user_model.User{ LowerName: "org26", - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, } team := &models.Team{ @@ -324,7 +325,7 @@ func TestAddTeamPost_AddTeamTwice(t *testing.T) { } repo := &context.Repository{ - Owner: &models.User{ + Owner: &user_model.User{ ID: 26, LowerName: "org26", RepoAdminChangeTeamAccess: true, @@ -348,9 +349,9 @@ func TestAddTeamPost_NonExistentTeam(t *testing.T) { ctx.Req.Form.Set("team", "team-non-existent") - org := &models.User{ + org := &user_model.User{ LowerName: "org26", - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, } re := &models.Repository{ @@ -360,7 +361,7 @@ func TestAddTeamPost_NonExistentTeam(t *testing.T) { } repo := &context.Repository{ - Owner: &models.User{ + Owner: &user_model.User{ ID: 26, LowerName: "org26", RepoAdminChangeTeamAccess: true, @@ -381,9 +382,9 @@ func TestDeleteTeam(t *testing.T) { ctx.Req.Form.Set("id", "2") - org := &models.User{ + org := &user_model.User{ LowerName: "org3", - Type: models.UserTypeOrganization, + Type: user_model.UserTypeOrganization, } team := &models.Team{ @@ -398,7 +399,7 @@ func TestDeleteTeam(t *testing.T) { } repo := &context.Repository{ - Owner: &models.User{ + Owner: &user_model.User{ ID: 3, LowerName: "org3", RepoAdminChangeTeamAccess: true, diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 1293882cc5..33420aab3b 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" unit_model "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/charset" @@ -449,7 +450,12 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st return } if lfsLock != nil { - ctx.Data["LFSLockOwner"] = lfsLock.Owner.DisplayName() + u, err := user_model.GetUserByID(lfsLock.OwnerID) + if err != nil { + ctx.ServerError("GetTreePathLock", err) + return + } + ctx.Data["LFSLockOwner"] = u.DisplayName() ctx.Data["LFSLockHint"] = ctx.Tr("repo.editor.this_file_locked") } @@ -777,7 +783,7 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri return nil } ctx.Data["LatestCommitVerification"] = verification - ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit) + ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(latestCommit) } statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), db.ListOptions{}) @@ -893,7 +899,7 @@ func renderCode(ctx *context.Context) { } // RenderUserCards render a page show users according the input template -func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*models.User, error), tpl base.TplName) { +func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) { page := ctx.FormInt("page") if page <= 0 { page = 1 @@ -928,7 +934,7 @@ func Stars(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.stargazers") ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers") ctx.Data["PageIsStargazers"] = true - RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*models.User, error) { + RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) { return models.GetStargazers(ctx.Repo.Repository, opts) }, tplWatchers) } diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 4f6660926e..28ca3e0de5 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -1149,7 +1150,7 @@ func TestWebhook(ctx *context.Context) { // Grab latest commit or fake one if it's empty repository. commit := ctx.Repo.Commit if commit == nil { - ghost := models.NewGhostUser() + ghost := user_model.NewGhostUser() commit = &git.Commit{ ID: git.MustIDFromString(git.EmptySHA), Author: ghost.NewGitSig(), diff --git a/routers/web/user/auth.go b/routers/web/user/auth.go index c5164c4956..02f5afd8ba 100644 --- a/routers/web/user/auth.go +++ b/routers/web/user/auth.go @@ -76,9 +76,9 @@ func AutoSignIn(ctx *context.Context) (bool, error) { } }() - u, err := models.GetUserByName(uname) + u, err := user_model.GetUserByName(uname) if err != nil { - if !models.IsErrUserNotExist(err) { + if !user_model.IsErrUserNotExist(err) { return false, fmt.Errorf("GetUserByName: %v", err) } return false, nil @@ -180,17 +180,17 @@ func SignInPost(ctx *context.Context) { form := web.GetForm(ctx).(*forms.SignInForm) u, source, err := auth.UserSignIn(form.UserName, form.Password) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplSignIn, &form) log.Info("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err) } else if user_model.IsErrEmailAlreadyUsed(err) { ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSignIn, &form) log.Info("Failed authentication attempt for %s from %s: %v", form.UserName, ctx.RemoteAddr(), err) - } else if models.IsErrUserProhibitLogin(err) { + } else if user_model.IsErrUserProhibitLogin(err) { log.Info("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 models.IsErrUserInactive(err) { + } else if user_model.IsErrUserInactive(err) { if setting.Service.RegisterEmailConfirm { ctx.Data["Title"] = ctx.Tr("auth.active_your_account") ctx.HTML(http.StatusOK, TplActivate) @@ -314,7 +314,7 @@ func TwoFactorPost(ctx *context.Context) { if ok && twofa.LastUsedPasscode != form.Passcode { remember := ctx.Session.Get("twofaRemember").(bool) - u, err := models.GetUserByID(id) + u, err := user_model.GetUserByID(id) if err != nil { ctx.ServerError("UserSignIn", err) return @@ -398,7 +398,7 @@ func TwoFactorScratchPost(ctx *context.Context) { } remember := ctx.Session.Get("twofaRemember").(bool) - u, err := models.GetUserByID(id) + u, err := user_model.GetUserByID(id) if err != nil { ctx.ServerError("UserSignIn", err) return @@ -495,7 +495,7 @@ func U2FSign(ctx *context.Context) { newCounter, authErr := r.Authenticate(*signResp, *challenge, reg.Counter) if authErr == nil { reg.Counter = newCounter - user, err := models.GetUserByID(id) + user, err := user_model.GetUserByID(id) if err != nil { ctx.ServerError("UserSignIn", err) return @@ -531,11 +531,11 @@ func U2FSign(ctx *context.Context) { } // This handles the final part of the sign-in process of the user. -func handleSignIn(ctx *context.Context, u *models.User, remember bool) { +func handleSignIn(ctx *context.Context, u *user_model.User, remember bool) { handleSignInFull(ctx, u, remember, true) } -func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) string { +func handleSignInFull(ctx *context.Context, u *user_model.User, remember bool, obeyRedirect bool) string { if remember { days := 86400 * setting.LogInRememberDays ctx.SetCookie(setting.CookieUserName, u.Name, days) @@ -565,7 +565,7 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR // If the user does not have a locale set, we save the current one. if len(u.Language) == 0 { u.Language = ctx.Locale.Language() - if err := models.UpdateUserCols(db.DefaultContext, u, "language"); err != nil { + if err := user_model.UpdateUserCols(db.DefaultContext, u, "language"); err != nil { log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", u.ID, u.Language)) return setting.AppSubURL + "/" } @@ -582,7 +582,7 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR // Register last login u.SetLastLogin() - if err := models.UpdateUserCols(db.DefaultContext, u, "last_login_unix"); err != nil { + if err := user_model.UpdateUserCols(db.DefaultContext, u, "last_login_unix"); err != nil { ctx.ServerError("UpdateUserCols", err) return setting.AppSubURL + "/" } @@ -680,7 +680,7 @@ func SignInOAuthCallback(ctx *context.Context) { ctx.ServerError("CreateUser", err) return } - u = &models.User{ + u = &user_model.User{ Name: getUserName(&gothUser), FullName: gothUser.Name, Email: gothUser.Email, @@ -725,7 +725,7 @@ func showLinkingLogin(ctx *context.Context, gothUser goth.User) { ctx.Redirect(setting.AppSubURL + "/user/link_account") } -func updateAvatarIfNeed(url string, u *models.User) { +func updateAvatarIfNeed(url string, u *user_model.User) { if setting.OAuth2Client.UpdateAvatar && len(url) > 0 { resp, err := http.Get(url) if err == nil { @@ -743,7 +743,7 @@ func updateAvatarIfNeed(url string, u *models.User) { } } -func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *models.User, gothUser goth.User) { +func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *user_model.User, gothUser goth.User) { updateAvatarIfNeed(gothUser.AvatarURL, u) needs2FA := false @@ -774,7 +774,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *models.Us // Register last login u.SetLastLogin() - if err := models.UpdateUserCols(db.DefaultContext, u, "last_login_unix"); err != nil { + if err := user_model.UpdateUserCols(db.DefaultContext, u, "last_login_unix"); err != nil { ctx.ServerError("UpdateUserCols", err) return } @@ -817,7 +817,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *models.Us // OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful // login the user -func oAuth2UserLoginCallback(loginSource *login.Source, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) { +func oAuth2UserLoginCallback(loginSource *login.Source, request *http.Request, response http.ResponseWriter) (*user_model.User, goth.User, error) { gothUser, err := loginSource.Cfg.(*oauth2.Source).Callback(request, response) if err != nil { if err.Error() == "securecookie: the value is too long" || strings.Contains(err.Error(), "Data too long") { @@ -827,13 +827,13 @@ func oAuth2UserLoginCallback(loginSource *login.Source, request *http.Request, r return nil, goth.User{}, err } - user := &models.User{ + user := &user_model.User{ LoginName: gothUser.UserID, LoginType: login.OAuth2, LoginSource: loginSource.ID, } - hasUser, err := models.GetUser(user) + hasUser, err := user_model.GetUser(user) if err != nil { return nil, goth.User{}, err } @@ -852,7 +852,7 @@ func oAuth2UserLoginCallback(loginSource *login.Source, request *http.Request, r return nil, goth.User{}, err } if hasUser { - user, err = models.GetUserByID(externalLoginUser.UserID) + user, err = user_model.GetUserByID(externalLoginUser.UserID) return user, gothUser, err } @@ -893,8 +893,8 @@ func LinkAccount(ctx *context.Context) { ctx.Data["email"] = email if len(email) != 0 { - u, err := models.GetUserByEmail(email) - if err != nil && !models.IsErrUserNotExist(err) { + u, err := user_model.GetUserByEmail(email) + if err != nil && !user_model.IsErrUserNotExist(err) { ctx.ServerError("UserSignIn", err) return } @@ -902,8 +902,8 @@ func LinkAccount(ctx *context.Context) { ctx.Data["user_exists"] = true } } else if len(uname) != 0 { - u, err := models.GetUserByName(uname) - if err != nil && !models.IsErrUserNotExist(err) { + u, err := user_model.GetUserByName(uname) + if err != nil && !user_model.IsErrUserNotExist(err) { ctx.ServerError("UserSignIn", err) return } @@ -948,7 +948,7 @@ func LinkAccountPostSignIn(ctx *context.Context) { u, _, err := auth.UserSignIn(signInForm.UserName, signInForm.Password) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Data["user_exists"] = true ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplLinkAccount, &signInForm) } else { @@ -960,7 +960,7 @@ func LinkAccountPostSignIn(ctx *context.Context) { linkAccount(ctx, u, gothUser.(goth.User), signInForm.Remember) } -func linkAccount(ctx *context.Context, u *models.User, gothUser goth.User, remember bool) { +func linkAccount(ctx *context.Context, u *user_model.User, gothUser goth.User, remember bool) { updateAvatarIfNeed(gothUser.AvatarURL, u) // If this user is enrolled in 2FA, we can't sign the user in just yet. @@ -1081,7 +1081,7 @@ func LinkAccountPostRegister(ctx *context.Context) { } if setting.Service.AllowOnlyExternalRegistration || !setting.Service.RequireExternalRegistrationPassword { - // In models.User an empty password is classed as not set, so we set form.Password to empty. + // In user_model.User an empty password is classed as not set, so we set form.Password to empty. // Eventually the database should be changed to indicate "Second Factor"-enabled accounts // (accounts that do not introduce the security vulnerabilities of a password). // If a user decides to circumvent second-factor security, and purposefully create a password, @@ -1105,7 +1105,7 @@ func LinkAccountPostRegister(ctx *context.Context) { ctx.ServerError("CreateUser", err) } - u := &models.User{ + u := &user_model.User{ Name: form.UserName, Email: form.Email, Passwd: form.Password, @@ -1249,7 +1249,7 @@ func SignUpPost(ctx *context.Context) { return } - u := &models.User{ + u := &user_model.User{ Name: form.UserName, Email: form.Email, Passwd: form.Password, @@ -1268,7 +1268,7 @@ func SignUpPost(ctx *context.Context) { // createAndHandleCreatedUser calls createUserInContext and // then handleUserCreated. -func createAndHandleCreatedUser(ctx *context.Context, tpl base.TplName, form interface{}, u *models.User, gothUser *goth.User, allowLink bool) bool { +func createAndHandleCreatedUser(ctx *context.Context, tpl base.TplName, form interface{}, u *user_model.User, gothUser *goth.User, allowLink bool) bool { if !createUserInContext(ctx, tpl, form, u, gothUser, allowLink) { return false } @@ -1277,16 +1277,16 @@ func createAndHandleCreatedUser(ctx *context.Context, tpl base.TplName, form int // createUserInContext creates a user and handles errors within a given context. // Optionally a template can be specified. -func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{}, u *models.User, gothUser *goth.User, allowLink bool) (ok bool) { - if err := models.CreateUser(u); err != nil { - if allowLink && (models.IsErrUserAlreadyExist(err) || user_model.IsErrEmailAlreadyUsed(err)) { +func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{}, u *user_model.User, gothUser *goth.User, allowLink bool) (ok bool) { + if err := user_model.CreateUser(u); err != nil { + if allowLink && (user_model.IsErrUserAlreadyExist(err) || user_model.IsErrEmailAlreadyUsed(err)) { if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingAuto { - var user *models.User - user = &models.User{Name: u.Name} - hasUser, err := models.GetUser(user) + var user *user_model.User + user = &user_model.User{Name: u.Name} + hasUser, err := user_model.GetUser(user) if !hasUser || err != nil { - user = &models.User{Email: u.Email} - hasUser, err = models.GetUser(user) + user = &user_model.User{Email: u.Email} + hasUser, err = user_model.GetUser(user) if !hasUser || err != nil { ctx.ServerError("UserLinkAccount", err) return @@ -1310,7 +1310,7 @@ func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{ // handle error with template switch { - case models.IsErrUserAlreadyExist(err): + case user_model.IsErrUserAlreadyExist(err): ctx.Data["Err_UserName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tpl, form) case user_model.IsErrEmailAlreadyUsed(err): @@ -1319,15 +1319,15 @@ func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{ case user_model.IsErrEmailInvalid(err): ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tpl, form) - case models.IsErrNameReserved(err): + case db.IsErrNameReserved(err): ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form) - case models.IsErrNamePatternNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(db.ErrNameReserved).Name), tpl, form) + case db.IsErrNamePatternNotAllowed(err): ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form) - case models.IsErrNameCharsNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tpl, form) + case db.IsErrNameCharsNotAllowed(err): ctx.Data["Err_UserName"] = true - ctx.RenderWithErr(ctx.Tr("user.form.name_chars_not_allowed", err.(models.ErrNameCharsNotAllowed).Name), tpl, form) + ctx.RenderWithErr(ctx.Tr("user.form.name_chars_not_allowed", err.(db.ErrNameCharsNotAllowed).Name), tpl, form) default: ctx.ServerError("CreateUser", err) } @@ -1340,13 +1340,13 @@ func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{ // handleUserCreated does additional steps after a new user is created. // It auto-sets admin for the only user, updates the optional external user and // sends a confirmation email if required. -func handleUserCreated(ctx *context.Context, u *models.User, gothUser *goth.User) (ok bool) { +func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.User) (ok bool) { // Auto-set admin for the only user. - if models.CountUsers() == 1 { + if user_model.CountUsers() == 1 { u.IsAdmin = true u.IsActive = true u.SetLastLogin() - if err := models.UpdateUserCols(db.DefaultContext, u, "is_admin", "is_active", "last_login_unix"); err != nil { + if err := user_model.UpdateUserCols(db.DefaultContext, u, "is_admin", "is_active", "last_login_unix"); err != nil { ctx.ServerError("UpdateUser", err) return } @@ -1406,7 +1406,7 @@ func Activate(ctx *context.Context) { return } - user := models.VerifyUserActiveCode(code) + user := user_model.VerifyUserActiveCode(code) // if code is wrong if user == nil { ctx.Data["IsActivateFailed"] = true @@ -1433,7 +1433,7 @@ func ActivatePost(ctx *context.Context) { return } - user := models.VerifyUserActiveCode(code) + user := user_model.VerifyUserActiveCode(code) // if code is wrong if user == nil { ctx.Data["IsActivateFailed"] = true @@ -1460,15 +1460,15 @@ func ActivatePost(ctx *context.Context) { handleAccountActivation(ctx, user) } -func handleAccountActivation(ctx *context.Context, user *models.User) { +func handleAccountActivation(ctx *context.Context, user *user_model.User) { user.IsActive = true var err error - if user.Rands, err = models.GetUserSalt(); err != nil { + if user.Rands, err = user_model.GetUserSalt(); err != nil { ctx.ServerError("UpdateUser", err) return } - if err := models.UpdateUserCols(db.DefaultContext, user, "is_active", "rands"); err != nil { - if models.IsErrUserNotExist(err) { + if err := user_model.UpdateUserCols(db.DefaultContext, user, "is_active", "rands"); err != nil { + if user_model.IsErrUserNotExist(err) { ctx.NotFound("UpdateUserCols", err) } else { ctx.ServerError("UpdateUser", err) @@ -1512,7 +1512,7 @@ func ActivateEmail(ctx *context.Context) { log.Trace("Email activated: %s", email.Email) ctx.Flash.Success(ctx.Tr("settings.add_email_success")) - if u, err := models.GetUserByID(email.UID); err != nil { + if u, err := user_model.GetUserByID(email.UID); err != nil { log.Warn("GetUserByID: %d", email.UID) } else { // Allow user to validate more emails @@ -1556,9 +1556,9 @@ func ForgotPasswdPost(ctx *context.Context) { email := ctx.FormString("email") ctx.Data["Email"] = email - u, err := models.GetUserByEmail(email) + u, err := user_model.GetUserByEmail(email) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.Data["ResetPwdCodeLives"] = timeutil.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language()) ctx.Data["IsResetSent"] = true ctx.HTML(http.StatusOK, tplForgotPassword) @@ -1592,7 +1592,7 @@ func ForgotPasswdPost(ctx *context.Context) { ctx.HTML(http.StatusOK, tplForgotPassword) } -func commonResetPassword(ctx *context.Context) (*models.User, *login.TwoFactor) { +func commonResetPassword(ctx *context.Context) (*user_model.User, *login.TwoFactor) { code := ctx.FormString("code") ctx.Data["Title"] = ctx.Tr("auth.reset_password") @@ -1608,7 +1608,7 @@ func commonResetPassword(ctx *context.Context) (*models.User, *login.TwoFactor) } // Fail early, don't frustrate the user - u := models.VerifyUserActiveCode(code) + u := user_model.VerifyUserActiveCode(code) if u == nil { ctx.Flash.Error(ctx.Tr("auth.invalid_code")) return nil, nil @@ -1718,7 +1718,7 @@ func ResetPasswdPost(ctx *context.Context) { } } var err error - if u.Rands, err = models.GetUserSalt(); err != nil { + if u.Rands, err = user_model.GetUserSalt(); err != nil { ctx.ServerError("UpdateUser", err) return } @@ -1727,7 +1727,7 @@ func ResetPasswdPost(ctx *context.Context) { return } u.MustChangePassword = false - if err := models.UpdateUserCols(db.DefaultContext, u, "must_change_password", "passwd", "passwd_hash_algo", "rands", "salt"); err != nil { + if err := user_model.UpdateUserCols(db.DefaultContext, u, "must_change_password", "passwd", "passwd_hash_algo", "rands", "salt"); err != nil { ctx.ServerError("UpdateUser", err) return } @@ -1803,7 +1803,7 @@ func MustChangePasswordPost(ctx *context.Context) { u.MustChangePassword = false - if err := models.UpdateUserCols(db.DefaultContext, u, "must_change_password", "passwd", "passwd_hash_algo", "salt"); err != nil { + if err := user_model.UpdateUserCols(db.DefaultContext, u, "must_change_password", "passwd", "passwd_hash_algo", "salt"); err != nil { ctx.ServerError("UpdateUser", err) return } diff --git a/routers/web/user/auth_openid.go b/routers/web/user/auth_openid.go index 4724a7b431..68e166d12c 100644 --- a/routers/web/user/auth_openid.go +++ b/routers/web/user/auth_openid.go @@ -9,7 +9,6 @@ import ( "net/http" "net/url" - "code.gitea.io/gitea/models" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/auth/openid" "code.gitea.io/gitea/modules/base" @@ -163,9 +162,9 @@ func signInOpenIDVerify(ctx *context.Context) { /* Now we should seek for the user and log him in, or prompt * to register if not found */ - u, err := models.GetUserByOpenID(id) + u, err := user_model.GetUserByOpenID(id) if err != nil { - if !models.IsErrUserNotExist(err) { + if !user_model.IsErrUserNotExist(err) { ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{ Openid: id, }) @@ -203,9 +202,9 @@ func signInOpenIDVerify(ctx *context.Context) { log.Trace("User has email=" + email + " and nickname=" + nickname) if email != "" { - u, err = models.GetUserByEmail(email) + u, err = user_model.GetUserByEmail(email) if err != nil { - if !models.IsErrUserNotExist(err) { + if !user_model.IsErrUserNotExist(err) { ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{ Openid: id, }) @@ -219,9 +218,9 @@ func signInOpenIDVerify(ctx *context.Context) { } if u == nil && nickname != "" { - u, _ = models.GetUserByName(nickname) + u, _ = user_model.GetUserByName(nickname) if err != nil { - if !models.IsErrUserNotExist(err) { + if !user_model.IsErrUserNotExist(err) { ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{ Openid: id, }) @@ -294,7 +293,7 @@ func ConnectOpenIDPost(ctx *context.Context) { u, _, err := auth.UserSignIn(form.UserName, form.Password) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplConnectOID, &form) } else { ctx.ServerError("ConnectOpenIDPost", err) @@ -419,7 +418,7 @@ func RegisterOpenIDPost(ctx *context.Context) { return } - u := &models.User{ + u := &user_model.User{ Name: form.UserName, Email: form.Email, Passwd: password, diff --git a/routers/web/user/avatar.go b/routers/web/user/avatar.go index f39bcc36d3..c8bca9dc2c 100644 --- a/routers/web/user/avatar.go +++ b/routers/web/user/avatar.go @@ -8,8 +8,8 @@ import ( "strings" "time" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/avatars" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/httpcache" ) @@ -27,15 +27,15 @@ func AvatarByUserName(ctx *context.Context) { userName := ctx.Params(":username") size := int(ctx.ParamsInt64(":size")) - var user *models.User + var user *user_model.User if strings.ToLower(userName) != "ghost" { var err error - if user, err = models.GetUserByName(userName); err != nil { + if user, err = user_model.GetUserByName(userName); err != nil { ctx.ServerError("Invalid user: "+userName, err) return } } else { - user = models.NewGhostUser() + user = user_model.NewGhostUser() } cacheableRedirect(ctx, user.AvatarLinkWithSize(size)) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 9efbe755d9..ca2705a42f 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" issue_indexer "code.gitea.io/gitea/modules/indexer/issues" @@ -43,7 +44,7 @@ const ( ) // getDashboardContextUser finds out which context user dashboard is being viewed as . -func getDashboardContextUser(ctx *context.Context) *models.User { +func getDashboardContextUser(ctx *context.Context) *user_model.User { ctxUser := ctx.User orgName := ctx.Params(":org") if len(orgName) > 0 { @@ -727,7 +728,7 @@ func getRepoIDs(reposQuery string) []int64 { return repoIDs } -func getActiveUserRepoIDs(ctxUser *models.User, team *models.Team, unitType unit.Type) ([]int64, error) { +func getActiveUserRepoIDs(ctxUser *user_model.User, team *models.Team, unitType unit.Type) ([]int64, error) { var userRepoIDs []int64 var err error @@ -737,7 +738,7 @@ func getActiveUserRepoIDs(ctxUser *models.User, team *models.Team, unitType unit return nil, fmt.Errorf("orgRepoIds: %v", err) } } else { - userRepoIDs, err = ctxUser.GetActiveAccessRepoIDs(unitType) + userRepoIDs, err = models.GetActiveAccessRepoIDs(ctxUser, unitType) if err != nil { return nil, fmt.Errorf("ctxUser.GetAccessRepoIDs: %v", err) } @@ -752,7 +753,7 @@ func getActiveUserRepoIDs(ctxUser *models.User, team *models.Team, unitType unit // getActiveTeamOrOrgRepoIds gets RepoIDs for ctxUser as Organization. // Should be called if and only if ctxUser.IsOrganization == true. -func getActiveTeamOrOrgRepoIds(ctxUser *models.User, team *models.Team, unitType unit.Type) ([]int64, error) { +func getActiveTeamOrOrgRepoIds(ctxUser *user_model.User, team *models.Team, unitType unit.Type) ([]int64, error) { var orgRepoIDs []int64 var err error var env models.AccessibleReposEnvironment @@ -777,7 +778,7 @@ func getActiveTeamOrOrgRepoIds(ctxUser *models.User, team *models.Team, unitType return orgRepoIDs, nil } -func issueIDsFromSearch(ctxUser *models.User, keyword string, opts *models.IssuesOptions) ([]int64, error) { +func issueIDsFromSearch(ctxUser *user_model.User, keyword string, opts *models.IssuesOptions) ([]int64, error) { if len(keyword) == 0 { return []int64{}, nil } @@ -794,7 +795,7 @@ func issueIDsFromSearch(ctxUser *models.User, keyword string, opts *models.Issue return issueIDsFromSearch, nil } -func repoIDMap(ctxUser *models.User, issueCountByRepo map[int64]int64, unitType unit.Type) (map[int64]*models.Repository, error) { +func repoIDMap(ctxUser *user_model.User, issueCountByRepo map[int64]int64, unitType unit.Type) (map[int64]*models.Repository, error) { repoByID := make(map[int64]*models.Repository, len(issueCountByRepo)) for id := range issueCountByRepo { if id <= 0 { @@ -880,9 +881,9 @@ func ShowGPGKeys(ctx *context.Context, uid int64) { // Email2User show user page via email func Email2User(ctx *context.Context) { - u, err := models.GetUserByEmail(ctx.FormString("email")) + u, err := user_model.GetUserByEmail(ctx.FormString("email")) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { ctx.NotFound("GetUserByEmail", err) } else { ctx.ServerError("GetUserByEmail", err) diff --git a/routers/web/user/oauth.go b/routers/web/user/oauth.go index 7b1854c521..d3baeaedc4 100644 --- a/routers/web/user/oauth.go +++ b/routers/web/user/oauth.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/login" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/json" @@ -170,9 +171,9 @@ func newAccessTokenResponse(grant *login.OAuth2Grant, serverKey, clientKey oauth ErrorDescription: "cannot find application", } } - user, err := models.GetUserByID(grant.UserID) + user, err := user_model.GetUserByID(grant.UserID) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { return nil, &AccessTokenError{ ErrorCode: AccessTokenErrorCodeInvalidRequest, ErrorDescription: "cannot find user", @@ -274,7 +275,7 @@ func InfoOAuth(ctx *context.Context) { // returns a list of "org" and "org:team" strings, // that the given user is a part of. -func getOAuthGroupsForUser(user *models.User) ([]string, error) { +func getOAuthGroupsForUser(user *user_model.User) ([]string, error) { orgs, err := models.GetUserOrgsList(user) if err != nil { return nil, fmt.Errorf("GetUserOrgList: %v", err) @@ -359,7 +360,7 @@ func AuthorizeOAuth(ctx *context.Context) { return } - user, err := models.GetUserByID(app.UID) + user, err := user_model.GetUserByID(app.UID) if err != nil { ctx.ServerError("GetUserByID", err) return diff --git a/routers/web/user/oauth_test.go b/routers/web/user/oauth_test.go index dfdaa9a1ed..e26a42b2fc 100644 --- a/routers/web/user/oauth_test.go +++ b/routers/web/user/oauth_test.go @@ -7,9 +7,9 @@ package user import ( "testing" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/login" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/services/auth/source/oauth2" "github.com/golang-jwt/jwt" @@ -58,7 +58,7 @@ func TestNewAccessTokenResponse_OIDCToken(t *testing.T) { assert.Empty(t, oidcToken.Email) assert.False(t, oidcToken.EmailVerified) - user := unittest.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User) + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}).(*user_model.User) grants, err = login.GetOAuth2GrantsByUserID(user.ID) assert.NoError(t, err) assert.Len(t, grants, 1) diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index 2594ba3013..32d2bdde83 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -24,10 +24,10 @@ import ( ) // GetUserByName get user by name -func GetUserByName(ctx *context.Context, name string) *models.User { - user, err := models.GetUserByName(name) +func GetUserByName(ctx *context.Context, name string) *user_model.User { + user, err := user_model.GetUserByName(name) if err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { if redirectUserID, err := user_model.LookupUserRedirect(name); err == nil { context.RedirectToUser(ctx, name, redirectUserID) } else { @@ -42,7 +42,7 @@ func GetUserByName(ctx *context.Context, name string) *models.User { } // GetUserByParams returns user whose name is presented in URL paramenter. -func GetUserByParams(ctx *context.Context) *models.User { +func GetUserByParams(ctx *context.Context) *user_model.User { return GetUserByName(ctx, ctx.Params(":username")) } @@ -199,41 +199,41 @@ func Profile(ctx *context.Context) { repos []*models.Repository count int64 total int - orderBy models.SearchOrderBy + orderBy db.SearchOrderBy ) ctx.Data["SortType"] = ctx.FormString("sort") switch ctx.FormString("sort") { case "newest": - orderBy = models.SearchOrderByNewest + orderBy = db.SearchOrderByNewest case "oldest": - orderBy = models.SearchOrderByOldest + orderBy = db.SearchOrderByOldest case "recentupdate": - orderBy = models.SearchOrderByRecentUpdated + orderBy = db.SearchOrderByRecentUpdated case "leastupdate": - orderBy = models.SearchOrderByLeastUpdated + orderBy = db.SearchOrderByLeastUpdated case "reversealphabetically": - orderBy = models.SearchOrderByAlphabeticallyReverse + orderBy = db.SearchOrderByAlphabeticallyReverse case "alphabetically": - orderBy = models.SearchOrderByAlphabetically + orderBy = db.SearchOrderByAlphabetically case "moststars": - orderBy = models.SearchOrderByStarsReverse + orderBy = db.SearchOrderByStarsReverse case "feweststars": - orderBy = models.SearchOrderByStars + orderBy = db.SearchOrderByStars case "mostforks": - orderBy = models.SearchOrderByForksReverse + orderBy = db.SearchOrderByForksReverse case "fewestforks": - orderBy = models.SearchOrderByForks + orderBy = db.SearchOrderByForks default: ctx.Data["SortType"] = "recentupdate" - orderBy = models.SearchOrderByRecentUpdated + orderBy = db.SearchOrderByRecentUpdated } keyword := ctx.FormTrim("q") ctx.Data["Keyword"] = keyword switch tab { case "followers": - items, err := models.GetUserFollowers(ctxUser, db.ListOptions{ + items, err := user_model.GetUserFollowers(ctxUser, db.ListOptions{ PageSize: setting.UI.User.RepoPagingNum, Page: page, }) @@ -245,7 +245,7 @@ func Profile(ctx *context.Context) { total = ctxUser.NumFollowers case "following": - items, err := models.GetUserFollowing(ctxUser, db.ListOptions{ + items, err := user_model.GetUserFollowing(ctxUser, db.ListOptions{ PageSize: setting.UI.User.RepoPagingNum, Page: page, }) diff --git a/routers/web/user/setting/account.go b/routers/web/user/setting/account.go index 603786697a..3b51fdb777 100644 --- a/routers/web/user/setting/account.go +++ b/routers/web/user/setting/account.go @@ -75,7 +75,7 @@ func AccountPost(ctx *context.Context) { ctx.ServerError("UpdateUser", err) return } - if err := models.UpdateUserCols(db.DefaultContext, ctx.User, "salt", "passwd_hash_algo", "passwd"); err != nil { + if err := user_model.UpdateUserCols(db.DefaultContext, ctx.User, "salt", "passwd_hash_algo", "passwd"); err != nil { ctx.ServerError("UpdateUser", err) return } @@ -152,14 +152,14 @@ func EmailPost(ctx *context.Context) { // Set Email Notification Preference if ctx.FormString("_method") == "NOTIFICATION" { preference := ctx.FormString("preference") - if !(preference == models.EmailNotificationsEnabled || - preference == models.EmailNotificationsOnMention || - preference == models.EmailNotificationsDisabled) { + if !(preference == user_model.EmailNotificationsEnabled || + preference == user_model.EmailNotificationsOnMention || + preference == user_model.EmailNotificationsDisabled) { log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.User.Name) ctx.ServerError("SetEmailPreference", errors.New("option unrecognized")) return } - if err := models.SetEmailNotifications(ctx.User, preference); err != nil { + if err := user_model.SetEmailNotifications(ctx.User, preference); err != nil { log.Error("Set Email Notifications failed: %v", err) ctx.ServerError("SetEmailNotifications", err) return @@ -233,7 +233,7 @@ func DeleteAccount(ctx *context.Context) { ctx.Data["PageIsSettingsAccount"] = true if _, _, err := auth.UserSignIn(ctx.User.Name, ctx.FormString("password")); err != nil { - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { loadAccountData(ctx) ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsAccount, nil) diff --git a/routers/web/user/setting/adopt.go b/routers/web/user/setting/adopt.go index 948cc1e936..56ab6ec999 100644 --- a/routers/web/user/setting/adopt.go +++ b/routers/web/user/setting/adopt.go @@ -8,6 +8,7 @@ import ( "path/filepath" "code.gitea.io/gitea/models" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -27,7 +28,7 @@ func AdoptOrDeleteRepository(ctx *context.Context) { action := ctx.FormString("action") ctxUser := ctx.User - root := models.UserPath(ctxUser.LowerName) + root := user_model.UserPath(ctxUser.LowerName) // check not a repo has, err := models.IsRepositoryExist(ctxUser, dir) diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index 1e515f2fd3..0475398a9a 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -49,7 +49,7 @@ func Profile(ctx *context.Context) { } // HandleUsernameChange handle username changes from user settings and admin interface -func HandleUsernameChange(ctx *context.Context, user *models.User, newName string) error { +func HandleUsernameChange(ctx *context.Context, user *user_model.User, newName string) error { // Non-local users are not allowed to change their username. if !user.IsLocal() { ctx.Flash.Error(ctx.Tr("form.username_change_not_local_user")) @@ -58,17 +58,17 @@ func HandleUsernameChange(ctx *context.Context, user *models.User, newName strin // Check if user name has been changed if user.LowerName != strings.ToLower(newName) { - if err := models.ChangeUserName(user, newName); err != nil { + if err := user_model.ChangeUserName(user, newName); err != nil { switch { - case models.IsErrUserAlreadyExist(err): + case user_model.IsErrUserAlreadyExist(err): ctx.Flash.Error(ctx.Tr("form.username_been_taken")) case user_model.IsErrEmailAlreadyUsed(err): ctx.Flash.Error(ctx.Tr("form.email_been_used")) - case models.IsErrNameReserved(err): + case db.IsErrNameReserved(err): ctx.Flash.Error(ctx.Tr("user.form.name_reserved", newName)) - case models.IsErrNamePatternNotAllowed(err): + case db.IsErrNamePatternNotAllowed(err): ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed", newName)) - case models.IsErrNameCharsNotAllowed(err): + case db.IsErrNameCharsNotAllowed(err): ctx.Flash.Error(ctx.Tr("user.form.name_chars_not_allowed", newName)) default: ctx.ServerError("ChangeUserName", err) @@ -121,7 +121,7 @@ func ProfilePost(ctx *context.Context) { ctx.User.Description = form.Description ctx.User.KeepActivityPrivate = form.KeepActivityPrivate ctx.User.Visibility = form.Visibility - if err := models.UpdateUserSetting(ctx.User); err != nil { + if err := user_model.UpdateUserSetting(ctx.User); err != nil { if _, ok := err.(user_model.ErrEmailAlreadyUsed); ok { ctx.Flash.Error(ctx.Tr("form.email_been_used")) ctx.Redirect(setting.AppSubURL + "/user/settings") @@ -141,7 +141,7 @@ func ProfilePost(ctx *context.Context) { // UpdateAvatarSetting update user's avatar // FIXME: limit size. -func UpdateAvatarSetting(ctx *context.Context, form *forms.AvatarForm, ctxUser *models.User) error { +func UpdateAvatarSetting(ctx *context.Context, form *forms.AvatarForm, ctxUser *user_model.User) error { ctxUser.UseCustomAvatar = form.Source == forms.AvatarLocal if len(form.Gravatar) > 0 { if form.Avatar != nil { @@ -178,12 +178,12 @@ func UpdateAvatarSetting(ctx *context.Context, form *forms.AvatarForm, ctxUser * } else if ctxUser.UseCustomAvatar && ctxUser.Avatar == "" { // No avatar is uploaded but setting has been changed to enable, // generate a random one when needed. - if err := models.GenerateRandomAvatar(ctxUser); err != nil { + if err := user_model.GenerateRandomAvatar(ctxUser); err != nil { log.Error("GenerateRandomAvatar[%d]: %v", ctxUser.ID, err) } } - if err := models.UpdateUserCols(db.DefaultContext, ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil { + if err := user_model.UpdateUserCols(db.DefaultContext, ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil { return fmt.Errorf("UpdateUser: %v", err) } @@ -273,7 +273,7 @@ func Repos(ctx *context.Context) { repoNames := make([]string, 0, setting.UI.Admin.UserPagingNum) repos := map[string]*models.Repository{} // We're going to iterate by pagesize. - root := models.UserPath(ctxUser.Name) + root := user_model.UserPath(ctxUser.Name) if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { if os.IsNotExist(err) { @@ -377,7 +377,7 @@ func UpdateUIThemePost(ctx *context.Context) { return } - if err := models.UpdateUserTheme(ctx.User, form.Theme); err != nil { + if err := user_model.UpdateUserTheme(ctx.User, form.Theme); err != nil { ctx.Flash.Error(ctx.Tr("settings.theme_update_error")) ctx.Redirect(setting.AppSubURL + "/user/settings/appearance") return @@ -403,7 +403,7 @@ func UpdateUserLang(ctx *context.Context) { ctx.User.Language = form.Language } - if err := models.UpdateUserSetting(ctx.User); err != nil { + if err := user_model.UpdateUserSetting(ctx.User); err != nil { ctx.ServerError("UpdateUserSetting", err) return } |