Move login related structs and functions to models/login (#17093)

* Move login related structs and functions to models/login

* Fix test

* Fix lint

* Fix lint

* Fix lint of windows

* Fix lint

* Fix test

* Fix test

* Only load necessary fixtures when preparing unit tests envs

* Fix lint

* Fix test

* Fix test

* Fix error log

* Fix error log

* Fix error log

* remove unnecessary change

* fix error log

* merge main branch
tokarchuk/v1.17
Lunny Xiao 3 years ago committed by GitHub
parent 4a2655098f
commit 5842a55b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .golangci.yml
  2. 21
      cmd/admin.go
  3. 32
      cmd/admin_auth_ldap.go
  4. 242
      cmd/admin_auth_ldap_test.go
  5. 4
      contrib/fixtures/fixture_generation.go
  6. 4
      contrib/pr/checkout.go
  7. 27
      integrations/api_oauth2_apps_test.go
  8. 4
      integrations/integration_test.go
  9. 2
      models/access.go
  10. 14
      models/commit_status.go
  11. 2
      models/commit_status_test.go
  12. 22
      models/db/list_options.go
  13. 2
      models/db/list_options_test.go
  14. 9
      models/db/test_fixtures.go
  15. 24
      models/db/unit_tests.go
  16. 91
      models/error.go
  17. 3
      models/external_login_user.go
  18. 6
      models/gpg_key.go
  19. 3
      models/gpg_key_commit_verification.go
  20. 2
      models/issue.go
  21. 4
      models/issue_comment.go
  22. 12
      models/issue_label.go
  23. 8
      models/issue_label_test.go
  24. 4
      models/issue_milestone.go
  25. 4
      models/issue_milestone_test.go
  26. 6
      models/issue_reaction.go
  27. 4
      models/issue_stopwatch.go
  28. 6
      models/issue_test.go
  29. 4
      models/issue_tracked_time.go
  30. 6
      models/issue_watch.go
  31. 8
      models/issue_watch_test.go
  32. 21
      models/login/main_test.go
  33. 70
      models/login/oauth2.go
  34. 31
      models/login/oauth2_application.go
  35. 10
      models/login/oauth2_application_test.go
  36. 255
      models/login/source.go
  37. 10
      models/login/source_test.go
  38. 5
      models/migrations/migrations_test.go
  39. 4
      models/notification.go
  40. 27
      models/oauth2.go
  41. 6
      models/org.go
  42. 8
      models/org_team.go
  43. 4
      models/org_test.go
  44. 4
      models/pull_list.go
  45. 3
      models/pull_sign.go
  46. 4
      models/pull_test.go
  47. 4
      models/release.go
  48. 6
      models/repo.go
  49. 8
      models/repo_collaboration.go
  50. 2
      models/repo_collaboration_test.go
  51. 2
      models/repo_generate.go
  52. 2
      models/repo_list.go
  53. 68
      models/repo_list_test.go
  54. 7
      models/repo_sign.go
  55. 2
      models/repo_transfer.go
  56. 3
      models/repo_unit.go
  57. 4
      models/repo_watch.go
  58. 20
      models/repo_watch_test.go
  59. 4
      models/review.go
  60. 27
      models/ssh_key.go
  61. 4
      models/ssh_key_deploy.go
  62. 4
      models/ssh_key_principals.go
  63. 4
      models/star.go
  64. 4
      models/star_test.go
  65. 7
      models/statistic.go
  66. 4
      models/token.go
  67. 4
      models/topic.go
  68. 2
      models/topic_test.go
  69. 35
      models/user.go
  70. 4
      models/user_mail.go
  71. 4
      models/user_mail_test.go
  72. 31
      models/user_test.go
  73. 4
      models/webhook.go
  74. 5
      modules/convert/convert.go
  75. 3
      modules/gitgraph/graph_models.go
  76. 3
      modules/indexer/issues/indexer.go
  77. 6
      modules/migrations/gitea_uploader_test.go
  78. 35
      modules/repository/adopt.go
  79. 6
      modules/repository/repo.go
  80. 9
      routers/api/v1/admin/user.go
  81. 7
      routers/api/v1/repo/issue.go
  82. 17
      routers/api/v1/user/app.go
  83. 3
      routers/api/v1/user/gpg_key.go
  84. 3
      routers/api/v1/user/star.go
  85. 3
      routers/api/v1/user/watch.go
  86. 6
      routers/api/v1/utils/utils.go
  87. 82
      routers/web/admin/auths.go
  88. 3
      routers/web/admin/emails.go
  89. 3
      routers/web/admin/orgs.go
  90. 3
      routers/web/admin/repos.go
  91. 30
      routers/web/admin/users.go
  92. 3
      routers/web/events/events.go
  93. 3
      routers/web/explore/org.go
  94. 3
      routers/web/explore/repo.go
  95. 3
      routers/web/explore/user.go
  96. 5
      routers/web/org/home.go
  97. 2
      routers/web/org/org_labels.go
  98. 3
      routers/web/org/setting.go
  99. 3
      routers/web/repo/commit.go
  100. 19
      routers/web/repo/issue.go
  101. Some files were not shown because too many files have changed in this diff Show More

@ -111,4 +111,3 @@ issues:
linters: linters:
- staticcheck - staticcheck
text: "svc.IsAnInteractiveSession is deprecated: Use IsWindowsService instead." text: "svc.IsAnInteractiveSession is deprecated: Use IsWindowsService instead."

@ -14,6 +14,8 @@ import (
"text/tabwriter" "text/tabwriter"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -21,6 +23,7 @@ import (
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/storage"
auth_service "code.gitea.io/gitea/services/auth"
"code.gitea.io/gitea/services/auth/source/oauth2" "code.gitea.io/gitea/services/auth/source/oauth2"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -529,7 +532,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
log.Trace("Synchronizing repository releases (this may take a while)") log.Trace("Synchronizing repository releases (this may take a while)")
for page := 1; ; page++ { for page := 1; ; page++ {
repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{ repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
PageSize: models.RepositoryListDefaultPageSize, PageSize: models.RepositoryListDefaultPageSize,
Page: page, Page: page,
}, },
@ -629,8 +632,8 @@ func runAddOauth(c *cli.Context) error {
return err return err
} }
return models.CreateLoginSource(&models.LoginSource{ return login.CreateSource(&login.Source{
Type: models.LoginOAuth2, Type: login.OAuth2,
Name: c.String("name"), Name: c.String("name"),
IsActive: true, IsActive: true,
Cfg: parseOAuth2Config(c), Cfg: parseOAuth2Config(c),
@ -646,7 +649,7 @@ func runUpdateOauth(c *cli.Context) error {
return err return err
} }
source, err := models.GetLoginSourceByID(c.Int64("id")) source, err := login.GetSourceByID(c.Int64("id"))
if err != nil { if err != nil {
return err return err
} }
@ -705,7 +708,7 @@ func runUpdateOauth(c *cli.Context) error {
oAuth2Config.CustomURLMapping = customURLMapping oAuth2Config.CustomURLMapping = customURLMapping
source.Cfg = oAuth2Config source.Cfg = oAuth2Config
return models.UpdateSource(source) return login.UpdateSource(source)
} }
func runListAuth(c *cli.Context) error { func runListAuth(c *cli.Context) error {
@ -713,7 +716,7 @@ func runListAuth(c *cli.Context) error {
return err return err
} }
loginSources, err := models.LoginSources() loginSources, err := login.Sources()
if err != nil { if err != nil {
return err return err
@ -733,7 +736,7 @@ func runListAuth(c *cli.Context) error {
w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags) w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
fmt.Fprintf(w, "ID\tName\tType\tEnabled\n") fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
for _, source := range loginSources { for _, source := range loginSources {
fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, models.LoginNames[source.Type], source.IsActive) fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive)
} }
w.Flush() w.Flush()
@ -749,10 +752,10 @@ func runDeleteAuth(c *cli.Context) error {
return err return err
} }
source, err := models.GetLoginSourceByID(c.Int64("id")) source, err := login.GetSourceByID(c.Int64("id"))
if err != nil { if err != nil {
return err return err
} }
return models.DeleteSource(source) return auth_service.DeleteLoginSource(source)
} }

@ -8,7 +8,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/services/auth/source/ldap" "code.gitea.io/gitea/services/auth/source/ldap"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -17,9 +17,9 @@ import (
type ( type (
authService struct { authService struct {
initDB func() error initDB func() error
createLoginSource func(loginSource *models.LoginSource) error createLoginSource func(loginSource *login.Source) error
updateLoginSource func(loginSource *models.LoginSource) error updateLoginSource func(loginSource *login.Source) error
getLoginSourceByID func(id int64) (*models.LoginSource, error) getLoginSourceByID func(id int64) (*login.Source, error)
} }
) )
@ -164,14 +164,14 @@ var (
func newAuthService() *authService { func newAuthService() *authService {
return &authService{ return &authService{
initDB: initDB, initDB: initDB,
createLoginSource: models.CreateLoginSource, createLoginSource: login.CreateSource,
updateLoginSource: models.UpdateSource, updateLoginSource: login.UpdateSource,
getLoginSourceByID: models.GetLoginSourceByID, getLoginSourceByID: login.GetSourceByID,
} }
} }
// parseLoginSource assigns values on loginSource according to command line flags. // parseLoginSource assigns values on loginSource according to command line flags.
func parseLoginSource(c *cli.Context, loginSource *models.LoginSource) { func parseLoginSource(c *cli.Context, loginSource *login.Source) {
if c.IsSet("name") { if c.IsSet("name") {
loginSource.Name = c.String("name") loginSource.Name = c.String("name")
} }
@ -269,7 +269,7 @@ func findLdapSecurityProtocolByName(name string) (ldap.SecurityProtocol, bool) {
// getLoginSource gets the login source by its id defined in the command line flags. // getLoginSource gets the login source by its id defined in the command line flags.
// It returns an error if the id is not set, does not match any source or if the source is not of expected type. // It returns an error if the id is not set, does not match any source or if the source is not of expected type.
func (a *authService) getLoginSource(c *cli.Context, loginType models.LoginType) (*models.LoginSource, error) { func (a *authService) getLoginSource(c *cli.Context, loginType login.Type) (*login.Source, error) {
if err := argsSet(c, "id"); err != nil { if err := argsSet(c, "id"); err != nil {
return nil, err return nil, err
} }
@ -280,7 +280,7 @@ func (a *authService) getLoginSource(c *cli.Context, loginType models.LoginType)
} }
if loginSource.Type != loginType { if loginSource.Type != loginType {
return nil, fmt.Errorf("Invalid authentication type. expected: %s, actual: %s", models.LoginNames[loginType], models.LoginNames[loginSource.Type]) return nil, fmt.Errorf("Invalid authentication type. expected: %s, actual: %s", loginType.String(), loginSource.Type.String())
} }
return loginSource, nil return loginSource, nil
@ -296,8 +296,8 @@ func (a *authService) addLdapBindDn(c *cli.Context) error {
return err return err
} }
loginSource := &models.LoginSource{ loginSource := &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
IsActive: true, // active by default IsActive: true, // active by default
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Enabled: true, // always true Enabled: true, // always true
@ -318,7 +318,7 @@ func (a *authService) updateLdapBindDn(c *cli.Context) error {
return err return err
} }
loginSource, err := a.getLoginSource(c, models.LoginLDAP) loginSource, err := a.getLoginSource(c, login.LDAP)
if err != nil { if err != nil {
return err return err
} }
@ -341,8 +341,8 @@ func (a *authService) addLdapSimpleAuth(c *cli.Context) error {
return err return err
} }
loginSource := &models.LoginSource{ loginSource := &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
IsActive: true, // active by default IsActive: true, // active by default
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Enabled: true, // always true Enabled: true, // always true
@ -363,7 +363,7 @@ func (a *authService) updateLdapSimpleAuth(c *cli.Context) error {
return err return err
} }
loginSource, err := a.getLoginSource(c, models.LoginDLDAP) loginSource, err := a.getLoginSource(c, login.DLDAP)
if err != nil { if err != nil {
return err return err
} }

@ -7,7 +7,7 @@ package cmd
import ( import (
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/services/auth/source/ldap" "code.gitea.io/gitea/services/auth/source/ldap"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -23,7 +23,7 @@ func TestAddLdapBindDn(t *testing.T) {
// Test cases // Test cases
var cases = []struct { var cases = []struct {
args []string args []string
loginSource *models.LoginSource loginSource *login.Source
errMsg string errMsg string
}{ }{
// case 0 // case 0
@ -51,8 +51,8 @@ func TestAddLdapBindDn(t *testing.T) {
"--synchronize-users", "--synchronize-users",
"--page-size", "99", "--page-size", "99",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Name: "ldap (via Bind DN) source full", Name: "ldap (via Bind DN) source full",
IsActive: false, IsActive: false,
IsSyncEnabled: true, IsSyncEnabled: true,
@ -91,8 +91,8 @@ func TestAddLdapBindDn(t *testing.T) {
"--user-filter", "(memberOf=cn=user-group,ou=example,dc=min-domain-bind,dc=org)", "--user-filter", "(memberOf=cn=user-group,ou=example,dc=min-domain-bind,dc=org)",
"--email-attribute", "mail-bind min", "--email-attribute", "mail-bind min",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Name: "ldap (via Bind DN) source min", Name: "ldap (via Bind DN) source min",
IsActive: true, IsActive: true,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
@ -203,20 +203,20 @@ func TestAddLdapBindDn(t *testing.T) {
for n, c := range cases { for n, c := range cases {
// Mock functions. // Mock functions.
var createdLoginSource *models.LoginSource var createdLoginSource *login.Source
service := &authService{ service := &authService{
initDB: func() error { initDB: func() error {
return nil return nil
}, },
createLoginSource: func(loginSource *models.LoginSource) error { createLoginSource: func(loginSource *login.Source) error {
createdLoginSource = loginSource createdLoginSource = loginSource
return nil return nil
}, },
updateLoginSource: func(loginSource *models.LoginSource) error { updateLoginSource: func(loginSource *login.Source) error {
assert.FailNow(t, "case %d: should not call updateLoginSource", n) assert.FailNow(t, "case %d: should not call updateLoginSource", n)
return nil return nil
}, },
getLoginSourceByID: func(id int64) (*models.LoginSource, error) { getLoginSourceByID: func(id int64) (*login.Source, error) {
assert.FailNow(t, "case %d: should not call getLoginSourceByID", n) assert.FailNow(t, "case %d: should not call getLoginSourceByID", n)
return nil, nil return nil, nil
}, },
@ -247,7 +247,7 @@ func TestAddLdapSimpleAuth(t *testing.T) {
// Test cases // Test cases
var cases = []struct { var cases = []struct {
args []string args []string
loginSource *models.LoginSource loginSource *login.Source
errMsg string errMsg string
}{ }{
// case 0 // case 0
@ -271,8 +271,8 @@ func TestAddLdapSimpleAuth(t *testing.T) {
"--public-ssh-key-attribute", "publickey-simple full", "--public-ssh-key-attribute", "publickey-simple full",
"--user-dn", "cn=%s,ou=Users,dc=full-domain-simple,dc=org", "--user-dn", "cn=%s,ou=Users,dc=full-domain-simple,dc=org",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Name: "ldap (simple auth) source full", Name: "ldap (simple auth) source full",
IsActive: false, IsActive: false,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
@ -307,8 +307,8 @@ func TestAddLdapSimpleAuth(t *testing.T) {
"--email-attribute", "mail-simple min", "--email-attribute", "mail-simple min",
"--user-dn", "cn=%s,ou=Users,dc=min-domain-simple,dc=org", "--user-dn", "cn=%s,ou=Users,dc=min-domain-simple,dc=org",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Name: "ldap (simple auth) source min", Name: "ldap (simple auth) source min",
IsActive: true, IsActive: true,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
@ -432,20 +432,20 @@ func TestAddLdapSimpleAuth(t *testing.T) {
for n, c := range cases { for n, c := range cases {
// Mock functions. // Mock functions.
var createdLoginSource *models.LoginSource var createdLoginSource *login.Source
service := &authService{ service := &authService{
initDB: func() error { initDB: func() error {
return nil return nil
}, },
createLoginSource: func(loginSource *models.LoginSource) error { createLoginSource: func(loginSource *login.Source) error {
createdLoginSource = loginSource createdLoginSource = loginSource
return nil return nil
}, },
updateLoginSource: func(loginSource *models.LoginSource) error { updateLoginSource: func(loginSource *login.Source) error {
assert.FailNow(t, "case %d: should not call updateLoginSource", n) assert.FailNow(t, "case %d: should not call updateLoginSource", n)
return nil return nil
}, },
getLoginSourceByID: func(id int64) (*models.LoginSource, error) { getLoginSourceByID: func(id int64) (*login.Source, error) {
assert.FailNow(t, "case %d: should not call getLoginSourceByID", n) assert.FailNow(t, "case %d: should not call getLoginSourceByID", n)
return nil, nil return nil, nil
}, },
@ -477,8 +477,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
var cases = []struct { var cases = []struct {
args []string args []string
id int64 id int64
existingLoginSource *models.LoginSource existingLoginSource *login.Source
loginSource *models.LoginSource loginSource *login.Source
errMsg string errMsg string
}{ }{
// case 0 // case 0
@ -507,15 +507,15 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--page-size", "99", "--page-size", "99",
}, },
id: 23, id: 23,
existingLoginSource: &models.LoginSource{ existingLoginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
IsActive: true, IsActive: true,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Enabled: true, Enabled: true,
}, },
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Name: "ldap (via Bind DN) source full", Name: "ldap (via Bind DN) source full",
IsActive: false, IsActive: false,
IsSyncEnabled: true, IsSyncEnabled: true,
@ -548,8 +548,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"ldap-test", "ldap-test",
"--id", "1", "--id", "1",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
}, },
@ -560,8 +560,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--name", "ldap (via Bind DN) source", "--name", "ldap (via Bind DN) source",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Name: "ldap (via Bind DN) source", Name: "ldap (via Bind DN) source",
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Name: "ldap (via Bind DN) source", Name: "ldap (via Bind DN) source",
@ -575,13 +575,13 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--not-active", "--not-active",
}, },
existingLoginSource: &models.LoginSource{ existingLoginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
IsActive: true, IsActive: true,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
IsActive: false, IsActive: false,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
@ -593,8 +593,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--security-protocol", "LDAPS", "--security-protocol", "LDAPS",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
SecurityProtocol: ldap.SecurityProtocol(1), SecurityProtocol: ldap.SecurityProtocol(1),
}, },
@ -607,8 +607,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--skip-tls-verify", "--skip-tls-verify",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
SkipVerify: true, SkipVerify: true,
}, },
@ -621,8 +621,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--host", "ldap-server", "--host", "ldap-server",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Host: "ldap-server", Host: "ldap-server",
}, },
@ -635,8 +635,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--port", "389", "--port", "389",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Port: 389, Port: 389,
}, },
@ -649,8 +649,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--user-search-base", "ou=Users,dc=domain,dc=org", "--user-search-base", "ou=Users,dc=domain,dc=org",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
UserBase: "ou=Users,dc=domain,dc=org", UserBase: "ou=Users,dc=domain,dc=org",
}, },
@ -663,8 +663,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)", "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Filter: "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)", Filter: "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
}, },
@ -677,8 +677,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)", "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)", AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
}, },
@ -691,8 +691,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--username-attribute", "uid", "--username-attribute", "uid",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeUsername: "uid", AttributeUsername: "uid",
}, },
@ -705,8 +705,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--firstname-attribute", "givenName", "--firstname-attribute", "givenName",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeName: "givenName", AttributeName: "givenName",
}, },
@ -719,8 +719,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--surname-attribute", "sn", "--surname-attribute", "sn",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeSurname: "sn", AttributeSurname: "sn",
}, },
@ -733,8 +733,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--email-attribute", "mail", "--email-attribute", "mail",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeMail: "mail", AttributeMail: "mail",
}, },
@ -747,8 +747,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--attributes-in-bind", "--attributes-in-bind",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributesInBind: true, AttributesInBind: true,
}, },
@ -761,8 +761,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--public-ssh-key-attribute", "publickey", "--public-ssh-key-attribute", "publickey",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeSSHPublicKey: "publickey", AttributeSSHPublicKey: "publickey",
}, },
@ -775,8 +775,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--bind-dn", "cn=readonly,dc=domain,dc=org", "--bind-dn", "cn=readonly,dc=domain,dc=org",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
BindDN: "cn=readonly,dc=domain,dc=org", BindDN: "cn=readonly,dc=domain,dc=org",
}, },
@ -789,8 +789,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--bind-password", "secret", "--bind-password", "secret",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
BindPassword: "secret", BindPassword: "secret",
}, },
@ -803,8 +803,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--synchronize-users", "--synchronize-users",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
IsSyncEnabled: true, IsSyncEnabled: true,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
@ -816,8 +816,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"--id", "1", "--id", "1",
"--page-size", "12", "--page-size", "12",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
SearchPageSize: 12, SearchPageSize: 12,
}, },
@ -845,8 +845,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
"ldap-test", "ldap-test",
"--id", "1", "--id", "1",
}, },
existingLoginSource: &models.LoginSource{ existingLoginSource: &login.Source{
Type: models.LoginOAuth2, Type: login.OAuth2,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
errMsg: "Invalid authentication type. expected: LDAP (via BindDN), actual: OAuth2", errMsg: "Invalid authentication type. expected: LDAP (via BindDN), actual: OAuth2",
@ -855,28 +855,28 @@ func TestUpdateLdapBindDn(t *testing.T) {
for n, c := range cases { for n, c := range cases {
// Mock functions. // Mock functions.
var updatedLoginSource *models.LoginSource var updatedLoginSource *login.Source
service := &authService{ service := &authService{
initDB: func() error { initDB: func() error {
return nil return nil
}, },
createLoginSource: func(loginSource *models.LoginSource) error { createLoginSource: func(loginSource *login.Source) error {
assert.FailNow(t, "case %d: should not call createLoginSource", n) assert.FailNow(t, "case %d: should not call createLoginSource", n)
return nil return nil
}, },
updateLoginSource: func(loginSource *models.LoginSource) error { updateLoginSource: func(loginSource *login.Source) error {
updatedLoginSource = loginSource updatedLoginSource = loginSource
return nil return nil
}, },
getLoginSourceByID: func(id int64) (*models.LoginSource, error) { getLoginSourceByID: func(id int64) (*login.Source, error) {
if c.id != 0 { if c.id != 0 {
assert.Equal(t, c.id, id, "case %d: wrong id", n) assert.Equal(t, c.id, id, "case %d: wrong id", n)
} }
if c.existingLoginSource != nil { if c.existingLoginSource != nil {
return c.existingLoginSource, nil return c.existingLoginSource, nil
} }
return &models.LoginSource{ return &login.Source{
Type: models.LoginLDAP, Type: login.LDAP,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, nil }, nil
}, },
@ -908,8 +908,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
var cases = []struct { var cases = []struct {
args []string args []string
id int64 id int64
existingLoginSource *models.LoginSource existingLoginSource *login.Source
loginSource *models.LoginSource loginSource *login.Source
errMsg string errMsg string
}{ }{
// case 0 // case 0
@ -935,8 +935,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--user-dn", "cn=%s,ou=Users,dc=full-domain-simple,dc=org", "--user-dn", "cn=%s,ou=Users,dc=full-domain-simple,dc=org",
}, },
id: 7, id: 7,
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Name: "ldap (simple auth) source full", Name: "ldap (simple auth) source full",
IsActive: false, IsActive: false,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
@ -964,8 +964,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"ldap-test", "ldap-test",
"--id", "1", "--id", "1",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
}, },
@ -976,8 +976,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--name", "ldap (simple auth) source", "--name", "ldap (simple auth) source",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Name: "ldap (simple auth) source", Name: "ldap (simple auth) source",
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Name: "ldap (simple auth) source", Name: "ldap (simple auth) source",
@ -991,13 +991,13 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--not-active", "--not-active",
}, },
existingLoginSource: &models.LoginSource{ existingLoginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
IsActive: true, IsActive: true,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
IsActive: false, IsActive: false,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
@ -1009,8 +1009,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--security-protocol", "starttls", "--security-protocol", "starttls",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
SecurityProtocol: ldap.SecurityProtocol(2), SecurityProtocol: ldap.SecurityProtocol(2),
}, },
@ -1023,8 +1023,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--skip-tls-verify", "--skip-tls-verify",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
SkipVerify: true, SkipVerify: true,
}, },
@ -1037,8 +1037,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--host", "ldap-server", "--host", "ldap-server",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Host: "ldap-server", Host: "ldap-server",
}, },
@ -1051,8 +1051,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--port", "987", "--port", "987",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Port: 987, Port: 987,
}, },
@ -1065,8 +1065,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--user-search-base", "ou=Users,dc=domain,dc=org", "--user-search-base", "ou=Users,dc=domain,dc=org",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
UserBase: "ou=Users,dc=domain,dc=org", UserBase: "ou=Users,dc=domain,dc=org",
}, },
@ -1079,8 +1079,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--user-filter", "(&(objectClass=posixAccount)(cn=%s))", "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
Filter: "(&(objectClass=posixAccount)(cn=%s))", Filter: "(&(objectClass=posixAccount)(cn=%s))",
}, },
@ -1093,8 +1093,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)", "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)", AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
}, },
@ -1107,8 +1107,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--username-attribute", "uid", "--username-attribute", "uid",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeUsername: "uid", AttributeUsername: "uid",
}, },
@ -1121,8 +1121,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--firstname-attribute", "givenName", "--firstname-attribute", "givenName",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeName: "givenName", AttributeName: "givenName",
}, },
@ -1135,8 +1135,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--surname-attribute", "sn", "--surname-attribute", "sn",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeSurname: "sn", AttributeSurname: "sn",
}, },
@ -1149,8 +1149,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--email-attribute", "mail", "--email-attribute", "mail",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeMail: "mail", AttributeMail: "mail",
@ -1164,8 +1164,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--public-ssh-key-attribute", "publickey", "--public-ssh-key-attribute", "publickey",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
AttributeSSHPublicKey: "publickey", AttributeSSHPublicKey: "publickey",
}, },
@ -1178,8 +1178,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"--id", "1", "--id", "1",
"--user-dn", "cn=%s,ou=Users,dc=domain,dc=org", "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
}, },
loginSource: &models.LoginSource{ loginSource: &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{ Cfg: &ldap.Source{
UserDN: "cn=%s,ou=Users,dc=domain,dc=org", UserDN: "cn=%s,ou=Users,dc=domain,dc=org",
}, },
@ -1207,8 +1207,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
"ldap-test", "ldap-test",
"--id", "1", "--id", "1",
}, },
existingLoginSource: &models.LoginSource{ existingLoginSource: &login.Source{
Type: models.LoginPAM, Type: login.PAM,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, },
errMsg: "Invalid authentication type. expected: LDAP (simple auth), actual: PAM", errMsg: "Invalid authentication type. expected: LDAP (simple auth), actual: PAM",
@ -1217,28 +1217,28 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
for n, c := range cases { for n, c := range cases {
// Mock functions. // Mock functions.
var updatedLoginSource *models.LoginSource var updatedLoginSource *login.Source
service := &authService{ service := &authService{
initDB: func() error { initDB: func() error {
return nil return nil
}, },
createLoginSource: func(loginSource *models.LoginSource) error { createLoginSource: func(loginSource *login.Source) error {
assert.FailNow(t, "case %d: should not call createLoginSource", n) assert.FailNow(t, "case %d: should not call createLoginSource", n)
return nil return nil
}, },
updateLoginSource: func(loginSource *models.LoginSource) error { updateLoginSource: func(loginSource *login.Source) error {
updatedLoginSource = loginSource updatedLoginSource = loginSource
return nil return nil
}, },
getLoginSourceByID: func(id int64) (*models.LoginSource, error) { getLoginSourceByID: func(id int64) (*login.Source, error) {
if c.id != 0 { if c.id != 0 {
assert.Equal(t, c.id, id, "case %d: wrong id", n) assert.Equal(t, c.id, id, "case %d: wrong id", n)
} }
if c.existingLoginSource != nil { if c.existingLoginSource != nil {
return c.existingLoginSource, nil return c.existingLoginSource, nil
} }
return &models.LoginSource{ return &login.Source{
Type: models.LoginDLDAP, Type: login.DLDAP,
Cfg: &ldap.Source{}, Cfg: &ldap.Source{},
}, nil }, nil
}, },

@ -31,7 +31,9 @@ var (
func main() { func main() {
pathToGiteaRoot := "." pathToGiteaRoot := "."
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
if err := db.CreateTestEngine(fixturesDir); err != nil { if err := db.CreateTestEngine(db.FixturesOptions{
Dir: fixturesDir,
}); err != nil {
fmt.Printf("CreateTestEngine: %+v", err) fmt.Printf("CreateTestEngine: %+v", err)
os.Exit(1) os.Exit(1)
} }

@ -101,7 +101,9 @@ func runPR() {
db.HasEngine = true db.HasEngine = true
//x.ShowSQL(true) //x.ShowSQL(true)
err = db.InitFixtures( err = db.InitFixtures(
path.Join(curDir, "models/fixtures/"), db.FixturesOptions{
Dir: path.Join(curDir, "models/fixtures/"),
},
) )
if err != nil { if err != nil {
fmt.Printf("Error initializing test database: %v\n", err) fmt.Printf("Error initializing test database: %v\n", err)

@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -46,7 +47,7 @@ func testAPICreateOAuth2Application(t *testing.T) {
assert.Len(t, createdApp.ClientID, 36) assert.Len(t, createdApp.ClientID, 36)
assert.NotEmpty(t, createdApp.Created) assert.NotEmpty(t, createdApp.Created)
assert.EqualValues(t, appBody.RedirectURIs[0], createdApp.RedirectURIs[0]) assert.EqualValues(t, appBody.RedirectURIs[0], createdApp.RedirectURIs[0])
db.AssertExistsAndLoadBean(t, &models.OAuth2Application{UID: user.ID, Name: createdApp.Name}) db.AssertExistsAndLoadBean(t, &login.OAuth2Application{UID: user.ID, Name: createdApp.Name})
} }
func testAPIListOAuth2Applications(t *testing.T) { func testAPIListOAuth2Applications(t *testing.T) {
@ -54,13 +55,13 @@ func testAPIListOAuth2Applications(t *testing.T) {
session := loginUser(t, user.Name) session := loginUser(t, user.Name)
token := getTokenForLoggedInUser(t, session) token := getTokenForLoggedInUser(t, session)
existApp := db.AssertExistsAndLoadBean(t, &models.OAuth2Application{ existApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{
UID: user.ID, UID: user.ID,
Name: "test-app-1", Name: "test-app-1",
RedirectURIs: []string{ RedirectURIs: []string{
"http://www.google.com", "http://www.google.com",
}, },
}).(*models.OAuth2Application) }).(*login.OAuth2Application)
urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2?token=%s", token) urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2?token=%s", token)
req := NewRequest(t, "GET", urlStr) req := NewRequest(t, "GET", urlStr)
@ -75,7 +76,7 @@ func testAPIListOAuth2Applications(t *testing.T) {
assert.Len(t, expectedApp.ClientID, 36) assert.Len(t, expectedApp.ClientID, 36)
assert.Empty(t, expectedApp.ClientSecret) assert.Empty(t, expectedApp.ClientSecret)
assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0]) assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0])
db.AssertExistsAndLoadBean(t, &models.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name})
} }
func testAPIDeleteOAuth2Application(t *testing.T) { func testAPIDeleteOAuth2Application(t *testing.T) {
@ -83,16 +84,16 @@ func testAPIDeleteOAuth2Application(t *testing.T) {
session := loginUser(t, user.Name) session := loginUser(t, user.Name)
token := getTokenForLoggedInUser(t, session) token := getTokenForLoggedInUser(t, session)
oldApp := db.AssertExistsAndLoadBean(t, &models.OAuth2Application{ oldApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{
UID: user.ID, UID: user.ID,
Name: "test-app-1", Name: "test-app-1",
}).(*models.OAuth2Application) }).(*login.OAuth2Application)
urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", oldApp.ID, token) urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", oldApp.ID, token)
req := NewRequest(t, "DELETE", urlStr) req := NewRequest(t, "DELETE", urlStr)
session.MakeRequest(t, req, http.StatusNoContent) session.MakeRequest(t, req, http.StatusNoContent)
db.AssertNotExistsBean(t, &models.OAuth2Application{UID: oldApp.UID, Name: oldApp.Name}) db.AssertNotExistsBean(t, &login.OAuth2Application{UID: oldApp.UID, Name: oldApp.Name})
// Delete again will return not found // Delete again will return not found
req = NewRequest(t, "DELETE", urlStr) req = NewRequest(t, "DELETE", urlStr)
@ -104,13 +105,13 @@ func testAPIGetOAuth2Application(t *testing.T) {
session := loginUser(t, user.Name) session := loginUser(t, user.Name)
token := getTokenForLoggedInUser(t, session) token := getTokenForLoggedInUser(t, session)
existApp := db.AssertExistsAndLoadBean(t, &models.OAuth2Application{ existApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{
UID: user.ID, UID: user.ID,
Name: "test-app-1", Name: "test-app-1",
RedirectURIs: []string{ RedirectURIs: []string{
"http://www.google.com", "http://www.google.com",
}, },
}).(*models.OAuth2Application) }).(*login.OAuth2Application)
urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", existApp.ID, token) urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", existApp.ID, token)
req := NewRequest(t, "GET", urlStr) req := NewRequest(t, "GET", urlStr)
@ -126,19 +127,19 @@ func testAPIGetOAuth2Application(t *testing.T) {
assert.Empty(t, expectedApp.ClientSecret) assert.Empty(t, expectedApp.ClientSecret)
assert.Len(t, expectedApp.RedirectURIs, 1) assert.Len(t, expectedApp.RedirectURIs, 1)
assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0]) assert.EqualValues(t, existApp.RedirectURIs[0], expectedApp.RedirectURIs[0])
db.AssertExistsAndLoadBean(t, &models.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name})
} }
func testAPIUpdateOAuth2Application(t *testing.T) { func testAPIUpdateOAuth2Application(t *testing.T) {
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
existApp := db.AssertExistsAndLoadBean(t, &models.OAuth2Application{ existApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{
UID: user.ID, UID: user.ID,
Name: "test-app-1", Name: "test-app-1",
RedirectURIs: []string{ RedirectURIs: []string{
"http://www.google.com", "http://www.google.com",
}, },
}).(*models.OAuth2Application) }).(*login.OAuth2Application)
appBody := api.CreateOAuth2ApplicationOptions{ appBody := api.CreateOAuth2ApplicationOptions{
Name: "test-app-1", Name: "test-app-1",
@ -160,5 +161,5 @@ func testAPIUpdateOAuth2Application(t *testing.T) {
assert.Len(t, expectedApp.RedirectURIs, 2) assert.Len(t, expectedApp.RedirectURIs, 2)
assert.EqualValues(t, expectedApp.RedirectURIs[0], appBody.RedirectURIs[0]) assert.EqualValues(t, expectedApp.RedirectURIs[0], appBody.RedirectURIs[0])
assert.EqualValues(t, expectedApp.RedirectURIs[1], appBody.RedirectURIs[1]) assert.EqualValues(t, expectedApp.RedirectURIs[1], appBody.RedirectURIs[1])
db.AssertExistsAndLoadBean(t, &models.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name}) db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: expectedApp.ID, Name: expectedApp.Name})
} }

@ -113,7 +113,9 @@ func TestMain(m *testing.M) {
} }
err := db.InitFixtures( err := db.InitFixtures(
path.Join(filepath.Dir(setting.AppPath), "models/fixtures/"), db.FixturesOptions{
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
},
) )
if err != nil { if err != nil {
fmt.Printf("Error initializing test database: %v\n", err) fmt.Printf("Error initializing test database: %v\n", err)

@ -225,7 +225,7 @@ func (repo *Repository) refreshAccesses(e db.Engine, accessMap map[int64]*userAc
// refreshCollaboratorAccesses retrieves repository collaborations with their access modes. // refreshCollaboratorAccesses retrieves repository collaborations with their access modes.
func (repo *Repository) refreshCollaboratorAccesses(e db.Engine, accessMap map[int64]*userAccess) error { func (repo *Repository) refreshCollaboratorAccesses(e db.Engine, accessMap map[int64]*userAccess) error {
collaborators, err := repo.getCollaborators(e, ListOptions{}) collaborators, err := repo.getCollaborators(e, db.ListOptions{})
if err != nil { if err != nil {
return fmt.Errorf("getCollaborations: %v", err) return fmt.Errorf("getCollaborations: %v", err)
} }

@ -163,7 +163,7 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
// CommitStatusOptions holds the options for query commit statuses // CommitStatusOptions holds the options for query commit statuses
type CommitStatusOptions struct { type CommitStatusOptions struct {
ListOptions db.ListOptions
State string State string
SortType string SortType string
} }
@ -178,7 +178,7 @@ func GetCommitStatuses(repo *Repository, sha string, opts *CommitStatusOptions)
} }
countSession := listCommitStatusesStatement(repo, sha, opts) countSession := listCommitStatusesStatement(repo, sha, opts)
countSession = setSessionPagination(countSession, opts) countSession = db.SetSessionPagination(countSession, opts)
maxResults, err := countSession.Count(new(CommitStatus)) maxResults, err := countSession.Count(new(CommitStatus))
if err != nil { if err != nil {
log.Error("Count PRs: %v", err) log.Error("Count PRs: %v", err)
@ -187,7 +187,7 @@ func GetCommitStatuses(repo *Repository, sha string, opts *CommitStatusOptions)
statuses := make([]*CommitStatus, 0, opts.PageSize) statuses := make([]*CommitStatus, 0, opts.PageSize)
findSession := listCommitStatusesStatement(repo, sha, opts) findSession := listCommitStatusesStatement(repo, sha, opts)
findSession = setSessionPagination(findSession, opts) findSession = db.SetSessionPagination(findSession, opts)
sortCommitStatusesSession(findSession, opts.SortType) sortCommitStatusesSession(findSession, opts.SortType)
return statuses, maxResults, findSession.Find(&statuses) return statuses, maxResults, findSession.Find(&statuses)
} }
@ -227,18 +227,18 @@ type CommitStatusIndex struct {
} }
// GetLatestCommitStatus returns all statuses with a unique context for a given commit. // GetLatestCommitStatus returns all statuses with a unique context for a given commit.
func GetLatestCommitStatus(repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) { func GetLatestCommitStatus(repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
return getLatestCommitStatus(db.GetEngine(db.DefaultContext), repoID, sha, listOptions) return getLatestCommitStatus(db.GetEngine(db.DefaultContext), repoID, sha, listOptions)
} }
func getLatestCommitStatus(e db.Engine, repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) { func getLatestCommitStatus(e db.Engine, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
ids := make([]int64, 0, 10) ids := make([]int64, 0, 10)
sess := e.Table(&CommitStatus{}). sess := e.Table(&CommitStatus{}).
Where("repo_id = ?", repoID).And("sha = ?", sha). Where("repo_id = ?", repoID).And("sha = ?", sha).
Select("max( id ) as id"). Select("max( id ) as id").
GroupBy("context_hash").OrderBy("max( id ) desc") GroupBy("context_hash").OrderBy("max( id ) desc")
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
err := sess.Find(&ids) err := sess.Find(&ids)
if err != nil { if err != nil {
@ -336,7 +336,7 @@ func ParseCommitsWithStatus(oldCommits []*SignCommit, repo *Repository) []*SignC
commit := &SignCommitWithStatuses{ commit := &SignCommitWithStatuses{
SignCommit: c, SignCommit: c,
} }
statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{}) statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), db.ListOptions{})
if err != nil { if err != nil {
log.Error("GetLatestCommitStatus: %v", err) log.Error("GetLatestCommitStatus: %v", err)
} else { } else {

@ -19,7 +19,7 @@ func TestGetCommitStatuses(t *testing.T) {
sha1 := "1234123412341234123412341234123412341234" sha1 := "1234123412341234123412341234123412341234"
statuses, maxResults, err := GetCommitStatuses(repo1, sha1, &CommitStatusOptions{ListOptions: ListOptions{Page: 1, PageSize: 50}}) statuses, maxResults, err := GetCommitStatuses(repo1, sha1, &CommitStatusOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 50}})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, int(maxResults), 5) assert.Equal(t, int(maxResults), 5)
assert.Len(t, statuses, 5) assert.Len(t, statuses, 5)

@ -2,10 +2,9 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package db
import ( import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"xorm.io/xorm" "xorm.io/xorm"
@ -17,22 +16,22 @@ type Paginator interface {
GetStartEnd() (start, end int) GetStartEnd() (start, end int)
} }
// getPaginatedSession creates a paginated database session // GetPaginatedSession creates a paginated database session
func getPaginatedSession(p Paginator) *xorm.Session { func GetPaginatedSession(p Paginator) *xorm.Session {
skip, take := p.GetSkipTake() skip, take := p.GetSkipTake()
return db.GetEngine(db.DefaultContext).Limit(take, skip) return x.Limit(take, skip)
} }
// setSessionPagination sets pagination for a database session // SetSessionPagination sets pagination for a database session
func setSessionPagination(sess *xorm.Session, p Paginator) *xorm.Session { func SetSessionPagination(sess *xorm.Session, p Paginator) *xorm.Session {
skip, take := p.GetSkipTake() skip, take := p.GetSkipTake()
return sess.Limit(take, skip) return sess.Limit(take, skip)
} }
// setSessionPagination sets pagination for a database engine // SetEnginePagination sets pagination for a database engine
func setEnginePagination(e db.Engine, p Paginator) db.Engine { func SetEnginePagination(e Engine, p Paginator) Engine {
skip, take := p.GetSkipTake() skip, take := p.GetSkipTake()
return e.Limit(take, skip) return e.Limit(take, skip)
@ -46,7 +45,7 @@ type ListOptions struct {
// GetSkipTake returns the skip and take values // GetSkipTake returns the skip and take values
func (opts *ListOptions) GetSkipTake() (skip, take int) { func (opts *ListOptions) GetSkipTake() (skip, take int) {
opts.setDefaultValues() opts.SetDefaultValues()
return (opts.Page - 1) * opts.PageSize, opts.PageSize return (opts.Page - 1) * opts.PageSize, opts.PageSize
} }
@ -57,7 +56,8 @@ func (opts *ListOptions) GetStartEnd() (start, end int) {
return return
} }
func (opts *ListOptions) setDefaultValues() { // SetDefaultValues sets default values
func (opts *ListOptions) SetDefaultValues() {
if opts.PageSize <= 0 { if opts.PageSize <= 0 {
opts.PageSize = setting.API.DefaultPagingNum opts.PageSize = setting.API.DefaultPagingNum
} }

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package db
import ( import (
"testing" "testing"

@ -17,13 +17,18 @@ import (
var fixtures *testfixtures.Loader var fixtures *testfixtures.Loader
// InitFixtures initialize test fixtures for a test database // InitFixtures initialize test fixtures for a test database
func InitFixtures(dir string, engine ...*xorm.Engine) (err error) { func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
e := x e := x
if len(engine) == 1 { if len(engine) == 1 {
e = engine[0] e = engine[0]
} }
testfiles := testfixtures.Directory(dir) var testfiles func(*testfixtures.Loader) error
if opts.Dir != "" {
testfiles = testfixtures.Directory(opts.Dir)
} else {
testfiles = testfixtures.Files(opts.Files...)
}
dialect := "unknown" dialect := "unknown"
switch e.Dialect().URI().DBType { switch e.Dialect().URI().DBType {
case schemas.POSTGRES: case schemas.POSTGRES:

@ -44,11 +44,21 @@ func fatalTestError(fmtStr string, args ...interface{}) {
// MainTest a reusable TestMain(..) function for unit tests that need to use a // MainTest a reusable TestMain(..) function for unit tests that need to use a
// test database. Creates the test database, and sets necessary settings. // test database. Creates the test database, and sets necessary settings.
func MainTest(m *testing.M, pathToGiteaRoot string) { func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
var err error var err error
giteaRoot = pathToGiteaRoot giteaRoot = pathToGiteaRoot
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures") fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
if err = CreateTestEngine(fixturesDir); err != nil {
var opts FixturesOptions
if len(fixtureFiles) == 0 {
opts.Dir = fixturesDir
} else {
for _, f := range fixtureFiles {
opts.Files = append(opts.Files, filepath.Join(fixturesDir, f))
}
}
if err = CreateTestEngine(opts); err != nil {
fatalTestError("Error creating test engine: %v\n", err) fatalTestError("Error creating test engine: %v\n", err)
} }
@ -102,8 +112,14 @@ func MainTest(m *testing.M, pathToGiteaRoot string) {
os.Exit(exitStatus) os.Exit(exitStatus)
} }
// FixturesOptions fixtures needs to be loaded options
type FixturesOptions struct {
Dir string
Files []string
}
// CreateTestEngine creates a memory database and loads the fixture data from fixturesDir // CreateTestEngine creates a memory database and loads the fixture data from fixturesDir
func CreateTestEngine(fixturesDir string) error { func CreateTestEngine(opts FixturesOptions) error {
var err error var err error
x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate") x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate")
if err != nil { if err != nil {
@ -123,7 +139,7 @@ func CreateTestEngine(fixturesDir string) error {
e: x, e: x,
} }
return InitFixtures(fixturesDir) return InitFixtures(opts)
} }
// PrepareTestDatabase load test fixtures into test database // PrepareTestDatabase load test fixtures into test database

@ -1836,58 +1836,6 @@ func (err ErrAttachmentNotExist) Error() string {
return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID) return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID)
} }
// .____ .__ _________
// | | ____ ____ |__| ____ / _____/ ____ __ _________ ____ ____
// | | / _ \ / ___\| |/ \ \_____ \ / _ \| | \_ __ \_/ ___\/ __ \
// | |__( <_> ) /_/ > | | \ / ( <_> ) | /| | \/\ \__\ ___/
// |_______ \____/\___ /|__|___| / /_______ /\____/|____/ |__| \___ >___ >
// \/ /_____/ \/ \/ \/ \/
// ErrLoginSourceNotExist represents a "LoginSourceNotExist" kind of error.
type ErrLoginSourceNotExist struct {
ID int64
}
// IsErrLoginSourceNotExist checks if an error is a ErrLoginSourceNotExist.
func IsErrLoginSourceNotExist(err error) bool {
_, ok := err.(ErrLoginSourceNotExist)
return ok
}
func (err ErrLoginSourceNotExist) Error() string {
return fmt.Sprintf("login source does not exist [id: %d]", err.ID)
}
// ErrLoginSourceAlreadyExist represents a "LoginSourceAlreadyExist" kind of error.
type ErrLoginSourceAlreadyExist struct {
Name string
}
// IsErrLoginSourceAlreadyExist checks if an error is a ErrLoginSourceAlreadyExist.
func IsErrLoginSourceAlreadyExist(err error) bool {
_, ok := err.(ErrLoginSourceAlreadyExist)
return ok
}
func (err ErrLoginSourceAlreadyExist) Error() string {
return fmt.Sprintf("login source already exists [name: %s]", err.Name)
}
// ErrLoginSourceInUse represents a "LoginSourceInUse" kind of error.
type ErrLoginSourceInUse struct {
ID int64
}
// IsErrLoginSourceInUse checks if an error is a ErrLoginSourceInUse.
func IsErrLoginSourceInUse(err error) bool {
_, ok := err.(ErrLoginSourceInUse)
return ok
}
func (err ErrLoginSourceInUse) Error() string {
return fmt.Sprintf("login source is still used by some users [id: %d]", err.ID)
}
// ___________ // ___________
// \__ ___/___ _____ _____ // \__ ___/___ _____ _____
// | |_/ __ \\__ \ / \ // | |_/ __ \\__ \ / \
@ -2159,42 +2107,3 @@ func (err ErrNotValidReviewRequest) Error() string {
err.UserID, err.UserID,
err.RepoID) err.RepoID)
} }
// ________ _____ __ .__
// \_____ \ / _ \ __ ___/ |_| |__
// / | \ / /_\ \| | \ __\ | \
// / | \/ | \ | /| | | Y \
// \_______ /\____|__ /____/ |__| |___| /
// \/ \/ \/
// ErrOAuthClientIDInvalid will be thrown if client id cannot be found
type ErrOAuthClientIDInvalid struct {
ClientID string
}
// IsErrOauthClientIDInvalid checks if an error is a ErrReviewNotExist.
func IsErrOauthClientIDInvalid(err error) bool {
_, ok := err.(ErrOAuthClientIDInvalid)
return ok
}
// Error returns the error message
func (err ErrOAuthClientIDInvalid) Error() string {
return fmt.Sprintf("Client ID invalid [Client ID: %s]", err.ClientID)
}
// ErrOAuthApplicationNotFound will be thrown if id cannot be found
type ErrOAuthApplicationNotFound struct {
ID int64
}
// IsErrOAuthApplicationNotFound checks if an error is a ErrReviewNotExist.
func IsErrOAuthApplicationNotFound(err error) bool {
_, ok := err.(ErrOAuthApplicationNotFound)
return ok
}
// Error returns the error message
func (err ErrOAuthApplicationNotFound) Error() string {
return fmt.Sprintf("OAuth application not found [ID: %d]", err.ID)
}

@ -8,6 +8,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
"github.com/markbates/goth" "github.com/markbates/goth"
@ -106,7 +107,7 @@ func GetUserIDByExternalUserID(provider, userID string) (int64, error) {
// UpdateExternalUser updates external user's information // UpdateExternalUser updates external user's information
func UpdateExternalUser(user *User, gothUser goth.User) error { func UpdateExternalUser(user *User, gothUser goth.User) error {
loginSource, err := GetActiveOAuth2LoginSourceByName(gothUser.Provider) loginSource, err := login.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
if err != nil { if err != nil {
return err return err
} }

@ -62,14 +62,14 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) {
} }
// ListGPGKeys returns a list of public keys belongs to given user. // ListGPGKeys returns a list of public keys belongs to given user.
func ListGPGKeys(uid int64, listOptions ListOptions) ([]*GPGKey, error) { func ListGPGKeys(uid int64, listOptions db.ListOptions) ([]*GPGKey, error) {
return listGPGKeys(db.GetEngine(db.DefaultContext), uid, listOptions) return listGPGKeys(db.GetEngine(db.DefaultContext), uid, listOptions)
} }
func listGPGKeys(e db.Engine, uid int64, listOptions ListOptions) ([]*GPGKey, error) { func listGPGKeys(e db.Engine, uid int64, listOptions db.ListOptions) ([]*GPGKey, error) {
sess := e.Table(&GPGKey{}).Where("owner_id=? AND primary_key_id=''", uid) sess := e.Table(&GPGKey{}).Where("owner_id=? AND primary_key_id=''", uid)
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
} }
keys := make([]*GPGKey, 0, 2) keys := make([]*GPGKey, 0, 2)

@ -9,6 +9,7 @@ import (
"hash" "hash"
"strings" "strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -156,7 +157,7 @@ func ParseCommitWithSignature(c *git.Commit) *CommitVerification {
// Now try to associate the signature with the committer, if present // Now try to associate the signature with the committer, if present
if committer.ID != 0 { if committer.ID != 0 {
keys, err := ListGPGKeys(committer.ID, ListOptions{}) keys, err := ListGPGKeys(committer.ID, db.ListOptions{})
if err != nil { // Skipping failed to get gpg keys of user if err != nil { // Skipping failed to get gpg keys of user
log.Error("ListGPGKeys: %v", err) log.Error("ListGPGKeys: %v", err)
return &CommitVerification{ return &CommitVerification{

@ -1122,7 +1122,7 @@ func GetIssuesByIDs(issueIDs []int64) ([]*Issue, error) {
// IssuesOptions represents options of an issue. // IssuesOptions represents options of an issue.
type IssuesOptions struct { type IssuesOptions struct {
ListOptions db.ListOptions
RepoIDs []int64 // include all repos if empty RepoIDs []int64 // include all repos if empty
AssigneeID int64 AssigneeID int64
PosterID int64 PosterID int64

@ -964,7 +964,7 @@ func getCommentByID(e db.Engine, id int64) (*Comment, error) {
// FindCommentsOptions describes the conditions to Find comments // FindCommentsOptions describes the conditions to Find comments
type FindCommentsOptions struct { type FindCommentsOptions struct {
ListOptions db.ListOptions
RepoID int64 RepoID int64
IssueID int64 IssueID int64
ReviewID int64 ReviewID int64
@ -1012,7 +1012,7 @@ func findComments(e db.Engine, opts *FindCommentsOptions) ([]*Comment, error) {
} }
if opts.Page != 0 { if opts.Page != 0 {
sess = setSessionPagination(sess, opts) sess = db.SetSessionPagination(sess, opts)
} }
// WARNING: If you change this order you will need to fix createCodeComment // WARNING: If you change this order you will need to fix createCodeComment

@ -447,7 +447,7 @@ func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
Find(&labels) Find(&labels)
} }
func getLabelsByRepoID(e db.Engine, repoID int64, sortType string, listOptions ListOptions) ([]*Label, error) { func getLabelsByRepoID(e db.Engine, repoID int64, sortType string, listOptions db.ListOptions) ([]*Label, error) {
if repoID <= 0 { if repoID <= 0 {
return nil, ErrRepoLabelNotExist{0, repoID} return nil, ErrRepoLabelNotExist{0, repoID}
} }
@ -466,14 +466,14 @@ func getLabelsByRepoID(e db.Engine, repoID int64, sortType string, listOptions L
} }
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
} }
return labels, sess.Find(&labels) return labels, sess.Find(&labels)
} }
// GetLabelsByRepoID returns all labels that belong to given repository by ID. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
func GetLabelsByRepoID(repoID int64, sortType string, listOptions ListOptions) ([]*Label, error) { func GetLabelsByRepoID(repoID int64, sortType string, listOptions db.ListOptions) ([]*Label, error) {
return getLabelsByRepoID(db.GetEngine(db.DefaultContext), repoID, sortType, listOptions) return getLabelsByRepoID(db.GetEngine(db.DefaultContext), repoID, sortType, listOptions)
} }
@ -564,7 +564,7 @@ func GetLabelsInOrgByIDs(orgID int64, labelIDs []int64) ([]*Label, error) {
Find(&labels) Find(&labels)
} }
func getLabelsByOrgID(e db.Engine, orgID int64, sortType string, listOptions ListOptions) ([]*Label, error) { func getLabelsByOrgID(e db.Engine, orgID int64, sortType string, listOptions db.ListOptions) ([]*Label, error) {
if orgID <= 0 { if orgID <= 0 {
return nil, ErrOrgLabelNotExist{0, orgID} return nil, ErrOrgLabelNotExist{0, orgID}
} }
@ -583,14 +583,14 @@ func getLabelsByOrgID(e db.Engine, orgID int64, sortType string, listOptions Lis
} }
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
} }
return labels, sess.Find(&labels) return labels, sess.Find(&labels)
} }
// GetLabelsByOrgID returns all labels that belong to given organization by ID. // GetLabelsByOrgID returns all labels that belong to given organization by ID.
func GetLabelsByOrgID(orgID int64, sortType string, listOptions ListOptions) ([]*Label, error) { func GetLabelsByOrgID(orgID int64, sortType string, listOptions db.ListOptions) ([]*Label, error) {
return getLabelsByOrgID(db.GetEngine(db.DefaultContext), orgID, sortType, listOptions) return getLabelsByOrgID(db.GetEngine(db.DefaultContext), orgID, sortType, listOptions)
} }

@ -123,7 +123,7 @@ func TestGetLabelsInRepoByIDs(t *testing.T) {
func TestGetLabelsByRepoID(t *testing.T) { func TestGetLabelsByRepoID(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) { testSuccess := func(repoID int64, sortType string, expectedIssueIDs []int64) {
labels, err := GetLabelsByRepoID(repoID, sortType, ListOptions{}) labels, err := GetLabelsByRepoID(repoID, sortType, db.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, labels, len(expectedIssueIDs)) assert.Len(t, labels, len(expectedIssueIDs))
for i, label := range labels { for i, label := range labels {
@ -214,7 +214,7 @@ func TestGetLabelsInOrgByIDs(t *testing.T) {
func TestGetLabelsByOrgID(t *testing.T) { func TestGetLabelsByOrgID(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
testSuccess := func(orgID int64, sortType string, expectedIssueIDs []int64) { testSuccess := func(orgID int64, sortType string, expectedIssueIDs []int64) {
labels, err := GetLabelsByOrgID(orgID, sortType, ListOptions{}) labels, err := GetLabelsByOrgID(orgID, sortType, db.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, labels, len(expectedIssueIDs)) assert.Len(t, labels, len(expectedIssueIDs))
for i, label := range labels { for i, label := range labels {
@ -227,10 +227,10 @@ func TestGetLabelsByOrgID(t *testing.T) {
testSuccess(3, "default", []int64{3, 4}) testSuccess(3, "default", []int64{3, 4})
var err error var err error
_, err = GetLabelsByOrgID(0, "leastissues", ListOptions{}) _, err = GetLabelsByOrgID(0, "leastissues", db.ListOptions{})
assert.True(t, IsErrOrgLabelNotExist(err)) assert.True(t, IsErrOrgLabelNotExist(err))
_, err = GetLabelsByOrgID(-1, "leastissues", ListOptions{}) _, err = GetLabelsByOrgID(-1, "leastissues", db.ListOptions{})
assert.True(t, IsErrOrgLabelNotExist(err)) assert.True(t, IsErrOrgLabelNotExist(err))
} }

@ -378,7 +378,7 @@ func (milestones MilestoneList) getMilestoneIDs() []int64 {
// GetMilestonesOption contain options to get milestones // GetMilestonesOption contain options to get milestones
type GetMilestonesOption struct { type GetMilestonesOption struct {
ListOptions db.ListOptions
RepoID int64 RepoID int64
State api.StateType State api.StateType
Name string Name string
@ -413,7 +413,7 @@ func GetMilestones(opts GetMilestonesOption) (MilestoneList, int64, error) {
sess := db.GetEngine(db.DefaultContext).Where(opts.toCond()) sess := db.GetEngine(db.DefaultContext).Where(opts.toCond())
if opts.Page != 0 { if opts.Page != 0 {
sess = setSessionPagination(sess, &opts) sess = db.SetSessionPagination(sess, &opts)
} }
switch opts.SortType { switch opts.SortType {

@ -102,7 +102,7 @@ func TestGetMilestones(t *testing.T) {
test := func(sortType string, sortCond func(*Milestone) int) { test := func(sortType string, sortCond func(*Milestone) int) {
for _, page := range []int{0, 1} { for _, page := range []int{0, 1} {
milestones, _, err := GetMilestones(GetMilestonesOption{ milestones, _, err := GetMilestones(GetMilestonesOption{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: page, Page: page,
PageSize: setting.UI.IssuePagingNum, PageSize: setting.UI.IssuePagingNum,
}, },
@ -119,7 +119,7 @@ func TestGetMilestones(t *testing.T) {
assert.True(t, sort.IntsAreSorted(values)) assert.True(t, sort.IntsAreSorted(values))
milestones, _, err = GetMilestones(GetMilestonesOption{ milestones, _, err = GetMilestones(GetMilestonesOption{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: page, Page: page,
PageSize: setting.UI.IssuePagingNum, PageSize: setting.UI.IssuePagingNum,
}, },

@ -35,7 +35,7 @@ func init() {
// FindReactionsOptions describes the conditions to Find reactions // FindReactionsOptions describes the conditions to Find reactions
type FindReactionsOptions struct { type FindReactionsOptions struct {
ListOptions db.ListOptions
IssueID int64 IssueID int64
CommentID int64 CommentID int64
UserID int64 UserID int64
@ -78,7 +78,7 @@ func FindCommentReactions(comment *Comment) (ReactionList, error) {
} }
// FindIssueReactions returns a ReactionList of all reactions from an issue // FindIssueReactions returns a ReactionList of all reactions from an issue
func FindIssueReactions(issue *Issue, listOptions ListOptions) (ReactionList, error) { func FindIssueReactions(issue *Issue, listOptions db.ListOptions) (ReactionList, error) {
return findReactions(db.GetEngine(db.DefaultContext), FindReactionsOptions{ return findReactions(db.GetEngine(db.DefaultContext), FindReactionsOptions{
ListOptions: listOptions, ListOptions: listOptions,
IssueID: issue.ID, IssueID: issue.ID,
@ -92,7 +92,7 @@ func findReactions(e db.Engine, opts FindReactionsOptions) ([]*Reaction, error)
In("reaction.`type`", setting.UI.Reactions). In("reaction.`type`", setting.UI.Reactions).
Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id") Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id")
if opts.Page != 0 { if opts.Page != 0 {
e = setEnginePagination(e, &opts) e = db.SetEnginePagination(e, &opts)
reactions := make([]*Reaction, 0, opts.PageSize) reactions := make([]*Reaction, 0, opts.PageSize)
return reactions, e.Find(&reactions) return reactions, e.Find(&reactions)

@ -46,11 +46,11 @@ func getStopwatch(e db.Engine, userID, issueID int64) (sw *Stopwatch, exists boo
} }
// GetUserStopwatches return list of all stopwatches of a user // GetUserStopwatches return list of all stopwatches of a user
func GetUserStopwatches(userID int64, listOptions ListOptions) ([]*Stopwatch, error) { func GetUserStopwatches(userID int64, listOptions db.ListOptions) ([]*Stopwatch, error) {
sws := make([]*Stopwatch, 0, 8) sws := make([]*Stopwatch, 0, 8)
sess := db.GetEngine(db.DefaultContext).Where("stopwatch.user_id = ?", userID) sess := db.GetEngine(db.DefaultContext).Where("stopwatch.user_id = ?", userID)
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
} }
err := sess.Find(&sws) err := sess.Find(&sws)

@ -151,7 +151,7 @@ func TestIssues(t *testing.T) {
IssuesOptions{ IssuesOptions{
RepoIDs: []int64{1, 3}, RepoIDs: []int64{1, 3},
SortType: "oldest", SortType: "oldest",
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 4, PageSize: 4,
}, },
@ -161,7 +161,7 @@ func TestIssues(t *testing.T) {
{ {
IssuesOptions{ IssuesOptions{
LabelIDs: []int64{1}, LabelIDs: []int64{1},
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 4, PageSize: 4,
}, },
@ -171,7 +171,7 @@ func TestIssues(t *testing.T) {
{ {
IssuesOptions{ IssuesOptions{
LabelIDs: []int64{1, 2}, LabelIDs: []int64{1, 2},
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 4, PageSize: 4,
}, },

@ -75,7 +75,7 @@ func (tl TrackedTimeList) LoadAttributes() (err error) {
// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored. // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
type FindTrackedTimesOptions struct { type FindTrackedTimesOptions struct {
ListOptions db.ListOptions
IssueID int64 IssueID int64
UserID int64 UserID int64
RepositoryID int64 RepositoryID int64
@ -118,7 +118,7 @@ func (opts *FindTrackedTimesOptions) toSession(e db.Engine) db.Engine {
sess = sess.Where(opts.toCond()) sess = sess.Where(opts.toCond())
if opts.Page != 0 { if opts.Page != 0 {
sess = setEnginePagination(sess, opts) sess = db.SetEnginePagination(sess, opts)
} }
return sess return sess

@ -103,11 +103,11 @@ func getIssueWatchersIDs(e db.Engine, issueID int64, watching bool) ([]int64, er
} }
// GetIssueWatchers returns watchers/unwatchers of a given issue // GetIssueWatchers returns watchers/unwatchers of a given issue
func GetIssueWatchers(issueID int64, listOptions ListOptions) (IssueWatchList, error) { func GetIssueWatchers(issueID int64, listOptions db.ListOptions) (IssueWatchList, error) {
return getIssueWatchers(db.GetEngine(db.DefaultContext), issueID, listOptions) return getIssueWatchers(db.GetEngine(db.DefaultContext), issueID, listOptions)
} }
func getIssueWatchers(e db.Engine, issueID int64, listOptions ListOptions) (IssueWatchList, error) { func getIssueWatchers(e db.Engine, issueID int64, listOptions db.ListOptions) (IssueWatchList, error) {
sess := e. sess := e.
Where("`issue_watch`.issue_id = ?", issueID). Where("`issue_watch`.issue_id = ?", issueID).
And("`issue_watch`.is_watching = ?", true). And("`issue_watch`.is_watching = ?", true).
@ -116,7 +116,7 @@ func getIssueWatchers(e db.Engine, issueID int64, listOptions ListOptions) (Issu
Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id") Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id")
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
watches := make([]*IssueWatch, 0, listOptions.PageSize) watches := make([]*IssueWatch, 0, listOptions.PageSize)
return watches, sess.Find(&watches) return watches, sess.Find(&watches)
} }

@ -43,22 +43,22 @@ func TestGetIssueWatch(t *testing.T) {
func TestGetIssueWatchers(t *testing.T) { func TestGetIssueWatchers(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
iws, err := GetIssueWatchers(1, ListOptions{}) iws, err := GetIssueWatchers(1, db.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
// Watcher is inactive, thus 0 // Watcher is inactive, thus 0
assert.Len(t, iws, 0) assert.Len(t, iws, 0)
iws, err = GetIssueWatchers(2, ListOptions{}) iws, err = GetIssueWatchers(2, db.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
// Watcher is explicit not watching // Watcher is explicit not watching
assert.Len(t, iws, 0) assert.Len(t, iws, 0)
iws, err = GetIssueWatchers(5, ListOptions{}) iws, err = GetIssueWatchers(5, db.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
// Issue has no Watchers // Issue has no Watchers
assert.Len(t, iws, 0) assert.Len(t, iws, 0)
iws, err = GetIssueWatchers(7, ListOptions{}) iws, err = GetIssueWatchers(7, db.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
// Issue has one watcher // Issue has one watcher
assert.Len(t, iws, 1) assert.Len(t, iws, 1)

@ -0,0 +1,21 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package login
import (
"path/filepath"
"testing"
"code.gitea.io/gitea/models/db"
)
func TestMain(m *testing.M) {
db.MainTest(m, filepath.Join("..", ".."),
"login_source.yml",
"oauth2_application.yml",
"oauth2_authorization_code.yml",
"oauth2_grant.yml",
)
}

@ -0,0 +1,70 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package login
import (
"fmt"
"code.gitea.io/gitea/models/db"
)
// ________ _____ __ .__
// \_____ \ / _ \ __ ___/ |_| |__
// / | \ / /_\ \| | \ __\ | \
// / | \/ | \ | /| | | Y \
// \_______ /\____|__ /____/ |__| |___| /
// \/ \/ \/
// ErrOAuthClientIDInvalid will be thrown if client id cannot be found
type ErrOAuthClientIDInvalid struct {
ClientID string
}
// IsErrOauthClientIDInvalid checks if an error is a ErrReviewNotExist.
func IsErrOauthClientIDInvalid(err error) bool {
_, ok := err.(ErrOAuthClientIDInvalid)
return ok
}
// Error returns the error message
func (err ErrOAuthClientIDInvalid) Error() string {
return fmt.Sprintf("Client ID invalid [Client ID: %s]", err.ClientID)
}
// ErrOAuthApplicationNotFound will be thrown if id cannot be found
type ErrOAuthApplicationNotFound struct {
ID int64
}
// IsErrOAuthApplicationNotFound checks if an error is a ErrReviewNotExist.
func IsErrOAuthApplicationNotFound(err error) bool {
_, ok := err.(ErrOAuthApplicationNotFound)
return ok
}
// Error returns the error message
func (err ErrOAuthApplicationNotFound) Error() string {
return fmt.Sprintf("OAuth application not found [ID: %d]", err.ID)
}
// GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources
func GetActiveOAuth2ProviderLoginSources() ([]*Source, error) {
sources := make([]*Source, 0, 1)
if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, OAuth2).Find(&sources); err != nil {
return nil, err
}
return sources, nil
}
// GetActiveOAuth2LoginSourceByName returns a OAuth2 LoginSource based on the given name
func GetActiveOAuth2LoginSourceByName(name string) (*Source, error) {
loginSource := new(Source)
has, err := db.GetEngine(db.DefaultContext).Where("name = ? and type = ? and is_active = ?", name, OAuth2, true).Get(loginSource)
if !has || err != nil {
return nil, err
}
return loginSource, nil
}

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package login
import ( import (
"crypto/sha256" "crypto/sha256"
@ -23,19 +23,14 @@ import (
// OAuth2Application represents an OAuth2 client (RFC 6749) // OAuth2Application represents an OAuth2 client (RFC 6749)
type OAuth2Application struct { type OAuth2Application struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
UID int64 `xorm:"INDEX"` UID int64 `xorm:"INDEX"`
User *User `xorm:"-"` Name string
Name string
ClientID string `xorm:"unique"` ClientID string `xorm:"unique"`
ClientSecret string ClientSecret string
RedirectURIs []string `xorm:"redirect_uris JSON TEXT"`
RedirectURIs []string `xorm:"redirect_uris JSON TEXT"` CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
} }
func init() { func init() {
@ -57,14 +52,6 @@ func (app *OAuth2Application) PrimaryRedirectURI() string {
return app.RedirectURIs[0] return app.RedirectURIs[0]
} }
// LoadUser will load User by UID
func (app *OAuth2Application) LoadUser() (err error) {
if app.User == nil {
app.User, err = GetUserByID(app.UID)
}
return
}
// ContainsRedirectURI checks if redirectURI is allowed for app // ContainsRedirectURI checks if redirectURI is allowed for app
func (app *OAuth2Application) ContainsRedirectURI(redirectURI string) bool { func (app *OAuth2Application) ContainsRedirectURI(redirectURI string) bool {
return util.IsStringInSlice(redirectURI, app.RedirectURIs, true) return util.IsStringInSlice(redirectURI, app.RedirectURIs, true)
@ -276,13 +263,13 @@ func DeleteOAuth2Application(id, userid int64) error {
} }
// ListOAuth2Applications returns a list of oauth2 applications belongs to given user. // ListOAuth2Applications returns a list of oauth2 applications belongs to given user.
func ListOAuth2Applications(uid int64, listOptions ListOptions) ([]*OAuth2Application, int64, error) { func ListOAuth2Applications(uid int64, listOptions db.ListOptions) ([]*OAuth2Application, int64, error) {
sess := db.GetEngine(db.DefaultContext). sess := db.GetEngine(db.DefaultContext).
Where("uid=?", uid). Where("uid=?", uid).
Desc("id") Desc("id")
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
apps := make([]*OAuth2Application, 0, listOptions.PageSize) apps := make([]*OAuth2Application, 0, listOptions.PageSize)
total, err := sess.FindAndCount(&apps) total, err := sess.FindAndCount(&apps)

@ -2,12 +2,13 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package login
import ( import (
"testing" "testing"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -69,13 +70,6 @@ func TestCreateOAuth2Application(t *testing.T) {
db.AssertExistsAndLoadBean(t, &OAuth2Application{Name: "newapp"}) db.AssertExistsAndLoadBean(t, &OAuth2Application{Name: "newapp"})
} }
func TestOAuth2Application_LoadUser(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
assert.NoError(t, app.LoadUser())
assert.NotNil(t, app.User)
}
func TestOAuth2Application_TableName(t *testing.T) { func TestOAuth2Application_TableName(t *testing.T) {
assert.Equal(t, "oauth2_application", new(OAuth2Application).TableName()) assert.Equal(t, "oauth2_application", new(OAuth2Application).TableName())
} }

@ -3,9 +3,10 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package login
import ( import (
"fmt"
"reflect" "reflect"
"strconv" "strconv"
@ -17,43 +18,43 @@ import (
"xorm.io/xorm/convert" "xorm.io/xorm/convert"
) )
// LoginType represents an login type. // Type represents an login type.
type LoginType int type Type int
// Note: new type must append to the end of list to maintain compatibility. // Note: new type must append to the end of list to maintain compatibility.
const ( const (
LoginNoType LoginType = iota NoType Type = iota
LoginPlain // 1 Plain // 1
LoginLDAP // 2 LDAP // 2
LoginSMTP // 3 SMTP // 3
LoginPAM // 4 PAM // 4
LoginDLDAP // 5 DLDAP // 5
LoginOAuth2 // 6 OAuth2 // 6
LoginSSPI // 7 SSPI // 7
) )
// String returns the string name of the LoginType // String returns the string name of the LoginType
func (typ LoginType) String() string { func (typ Type) String() string {
return LoginNames[typ] return Names[typ]
} }
// Int returns the int value of the LoginType // Int returns the int value of the LoginType
func (typ LoginType) Int() int { func (typ Type) Int() int {
return int(typ) return int(typ)
} }
// LoginNames contains the name of LoginType values. // Names contains the name of LoginType values.
var LoginNames = map[LoginType]string{ var Names = map[Type]string{
LoginLDAP: "LDAP (via BindDN)", LDAP: "LDAP (via BindDN)",
LoginDLDAP: "LDAP (simple auth)", // Via direct bind DLDAP: "LDAP (simple auth)", // Via direct bind
LoginSMTP: "SMTP", SMTP: "SMTP",
LoginPAM: "PAM", PAM: "PAM",
LoginOAuth2: "OAuth2", OAuth2: "OAuth2",
LoginSSPI: "SPNEGO with SSPI", SSPI: "SPNEGO with SSPI",
} }
// LoginConfig represents login config as far as the db is concerned // Config represents login config as far as the db is concerned
type LoginConfig interface { type Config interface {
convert.Conversion convert.Conversion
} }
@ -83,33 +84,33 @@ type RegisterableSource interface {
UnregisterSource() error UnregisterSource() error
} }
// LoginSourceSettable configurations can have their loginSource set on them // SourceSettable configurations can have their loginSource set on them
type LoginSourceSettable interface { type SourceSettable interface {
SetLoginSource(*LoginSource) SetLoginSource(*Source)
} }
// RegisterLoginTypeConfig register a config for a provided type // RegisterTypeConfig register a config for a provided type
func RegisterLoginTypeConfig(typ LoginType, exemplar LoginConfig) { func RegisterTypeConfig(typ Type, exemplar Config) {
if reflect.TypeOf(exemplar).Kind() == reflect.Ptr { if reflect.TypeOf(exemplar).Kind() == reflect.Ptr {
// Pointer: // Pointer:
registeredLoginConfigs[typ] = func() LoginConfig { registeredConfigs[typ] = func() Config {
return reflect.New(reflect.ValueOf(exemplar).Elem().Type()).Interface().(LoginConfig) return reflect.New(reflect.ValueOf(exemplar).Elem().Type()).Interface().(Config)
} }
return return
} }
// Not a Pointer // Not a Pointer
registeredLoginConfigs[typ] = func() LoginConfig { registeredConfigs[typ] = func() Config {
return reflect.New(reflect.TypeOf(exemplar)).Elem().Interface().(LoginConfig) return reflect.New(reflect.TypeOf(exemplar)).Elem().Interface().(Config)
} }
} }
var registeredLoginConfigs = map[LoginType]func() LoginConfig{} var registeredConfigs = map[Type]func() Config{}
// LoginSource represents an external way for authorizing users. // Source represents an external way for authorizing users.
type LoginSource struct { type Source struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
Type LoginType Type Type
Name string `xorm:"UNIQUE"` Name string `xorm:"UNIQUE"`
IsActive bool `xorm:"INDEX NOT NULL DEFAULT false"` IsActive bool `xorm:"INDEX NOT NULL DEFAULT false"`
IsSyncEnabled bool `xorm:"INDEX NOT NULL DEFAULT false"` IsSyncEnabled bool `xorm:"INDEX NOT NULL DEFAULT false"`
@ -119,8 +120,13 @@ type LoginSource struct {
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
} }
// TableName xorm will read the table name from this method
func (Source) TableName() string {
return "login_source"
}
func init() { func init() {
db.RegisterModel(new(LoginSource)) db.RegisterModel(new(Source))
} }
// Cell2Int64 converts a xorm.Cell type to int64, // Cell2Int64 converts a xorm.Cell type to int64,
@ -137,82 +143,82 @@ func Cell2Int64(val xorm.Cell) int64 {
} }
// BeforeSet is invoked from XORM before setting the value of a field of this object. // BeforeSet is invoked from XORM before setting the value of a field of this object.
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) { func (source *Source) BeforeSet(colName string, val xorm.Cell) {
if colName == "type" { if colName == "type" {
typ := LoginType(Cell2Int64(val)) typ := Type(Cell2Int64(val))
constructor, ok := registeredLoginConfigs[typ] constructor, ok := registeredConfigs[typ]
if !ok { if !ok {
return return
} }
source.Cfg = constructor() source.Cfg = constructor()
if settable, ok := source.Cfg.(LoginSourceSettable); ok { if settable, ok := source.Cfg.(SourceSettable); ok {
settable.SetLoginSource(source) settable.SetLoginSource(source)
} }
} }
} }
// TypeName return name of this login source type. // TypeName return name of this login source type.
func (source *LoginSource) TypeName() string { func (source *Source) TypeName() string {
return LoginNames[source.Type] return Names[source.Type]
} }
// IsLDAP returns true of this source is of the LDAP type. // IsLDAP returns true of this source is of the LDAP type.
func (source *LoginSource) IsLDAP() bool { func (source *Source) IsLDAP() bool {
return source.Type == LoginLDAP return source.Type == LDAP
} }
// IsDLDAP returns true of this source is of the DLDAP type. // IsDLDAP returns true of this source is of the DLDAP type.
func (source *LoginSource) IsDLDAP() bool { func (source *Source) IsDLDAP() bool {
return source.Type == LoginDLDAP return source.Type == DLDAP
} }
// IsSMTP returns true of this source is of the SMTP type. // IsSMTP returns true of this source is of the SMTP type.
func (source *LoginSource) IsSMTP() bool { func (source *Source) IsSMTP() bool {
return source.Type == LoginSMTP return source.Type == SMTP
} }
// IsPAM returns true of this source is of the PAM type. // IsPAM returns true of this source is of the PAM type.
func (source *LoginSource) IsPAM() bool { func (source *Source) IsPAM() bool {
return source.Type == LoginPAM return source.Type == PAM
} }
// IsOAuth2 returns true of this source is of the OAuth2 type. // IsOAuth2 returns true of this source is of the OAuth2 type.
func (source *LoginSource) IsOAuth2() bool { func (source *Source) IsOAuth2() bool {
return source.Type == LoginOAuth2 return source.Type == OAuth2
} }
// IsSSPI returns true of this source is of the SSPI type. // IsSSPI returns true of this source is of the SSPI type.
func (source *LoginSource) IsSSPI() bool { func (source *Source) IsSSPI() bool {
return source.Type == LoginSSPI return source.Type == SSPI
} }
// HasTLS returns true of this source supports TLS. // HasTLS returns true of this source supports TLS.
func (source *LoginSource) HasTLS() bool { func (source *Source) HasTLS() bool {
hasTLSer, ok := source.Cfg.(HasTLSer) hasTLSer, ok := source.Cfg.(HasTLSer)
return ok && hasTLSer.HasTLS() return ok && hasTLSer.HasTLS()
} }
// UseTLS returns true of this source is configured to use TLS. // UseTLS returns true of this source is configured to use TLS.
func (source *LoginSource) UseTLS() bool { func (source *Source) UseTLS() bool {
useTLSer, ok := source.Cfg.(UseTLSer) useTLSer, ok := source.Cfg.(UseTLSer)
return ok && useTLSer.UseTLS() return ok && useTLSer.UseTLS()
} }
// SkipVerify returns true if this source is configured to skip SSL // SkipVerify returns true if this source is configured to skip SSL
// verification. // verification.
func (source *LoginSource) SkipVerify() bool { func (source *Source) SkipVerify() bool {
skipVerifiable, ok := source.Cfg.(SkipVerifiable) skipVerifiable, ok := source.Cfg.(SkipVerifiable)
return ok && skipVerifiable.IsSkipVerify() return ok && skipVerifiable.IsSkipVerify()
} }
// CreateLoginSource inserts a LoginSource in the DB if not already // CreateSource inserts a LoginSource in the DB if not already
// existing with the given name. // existing with the given name.
func CreateLoginSource(source *LoginSource) error { func CreateSource(source *Source) error {
has, err := db.GetEngine(db.DefaultContext).Where("name=?", source.Name).Exist(new(LoginSource)) has, err := db.GetEngine(db.DefaultContext).Where("name=?", source.Name).Exist(new(Source))
if err != nil { if err != nil {
return err return err
} else if has { } else if has {
return ErrLoginSourceAlreadyExist{source.Name} return ErrSourceAlreadyExist{source.Name}
} }
// Synchronization is only available with LDAP for now // Synchronization is only available with LDAP for now
if !source.IsLDAP() { if !source.IsLDAP() {
@ -228,7 +234,7 @@ func CreateLoginSource(source *LoginSource) error {
return nil return nil
} }
if settable, ok := source.Cfg.(LoginSourceSettable); ok { if settable, ok := source.Cfg.(SourceSettable); ok {
settable.SetLoginSource(source) settable.SetLoginSource(source)
} }
@ -241,40 +247,40 @@ func CreateLoginSource(source *LoginSource) error {
if err != nil { if err != nil {
// remove the LoginSource in case of errors while registering configuration // remove the LoginSource in case of errors while registering configuration
if _, err := db.GetEngine(db.DefaultContext).Delete(source); err != nil { if _, err := db.GetEngine(db.DefaultContext).Delete(source); err != nil {
log.Error("CreateLoginSource: Error while wrapOpenIDConnectInitializeError: %v", err) log.Error("CreateSource: Error while wrapOpenIDConnectInitializeError: %v", err)
} }
} }
return err return err
} }
// LoginSources returns a slice of all login sources found in DB. // Sources returns a slice of all login sources found in DB.
func LoginSources() ([]*LoginSource, error) { func Sources() ([]*Source, error) {
auths := make([]*LoginSource, 0, 6) auths := make([]*Source, 0, 6)
return auths, db.GetEngine(db.DefaultContext).Find(&auths) return auths, db.GetEngine(db.DefaultContext).Find(&auths)
} }
// LoginSourcesByType returns all sources of the specified type // SourcesByType returns all sources of the specified type
func LoginSourcesByType(loginType LoginType) ([]*LoginSource, error) { func SourcesByType(loginType Type) ([]*Source, error) {
sources := make([]*LoginSource, 0, 1) sources := make([]*Source, 0, 1)
if err := db.GetEngine(db.DefaultContext).Where("type = ?", loginType).Find(&sources); err != nil { if err := db.GetEngine(db.DefaultContext).Where("type = ?", loginType).Find(&sources); err != nil {
return nil, err return nil, err
} }
return sources, nil return sources, nil
} }
// AllActiveLoginSources returns all active sources // AllActiveSources returns all active sources
func AllActiveLoginSources() ([]*LoginSource, error) { func AllActiveSources() ([]*Source, error) {
sources := make([]*LoginSource, 0, 5) sources := make([]*Source, 0, 5)
if err := db.GetEngine(db.DefaultContext).Where("is_active = ?", true).Find(&sources); err != nil { if err := db.GetEngine(db.DefaultContext).Where("is_active = ?", true).Find(&sources); err != nil {
return nil, err return nil, err
} }
return sources, nil return sources, nil
} }
// ActiveLoginSources returns all active sources of the specified type // ActiveSources returns all active sources of the specified type
func ActiveLoginSources(loginType LoginType) ([]*LoginSource, error) { func ActiveSources(tp Type) ([]*Source, error) {
sources := make([]*LoginSource, 0, 1) sources := make([]*Source, 0, 1)
if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, loginType).Find(&sources); err != nil { if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, tp).Find(&sources); err != nil {
return nil, err return nil, err
} }
return sources, nil return sources, nil
@ -286,19 +292,19 @@ func IsSSPIEnabled() bool {
if !db.HasEngine { if !db.HasEngine {
return false return false
} }
sources, err := ActiveLoginSources(LoginSSPI) sources, err := ActiveSources(SSPI)
if err != nil { if err != nil {
log.Error("ActiveLoginSources: %v", err) log.Error("ActiveSources: %v", err)
return false return false
} }
return len(sources) > 0 return len(sources) > 0
} }
// GetLoginSourceByID returns login source by given ID. // GetSourceByID returns login source by given ID.
func GetLoginSourceByID(id int64) (*LoginSource, error) { func GetSourceByID(id int64) (*Source, error) {
source := new(LoginSource) source := new(Source)
if id == 0 { if id == 0 {
source.Cfg = registeredLoginConfigs[LoginNoType]() source.Cfg = registeredConfigs[NoType]()
// Set this source to active // Set this source to active
// FIXME: allow disabling of db based password authentication in future // FIXME: allow disabling of db based password authentication in future
source.IsActive = true source.IsActive = true
@ -309,18 +315,18 @@ func GetLoginSourceByID(id int64) (*LoginSource, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrLoginSourceNotExist{id} return nil, ErrSourceNotExist{id}
} }
return source, nil return source, nil
} }
// UpdateSource updates a LoginSource record in DB. // UpdateSource updates a Source record in DB.
func UpdateSource(source *LoginSource) error { func UpdateSource(source *Source) error {
var originalLoginSource *LoginSource var originalLoginSource *Source
if source.IsOAuth2() { if source.IsOAuth2() {
// keep track of the original values so we can restore in case of errors while registering OAuth2 providers // keep track of the original values so we can restore in case of errors while registering OAuth2 providers
var err error var err error
if originalLoginSource, err = GetLoginSourceByID(source.ID); err != nil { if originalLoginSource, err = GetSourceByID(source.ID); err != nil {
return err return err
} }
} }
@ -334,7 +340,7 @@ func UpdateSource(source *LoginSource) error {
return nil return nil
} }
if settable, ok := source.Cfg.(LoginSourceSettable); ok { if settable, ok := source.Cfg.(SourceSettable); ok {
settable.SetLoginSource(source) settable.SetLoginSource(source)
} }
@ -353,34 +359,53 @@ func UpdateSource(source *LoginSource) error {
return err return err
} }
// DeleteSource deletes a LoginSource record in DB. // CountSources returns number of login sources.
func DeleteSource(source *LoginSource) error { func CountSources() int64 {
count, err := db.GetEngine(db.DefaultContext).Count(&User{LoginSource: source.ID}) count, _ := db.GetEngine(db.DefaultContext).Count(new(Source))
if err != nil { return count
return err }
} else if count > 0 {
return ErrLoginSourceInUse{source.ID}
}
count, err = db.GetEngine(db.DefaultContext).Count(&ExternalLoginUser{LoginSourceID: source.ID}) // ErrSourceNotExist represents a "SourceNotExist" kind of error.
if err != nil { type ErrSourceNotExist struct {
return err ID int64
} else if count > 0 { }
return ErrLoginSourceInUse{source.ID}
}
if registerableSource, ok := source.Cfg.(RegisterableSource); ok { // IsErrSourceNotExist checks if an error is a ErrSourceNotExist.
if err := registerableSource.UnregisterSource(); err != nil { func IsErrSourceNotExist(err error) bool {
return err _, ok := err.(ErrSourceNotExist)
} return ok
} }
_, err = db.GetEngine(db.DefaultContext).ID(source.ID).Delete(new(LoginSource)) func (err ErrSourceNotExist) Error() string {
return err return fmt.Sprintf("login source does not exist [id: %d]", err.ID)
} }
// CountLoginSources returns number of login sources. // ErrSourceAlreadyExist represents a "SourceAlreadyExist" kind of error.
func CountLoginSources() int64 { type ErrSourceAlreadyExist struct {
count, _ := db.GetEngine(db.DefaultContext).Count(new(LoginSource)) Name string
return count }
// IsErrSourceAlreadyExist checks if an error is a ErrSourceAlreadyExist.
func IsErrSourceAlreadyExist(err error) bool {
_, ok := err.(ErrSourceAlreadyExist)
return ok
}
func (err ErrSourceAlreadyExist) Error() string {
return fmt.Sprintf("login source already exists [name: %s]", err.Name)
}
// ErrSourceInUse represents a "SourceInUse" kind of error.
type ErrSourceInUse struct {
ID int64
}
// IsErrSourceInUse checks if an error is a ErrSourceInUse.
func IsErrSourceInUse(err error) bool {
_, ok := err.(ErrSourceInUse)
return ok
}
func (err ErrSourceInUse) Error() string {
return fmt.Sprintf("login source is still used by some users [id: %d]", err.ID)
} }

@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package login
import ( import (
"strings" "strings"
@ -36,13 +36,13 @@ func (source *TestSource) ToDB() ([]byte, error) {
func TestDumpLoginSource(t *testing.T) { func TestDumpLoginSource(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
loginSourceSchema, err := db.TableInfo(new(LoginSource)) loginSourceSchema, err := db.TableInfo(new(Source))
assert.NoError(t, err) assert.NoError(t, err)
RegisterLoginTypeConfig(LoginOAuth2, new(TestSource)) RegisterTypeConfig(OAuth2, new(TestSource))
CreateLoginSource(&LoginSource{ CreateSource(&Source{
Type: LoginOAuth2, Type: OAuth2,
Name: "TestSource", Name: "TestSource",
IsActive: false, IsActive: false,
Cfg: &TestSource{ Cfg: &TestSource{

@ -241,7 +241,10 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
if _, err := os.Stat(fixturesDir); err == nil { if _, err := os.Stat(fixturesDir); err == nil {
t.Logf("initializing fixtures from: %s", fixturesDir) t.Logf("initializing fixtures from: %s", fixturesDir)
if err := db.InitFixtures(fixturesDir, x); err != nil { if err := db.InitFixtures(
db.FixturesOptions{
Dir: fixturesDir,
}, x); err != nil {
t.Errorf("error whilst initializing fixtures from %s: %v", fixturesDir, err) t.Errorf("error whilst initializing fixtures from %s: %v", fixturesDir, err)
return x, deferFn return x, deferFn
} }

@ -74,7 +74,7 @@ func init() {
// FindNotificationOptions represent the filters for notifications. If an ID is 0 it will be ignored. // FindNotificationOptions represent the filters for notifications. If an ID is 0 it will be ignored.
type FindNotificationOptions struct { type FindNotificationOptions struct {
ListOptions db.ListOptions
UserID int64 UserID int64
RepoID int64 RepoID int64
IssueID int64 IssueID int64
@ -115,7 +115,7 @@ func (opts *FindNotificationOptions) ToCond() builder.Cond {
func (opts *FindNotificationOptions) ToSession(e db.Engine) *xorm.Session { func (opts *FindNotificationOptions) ToSession(e db.Engine) *xorm.Session {
sess := e.Where(opts.ToCond()) sess := e.Where(opts.ToCond())
if opts.Page != 0 { if opts.Page != 0 {
sess = setSessionPagination(sess, opts) sess = db.SetSessionPagination(sess, opts)
} }
return sess return sess
} }

@ -1,27 +0,0 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import "code.gitea.io/gitea/models/db"
// GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources
func GetActiveOAuth2ProviderLoginSources() ([]*LoginSource, error) {
sources := make([]*LoginSource, 0, 1)
if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, LoginOAuth2).Find(&sources); err != nil {
return nil, err
}
return sources, nil
}
// GetActiveOAuth2LoginSourceByName returns a OAuth2 LoginSource based on the given name
func GetActiveOAuth2LoginSourceByName(name string) (*LoginSource, error) {
loginSource := new(LoginSource)
has, err := db.GetEngine(db.DefaultContext).Where("name = ? and type = ? and is_active = ?", name, LoginOAuth2, true).Get(loginSource)
if !has || err != nil {
return nil, err
}
return loginSource, nil
}

@ -78,7 +78,7 @@ func (org *User) GetMembers() (err error) {
// FindOrgMembersOpts represensts find org members conditions // FindOrgMembersOpts represensts find org members conditions
type FindOrgMembersOpts struct { type FindOrgMembersOpts struct {
ListOptions db.ListOptions
OrgID int64 OrgID int64
PublicOnly bool PublicOnly bool
} }
@ -574,7 +574,7 @@ func GetOrgUsersByUserID(uid int64, opts *SearchOrganizationsOptions) ([]*OrgUse
} }
if opts.PageSize != 0 { if opts.PageSize != 0 {
sess = setSessionPagination(sess, opts) sess = db.SetSessionPagination(sess, opts)
} }
err := sess. err := sess.
@ -594,7 +594,7 @@ func getOrgUsersByOrgID(e db.Engine, opts *FindOrgMembersOpts) ([]*OrgUser, erro
sess.And("is_public = ?", true) sess.And("is_public = ?", true)
} }
if opts.ListOptions.PageSize > 0 { if opts.ListOptions.PageSize > 0 {
sess = setSessionPagination(sess, opts) sess = db.SetSessionPagination(sess, opts)
ous := make([]*OrgUser, 0, opts.PageSize) ous := make([]*OrgUser, 0, opts.PageSize)
return ous, sess.Find(&ous) return ous, sess.Find(&ous)

@ -47,7 +47,7 @@ func init() {
// SearchTeamOptions holds the search options // SearchTeamOptions holds the search options
type SearchTeamOptions struct { type SearchTeamOptions struct {
ListOptions db.ListOptions
UserID int64 UserID int64
Keyword string Keyword string
OrgID int64 OrgID int64
@ -56,7 +56,7 @@ type SearchTeamOptions struct {
// SearchMembersOptions holds the search options // SearchMembersOptions holds the search options
type SearchMembersOptions struct { type SearchMembersOptions struct {
ListOptions db.ListOptions
} }
// SearchTeam search for teams. Caller is responsible to check permissions. // SearchTeam search for teams. Caller is responsible to check permissions.
@ -176,7 +176,7 @@ func (t *Team) GetRepositories(opts *SearchTeamOptions) error {
return t.getRepositories(db.GetEngine(db.DefaultContext)) return t.getRepositories(db.GetEngine(db.DefaultContext))
} }
return t.getRepositories(getPaginatedSession(opts)) return t.getRepositories(db.GetPaginatedSession(opts))
} }
func (t *Team) getMembers(e db.Engine) (err error) { func (t *Team) getMembers(e db.Engine) (err error) {
@ -190,7 +190,7 @@ func (t *Team) GetMembers(opts *SearchMembersOptions) (err error) {
return t.getMembers(db.GetEngine(db.DefaultContext)) return t.getMembers(db.GetEngine(db.DefaultContext))
} }
return t.getMembers(getPaginatedSession(opts)) return t.getMembers(db.GetPaginatedSession(opts))
} }
// AddMember adds new membership of the team to the organization, // AddMember adds new membership of the team to the organization,

@ -399,7 +399,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
orgUsers, err := GetOrgUsersByOrgID(&FindOrgMembersOpts{ orgUsers, err := GetOrgUsersByOrgID(&FindOrgMembersOpts{
ListOptions: ListOptions{}, ListOptions: db.ListOptions{},
OrgID: 3, OrgID: 3,
PublicOnly: false, PublicOnly: false,
}) })
@ -420,7 +420,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) {
} }
orgUsers, err = GetOrgUsersByOrgID(&FindOrgMembersOpts{ orgUsers, err = GetOrgUsersByOrgID(&FindOrgMembersOpts{
ListOptions: ListOptions{}, ListOptions: db.ListOptions{},
OrgID: db.NonexistentID, OrgID: db.NonexistentID,
PublicOnly: false, PublicOnly: false,
}) })

@ -17,7 +17,7 @@ import (
// PullRequestsOptions holds the options for PRs // PullRequestsOptions holds the options for PRs
type PullRequestsOptions struct { type PullRequestsOptions struct {
ListOptions db.ListOptions
State string State string
SortType string SortType string
Labels []string Labels []string
@ -101,7 +101,7 @@ func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest,
log.Error("listPullRequestStatement: %v", err) log.Error("listPullRequestStatement: %v", err)
return nil, maxResults, err return nil, maxResults, err
} }
findSession = setSessionPagination(findSession, opts) findSession = db.SetSessionPagination(findSession, opts)
prs := make([]*PullRequest, 0, opts.PageSize) prs := make([]*PullRequest, 0, opts.PageSize)
return prs, maxResults, findSession.Find(&prs) return prs, maxResults, findSession.Find(&prs)
} }

@ -5,6 +5,7 @@
package models package models
import ( import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -35,7 +36,7 @@ Loop:
case always: case always:
break Loop break Loop
case pubkey: case pubkey:
keys, err := ListGPGKeys(u.ID, ListOptions{}) keys, err := ListGPGKeys(u.ID, db.ListOptions{})
if err != nil { if err != nil {
return false, "", nil, err return false, "", nil, err
} }

@ -56,7 +56,7 @@ func TestPullRequest_LoadHeadRepo(t *testing.T) {
func TestPullRequestsNewest(t *testing.T) { func TestPullRequestsNewest(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
prs, count, err := PullRequests(1, &PullRequestsOptions{ prs, count, err := PullRequests(1, &PullRequestsOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
}, },
State: "open", State: "open",
@ -75,7 +75,7 @@ func TestPullRequestsNewest(t *testing.T) {
func TestPullRequestsOldest(t *testing.T) { func TestPullRequestsOldest(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
prs, count, err := PullRequests(1, &PullRequestsOptions{ prs, count, err := PullRequests(1, &PullRequestsOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
}, },
State: "open", State: "open",

@ -177,7 +177,7 @@ func GetReleaseByID(id int64) (*Release, error) {
// FindReleasesOptions describes the conditions to Find releases // FindReleasesOptions describes the conditions to Find releases
type FindReleasesOptions struct { type FindReleasesOptions struct {
ListOptions db.ListOptions
IncludeDrafts bool IncludeDrafts bool
IncludeTags bool IncludeTags bool
IsPreRelease util.OptionalBool IsPreRelease util.OptionalBool
@ -214,7 +214,7 @@ func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions) ([]*Release, er
Where(opts.toConds(repoID)) Where(opts.toConds(repoID))
if opts.PageSize != 0 { if opts.PageSize != 0 {
sess = setSessionPagination(sess, &opts.ListOptions) sess = db.SetSessionPagination(sess, &opts.ListOptions)
} }
rels := make([]*Release, 0, opts.PageSize) rels := make([]*Release, 0, opts.PageSize)

@ -1772,7 +1772,7 @@ func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, int64, error)
sess.Where(cond).OrderBy(opts.OrderBy.String()) sess.Where(cond).OrderBy(opts.OrderBy.String())
repos := make([]*Repository, 0, opts.PageSize) repos := make([]*Repository, 0, opts.PageSize)
return repos, count, setSessionPagination(sess, opts).Find(&repos) return repos, count, db.SetSessionPagination(sess, opts).Find(&repos)
} }
// GetUserMirrorRepositories returns a list of mirror repositories of given user. // GetUserMirrorRepositories returns a list of mirror repositories of given user.
@ -2057,13 +2057,13 @@ func CopyLFS(ctx context.Context, newRepo, oldRepo *Repository) error {
} }
// GetForks returns all the forks of the repository // GetForks returns all the forks of the repository
func (repo *Repository) GetForks(listOptions ListOptions) ([]*Repository, error) { func (repo *Repository) GetForks(listOptions db.ListOptions) ([]*Repository, error) {
if listOptions.Page == 0 { if listOptions.Page == 0 {
forks := make([]*Repository, 0, repo.NumForks) forks := make([]*Repository, 0, repo.NumForks)
return forks, db.GetEngine(db.DefaultContext).Find(&forks, &Repository{ForkID: repo.ID}) return forks, db.GetEngine(db.DefaultContext).Find(&forks, &Repository{ForkID: repo.ID})
} }
sess := getPaginatedSession(&listOptions) sess := db.GetPaginatedSession(&listOptions)
forks := make([]*Repository, 0, listOptions.PageSize) forks := make([]*Repository, 0, listOptions.PageSize)
return forks, sess.Find(&forks, &Repository{ForkID: repo.ID}) return forks, sess.Find(&forks, &Repository{ForkID: repo.ID})
} }

@ -64,13 +64,13 @@ func (repo *Repository) AddCollaborator(u *User) error {
return sess.Commit() return sess.Commit()
} }
func (repo *Repository) getCollaborations(e db.Engine, listOptions ListOptions) ([]*Collaboration, error) { func (repo *Repository) getCollaborations(e db.Engine, listOptions db.ListOptions) ([]*Collaboration, error) {
if listOptions.Page == 0 { if listOptions.Page == 0 {
collaborations := make([]*Collaboration, 0, 8) collaborations := make([]*Collaboration, 0, 8)
return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID}) return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
} }
e = setEnginePagination(e, &listOptions) e = db.SetEnginePagination(e, &listOptions)
collaborations := make([]*Collaboration, 0, listOptions.PageSize) collaborations := make([]*Collaboration, 0, listOptions.PageSize)
return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID}) return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
@ -82,7 +82,7 @@ type Collaborator struct {
Collaboration *Collaboration Collaboration *Collaboration
} }
func (repo *Repository) getCollaborators(e db.Engine, listOptions ListOptions) ([]*Collaborator, error) { func (repo *Repository) getCollaborators(e db.Engine, listOptions db.ListOptions) ([]*Collaborator, error) {
collaborations, err := repo.getCollaborations(e, listOptions) collaborations, err := repo.getCollaborations(e, listOptions)
if err != nil { if err != nil {
return nil, fmt.Errorf("getCollaborations: %v", err) return nil, fmt.Errorf("getCollaborations: %v", err)
@ -103,7 +103,7 @@ func (repo *Repository) getCollaborators(e db.Engine, listOptions ListOptions) (
} }
// GetCollaborators returns the collaborators for a repository // GetCollaborators returns the collaborators for a repository
func (repo *Repository) GetCollaborators(listOptions ListOptions) ([]*Collaborator, error) { func (repo *Repository) GetCollaborators(listOptions db.ListOptions) ([]*Collaborator, error) {
return repo.getCollaborators(db.GetEngine(db.DefaultContext), listOptions) return repo.getCollaborators(db.GetEngine(db.DefaultContext), listOptions)
} }

@ -30,7 +30,7 @@ func TestRepository_GetCollaborators(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
test := func(repoID int64) { test := func(repoID int64) {
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
collaborators, err := repo.GetCollaborators(ListOptions{}) collaborators, err := repo.GetCollaborators(db.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
expectedLen, err := db.GetEngine(db.DefaultContext).Count(&Collaboration{RepoID: repoID}) expectedLen, err := db.GetEngine(db.DefaultContext).Count(&Collaboration{RepoID: repoID})
assert.NoError(t, err) assert.NoError(t, err)

@ -151,7 +151,7 @@ func GenerateAvatar(ctx context.Context, templateRepo, generateRepo *Repository)
// GenerateIssueLabels generates issue labels from a template repository // GenerateIssueLabels generates issue labels from a template repository
func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *Repository) error { func GenerateIssueLabels(ctx context.Context, templateRepo, generateRepo *Repository) error {
templateLabels, err := getLabelsByRepoID(db.GetEngine(ctx), templateRepo.ID, "", ListOptions{}) templateLabels, err := getLabelsByRepoID(db.GetEngine(ctx), templateRepo.ID, "", db.ListOptions{})
if err != nil { if err != nil {
return err return err
} }

@ -135,7 +135,7 @@ func (repos MirrorRepositoryList) LoadAttributes() error {
// SearchRepoOptions holds the search options // SearchRepoOptions holds the search options
type SearchRepoOptions struct { type SearchRepoOptions struct {
ListOptions db.ListOptions
Actor *User Actor *User
Keyword string Keyword string
OwnerID int64 OwnerID int64

@ -18,7 +18,7 @@ func TestSearchRepository(t *testing.T) {
// test search public repository on explore page // test search public repository on explore page
repos, count, err := SearchRepositoryByName(&SearchRepoOptions{ repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 10, PageSize: 10,
}, },
@ -33,7 +33,7 @@ func TestSearchRepository(t *testing.T) {
assert.Equal(t, int64(1), count) assert.Equal(t, int64(1), count)
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{ repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 10, PageSize: 10,
}, },
@ -47,7 +47,7 @@ func TestSearchRepository(t *testing.T) {
// test search private repository on explore page // test search private repository on explore page
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{ repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 10, PageSize: 10,
}, },
@ -63,7 +63,7 @@ func TestSearchRepository(t *testing.T) {
assert.Equal(t, int64(1), count) assert.Equal(t, int64(1), count)
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{ repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 10, PageSize: 10,
}, },
@ -85,7 +85,7 @@ func TestSearchRepository(t *testing.T) {
// Test search within description // Test search within description
repos, count, err = SearchRepository(&SearchRepoOptions{ repos, count, err = SearchRepository(&SearchRepoOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 10, PageSize: 10,
}, },
@ -102,7 +102,7 @@ func TestSearchRepository(t *testing.T) {
// Test NOT search within description // Test NOT search within description
repos, count, err = SearchRepository(&SearchRepoOptions{ repos, count, err = SearchRepository(&SearchRepoOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
Page: 1, Page: 1,
PageSize: 10, PageSize: 10,
}, },
@ -122,142 +122,142 @@ func TestSearchRepository(t *testing.T) {
}{ }{
{ {
name: "PublicRepositoriesByName", name: "PublicRepositoriesByName",
opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{PageSize: 10}, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, Collaborate: util.OptionalBoolFalse},
count: 7, count: 7,
}, },
{ {
name: "PublicAndPrivateRepositoriesByName", name: "PublicAndPrivateRepositoriesByName",
opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 1, PageSize: 10}, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, Collaborate: util.OptionalBoolFalse},
count: 14, count: 14,
}, },
{ {
name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage", name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFirstPage",
opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 1, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
count: 14, count: 14,
}, },
{ {
name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage", name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitSecondPage",
opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 2, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 2, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
count: 14, count: 14,
}, },
{ {
name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage", name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitThirdPage",
opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
count: 14, count: 14,
}, },
{ {
name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage", name: "PublicAndPrivateRepositoriesByNameWithPagesizeLimitFourthPage",
opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 3, PageSize: 5}, Private: true, Collaborate: util.OptionalBoolFalse},
count: 14, count: 14,
}, },
{ {
name: "PublicRepositoriesOfUser", name: "PublicRepositoriesOfUser",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Collaborate: util.OptionalBoolFalse},
count: 2, count: 2,
}, },
{ {
name: "PublicRepositoriesOfUser2", name: "PublicRepositoriesOfUser2",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Collaborate: util.OptionalBoolFalse},
count: 0, count: 0,
}, },
{ {
name: "PublicRepositoriesOfUser3", name: "PublicRepositoriesOfUser3",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Collaborate: util.OptionalBoolFalse},
count: 2, count: 2,
}, },
{ {
name: "PublicAndPrivateRepositoriesOfUser", name: "PublicAndPrivateRepositoriesOfUser",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, Collaborate: util.OptionalBoolFalse},
count: 4, count: 4,
}, },
{ {
name: "PublicAndPrivateRepositoriesOfUser2", name: "PublicAndPrivateRepositoriesOfUser2",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, Collaborate: util.OptionalBoolFalse},
count: 0, count: 0,
}, },
{ {
name: "PublicAndPrivateRepositoriesOfUser3", name: "PublicAndPrivateRepositoriesOfUser3",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true, Collaborate: util.OptionalBoolFalse},
count: 4, count: 4,
}, },
{ {
name: "PublicRepositoriesOfUserIncludingCollaborative", name: "PublicRepositoriesOfUserIncludingCollaborative",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15},
count: 5, count: 5,
}, },
{ {
name: "PublicRepositoriesOfUser2IncludingCollaborative", name: "PublicRepositoriesOfUser2IncludingCollaborative",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18},
count: 1, count: 1,
}, },
{ {
name: "PublicRepositoriesOfUser3IncludingCollaborative", name: "PublicRepositoriesOfUser3IncludingCollaborative",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 20}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20},
count: 3, count: 3,
}, },
{ {
name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative", name: "PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true},
count: 9, count: 9,
}, },
{ {
name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative", name: "PublicAndPrivateRepositoriesOfUser2IncludingCollaborative",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true},
count: 4, count: 4,
}, },
{ {
name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative", name: "PublicAndPrivateRepositoriesOfUser3IncludingCollaborative",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 20, Private: true},
count: 7, count: 7,
}, },
{ {
name: "PublicRepositoriesOfOrganization", name: "PublicRepositoriesOfOrganization",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Collaborate: util.OptionalBoolFalse},
count: 1, count: 1,
}, },
{ {
name: "PublicAndPrivateRepositoriesOfOrganization", name: "PublicAndPrivateRepositoriesOfOrganization",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, Private: true, Collaborate: util.OptionalBoolFalse},
count: 2, count: 2,
}, },
{ {
name: "AllPublic/PublicRepositoriesByName", name: "AllPublic/PublicRepositoriesByName",
opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{PageSize: 10}, AllPublic: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{PageSize: 10}, AllPublic: true, Collaborate: util.OptionalBoolFalse},
count: 7, count: 7,
}, },
{ {
name: "AllPublic/PublicAndPrivateRepositoriesByName", name: "AllPublic/PublicAndPrivateRepositoriesByName",
opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: ListOptions{Page: 1, PageSize: 10}, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse}, opts: &SearchRepoOptions{Keyword: "big_test_", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Private: true, AllPublic: true, Collaborate: util.OptionalBoolFalse},
count: 14, count: 14,
}, },
{ {
name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative", name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, AllPublic: true, Template: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, AllPublic: true, Template: util.OptionalBoolFalse},
count: 28, count: 28,
}, },
{ {
name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative", name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true, Template: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true, Template: util.OptionalBoolFalse},
count: 33, count: 33,
}, },
{ {
name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName", name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName",
opts: &SearchRepoOptions{Keyword: "test", ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true}, opts: &SearchRepoOptions{Keyword: "test", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true},
count: 15, count: 15,
}, },
{ {
name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName", name: "AllPublic/PublicAndPrivateRepositoriesOfUser2IncludingCollaborativeByName",
opts: &SearchRepoOptions{Keyword: "test", ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, AllPublic: true}, opts: &SearchRepoOptions{Keyword: "test", ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 18, Private: true, AllPublic: true},
count: 13, count: 13,
}, },
{ {
name: "AllPublic/PublicRepositoriesOfOrganization", name: "AllPublic/PublicRepositoriesOfOrganization",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse, Template: util.OptionalBoolFalse}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse, Template: util.OptionalBoolFalse},
count: 28, count: 28,
}, },
{ {
name: "AllTemplates", name: "AllTemplates",
opts: &SearchRepoOptions{ListOptions: ListOptions{Page: 1, PageSize: 10}, Template: util.OptionalBoolTrue}, opts: &SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, Template: util.OptionalBoolTrue},
count: 2, count: 2,
}, },
} }

@ -7,6 +7,7 @@ package models
import ( import (
"strings" "strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
@ -120,7 +121,7 @@ Loop:
case always: case always:
break Loop break Loop
case pubkey: case pubkey:
keys, err := ListGPGKeys(u.ID, ListOptions{}) keys, err := ListGPGKeys(u.ID, db.ListOptions{})
if err != nil { if err != nil {
return false, "", nil, err return false, "", nil, err
} }
@ -156,7 +157,7 @@ Loop:
case always: case always:
break Loop break Loop
case pubkey: case pubkey:
keys, err := ListGPGKeys(u.ID, ListOptions{}) keys, err := ListGPGKeys(u.ID, db.ListOptions{})
if err != nil { if err != nil {
return false, "", nil, err return false, "", nil, err
} }
@ -209,7 +210,7 @@ Loop:
case always: case always:
break Loop break Loop
case pubkey: case pubkey:
keys, err := ListGPGKeys(u.ID, ListOptions{}) keys, err := ListGPGKeys(u.ID, db.ListOptions{})
if err != nil { if err != nil {
return false, "", nil, err return false, "", nil, err
} }

@ -266,7 +266,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) (err e
} }
// Remove redundant collaborators. // Remove redundant collaborators.
collaborators, err := repo.getCollaborators(sess, ListOptions{}) collaborators, err := repo.getCollaborators(sess, db.ListOptions{})
if err != nil { if err != nil {
return fmt.Errorf("getCollaborators: %v", err) return fmt.Errorf("getCollaborators: %v", err)
} }

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
@ -153,7 +154,7 @@ func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int {
func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) { func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
switch colName { switch colName {
case "type": case "type":
switch UnitType(Cell2Int64(val)) { switch UnitType(login.Cell2Int64(val)) {
case UnitTypeCode, UnitTypeReleases, UnitTypeWiki, UnitTypeProjects: case UnitTypeCode, UnitTypeReleases, UnitTypeWiki, UnitTypeProjects:
r.Config = new(UnitConfig) r.Config = new(UnitConfig)
case UnitTypeExternalWiki: case UnitTypeExternalWiki:

@ -165,12 +165,12 @@ func getRepoWatchersIDs(e db.Engine, repoID int64) ([]int64, error) {
} }
// GetWatchers returns range of users watching given repository. // GetWatchers returns range of users watching given repository.
func (repo *Repository) GetWatchers(opts ListOptions) ([]*User, error) { func (repo *Repository) GetWatchers(opts db.ListOptions) ([]*User, error) {
sess := db.GetEngine(db.DefaultContext).Where("watch.repo_id=?", repo.ID). sess := db.GetEngine(db.DefaultContext).Where("watch.repo_id=?", repo.ID).
Join("LEFT", "watch", "`user`.id=`watch`.user_id"). Join("LEFT", "watch", "`user`.id=`watch`.user_id").
And("`watch`.mode<>?", RepoWatchModeDont) And("`watch`.mode<>?", RepoWatchModeDont)
if opts.Page > 0 { if opts.Page > 0 {
sess = setSessionPagination(sess, &opts) sess = db.SetSessionPagination(sess, &opts)
users := make([]*User, 0, opts.PageSize) users := make([]*User, 0, opts.PageSize)
return users, sess.Find(&users) return users, sess.Find(&users)

@ -60,7 +60,7 @@ func TestRepository_GetWatchers(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
watchers, err := repo.GetWatchers(ListOptions{Page: 1}) watchers, err := repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, repo.NumWatches) assert.Len(t, watchers, repo.NumWatches)
for _, watcher := range watchers { for _, watcher := range watchers {
@ -68,7 +68,7 @@ func TestRepository_GetWatchers(t *testing.T) {
} }
repo = db.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository) repo = db.AssertExistsAndLoadBean(t, &Repository{ID: 9}).(*Repository)
watchers, err = repo.GetWatchers(ListOptions{Page: 1}) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, 0) assert.Len(t, watchers, 0)
} }
@ -114,7 +114,7 @@ func TestWatchIfAuto(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
watchers, err := repo.GetWatchers(ListOptions{Page: 1}) watchers, err := repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, repo.NumWatches) assert.Len(t, watchers, repo.NumWatches)
@ -124,13 +124,13 @@ func TestWatchIfAuto(t *testing.T) {
// Must not add watch // Must not add watch
assert.NoError(t, WatchIfAuto(8, 1, true)) assert.NoError(t, WatchIfAuto(8, 1, true))
watchers, err = repo.GetWatchers(ListOptions{Page: 1}) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, prevCount) assert.Len(t, watchers, prevCount)
// Should not add watch // Should not add watch
assert.NoError(t, WatchIfAuto(10, 1, true)) assert.NoError(t, WatchIfAuto(10, 1, true))
watchers, err = repo.GetWatchers(ListOptions{Page: 1}) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, prevCount) assert.Len(t, watchers, prevCount)
@ -138,31 +138,31 @@ func TestWatchIfAuto(t *testing.T) {
// Must not add watch // Must not add watch
assert.NoError(t, WatchIfAuto(8, 1, true)) assert.NoError(t, WatchIfAuto(8, 1, true))
watchers, err = repo.GetWatchers(ListOptions{Page: 1}) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, prevCount) assert.Len(t, watchers, prevCount)
// Should not add watch // Should not add watch
assert.NoError(t, WatchIfAuto(12, 1, false)) assert.NoError(t, WatchIfAuto(12, 1, false))
watchers, err = repo.GetWatchers(ListOptions{Page: 1}) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, prevCount) assert.Len(t, watchers, prevCount)
// Should add watch // Should add watch
assert.NoError(t, WatchIfAuto(12, 1, true)) assert.NoError(t, WatchIfAuto(12, 1, true))
watchers, err = repo.GetWatchers(ListOptions{Page: 1}) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, prevCount+1) assert.Len(t, watchers, prevCount+1)
// Should remove watch, inhibit from adding auto // Should remove watch, inhibit from adding auto
assert.NoError(t, WatchRepo(12, 1, false)) assert.NoError(t, WatchRepo(12, 1, false))
watchers, err = repo.GetWatchers(ListOptions{Page: 1}) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, prevCount) assert.Len(t, watchers, prevCount)
// Must not add watch // Must not add watch
assert.NoError(t, WatchIfAuto(12, 1, true)) assert.NoError(t, WatchIfAuto(12, 1, true))
watchers, err = repo.GetWatchers(ListOptions{Page: 1}) watchers, err = repo.GetWatchers(db.ListOptions{Page: 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, watchers, prevCount) assert.Len(t, watchers, prevCount)
} }

@ -172,7 +172,7 @@ func GetReviewByID(id int64) (*Review, error) {
// FindReviewOptions represent possible filters to find reviews // FindReviewOptions represent possible filters to find reviews
type FindReviewOptions struct { type FindReviewOptions struct {
ListOptions db.ListOptions
Type ReviewType Type ReviewType
IssueID int64 IssueID int64
ReviewerID int64 ReviewerID int64
@ -200,7 +200,7 @@ func findReviews(e db.Engine, opts FindReviewOptions) ([]*Review, error) {
reviews := make([]*Review, 0, 10) reviews := make([]*Review, 0, 10)
sess := e.Where(opts.toCond()) sess := e.Where(opts.toCond())
if opts.Page > 0 { if opts.Page > 0 {
sess = setSessionPagination(sess, &opts) sess = db.SetSessionPagination(sess, &opts)
} }
return reviews, sess. return reviews, sess.
Asc("created_unix"). Asc("created_unix").

@ -11,6 +11,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -197,10 +198,10 @@ func SearchPublicKey(uid int64, fingerprint string) ([]*PublicKey, error) {
} }
// ListPublicKeys returns a list of public keys belongs to given user. // ListPublicKeys returns a list of public keys belongs to given user.
func ListPublicKeys(uid int64, listOptions ListOptions) ([]*PublicKey, error) { func ListPublicKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type != ?", uid, KeyTypePrincipal) sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type != ?", uid, KeyTypePrincipal)
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
keys := make([]*PublicKey, 0, listOptions.PageSize) keys := make([]*PublicKey, 0, listOptions.PageSize)
return keys, sess.Find(&keys) return keys, sess.Find(&keys)
@ -255,7 +256,7 @@ func deletePublicKeys(e db.Engine, keyIDs ...int64) error {
// PublicKeysAreExternallyManaged returns whether the provided KeyID represents an externally managed Key // PublicKeysAreExternallyManaged returns whether the provided KeyID represents an externally managed Key
func PublicKeysAreExternallyManaged(keys []*PublicKey) ([]bool, error) { func PublicKeysAreExternallyManaged(keys []*PublicKey) ([]bool, error) {
sources := make([]*LoginSource, 0, 5) sources := make([]*login.Source, 0, 5)
externals := make([]bool, len(keys)) externals := make([]bool, len(keys))
keyloop: keyloop:
for i, key := range keys { for i, key := range keys {
@ -264,7 +265,7 @@ keyloop:
continue keyloop continue keyloop
} }
var source *LoginSource var source *login.Source
sourceloop: sourceloop:
for _, s := range sources { for _, s := range sources {
@ -276,11 +277,11 @@ keyloop:
if source == nil { if source == nil {
var err error var err error
source, err = GetLoginSourceByID(key.LoginSourceID) source, err = login.GetSourceByID(key.LoginSourceID)
if err != nil { if err != nil {
if IsErrLoginSourceNotExist(err) { if login.IsErrSourceNotExist(err) {
externals[i] = false externals[i] = false
sources[i] = &LoginSource{ sources[i] = &login.Source{
ID: key.LoginSourceID, ID: key.LoginSourceID,
} }
continue keyloop continue keyloop
@ -289,7 +290,7 @@ keyloop:
} }
} }
if sshKeyProvider, ok := source.Cfg.(SSHKeyProvider); ok && sshKeyProvider.ProvidesSSHKeys() { if sshKeyProvider, ok := source.Cfg.(login.SSHKeyProvider); ok && sshKeyProvider.ProvidesSSHKeys() {
// Disable setting SSH keys for this user // Disable setting SSH keys for this user
externals[i] = true externals[i] = true
} }
@ -307,14 +308,14 @@ func PublicKeyIsExternallyManaged(id int64) (bool, error) {
if key.LoginSourceID == 0 { if key.LoginSourceID == 0 {
return false, nil return false, nil
} }
source, err := GetLoginSourceByID(key.LoginSourceID) source, err := login.GetSourceByID(key.LoginSourceID)
if err != nil { if err != nil {
if IsErrLoginSourceNotExist(err) { if login.IsErrSourceNotExist(err) {
return false, nil return false, nil
} }
return false, err return false, err
} }
if sshKeyProvider, ok := source.Cfg.(SSHKeyProvider); ok && sshKeyProvider.ProvidesSSHKeys() { if sshKeyProvider, ok := source.Cfg.(login.SSHKeyProvider); ok && sshKeyProvider.ProvidesSSHKeys() {
// Disable setting SSH keys for this user // Disable setting SSH keys for this user
return true, nil return true, nil
} }
@ -387,7 +388,7 @@ func deleteKeysMarkedForDeletion(keys []string) (bool, error) {
} }
// AddPublicKeysBySource add a users public keys. Returns true if there are changes. // AddPublicKeysBySource add a users public keys. Returns true if there are changes.
func AddPublicKeysBySource(usr *User, s *LoginSource, sshPublicKeys []string) bool { func AddPublicKeysBySource(usr *User, s *login.Source, sshPublicKeys []string) bool {
var sshKeysNeedUpdate bool var sshKeysNeedUpdate bool
for _, sshKey := range sshPublicKeys { for _, sshKey := range sshPublicKeys {
var err error var err error
@ -425,7 +426,7 @@ func AddPublicKeysBySource(usr *User, s *LoginSource, sshPublicKeys []string) bo
} }
// SynchronizePublicKeys updates a users public keys. Returns true if there are changes. // SynchronizePublicKeys updates a users public keys. Returns true if there are changes.
func SynchronizePublicKeys(usr *User, s *LoginSource, sshPublicKeys []string) bool { func SynchronizePublicKeys(usr *User, s *login.Source, sshPublicKeys []string) bool {
var sshKeysNeedUpdate bool var sshKeysNeedUpdate bool
log.Trace("synchronizePublicKeys[%s]: Handling Public SSH Key synchronization for user %s", s.Name, usr.Name) log.Trace("synchronizePublicKeys[%s]: Handling Public SSH Key synchronization for user %s", s.Name, usr.Name)

@ -271,7 +271,7 @@ func deleteDeployKey(sess db.Engine, doer *User, id int64) error {
// ListDeployKeysOptions are options for ListDeployKeys // ListDeployKeysOptions are options for ListDeployKeys
type ListDeployKeysOptions struct { type ListDeployKeysOptions struct {
ListOptions db.ListOptions
RepoID int64 RepoID int64
KeyID int64 KeyID int64
Fingerprint string Fingerprint string
@ -300,7 +300,7 @@ func listDeployKeys(e db.Engine, opts *ListDeployKeysOptions) ([]*DeployKey, err
sess := e.Where(opts.toCond()) sess := e.Where(opts.toCond())
if opts.Page != 0 { if opts.Page != 0 {
sess = setSessionPagination(sess, opts) sess = db.SetSessionPagination(sess, opts)
keys := make([]*DeployKey, 0, opts.PageSize) keys := make([]*DeployKey, 0, opts.PageSize)
return keys, sess.Find(&keys) return keys, sess.Find(&keys)

@ -112,10 +112,10 @@ func CheckPrincipalKeyString(user *User, content string) (_ string, err error) {
} }
// ListPrincipalKeys returns a list of principals belongs to given user. // ListPrincipalKeys returns a list of principals belongs to given user.
func ListPrincipalKeys(uid int64, listOptions ListOptions) ([]*PublicKey, error) { func ListPrincipalKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type = ?", uid, KeyTypePrincipal) sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type = ?", uid, KeyTypePrincipal)
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
keys := make([]*PublicKey, 0, listOptions.PageSize) keys := make([]*PublicKey, 0, listOptions.PageSize)
return keys, sess.Find(&keys) return keys, sess.Find(&keys)

@ -74,11 +74,11 @@ func isStaring(e db.Engine, userID, repoID int64) bool {
} }
// GetStargazers returns the users that starred the repo. // GetStargazers returns the users that starred the repo.
func (repo *Repository) GetStargazers(opts ListOptions) ([]*User, error) { func (repo *Repository) GetStargazers(opts db.ListOptions) ([]*User, error) {
sess := db.GetEngine(db.DefaultContext).Where("star.repo_id = ?", repo.ID). sess := db.GetEngine(db.DefaultContext).Where("star.repo_id = ?", repo.ID).
Join("LEFT", "star", "`user`.id = star.uid") Join("LEFT", "star", "`user`.id = star.uid")
if opts.Page > 0 { if opts.Page > 0 {
sess = setSessionPagination(sess, &opts) sess = db.SetSessionPagination(sess, &opts)
users := make([]*User, 0, opts.PageSize) users := make([]*User, 0, opts.PageSize)
return users, sess.Find(&users) return users, sess.Find(&users)

@ -34,7 +34,7 @@ func TestRepository_GetStargazers(t *testing.T) {
// repo with stargazers // repo with stargazers
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
gazers, err := repo.GetStargazers(ListOptions{Page: 0}) gazers, err := repo.GetStargazers(db.ListOptions{Page: 0})
assert.NoError(t, err) assert.NoError(t, err)
if assert.Len(t, gazers, 1) { if assert.Len(t, gazers, 1) {
assert.Equal(t, int64(2), gazers[0].ID) assert.Equal(t, int64(2), gazers[0].ID)
@ -45,7 +45,7 @@ func TestRepository_GetStargazers2(t *testing.T) {
// repo with stargazers // repo with stargazers
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository) repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
gazers, err := repo.GetStargazers(ListOptions{Page: 0}) gazers, err := repo.GetStargazers(db.ListOptions{Page: 0})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, gazers, 0) assert.Len(t, gazers, 0)
} }

@ -4,7 +4,10 @@
package models package models
import "code.gitea.io/gitea/models/db" import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
)
// Statistic contains the database statistics // Statistic contains the database statistics
type Statistic struct { type Statistic struct {
@ -52,7 +55,7 @@ func GetStatistic() (stats Statistic) {
stats.Counter.Follow, _ = db.GetEngine(db.DefaultContext).Count(new(Follow)) stats.Counter.Follow, _ = db.GetEngine(db.DefaultContext).Count(new(Follow))
stats.Counter.Mirror, _ = db.GetEngine(db.DefaultContext).Count(new(Mirror)) stats.Counter.Mirror, _ = db.GetEngine(db.DefaultContext).Count(new(Mirror))
stats.Counter.Release, _ = db.GetEngine(db.DefaultContext).Count(new(Release)) stats.Counter.Release, _ = db.GetEngine(db.DefaultContext).Count(new(Release))
stats.Counter.LoginSource = CountLoginSources() stats.Counter.LoginSource = login.CountSources()
stats.Counter.Webhook, _ = db.GetEngine(db.DefaultContext).Count(new(Webhook)) stats.Counter.Webhook, _ = db.GetEngine(db.DefaultContext).Count(new(Webhook))
stats.Counter.Milestone, _ = db.GetEngine(db.DefaultContext).Count(new(Milestone)) stats.Counter.Milestone, _ = db.GetEngine(db.DefaultContext).Count(new(Milestone))
stats.Counter.Label, _ = db.GetEngine(db.DefaultContext).Count(new(Label)) stats.Counter.Label, _ = db.GetEngine(db.DefaultContext).Count(new(Label))

@ -147,7 +147,7 @@ func AccessTokenByNameExists(token *AccessToken) (bool, error) {
// ListAccessTokensOptions contain filter options // ListAccessTokensOptions contain filter options
type ListAccessTokensOptions struct { type ListAccessTokensOptions struct {
ListOptions db.ListOptions
Name string Name string
UserID int64 UserID int64
} }
@ -163,7 +163,7 @@ func ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
sess = sess.Desc("id") sess = sess.Desc("id")
if opts.Page != 0 { if opts.Page != 0 {
sess = setSessionPagination(sess, &opts) sess = db.SetSessionPagination(sess, &opts)
tokens := make([]*AccessToken, 0, opts.PageSize) tokens := make([]*AccessToken, 0, opts.PageSize)
return tokens, sess.Find(&tokens) return tokens, sess.Find(&tokens)

@ -164,7 +164,7 @@ func removeTopicsFromRepo(e db.Engine, repoID int64) error {
// FindTopicOptions represents the options when fdin topics // FindTopicOptions represents the options when fdin topics
type FindTopicOptions struct { type FindTopicOptions struct {
ListOptions db.ListOptions
RepoID int64 RepoID int64
Keyword string Keyword string
} }
@ -189,7 +189,7 @@ func FindTopics(opts *FindTopicOptions) ([]*Topic, int64, error) {
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id") sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
} }
if opts.PageSize != 0 && opts.Page != 0 { if opts.PageSize != 0 && opts.Page != 0 {
sess = setSessionPagination(sess, opts) sess = db.SetSessionPagination(sess, opts)
} }
topics := make([]*Topic, 0, 10) topics := make([]*Topic, 0, 10)
total, err := sess.Desc("topic.repo_count").FindAndCount(&topics) total, err := sess.Desc("topic.repo_count").FindAndCount(&topics)

@ -23,7 +23,7 @@ func TestAddTopic(t *testing.T) {
assert.Len(t, topics, totalNrOfTopics) assert.Len(t, topics, totalNrOfTopics)
topics, total, err := FindTopics(&FindTopicOptions{ topics, total, err := FindTopics(&FindTopicOptions{
ListOptions: ListOptions{Page: 1, PageSize: 2}, ListOptions: db.ListOptions{Page: 1, PageSize: 2},
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, topics, 2) assert.Len(t, topics, 2)

@ -21,6 +21,7 @@ import (
"unicode/utf8" "unicode/utf8"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -106,7 +107,7 @@ type User struct {
// is to change his/her password after registration. // is to change his/her password after registration.
MustChangePassword bool `xorm:"NOT NULL DEFAULT false"` MustChangePassword bool `xorm:"NOT NULL DEFAULT false"`
LoginType LoginType LoginType login.Type
LoginSource int64 `xorm:"NOT NULL DEFAULT 0"` LoginSource int64 `xorm:"NOT NULL DEFAULT 0"`
LoginName string LoginName string
Type UserType Type UserType
@ -169,7 +170,7 @@ func init() {
// SearchOrganizationsOptions options to filter organizations // SearchOrganizationsOptions options to filter organizations
type SearchOrganizationsOptions struct { type SearchOrganizationsOptions struct {
ListOptions db.ListOptions
All bool All bool
} }
@ -241,12 +242,12 @@ func GetAllUsers() ([]*User, error) {
// IsLocal returns true if user login type is LoginPlain. // IsLocal returns true if user login type is LoginPlain.
func (u *User) IsLocal() bool { func (u *User) IsLocal() bool {
return u.LoginType <= LoginPlain return u.LoginType <= login.Plain
} }
// IsOAuth2 returns true if user login type is LoginOAuth2. // IsOAuth2 returns true if user login type is LoginOAuth2.
func (u *User) IsOAuth2() bool { func (u *User) IsOAuth2() bool {
return u.LoginType == LoginOAuth2 return u.LoginType == login.OAuth2
} }
// HasForkedRepo checks if user has already forked a repository with given ID. // HasForkedRepo checks if user has already forked a repository with given ID.
@ -331,13 +332,13 @@ func (u *User) GenerateEmailActivateCode(email string) string {
} }
// GetFollowers returns range of user's followers. // GetFollowers returns range of user's followers.
func (u *User) GetFollowers(listOptions ListOptions) ([]*User, error) { func (u *User) GetFollowers(listOptions db.ListOptions) ([]*User, error) {
sess := db.GetEngine(db.DefaultContext). sess := db.GetEngine(db.DefaultContext).
Where("follow.follow_id=?", u.ID). Where("follow.follow_id=?", u.ID).
Join("LEFT", "follow", "`user`.id=follow.user_id") Join("LEFT", "follow", "`user`.id=follow.user_id")
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
users := make([]*User, 0, listOptions.PageSize) users := make([]*User, 0, listOptions.PageSize)
return users, sess.Find(&users) return users, sess.Find(&users)
@ -353,13 +354,13 @@ func (u *User) IsFollowing(followID int64) bool {
} }
// GetFollowing returns range of user's following. // GetFollowing returns range of user's following.
func (u *User) GetFollowing(listOptions ListOptions) ([]*User, error) { func (u *User) GetFollowing(listOptions db.ListOptions) ([]*User, error) {
sess := db.GetEngine(db.DefaultContext). sess := db.GetEngine(db.DefaultContext).
Where("follow.user_id=?", u.ID). Where("follow.user_id=?", u.ID).
Join("LEFT", "follow", "`user`.id=follow.follow_id") Join("LEFT", "follow", "`user`.id=follow.follow_id")
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
users := make([]*User, 0, listOptions.PageSize) users := make([]*User, 0, listOptions.PageSize)
return users, sess.Find(&users) return users, sess.Find(&users)
@ -542,7 +543,7 @@ func (u *User) GetOrganizationCount() (int64, error) {
} }
// GetRepositories returns repositories that user owns, including private repositories. // GetRepositories returns repositories that user owns, including private repositories.
func (u *User) GetRepositories(listOpts ListOptions, names ...string) (err error) { func (u *User) GetRepositories(listOpts db.ListOptions, names ...string) (err error) {
u.Repos, _, err = GetUserRepositories(&SearchRepoOptions{Actor: u, Private: true, ListOptions: listOpts, LowerNames: names}) u.Repos, _, err = GetUserRepositories(&SearchRepoOptions{Actor: u, Private: true, ListOptions: listOpts, LowerNames: names})
return err return err
} }
@ -1252,7 +1253,7 @@ func deleteUser(e db.Engine, u *User) error {
// ***** END: PublicKey ***** // ***** END: PublicKey *****
// ***** START: GPGPublicKey ***** // ***** START: GPGPublicKey *****
keys, err := listGPGKeys(e, u.ID, ListOptions{}) keys, err := listGPGKeys(e, u.ID, db.ListOptions{})
if err != nil { if err != nil {
return fmt.Errorf("ListGPGKeys: %v", err) return fmt.Errorf("ListGPGKeys: %v", err)
} }
@ -1488,7 +1489,7 @@ func GetUserIDsByNames(names []string, ignoreNonExistent bool) ([]int64, error)
} }
// GetUsersBySource returns a list of Users for a login source // GetUsersBySource returns a list of Users for a login source
func GetUsersBySource(s *LoginSource) ([]*User, error) { func GetUsersBySource(s *login.Source) ([]*User, error) {
var users []*User var users []*User
err := db.GetEngine(db.DefaultContext).Where("login_type = ? AND login_source = ?", s.Type, s.ID).Find(&users) err := db.GetEngine(db.DefaultContext).Where("login_type = ? AND login_source = ?", s.Type, s.ID).Find(&users)
return users, err return users, err
@ -1592,7 +1593,7 @@ func GetUser(user *User) (bool, error) {
// SearchUserOptions contains the options for searching // SearchUserOptions contains the options for searching
type SearchUserOptions struct { type SearchUserOptions struct {
ListOptions db.ListOptions
Keyword string Keyword string
Type UserType Type UserType
UID int64 UID int64
@ -1675,7 +1676,7 @@ func SearchUsers(opts *SearchUserOptions) (users []*User, _ int64, _ error) {
sess := db.GetEngine(db.DefaultContext).Where(cond).OrderBy(opts.OrderBy.String()) sess := db.GetEngine(db.DefaultContext).Where(cond).OrderBy(opts.OrderBy.String())
if opts.Page != 0 { if opts.Page != 0 {
sess = setSessionPagination(sess, opts) sess = db.SetSessionPagination(sess, opts)
} }
users = make([]*User, 0, opts.PageSize) users = make([]*User, 0, opts.PageSize)
@ -1683,7 +1684,7 @@ func SearchUsers(opts *SearchUserOptions) (users []*User, _ int64, _ error) {
} }
// GetStarredRepos returns the repos starred by a particular user // GetStarredRepos returns the repos starred by a particular user
func GetStarredRepos(userID int64, private bool, listOptions ListOptions) ([]*Repository, error) { func GetStarredRepos(userID int64, private bool, listOptions db.ListOptions) ([]*Repository, error) {
sess := db.GetEngine(db.DefaultContext).Where("star.uid=?", userID). sess := db.GetEngine(db.DefaultContext).Where("star.uid=?", userID).
Join("LEFT", "star", "`repository`.id=`star`.repo_id") Join("LEFT", "star", "`repository`.id=`star`.repo_id")
if !private { if !private {
@ -1691,7 +1692,7 @@ func GetStarredRepos(userID int64, private bool, listOptions ListOptions) ([]*Re
} }
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
repos := make([]*Repository, 0, listOptions.PageSize) repos := make([]*Repository, 0, listOptions.PageSize)
return repos, sess.Find(&repos) return repos, sess.Find(&repos)
@ -1702,7 +1703,7 @@ func GetStarredRepos(userID int64, private bool, listOptions ListOptions) ([]*Re
} }
// GetWatchedRepos returns the repos watched by a particular user // GetWatchedRepos returns the repos watched by a particular user
func GetWatchedRepos(userID int64, private bool, listOptions ListOptions) ([]*Repository, int64, error) { func GetWatchedRepos(userID int64, private bool, listOptions db.ListOptions) ([]*Repository, int64, error) {
sess := db.GetEngine(db.DefaultContext).Where("watch.user_id=?", userID). sess := db.GetEngine(db.DefaultContext).Where("watch.user_id=?", userID).
And("`watch`.mode<>?", RepoWatchModeDont). And("`watch`.mode<>?", RepoWatchModeDont).
Join("LEFT", "watch", "`repository`.id=`watch`.repo_id") Join("LEFT", "watch", "`repository`.id=`watch`.repo_id")
@ -1711,7 +1712,7 @@ func GetWatchedRepos(userID int64, private bool, listOptions ListOptions) ([]*Re
} }
if listOptions.Page != 0 { if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions) sess = db.SetSessionPagination(sess, &listOptions)
repos := make([]*Repository, 0, listOptions.PageSize) repos := make([]*Repository, 0, listOptions.PageSize)
total, err := sess.FindAndCount(&repos) total, err := sess.FindAndCount(&repos)

@ -301,7 +301,7 @@ const (
// SearchEmailOptions are options to search e-mail addresses for the admin panel // SearchEmailOptions are options to search e-mail addresses for the admin panel
type SearchEmailOptions struct { type SearchEmailOptions struct {
ListOptions db.ListOptions
Keyword string Keyword string
SortType SearchEmailOrderBy SortType SearchEmailOrderBy
IsPrimary util.OptionalBool IsPrimary util.OptionalBool
@ -357,7 +357,7 @@ func SearchEmails(opts *SearchEmailOptions) ([]*SearchEmailResult, int64, error)
orderby = SearchEmailOrderByEmail.String() orderby = SearchEmailOrderByEmail.String()
} }
opts.setDefaultValues() opts.SetDefaultValues()
emails := make([]*SearchEmailResult, 0, opts.PageSize) emails := make([]*SearchEmailResult, 0, opts.PageSize)
err = db.GetEngine(db.DefaultContext).Table("email_address"). err = db.GetEngine(db.DefaultContext).Table("email_address").

@ -189,7 +189,7 @@ func TestListEmails(t *testing.T) {
// Must find all users and their emails // Must find all users and their emails
opts := &SearchEmailOptions{ opts := &SearchEmailOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
PageSize: 10000, PageSize: 10000,
}, },
} }
@ -241,7 +241,7 @@ func TestListEmails(t *testing.T) {
// Must find more than one page, but retrieve only one // Must find more than one page, but retrieve only one
opts = &SearchEmailOptions{ opts = &SearchEmailOptions{
ListOptions: ListOptions{ ListOptions: db.ListOptions{
PageSize: 5, PageSize: 5,
Page: 1, Page: 1,
}, },

@ -11,6 +11,7 @@ import (
"testing" "testing"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
@ -18,6 +19,14 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestOAuth2Application_LoadUser(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
app := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{ID: 1}).(*login.OAuth2Application)
user, err := GetUserByID(app.UID)
assert.NoError(t, err)
assert.NotNil(t, user)
}
func TestUserIsPublicMember(t *testing.T) { func TestUserIsPublicMember(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
@ -116,19 +125,19 @@ func TestSearchUsers(t *testing.T) {
testSuccess(opts, expectedOrgIDs) testSuccess(opts, expectedOrgIDs)
} }
testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: ListOptions{Page: 1, PageSize: 2}}, testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1, PageSize: 2}},
[]int64{3, 6}) []int64{3, 6})
testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: ListOptions{Page: 2, PageSize: 2}}, testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 2, PageSize: 2}},
[]int64{7, 17}) []int64{7, 17})
testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: ListOptions{Page: 3, PageSize: 2}}, testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 3, PageSize: 2}},
[]int64{19, 25}) []int64{19, 25})
testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: ListOptions{Page: 4, PageSize: 2}}, testOrgSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 4, PageSize: 2}},
[]int64{26}) []int64{26})
testOrgSuccess(&SearchUserOptions{ListOptions: ListOptions{Page: 5, PageSize: 2}}, testOrgSuccess(&SearchUserOptions{ListOptions: db.ListOptions{Page: 5, PageSize: 2}},
[]int64{}) []int64{})
// test users // test users
@ -137,20 +146,20 @@ func TestSearchUsers(t *testing.T) {
testSuccess(opts, expectedUserIDs) testSuccess(opts, expectedUserIDs)
} }
testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: ListOptions{Page: 1}}, testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}},
[]int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30}) []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30})
testUserSuccess(&SearchUserOptions{ListOptions: ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse}, testUserSuccess(&SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse},
[]int64{9}) []int64{9})
testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, testUserSuccess(&SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue},
[]int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 28, 29, 30}) []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 28, 29, 30})
testUserSuccess(&SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, testUserSuccess(&SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue},
[]int64{1, 10, 11, 12, 13, 14, 15, 16, 18}) []int64{1, 10, 11, 12, 13, 14, 15, 16, 18})
// order by name asc default // order by name asc default
testUserSuccess(&SearchUserOptions{Keyword: "user1", ListOptions: ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, testUserSuccess(&SearchUserOptions{Keyword: "user1", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue},
[]int64{1, 10, 11, 12, 13, 14, 15, 16, 18}) []int64{1, 10, 11, 12, 13, 14, 15, 16, 18})
} }
@ -407,7 +416,7 @@ func TestAddLdapSSHPublicKeys(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase()) assert.NoError(t, db.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
s := &LoginSource{ID: 1} s := &login.Source{ID: 1}
testCases := []struct { testCases := []struct {
keyString string keyString string

@ -397,7 +397,7 @@ func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
// ListWebhookOptions are options to filter webhooks on ListWebhooksByOpts // ListWebhookOptions are options to filter webhooks on ListWebhooksByOpts
type ListWebhookOptions struct { type ListWebhookOptions struct {
ListOptions db.ListOptions
RepoID int64 RepoID int64
OrgID int64 OrgID int64
IsActive util.OptionalBool IsActive util.OptionalBool
@ -421,7 +421,7 @@ func listWebhooksByOpts(e db.Engine, opts *ListWebhookOptions) ([]*Webhook, erro
sess := e.Where(opts.toCond()) sess := e.Where(opts.toCond())
if opts.Page != 0 { if opts.Page != 0 {
sess = setSessionPagination(sess, opts) sess = db.SetSessionPagination(sess, opts)
webhooks := make([]*Webhook, 0, opts.PageSize) webhooks := make([]*Webhook, 0, opts.PageSize)
err := sess.Find(&webhooks) err := sess.Find(&webhooks)
return webhooks, err return webhooks, err

@ -12,6 +12,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
@ -338,8 +339,8 @@ func ToTopicResponse(topic *models.Topic) *api.TopicResponse {
} }
} }
// ToOAuth2Application convert from models.OAuth2Application to api.OAuth2Application // ToOAuth2Application convert from login.OAuth2Application to api.OAuth2Application
func ToOAuth2Application(app *models.OAuth2Application) *api.OAuth2Application { func ToOAuth2Application(app *login.OAuth2Application) *api.OAuth2Application {
return &api.OAuth2Application{ return &api.OAuth2Application{
ID: app.ID, ID: app.ID,
Name: app.Name, Name: app.Name,

@ -10,6 +10,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
) )
@ -114,7 +115,7 @@ func (graph *Graph) LoadAndProcessCommits(repository *models.Repository, gitRepo
_ = models.CalculateTrustStatus(c.Verification, repository, &keyMap) _ = models.CalculateTrustStatus(c.Verification, repository, &keyMap)
statuses, err := models.GetLatestCommitStatus(repository.ID, c.Commit.ID.String(), models.ListOptions{}) statuses, err := models.GetLatestCommitStatus(repository.ID, c.Commit.ID.String(), db.ListOptions{})
if err != nil { if err != nil {
log.Error("GetLatestCommitStatus: %v", err) log.Error("GetLatestCommitStatus: %v", err)
} else { } else {

@ -12,6 +12,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/queue"
@ -241,7 +242,7 @@ func populateIssueIndexer(ctx context.Context) {
default: default:
} }
repos, _, err := models.SearchRepositoryByName(&models.SearchRepoOptions{ repos, _, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
ListOptions: models.ListOptions{Page: page, PageSize: models.RepositoryListDefaultPageSize}, ListOptions: db.ListOptions{Page: page, PageSize: models.RepositoryListDefaultPageSize},
OrderBy: models.SearchOrderByID, OrderBy: models.SearchOrderByID,
Private: true, Private: true,
Collaborate: util.OptionalBoolFalse, Collaborate: util.OptionalBoolFalse,

@ -69,12 +69,12 @@ func TestGiteaUploadRepo(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Empty(t, milestones) assert.Empty(t, milestones)
labels, err := models.GetLabelsByRepoID(repo.ID, "", models.ListOptions{}) labels, err := models.GetLabelsByRepoID(repo.ID, "", db.ListOptions{})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, labels, 12) assert.Len(t, labels, 12)
releases, err := models.GetReleasesByRepoID(repo.ID, models.FindReleasesOptions{ releases, err := models.GetReleasesByRepoID(repo.ID, models.FindReleasesOptions{
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
PageSize: 10, PageSize: 10,
Page: 0, Page: 0,
}, },
@ -84,7 +84,7 @@ func TestGiteaUploadRepo(t *testing.T) {
assert.Len(t, releases, 8) assert.Len(t, releases, 8)
releases, err = models.GetReleasesByRepoID(repo.ID, models.FindReleasesOptions{ releases, err = models.GetReleasesByRepoID(repo.ID, models.FindReleasesOptions{
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
PageSize: 10, PageSize: 10,
Page: 0, Page: 0,
}, },

@ -122,7 +122,7 @@ func DeleteUnadoptedRepository(doer, u *models.User, repoName string) error {
} }
// ListUnadoptedRepositories lists all the unadopted repositories that match the provided query // ListUnadoptedRepositories lists all the unadopted repositories that match the provided query
func ListUnadoptedRepositories(query string, opts *models.ListOptions) ([]string, int, error) { func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, int, error) {
globUser, _ := glob.Compile("*") globUser, _ := glob.Compile("*")
globRepo, _ := glob.Compile("*") globRepo, _ := glob.Compile("*")
@ -165,10 +165,13 @@ func ListUnadoptedRepositories(query string, opts *models.ListOptions) ([]string
// Clean up old repoNamesToCheck // Clean up old repoNamesToCheck
if len(repoNamesToCheck) > 0 { if len(repoNamesToCheck) > 0 {
repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: models.ListOptions{ repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
Page: 1, Actor: ctxUser,
PageSize: opts.PageSize, Private: true,
}, LowerNames: repoNamesToCheck}) ListOptions: db.ListOptions{
Page: 1,
PageSize: opts.PageSize,
}, LowerNames: repoNamesToCheck})
if err != nil { if err != nil {
return err return err
} }
@ -219,10 +222,13 @@ func ListUnadoptedRepositories(query string, opts *models.ListOptions) ([]string
if count < end { if count < end {
repoNamesToCheck = append(repoNamesToCheck, name) repoNamesToCheck = append(repoNamesToCheck, name)
if len(repoNamesToCheck) >= opts.PageSize { if len(repoNamesToCheck) >= opts.PageSize {
repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: models.ListOptions{ repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
Page: 1, Actor: ctxUser,
PageSize: opts.PageSize, Private: true,
}, LowerNames: repoNamesToCheck}) ListOptions: db.ListOptions{
Page: 1,
PageSize: opts.PageSize,
}, LowerNames: repoNamesToCheck})
if err != nil { if err != nil {
return err return err
} }
@ -254,10 +260,13 @@ func ListUnadoptedRepositories(query string, opts *models.ListOptions) ([]string
} }
if len(repoNamesToCheck) > 0 { if len(repoNamesToCheck) > 0 {
repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: models.ListOptions{ repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
Page: 1, Actor: ctxUser,
PageSize: opts.PageSize, Private: true,
}, LowerNames: repoNamesToCheck}) ListOptions: db.ListOptions{
Page: 1,
PageSize: opts.PageSize,
}, LowerNames: repoNamesToCheck})
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }

@ -224,7 +224,11 @@ func CleanUpMigrateInfo(repo *models.Repository) (*models.Repository, error) {
// SyncReleasesWithTags synchronizes release table with repository tags // SyncReleasesWithTags synchronizes release table with repository tags
func SyncReleasesWithTags(repo *models.Repository, gitRepo *git.Repository) error { func SyncReleasesWithTags(repo *models.Repository, gitRepo *git.Repository) error {
existingRelTags := make(map[string]struct{}) existingRelTags := make(map[string]struct{})
opts := models.FindReleasesOptions{IncludeDrafts: true, IncludeTags: true, ListOptions: models.ListOptions{PageSize: 50}} opts := models.FindReleasesOptions{
IncludeDrafts: true,
IncludeTags: true,
ListOptions: db.ListOptions{PageSize: 50},
}
for page := 1; ; page++ { for page := 1; ; page++ {
opts.Page = page opts.Page = page
rels, err := models.GetReleasesByRepoID(repo.ID, opts) rels, err := models.GetReleasesByRepoID(repo.ID, opts)

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -27,12 +28,12 @@ func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, l
return return
} }
source, err := models.GetLoginSourceByID(sourceID) source, err := login.GetSourceByID(sourceID)
if err != nil { if err != nil {
if models.IsErrLoginSourceNotExist(err) { if login.IsErrSourceNotExist(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err) ctx.Error(http.StatusUnprocessableEntity, "", err)
} else { } else {
ctx.Error(http.StatusInternalServerError, "GetLoginSourceByID", err) ctx.Error(http.StatusInternalServerError, "login.GetSourceByID", err)
} }
return return
} }
@ -74,7 +75,7 @@ func CreateUser(ctx *context.APIContext) {
Passwd: form.Password, Passwd: form.Password,
MustChangePassword: true, MustChangePassword: true,
IsActive: true, IsActive: true,
LoginType: models.LoginPlain, LoginType: login.Plain,
} }
if form.MustChangePassword != nil { if form.MustChangePassword != nil {
u.MustChangePassword = *form.MustChangePassword u.MustChangePassword = *form.MustChangePassword

@ -13,6 +13,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
issue_indexer "code.gitea.io/gitea/modules/indexer/issues" issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
@ -226,7 +227,7 @@ func SearchIssues(ctx *context.APIContext) {
// This would otherwise return all issues if no issues were found by the search. // This would otherwise return all issues if no issues were found by the search.
if len(keyword) == 0 || len(issueIDs) > 0 || len(includedLabelNames) > 0 || len(includedMilestones) > 0 { if len(keyword) == 0 || len(issueIDs) > 0 || len(includedLabelNames) > 0 || len(includedMilestones) > 0 {
issuesOpt := &models.IssuesOptions{ issuesOpt := &models.IssuesOptions{
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
Page: ctx.FormInt("page"), Page: ctx.FormInt("page"),
PageSize: limit, PageSize: limit,
}, },
@ -261,7 +262,7 @@ func SearchIssues(ctx *context.APIContext) {
return return
} }
issuesOpt.ListOptions = models.ListOptions{ issuesOpt.ListOptions = db.ListOptions{
Page: -1, Page: -1,
} }
if filteredCount, err = models.CountIssues(issuesOpt); err != nil { if filteredCount, err = models.CountIssues(issuesOpt); err != nil {
@ -470,7 +471,7 @@ func ListIssues(ctx *context.APIContext) {
return return
} }
issuesOpt.ListOptions = models.ListOptions{ issuesOpt.ListOptions = db.ListOptions{
Page: -1, Page: -1,
} }
if filteredCount, err = models.CountIssues(issuesOpt); err != nil { if filteredCount, err = models.CountIssues(issuesOpt); err != nil {

@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -212,7 +213,7 @@ func CreateOauth2Application(ctx *context.APIContext) {
data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions) data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions)
app, err := models.CreateOAuth2Application(models.CreateOAuth2ApplicationOptions{ app, err := login.CreateOAuth2Application(login.CreateOAuth2ApplicationOptions{
Name: data.Name, Name: data.Name,
UserID: ctx.User.ID, UserID: ctx.User.ID,
RedirectURIs: data.RedirectURIs, RedirectURIs: data.RedirectURIs,
@ -251,7 +252,7 @@ func ListOauth2Applications(ctx *context.APIContext) {
// "200": // "200":
// "$ref": "#/responses/OAuth2ApplicationList" // "$ref": "#/responses/OAuth2ApplicationList"
apps, total, err := models.ListOAuth2Applications(ctx.User.ID, utils.GetListOptions(ctx)) apps, total, err := login.ListOAuth2Applications(ctx.User.ID, utils.GetListOptions(ctx))
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "ListOAuth2Applications", err) ctx.Error(http.StatusInternalServerError, "ListOAuth2Applications", err)
return return
@ -287,8 +288,8 @@ func DeleteOauth2Application(ctx *context.APIContext) {
// "404": // "404":
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
appID := ctx.ParamsInt64(":id") appID := ctx.ParamsInt64(":id")
if err := models.DeleteOAuth2Application(appID, ctx.User.ID); err != nil { if err := login.DeleteOAuth2Application(appID, ctx.User.ID); err != nil {
if models.IsErrOAuthApplicationNotFound(err) { if login.IsErrOAuthApplicationNotFound(err) {
ctx.NotFound() ctx.NotFound()
} else { } else {
ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err) ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
@ -319,9 +320,9 @@ func GetOauth2Application(ctx *context.APIContext) {
// "404": // "404":
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
appID := ctx.ParamsInt64(":id") appID := ctx.ParamsInt64(":id")
app, err := models.GetOAuth2ApplicationByID(appID) app, err := login.GetOAuth2ApplicationByID(appID)
if err != nil { if err != nil {
if models.IsErrOauthClientIDInvalid(err) || models.IsErrOAuthApplicationNotFound(err) { if login.IsErrOauthClientIDInvalid(err) || login.IsErrOAuthApplicationNotFound(err) {
ctx.NotFound() ctx.NotFound()
} else { } else {
ctx.Error(http.StatusInternalServerError, "GetOauth2ApplicationByID", err) ctx.Error(http.StatusInternalServerError, "GetOauth2ApplicationByID", err)
@ -362,14 +363,14 @@ func UpdateOauth2Application(ctx *context.APIContext) {
data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions) data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions)
app, err := models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{ app, err := login.UpdateOAuth2Application(login.UpdateOAuth2ApplicationOptions{
Name: data.Name, Name: data.Name,
UserID: ctx.User.ID, UserID: ctx.User.ID,
ID: appID, ID: appID,
RedirectURIs: data.RedirectURIs, RedirectURIs: data.RedirectURIs,
}) })
if err != nil { if err != nil {
if models.IsErrOauthClientIDInvalid(err) || models.IsErrOAuthApplicationNotFound(err) { if login.IsErrOauthClientIDInvalid(err) || login.IsErrOAuthApplicationNotFound(err) {
ctx.NotFound() ctx.NotFound()
} else { } else {
ctx.Error(http.StatusInternalServerError, "UpdateOauth2ApplicationByID", err) ctx.Error(http.StatusInternalServerError, "UpdateOauth2ApplicationByID", err)

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -16,7 +17,7 @@ import (
"code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/routers/api/v1/utils"
) )
func listGPGKeys(ctx *context.APIContext, uid int64, listOptions models.ListOptions) { func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions) {
keys, err := models.ListGPGKeys(uid, listOptions) keys, err := models.ListGPGKeys(uid, listOptions)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err) ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -17,7 +18,7 @@ import (
// getStarredRepos returns the repos that the user with the specified userID has // getStarredRepos returns the repos that the user with the specified userID has
// starred // starred
func getStarredRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, error) { func getStarredRepos(user *models.User, private bool, listOptions db.ListOptions) ([]*api.Repository, error) {
starredRepos, err := models.GetStarredRepos(user.ID, private, listOptions) starredRepos, err := models.GetStarredRepos(user.ID, private, listOptions)
if err != nil { if err != nil {
return nil, err return nil, err

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -15,7 +16,7 @@ import (
) )
// getWatchedRepos returns the repos that the user with the specified userID is watching // getWatchedRepos returns the repos that the user with the specified userID is watching
func getWatchedRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, int64, error) { func getWatchedRepos(user *models.User, private bool, listOptions db.ListOptions) ([]*api.Repository, int64, error) {
watchedRepos, total, err := models.GetWatchedRepos(user.ID, private, listOptions) watchedRepos, total, err := models.GetWatchedRepos(user.ID, private, listOptions)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err

@ -9,7 +9,7 @@ import (
"strings" "strings"
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
) )
@ -60,8 +60,8 @@ func prepareQueryArg(ctx *context.APIContext, name string) (value string, err er
} }
// GetListOptions returns list options using the page and limit parameters // GetListOptions returns list options using the page and limit parameters
func GetListOptions(ctx *context.APIContext) models.ListOptions { func GetListOptions(ctx *context.APIContext) db.ListOptions {
return models.ListOptions{ return db.ListOptions{
Page: ctx.FormInt("page"), Page: ctx.FormInt("page"),
PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")), PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
} }

@ -11,6 +11,7 @@ import (
"regexp" "regexp"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/auth/pam" "code.gitea.io/gitea/modules/auth/pam"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -18,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/modules/web"
auth_service "code.gitea.io/gitea/services/auth"
"code.gitea.io/gitea/services/auth/source/ldap" "code.gitea.io/gitea/services/auth/source/ldap"
"code.gitea.io/gitea/services/auth/source/oauth2" "code.gitea.io/gitea/services/auth/source/oauth2"
pamService "code.gitea.io/gitea/services/auth/source/pam" pamService "code.gitea.io/gitea/services/auth/source/pam"
@ -46,13 +48,13 @@ func Authentications(ctx *context.Context) {
ctx.Data["PageIsAdminAuthentications"] = true ctx.Data["PageIsAdminAuthentications"] = true
var err error var err error
ctx.Data["Sources"], err = models.LoginSources() ctx.Data["Sources"], err = login.Sources()
if err != nil { if err != nil {
ctx.ServerError("LoginSources", err) ctx.ServerError("login.Sources", err)
return return
} }
ctx.Data["Total"] = models.CountLoginSources() ctx.Data["Total"] = login.CountSources()
ctx.HTML(http.StatusOK, tplAuths) ctx.HTML(http.StatusOK, tplAuths)
} }
@ -64,14 +66,14 @@ type dropdownItem struct {
var ( var (
authSources = func() []dropdownItem { authSources = func() []dropdownItem {
items := []dropdownItem{ items := []dropdownItem{
{models.LoginNames[models.LoginLDAP], models.LoginLDAP}, {login.LDAP.String(), login.LDAP},
{models.LoginNames[models.LoginDLDAP], models.LoginDLDAP}, {login.DLDAP.String(), login.DLDAP},
{models.LoginNames[models.LoginSMTP], models.LoginSMTP}, {login.SMTP.String(), login.SMTP},
{models.LoginNames[models.LoginOAuth2], models.LoginOAuth2}, {login.OAuth2.String(), login.OAuth2},
{models.LoginNames[models.LoginSSPI], models.LoginSSPI}, {login.SSPI.String(), login.SSPI},
} }
if pam.Supported { if pam.Supported {
items = append(items, dropdownItem{models.LoginNames[models.LoginPAM], models.LoginPAM}) items = append(items, dropdownItem{login.Names[login.PAM], login.PAM})
} }
return items return items
}() }()
@ -89,8 +91,8 @@ func NewAuthSource(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminAuthentications"] = true ctx.Data["PageIsAdminAuthentications"] = true
ctx.Data["type"] = models.LoginLDAP ctx.Data["type"] = login.LDAP
ctx.Data["CurrentTypeName"] = models.LoginNames[models.LoginLDAP] ctx.Data["CurrentTypeName"] = login.Names[login.LDAP]
ctx.Data["CurrentSecurityProtocol"] = ldap.SecurityProtocolNames[ldap.SecurityProtocolUnencrypted] ctx.Data["CurrentSecurityProtocol"] = ldap.SecurityProtocolNames[ldap.SecurityProtocolUnencrypted]
ctx.Data["smtp_auth"] = "PLAIN" ctx.Data["smtp_auth"] = "PLAIN"
ctx.Data["is_active"] = true ctx.Data["is_active"] = true
@ -217,7 +219,7 @@ func NewAuthSourcePost(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminAuthentications"] = true ctx.Data["PageIsAdminAuthentications"] = true
ctx.Data["CurrentTypeName"] = models.LoginNames[models.LoginType(form.Type)] ctx.Data["CurrentTypeName"] = login.Type(form.Type).String()
ctx.Data["CurrentSecurityProtocol"] = ldap.SecurityProtocolNames[ldap.SecurityProtocol(form.SecurityProtocol)] ctx.Data["CurrentSecurityProtocol"] = ldap.SecurityProtocolNames[ldap.SecurityProtocol(form.SecurityProtocol)]
ctx.Data["AuthSources"] = authSources ctx.Data["AuthSources"] = authSources
ctx.Data["SecurityProtocols"] = securityProtocols ctx.Data["SecurityProtocols"] = securityProtocols
@ -233,28 +235,28 @@ func NewAuthSourcePost(ctx *context.Context) {
hasTLS := false hasTLS := false
var config convert.Conversion var config convert.Conversion
switch models.LoginType(form.Type) { switch login.Type(form.Type) {
case models.LoginLDAP, models.LoginDLDAP: case login.LDAP, login.DLDAP:
config = parseLDAPConfig(form) config = parseLDAPConfig(form)
hasTLS = ldap.SecurityProtocol(form.SecurityProtocol) > ldap.SecurityProtocolUnencrypted hasTLS = ldap.SecurityProtocol(form.SecurityProtocol) > ldap.SecurityProtocolUnencrypted
case models.LoginSMTP: case login.SMTP:
config = parseSMTPConfig(form) config = parseSMTPConfig(form)
hasTLS = true hasTLS = true
case models.LoginPAM: case login.PAM:
config = &pamService.Source{ config = &pamService.Source{
ServiceName: form.PAMServiceName, ServiceName: form.PAMServiceName,
EmailDomain: form.PAMEmailDomain, EmailDomain: form.PAMEmailDomain,
} }
case models.LoginOAuth2: case login.OAuth2:
config = parseOAuth2Config(form) config = parseOAuth2Config(form)
case models.LoginSSPI: case login.SSPI:
var err error var err error
config, err = parseSSPIConfig(ctx, form) config, err = parseSSPIConfig(ctx, form)
if err != nil { if err != nil {
ctx.RenderWithErr(err.Error(), tplAuthNew, form) ctx.RenderWithErr(err.Error(), tplAuthNew, form)
return return
} }
existing, err := models.LoginSourcesByType(models.LoginSSPI) existing, err := login.SourcesByType(login.SSPI)
if err != nil || len(existing) > 0 { if err != nil || len(existing) > 0 {
ctx.Data["Err_Type"] = true ctx.Data["Err_Type"] = true
ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_of_type_exist"), tplAuthNew, form) ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_of_type_exist"), tplAuthNew, form)
@ -271,18 +273,18 @@ func NewAuthSourcePost(ctx *context.Context) {
return return
} }
if err := models.CreateLoginSource(&models.LoginSource{ if err := login.CreateSource(&login.Source{
Type: models.LoginType(form.Type), Type: login.Type(form.Type),
Name: form.Name, Name: form.Name,
IsActive: form.IsActive, IsActive: form.IsActive,
IsSyncEnabled: form.IsSyncEnabled, IsSyncEnabled: form.IsSyncEnabled,
Cfg: config, Cfg: config,
}); err != nil { }); err != nil {
if models.IsErrLoginSourceAlreadyExist(err) { if login.IsErrSourceAlreadyExist(err) {
ctx.Data["Err_Name"] = true ctx.Data["Err_Name"] = true
ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_exist", err.(models.ErrLoginSourceAlreadyExist).Name), tplAuthNew, form) ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_exist", err.(login.ErrSourceAlreadyExist).Name), tplAuthNew, form)
} else { } else {
ctx.ServerError("CreateSource", err) ctx.ServerError("login.CreateSource", err)
} }
return return
} }
@ -304,9 +306,9 @@ func EditAuthSource(ctx *context.Context) {
oauth2providers := oauth2.GetOAuth2Providers() oauth2providers := oauth2.GetOAuth2Providers()
ctx.Data["OAuth2Providers"] = oauth2providers ctx.Data["OAuth2Providers"] = oauth2providers
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid")) source, err := login.GetSourceByID(ctx.ParamsInt64(":authid"))
if err != nil { if err != nil {
ctx.ServerError("GetLoginSourceByID", err) ctx.ServerError("login.GetSourceByID", err)
return return
} }
ctx.Data["Source"] = source ctx.Data["Source"] = source
@ -339,9 +341,9 @@ func EditAuthSourcePost(ctx *context.Context) {
oauth2providers := oauth2.GetOAuth2Providers() oauth2providers := oauth2.GetOAuth2Providers()
ctx.Data["OAuth2Providers"] = oauth2providers ctx.Data["OAuth2Providers"] = oauth2providers
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid")) source, err := login.GetSourceByID(ctx.ParamsInt64(":authid"))
if err != nil { if err != nil {
ctx.ServerError("GetLoginSourceByID", err) ctx.ServerError("login.GetSourceByID", err)
return return
} }
ctx.Data["Source"] = source ctx.Data["Source"] = source
@ -353,19 +355,19 @@ func EditAuthSourcePost(ctx *context.Context) {
} }
var config convert.Conversion var config convert.Conversion
switch models.LoginType(form.Type) { switch login.Type(form.Type) {
case models.LoginLDAP, models.LoginDLDAP: case login.LDAP, login.DLDAP:
config = parseLDAPConfig(form) config = parseLDAPConfig(form)
case models.LoginSMTP: case login.SMTP:
config = parseSMTPConfig(form) config = parseSMTPConfig(form)
case models.LoginPAM: case login.PAM:
config = &pamService.Source{ config = &pamService.Source{
ServiceName: form.PAMServiceName, ServiceName: form.PAMServiceName,
EmailDomain: form.PAMEmailDomain, EmailDomain: form.PAMEmailDomain,
} }
case models.LoginOAuth2: case login.OAuth2:
config = parseOAuth2Config(form) config = parseOAuth2Config(form)
case models.LoginSSPI: case login.SSPI:
config, err = parseSSPIConfig(ctx, form) config, err = parseSSPIConfig(ctx, form)
if err != nil { if err != nil {
ctx.RenderWithErr(err.Error(), tplAuthEdit, form) ctx.RenderWithErr(err.Error(), tplAuthEdit, form)
@ -380,7 +382,7 @@ func EditAuthSourcePost(ctx *context.Context) {
source.IsActive = form.IsActive source.IsActive = form.IsActive
source.IsSyncEnabled = form.IsSyncEnabled source.IsSyncEnabled = form.IsSyncEnabled
source.Cfg = config source.Cfg = config
if err := models.UpdateSource(source); err != nil { if err := login.UpdateSource(source); err != nil {
if models.IsErrOpenIDConnectInitialize(err) { if models.IsErrOpenIDConnectInitialize(err) {
ctx.Flash.Error(err.Error(), true) ctx.Flash.Error(err.Error(), true)
ctx.HTML(http.StatusOK, tplAuthEdit) ctx.HTML(http.StatusOK, tplAuthEdit)
@ -397,17 +399,17 @@ func EditAuthSourcePost(ctx *context.Context) {
// DeleteAuthSource response for deleting an auth source // DeleteAuthSource response for deleting an auth source
func DeleteAuthSource(ctx *context.Context) { func DeleteAuthSource(ctx *context.Context) {
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid")) source, err := login.GetSourceByID(ctx.ParamsInt64(":authid"))
if err != nil { if err != nil {
ctx.ServerError("GetLoginSourceByID", err) ctx.ServerError("login.GetSourceByID", err)
return return
} }
if err = models.DeleteSource(source); err != nil { if err = auth_service.DeleteLoginSource(source); err != nil {
if models.IsErrLoginSourceInUse(err) { if login.IsErrSourceInUse(err) {
ctx.Flash.Error(ctx.Tr("admin.auths.still_in_used")) ctx.Flash.Error(ctx.Tr("admin.auths.still_in_used"))
} else { } else {
ctx.Flash.Error(fmt.Sprintf("DeleteSource: %v", err)) ctx.Flash.Error(fmt.Sprintf("DeleteLoginSource: %v", err))
} }
ctx.JSON(http.StatusOK, map[string]interface{}{ ctx.JSON(http.StatusOK, map[string]interface{}{
"redirect": setting.AppSubURL + "/admin/auths/" + ctx.Params(":authid"), "redirect": setting.AppSubURL + "/admin/auths/" + ctx.Params(":authid"),

@ -10,6 +10,7 @@ import (
"net/url" "net/url"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -28,7 +29,7 @@ func Emails(ctx *context.Context) {
ctx.Data["PageIsAdminEmails"] = true ctx.Data["PageIsAdminEmails"] = true
opts := &models.SearchEmailOptions{ opts := &models.SearchEmailOptions{
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
PageSize: setting.UI.Admin.UserPagingNum, PageSize: setting.UI.Admin.UserPagingNum,
Page: ctx.FormInt("page"), Page: ctx.FormInt("page"),
}, },

@ -7,6 +7,7 @@ package admin
import ( import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -27,7 +28,7 @@ func Organizations(ctx *context.Context) {
explore.RenderUserSearch(ctx, &models.SearchUserOptions{ explore.RenderUserSearch(ctx, &models.SearchUserOptions{
Actor: ctx.User, Actor: ctx.User,
Type: models.UserTypeOrganization, Type: models.UserTypeOrganization,
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
PageSize: setting.UI.Admin.OrgPagingNum, PageSize: setting.UI.Admin.OrgPagingNum,
}, },
Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate}, Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},

@ -10,6 +10,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -68,7 +69,7 @@ func UnadoptedRepos(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminRepositories"] = true ctx.Data["PageIsAdminRepositories"] = true
opts := models.ListOptions{ opts := db.ListOptions{
PageSize: setting.UI.Admin.UserPagingNum, PageSize: setting.UI.Admin.UserPagingNum,
Page: ctx.FormInt("page"), Page: ctx.FormInt("page"),
} }

@ -12,6 +12,8 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -39,7 +41,7 @@ func Users(ctx *context.Context) {
explore.RenderUserSearch(ctx, &models.SearchUserOptions{ explore.RenderUserSearch(ctx, &models.SearchUserOptions{
Actor: ctx.User, Actor: ctx.User,
Type: models.UserTypeIndividual, Type: models.UserTypeIndividual,
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
PageSize: setting.UI.Admin.UserPagingNum, PageSize: setting.UI.Admin.UserPagingNum,
}, },
SearchByEmail: true, SearchByEmail: true,
@ -56,9 +58,9 @@ func NewUser(ctx *context.Context) {
ctx.Data["login_type"] = "0-0" ctx.Data["login_type"] = "0-0"
sources, err := models.LoginSources() sources, err := login.Sources()
if err != nil { if err != nil {
ctx.ServerError("LoginSources", err) ctx.ServerError("login.Sources", err)
return return
} }
ctx.Data["Sources"] = sources ctx.Data["Sources"] = sources
@ -75,9 +77,9 @@ func NewUserPost(ctx *context.Context) {
ctx.Data["PageIsAdminUsers"] = true ctx.Data["PageIsAdminUsers"] = true
ctx.Data["DefaultUserVisibilityMode"] = setting.Service.DefaultUserVisibilityMode ctx.Data["DefaultUserVisibilityMode"] = setting.Service.DefaultUserVisibilityMode
sources, err := models.LoginSources() sources, err := login.Sources()
if err != nil { if err != nil {
ctx.ServerError("LoginSources", err) ctx.ServerError("login.Sources", err)
return return
} }
ctx.Data["Sources"] = sources ctx.Data["Sources"] = sources
@ -94,19 +96,19 @@ func NewUserPost(ctx *context.Context) {
Email: form.Email, Email: form.Email,
Passwd: form.Password, Passwd: form.Password,
IsActive: true, IsActive: true,
LoginType: models.LoginPlain, LoginType: login.Plain,
} }
if len(form.LoginType) > 0 { if len(form.LoginType) > 0 {
fields := strings.Split(form.LoginType, "-") fields := strings.Split(form.LoginType, "-")
if len(fields) == 2 { if len(fields) == 2 {
lType, _ := strconv.ParseInt(fields[0], 10, 0) lType, _ := strconv.ParseInt(fields[0], 10, 0)
u.LoginType = models.LoginType(lType) u.LoginType = login.Type(lType)
u.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64) u.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64)
u.LoginName = form.LoginName u.LoginName = form.LoginName
} }
} }
if u.LoginType == models.LoginNoType || u.LoginType == models.LoginPlain { if u.LoginType == login.NoType || u.LoginType == login.Plain {
if len(form.Password) < setting.MinPasswordLength { if len(form.Password) < setting.MinPasswordLength {
ctx.Data["Err_Password"] = true ctx.Data["Err_Password"] = true
ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplUserNew, &form) ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplUserNew, &form)
@ -176,18 +178,18 @@ func prepareUserInfo(ctx *context.Context) *models.User {
ctx.Data["User"] = u ctx.Data["User"] = u
if u.LoginSource > 0 { if u.LoginSource > 0 {
ctx.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource) ctx.Data["LoginSource"], err = login.GetSourceByID(u.LoginSource)
if err != nil { if err != nil {
ctx.ServerError("GetLoginSourceByID", err) ctx.ServerError("login.GetSourceByID", err)
return nil return nil
} }
} else { } else {
ctx.Data["LoginSource"] = &models.LoginSource{} ctx.Data["LoginSource"] = &login.Source{}
} }
sources, err := models.LoginSources() sources, err := login.Sources()
if err != nil { if err != nil {
ctx.ServerError("LoginSources", err) ctx.ServerError("login.Sources", err)
return nil return nil
} }
ctx.Data["Sources"] = sources ctx.Data["Sources"] = sources
@ -247,7 +249,7 @@ func EditUserPost(ctx *context.Context) {
if u.LoginSource != loginSource { if u.LoginSource != loginSource {
u.LoginSource = loginSource u.LoginSource = loginSource
u.LoginType = models.LoginType(loginType) u.LoginType = login.Type(loginType)
} }
} }

@ -9,6 +9,7 @@ import (
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/eventsource" "code.gitea.io/gitea/modules/eventsource"
@ -93,7 +94,7 @@ loop:
go unregister() go unregister()
break loop break loop
case <-stopwatchTimer.C: case <-stopwatchTimer.C:
sws, err := models.GetUserStopwatches(ctx.User.ID, models.ListOptions{}) sws, err := models.GetUserStopwatches(ctx.User.ID, db.ListOptions{})
if err != nil { if err != nil {
log.Error("Unable to GetUserStopwatches: %v", err) log.Error("Unable to GetUserStopwatches: %v", err)
continue continue

@ -6,6 +6,7 @@ package explore
import ( import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -33,7 +34,7 @@ func Organizations(ctx *context.Context) {
RenderUserSearch(ctx, &models.SearchUserOptions{ RenderUserSearch(ctx, &models.SearchUserOptions{
Actor: ctx.User, Actor: ctx.User,
Type: models.UserTypeOrganization, Type: models.UserTypeOrganization,
ListOptions: models.ListOptions{PageSize: setting.UI.ExplorePagingNum}, ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum},
Visible: visibleTypes, Visible: visibleTypes,
}, tplExploreOrganizations) }, tplExploreOrganizations)
} }

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -77,7 +78,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
ctx.Data["TopicOnly"] = topicOnly ctx.Data["TopicOnly"] = topicOnly
repos, count, err = models.SearchRepository(&models.SearchRepoOptions{ repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
Page: page, Page: page,
PageSize: opts.PageSize, PageSize: opts.PageSize,
}, },

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -99,7 +100,7 @@ func Users(ctx *context.Context) {
RenderUserSearch(ctx, &models.SearchUserOptions{ RenderUserSearch(ctx, &models.SearchUserOptions{
Actor: ctx.User, Actor: ctx.User,
Type: models.UserTypeIndividual, Type: models.UserTypeIndividual,
ListOptions: models.ListOptions{PageSize: setting.UI.ExplorePagingNum}, ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum},
IsActive: util.OptionalBoolTrue, IsActive: util.OptionalBoolTrue,
Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate}, Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
}, tplExploreUsers) }, tplExploreUsers)

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup"
@ -91,7 +92,7 @@ func Home(ctx *context.Context) {
err error err error
) )
repos, count, err = models.SearchRepository(&models.SearchRepoOptions{ repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
PageSize: setting.UI.User.RepoPagingNum, PageSize: setting.UI.User.RepoPagingNum,
Page: page, Page: page,
}, },
@ -110,7 +111,7 @@ func Home(ctx *context.Context) {
var opts = &models.FindOrgMembersOpts{ var opts = &models.FindOrgMembersOpts{
OrgID: org.ID, OrgID: org.ID,
PublicOnly: true, PublicOnly: true,
ListOptions: models.ListOptions{Page: 1, PageSize: 25}, ListOptions: db.ListOptions{Page: 1, PageSize: 25},
} }
if ctx.User != nil { if ctx.User != nil {

@ -16,7 +16,7 @@ import (
// RetrieveLabels find all the labels of an organization // RetrieveLabels find all the labels of an organization
func RetrieveLabels(ctx *context.Context) { func RetrieveLabels(ctx *context.Context) {
labels, err := models.GetLabelsByOrgID(ctx.Org.Organization.ID, ctx.FormString("sort"), models.ListOptions{}) labels, err := models.GetLabelsByOrgID(ctx.Org.Organization.ID, ctx.FormString("sort"), db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("RetrieveLabels.GetLabels", err) ctx.ServerError("RetrieveLabels.GetLabels", err)
return return

@ -10,6 +10,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@ -103,7 +104,7 @@ func SettingsPost(ctx *context.Context) {
// update forks visibility // update forks visibility
if visibilityChanged { if visibilityChanged {
if err := org.GetRepositories(models.ListOptions{Page: 1, PageSize: org.NumRepos}); err != nil { if err := org.GetRepositories(db.ListOptions{Page: 1, PageSize: org.NumRepos}); err != nil {
ctx.ServerError("GetRepositories", err) ctx.ServerError("GetRepositories", err)
return return
} }

@ -12,6 +12,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
@ -287,7 +288,7 @@ func Diff(ctx *context.Context) {
commitID = commit.ID.String() commitID = commit.ID.String()
} }
statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, commitID, models.ListOptions{}) statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, commitID, db.ListOptions{})
if err != nil { if err != nil {
log.Error("GetLatestCommitStatus: %v", err) log.Error("GetLatestCommitStatus: %v", err)
} }

@ -16,6 +16,7 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
@ -216,7 +217,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti
issues = []*models.Issue{} issues = []*models.Issue{}
} else { } else {
issues, err = models.Issues(&models.IssuesOptions{ issues, err = models.Issues(&models.IssuesOptions{
ListOptions: models.ListOptions{ ListOptions: db.ListOptions{
Page: pager.Paginater.Current(), Page: pager.Paginater.Current(),
PageSize: setting.UI.IssuePagingNum, PageSize: setting.UI.IssuePagingNum,
}, },
@ -278,14 +279,14 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti
return return
} }
labels, err := models.GetLabelsByRepoID(repo.ID, "", models.ListOptions{}) labels, err := models.GetLabelsByRepoID(repo.ID, "", db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetLabelsByRepoID", err) ctx.ServerError("GetLabelsByRepoID", err)
return return
} }
if repo.Owner.IsOrganization() { if repo.Owner.IsOrganization() {
orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.FormString("sort"), models.ListOptions{}) orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.FormString("sort"), db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetLabelsByOrgID", err) ctx.ServerError("GetLabelsByOrgID", err)
return return
@ -645,14 +646,14 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository, isPull boo
return nil return nil
} }
labels, err := models.GetLabelsByRepoID(repo.ID, "", models.ListOptions{}) labels, err := models.GetLabelsByRepoID(repo.ID, "", db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetLabelsByRepoID", err) ctx.ServerError("GetLabelsByRepoID", err)
return nil return nil
} }
ctx.Data["Labels"] = labels ctx.Data["Labels"] = labels
if repo.Owner.IsOrganization() { if repo.Owner.IsOrganization() {
orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.FormString("sort"), models.ListOptions{}) orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.FormString("sort"), db.ListOptions{})
if err != nil { if err != nil {
return nil return nil
} }
@ -735,10 +736,10 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleDirs [
ctx.Data[issueTemplateTitleKey] = meta.Title ctx.Data[issueTemplateTitleKey] = meta.Title
ctx.Data[ctxDataKey] = templateBody ctx.Data[ctxDataKey] = templateBody
labelIDs := make([]string, 0, len(meta.Labels)) labelIDs := make([]string, 0, len(meta.Labels))
if repoLabels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, "", models.ListOptions{}); err == nil { if repoLabels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, "", db.ListOptions{}); err == nil {
ctx.Data["Labels"] = repoLabels ctx.Data["Labels"] = repoLabels
if ctx.Repo.Owner.IsOrganization() { if ctx.Repo.Owner.IsOrganization() {
if orgLabels, err := models.GetLabelsByOrgID(ctx.Repo.Owner.ID, ctx.FormString("sort"), models.ListOptions{}); err == nil { if orgLabels, err := models.GetLabelsByOrgID(ctx.Repo.Owner.ID, ctx.FormString("sort"), db.ListOptions{}); err == nil {
ctx.Data["OrgLabels"] = orgLabels ctx.Data["OrgLabels"] = orgLabels
repoLabels = append(repoLabels, orgLabels...) repoLabels = append(repoLabels, orgLabels...)
} }
@ -1164,7 +1165,7 @@ func ViewIssue(ctx *context.Context) {
for i := range issue.Labels { for i := range issue.Labels {
labelIDMark[issue.Labels[i].ID] = true labelIDMark[issue.Labels[i].ID] = true
} }
labels, err := models.GetLabelsByRepoID(repo.ID, "", models.ListOptions{}) labels, err := models.GetLabelsByRepoID(repo.ID, "", db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetLabelsByRepoID", err) ctx.ServerError("GetLabelsByRepoID", err)
return return
@ -1172,7 +1173,7 @@ func ViewIssue(ctx *context.Context) {
ctx.Data["Labels"] = labels ctx.Data["Labels"] = labels
if repo.Owner.IsOrganization() { if repo.Owner.IsOrganization() {
orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.FormString("sort"), models.ListOptions{}) orgLabels, err := models.GetLabelsByOrgID(repo.Owner.ID, ctx.FormString("sort"), db.ListOptions{})
if err != nil { if err != nil {
ctx.ServerError("GetLabelsByOrgID", err) ctx.ServerError("GetLabelsByOrgID", err)
return return

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save