]> source.dussan.org Git - gitea.git/commitdiff
Move login related structs and functions to models/login (#17093)
authorLunny Xiao <xiaolunwen@gmail.com>
Fri, 24 Sep 2021 11:32:56 +0000 (19:32 +0800)
committerGitHub <noreply@github.com>
Fri, 24 Sep 2021 11:32:56 +0000 (19:32 +0800)
* 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

148 files changed:
.golangci.yml
cmd/admin.go
cmd/admin_auth_ldap.go
cmd/admin_auth_ldap_test.go
contrib/fixtures/fixture_generation.go
contrib/pr/checkout.go
integrations/api_oauth2_apps_test.go
integrations/integration_test.go
models/access.go
models/commit_status.go
models/commit_status_test.go
models/db/list_options.go [new file with mode: 0644]
models/db/list_options_test.go [new file with mode: 0644]
models/db/test_fixtures.go
models/db/unit_tests.go
models/error.go
models/external_login_user.go
models/gpg_key.go
models/gpg_key_commit_verification.go
models/issue.go
models/issue_comment.go
models/issue_label.go
models/issue_label_test.go
models/issue_milestone.go
models/issue_milestone_test.go
models/issue_reaction.go
models/issue_stopwatch.go
models/issue_test.go
models/issue_tracked_time.go
models/issue_watch.go
models/issue_watch_test.go
models/list_options.go [deleted file]
models/list_options_test.go [deleted file]
models/login/main_test.go [new file with mode: 0644]
models/login/oauth2.go [new file with mode: 0644]
models/login/oauth2_application.go [new file with mode: 0644]
models/login/oauth2_application_test.go [new file with mode: 0644]
models/login/source.go [new file with mode: 0644]
models/login/source_test.go [new file with mode: 0644]
models/login_source.go [deleted file]
models/login_source_test.go [deleted file]
models/migrations/migrations_test.go
models/notification.go
models/oauth2.go [deleted file]
models/oauth2_application.go [deleted file]
models/oauth2_application_test.go [deleted file]
models/org.go
models/org_team.go
models/org_test.go
models/pull_list.go
models/pull_sign.go
models/pull_test.go
models/release.go
models/repo.go
models/repo_collaboration.go
models/repo_collaboration_test.go
models/repo_generate.go
models/repo_list.go
models/repo_list_test.go
models/repo_sign.go
models/repo_transfer.go
models/repo_unit.go
models/repo_watch.go
models/repo_watch_test.go
models/review.go
models/ssh_key.go
models/ssh_key_deploy.go
models/ssh_key_principals.go
models/star.go
models/star_test.go
models/statistic.go
models/token.go
models/topic.go
models/topic_test.go
models/user.go
models/user_mail.go
models/user_mail_test.go
models/user_test.go
models/webhook.go
modules/convert/convert.go
modules/gitgraph/graph_models.go
modules/indexer/issues/indexer.go
modules/migrations/gitea_uploader_test.go
modules/repository/adopt.go
modules/repository/repo.go
routers/api/v1/admin/user.go
routers/api/v1/repo/issue.go
routers/api/v1/user/app.go
routers/api/v1/user/gpg_key.go
routers/api/v1/user/star.go
routers/api/v1/user/watch.go
routers/api/v1/utils/utils.go
routers/web/admin/auths.go
routers/web/admin/emails.go
routers/web/admin/orgs.go
routers/web/admin/repos.go
routers/web/admin/users.go
routers/web/events/events.go
routers/web/explore/org.go
routers/web/explore/repo.go
routers/web/explore/user.go
routers/web/org/home.go
routers/web/org/org_labels.go
routers/web/org/setting.go
routers/web/repo/commit.go
routers/web/repo/issue.go
routers/web/repo/issue_label.go
routers/web/repo/milestone.go
routers/web/repo/pull.go
routers/web/repo/release.go
routers/web/repo/setting.go
routers/web/repo/view.go
routers/web/user/auth.go
routers/web/user/home.go
routers/web/user/oauth.go
routers/web/user/oauth_test.go
routers/web/user/profile.go
routers/web/user/setting/applications.go
routers/web/user/setting/keys.go
routers/web/user/setting/oauth2.go
routers/web/user/setting/profile.go
routers/web/user/setting/security.go
services/auth/login_source.go [new file with mode: 0644]
services/auth/oauth2.go
services/auth/signin.go
services/auth/source/db/assert_interface_test.go
services/auth/source/db/source.go
services/auth/source/ldap/assert_interface_test.go
services/auth/source/ldap/source.go
services/auth/source/ldap/source_authenticate.go
services/auth/source/oauth2/assert_interface_test.go
services/auth/source/oauth2/init.go
services/auth/source/oauth2/providers.go
services/auth/source/oauth2/source.go
services/auth/source/pam/assert_interface_test.go
services/auth/source/pam/source.go
services/auth/source/pam/source_authenticate.go
services/auth/source/smtp/assert_interface_test.go
services/auth/source/smtp/source.go
services/auth/source/smtp/source_authenticate.go
services/auth/source/sspi/assert_interface_test.go
services/auth/source/sspi/source.go
services/auth/sspi_windows.go
services/auth/sync.go
services/externalaccount/user.go
services/pull/commit_status.go
services/pull/pull.go
services/pull/review.go

index c3dd47ec29da67e8231149355f1f25149583c058..2d66e01ffaf8a86d04564f9e0d86b39676eaf76e 100644 (file)
@@ -111,4 +111,3 @@ issues:
       linters:
         - staticcheck
       text: "svc.IsAnInteractiveSession is deprecated: Use IsWindowsService instead."
-
index cfc297c47464674264bdc962bd0ce1b55c1f91a8..099083ae9100b75acfccbbc7932c5cc6944bc7d1 100644 (file)
@@ -14,6 +14,8 @@ import (
        "text/tabwriter"
 
        "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/graceful"
        "code.gitea.io/gitea/modules/log"
@@ -21,6 +23,7 @@ import (
        repo_module "code.gitea.io/gitea/modules/repository"
        "code.gitea.io/gitea/modules/setting"
        "code.gitea.io/gitea/modules/storage"
+       auth_service "code.gitea.io/gitea/services/auth"
        "code.gitea.io/gitea/services/auth/source/oauth2"
 
        "github.com/urfave/cli"
@@ -529,7 +532,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
        log.Trace("Synchronizing repository releases (this may take a while)")
        for page := 1; ; page++ {
                repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
-                       ListOptions: models.ListOptions{
+                       ListOptions: db.ListOptions{
                                PageSize: models.RepositoryListDefaultPageSize,
                                Page:     page,
                        },
@@ -629,8 +632,8 @@ func runAddOauth(c *cli.Context) error {
                return err
        }
 
-       return models.CreateLoginSource(&models.LoginSource{
-               Type:     models.LoginOAuth2,
+       return login.CreateSource(&login.Source{
+               Type:     login.OAuth2,
                Name:     c.String("name"),
                IsActive: true,
                Cfg:      parseOAuth2Config(c),
@@ -646,7 +649,7 @@ func runUpdateOauth(c *cli.Context) error {
                return err
        }
 
-       source, err := models.GetLoginSourceByID(c.Int64("id"))
+       source, err := login.GetSourceByID(c.Int64("id"))
        if err != nil {
                return err
        }
@@ -705,7 +708,7 @@ func runUpdateOauth(c *cli.Context) error {
        oAuth2Config.CustomURLMapping = customURLMapping
        source.Cfg = oAuth2Config
 
-       return models.UpdateSource(source)
+       return login.UpdateSource(source)
 }
 
 func runListAuth(c *cli.Context) error {
@@ -713,7 +716,7 @@ func runListAuth(c *cli.Context) error {
                return err
        }
 
-       loginSources, err := models.LoginSources()
+       loginSources, err := login.Sources()
 
        if err != nil {
                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)
        fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
        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()
 
@@ -749,10 +752,10 @@ func runDeleteAuth(c *cli.Context) error {
                return err
        }
 
-       source, err := models.GetLoginSourceByID(c.Int64("id"))
+       source, err := login.GetSourceByID(c.Int64("id"))
        if err != nil {
                return err
        }
 
-       return models.DeleteSource(source)
+       return auth_service.DeleteLoginSource(source)
 }
index feeaf17661f7ce4ad3a89f8d524af8977d37fa67..e95e1d15c64c26063636b82bb7fd1d9f3bdec82e 100644 (file)
@@ -8,7 +8,7 @@ import (
        "fmt"
        "strings"
 
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth/source/ldap"
 
        "github.com/urfave/cli"
@@ -17,9 +17,9 @@ import (
 type (
        authService struct {
                initDB             func() error
-               createLoginSource  func(loginSource *models.LoginSource) error
-               updateLoginSource  func(loginSource *models.LoginSource) error
-               getLoginSourceByID func(id int64) (*models.LoginSource, error)
+               createLoginSource  func(loginSource *login.Source) error
+               updateLoginSource  func(loginSource *login.Source) error
+               getLoginSourceByID func(id int64) (*login.Source, error)
        }
 )
 
@@ -164,14 +164,14 @@ var (
 func newAuthService() *authService {
        return &authService{
                initDB:             initDB,
-               createLoginSource:  models.CreateLoginSource,
-               updateLoginSource:  models.UpdateSource,
-               getLoginSourceByID: models.GetLoginSourceByID,
+               createLoginSource:  login.CreateSource,
+               updateLoginSource:  login.UpdateSource,
+               getLoginSourceByID: login.GetSourceByID,
        }
 }
 
 // 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") {
                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.
 // 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 {
                return nil, err
        }
@@ -280,7 +280,7 @@ func (a *authService) getLoginSource(c *cli.Context, loginType models.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
@@ -296,8 +296,8 @@ func (a *authService) addLdapBindDn(c *cli.Context) error {
                return err
        }
 
-       loginSource := &models.LoginSource{
-               Type:     models.LoginLDAP,
+       loginSource := &login.Source{
+               Type:     login.LDAP,
                IsActive: true, // active by default
                Cfg: &ldap.Source{
                        Enabled: true, // always true
@@ -318,7 +318,7 @@ func (a *authService) updateLdapBindDn(c *cli.Context) error {
                return err
        }
 
-       loginSource, err := a.getLoginSource(c, models.LoginLDAP)
+       loginSource, err := a.getLoginSource(c, login.LDAP)
        if err != nil {
                return err
        }
@@ -341,8 +341,8 @@ func (a *authService) addLdapSimpleAuth(c *cli.Context) error {
                return err
        }
 
-       loginSource := &models.LoginSource{
-               Type:     models.LoginDLDAP,
+       loginSource := &login.Source{
+               Type:     login.DLDAP,
                IsActive: true, // active by default
                Cfg: &ldap.Source{
                        Enabled: true, // always true
@@ -363,7 +363,7 @@ func (a *authService) updateLdapSimpleAuth(c *cli.Context) error {
                return err
        }
 
-       loginSource, err := a.getLoginSource(c, models.LoginDLDAP)
+       loginSource, err := a.getLoginSource(c, login.DLDAP)
        if err != nil {
                return err
        }
index 692b11e3f422db740d038881a16b217e0775f3c4..c26cbdaf39c6cef587f22437d0142e9242871e37 100644 (file)
@@ -7,7 +7,7 @@ package cmd
 import (
        "testing"
 
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth/source/ldap"
 
        "github.com/stretchr/testify/assert"
@@ -23,7 +23,7 @@ func TestAddLdapBindDn(t *testing.T) {
        // Test cases
        var cases = []struct {
                args        []string
-               loginSource *models.LoginSource
+               loginSource *login.Source
                errMsg      string
        }{
                // case 0
@@ -51,8 +51,8 @@ func TestAddLdapBindDn(t *testing.T) {
                                "--synchronize-users",
                                "--page-size", "99",
                        },
-                       loginSource: &models.LoginSource{
-                               Type:          models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type:          login.LDAP,
                                Name:          "ldap (via Bind DN) source full",
                                IsActive:      false,
                                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)",
                                "--email-attribute", "mail-bind min",
                        },
-                       loginSource: &models.LoginSource{
-                               Type:     models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type:     login.LDAP,
                                Name:     "ldap (via Bind DN) source min",
                                IsActive: true,
                                Cfg: &ldap.Source{
@@ -203,20 +203,20 @@ func TestAddLdapBindDn(t *testing.T) {
 
        for n, c := range cases {
                // Mock functions.
-               var createdLoginSource *models.LoginSource
+               var createdLoginSource *login.Source
                service := &authService{
                        initDB: func() error {
                                return nil
                        },
-                       createLoginSource: func(loginSource *models.LoginSource) error {
+                       createLoginSource: func(loginSource *login.Source) error {
                                createdLoginSource = loginSource
                                return nil
                        },
-                       updateLoginSource: func(loginSource *models.LoginSource) error {
+                       updateLoginSource: func(loginSource *login.Source) error {
                                assert.FailNow(t, "case %d: should not call updateLoginSource", n)
                                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)
                                return nil, nil
                        },
@@ -247,7 +247,7 @@ func TestAddLdapSimpleAuth(t *testing.T) {
        // Test cases
        var cases = []struct {
                args        []string
-               loginSource *models.LoginSource
+               loginSource *login.Source
                errMsg      string
        }{
                // case 0
@@ -271,8 +271,8 @@ func TestAddLdapSimpleAuth(t *testing.T) {
                                "--public-ssh-key-attribute", "publickey-simple full",
                                "--user-dn", "cn=%s,ou=Users,dc=full-domain-simple,dc=org",
                        },
-                       loginSource: &models.LoginSource{
-                               Type:     models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type:     login.DLDAP,
                                Name:     "ldap (simple auth) source full",
                                IsActive: false,
                                Cfg: &ldap.Source{
@@ -307,8 +307,8 @@ func TestAddLdapSimpleAuth(t *testing.T) {
                                "--email-attribute", "mail-simple min",
                                "--user-dn", "cn=%s,ou=Users,dc=min-domain-simple,dc=org",
                        },
-                       loginSource: &models.LoginSource{
-                               Type:     models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type:     login.DLDAP,
                                Name:     "ldap (simple auth) source min",
                                IsActive: true,
                                Cfg: &ldap.Source{
@@ -432,20 +432,20 @@ func TestAddLdapSimpleAuth(t *testing.T) {
 
        for n, c := range cases {
                // Mock functions.
-               var createdLoginSource *models.LoginSource
+               var createdLoginSource *login.Source
                service := &authService{
                        initDB: func() error {
                                return nil
                        },
-                       createLoginSource: func(loginSource *models.LoginSource) error {
+                       createLoginSource: func(loginSource *login.Source) error {
                                createdLoginSource = loginSource
                                return nil
                        },
-                       updateLoginSource: func(loginSource *models.LoginSource) error {
+                       updateLoginSource: func(loginSource *login.Source) error {
                                assert.FailNow(t, "case %d: should not call updateLoginSource", n)
                                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)
                                return nil, nil
                        },
@@ -477,8 +477,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
        var cases = []struct {
                args                []string
                id                  int64
-               existingLoginSource *models.LoginSource
-               loginSource         *models.LoginSource
+               existingLoginSource *login.Source
+               loginSource         *login.Source
                errMsg              string
        }{
                // case 0
@@ -507,15 +507,15 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--page-size", "99",
                        },
                        id: 23,
-                       existingLoginSource: &models.LoginSource{
-                               Type:     models.LoginLDAP,
+                       existingLoginSource: &login.Source{
+                               Type:     login.LDAP,
                                IsActive: true,
                                Cfg: &ldap.Source{
                                        Enabled: true,
                                },
                        },
-                       loginSource: &models.LoginSource{
-                               Type:          models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type:          login.LDAP,
                                Name:          "ldap (via Bind DN) source full",
                                IsActive:      false,
                                IsSyncEnabled: true,
@@ -548,8 +548,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "ldap-test",
                                "--id", "1",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg:  &ldap.Source{},
                        },
                },
@@ -560,8 +560,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--name", "ldap (via Bind DN) source",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Name: "ldap (via Bind DN) source",
                                Cfg: &ldap.Source{
                                        Name: "ldap (via Bind DN) source",
@@ -575,13 +575,13 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--not-active",
                        },
-                       existingLoginSource: &models.LoginSource{
-                               Type:     models.LoginLDAP,
+                       existingLoginSource: &login.Source{
+                               Type:     login.LDAP,
                                IsActive: true,
                                Cfg:      &ldap.Source{},
                        },
-                       loginSource: &models.LoginSource{
-                               Type:     models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type:     login.LDAP,
                                IsActive: false,
                                Cfg:      &ldap.Source{},
                        },
@@ -593,8 +593,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--security-protocol", "LDAPS",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        SecurityProtocol: ldap.SecurityProtocol(1),
                                },
@@ -607,8 +607,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--skip-tls-verify",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        SkipVerify: true,
                                },
@@ -621,8 +621,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--host", "ldap-server",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        Host: "ldap-server",
                                },
@@ -635,8 +635,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--port", "389",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        Port: 389,
                                },
@@ -649,8 +649,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--user-search-base", "ou=Users,dc=domain,dc=org",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        UserBase: "ou=Users,dc=domain,dc=org",
                                },
@@ -663,8 +663,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--user-filter", "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        Filter: "(memberOf=cn=user-group,ou=example,dc=domain,dc=org)",
                                },
@@ -677,8 +677,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
                                },
@@ -691,8 +691,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--username-attribute", "uid",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        AttributeUsername: "uid",
                                },
@@ -705,8 +705,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--firstname-attribute", "givenName",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        AttributeName: "givenName",
                                },
@@ -719,8 +719,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--surname-attribute", "sn",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        AttributeSurname: "sn",
                                },
@@ -733,8 +733,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--email-attribute", "mail",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        AttributeMail: "mail",
                                },
@@ -747,8 +747,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--attributes-in-bind",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        AttributesInBind: true,
                                },
@@ -761,8 +761,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--public-ssh-key-attribute", "publickey",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        AttributeSSHPublicKey: "publickey",
                                },
@@ -775,8 +775,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--bind-dn", "cn=readonly,dc=domain,dc=org",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        BindDN: "cn=readonly,dc=domain,dc=org",
                                },
@@ -789,8 +789,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--bind-password", "secret",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        BindPassword: "secret",
                                },
@@ -803,8 +803,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--synchronize-users",
                        },
-                       loginSource: &models.LoginSource{
-                               Type:          models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type:          login.LDAP,
                                IsSyncEnabled: true,
                                Cfg:           &ldap.Source{},
                        },
@@ -816,8 +816,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "--id", "1",
                                "--page-size", "12",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginLDAP,
+                       loginSource: &login.Source{
+                               Type: login.LDAP,
                                Cfg: &ldap.Source{
                                        SearchPageSize: 12,
                                },
@@ -845,8 +845,8 @@ func TestUpdateLdapBindDn(t *testing.T) {
                                "ldap-test",
                                "--id", "1",
                        },
-                       existingLoginSource: &models.LoginSource{
-                               Type: models.LoginOAuth2,
+                       existingLoginSource: &login.Source{
+                               Type: login.OAuth2,
                                Cfg:  &ldap.Source{},
                        },
                        errMsg: "Invalid authentication type. expected: LDAP (via BindDN), actual: OAuth2",
@@ -855,28 +855,28 @@ func TestUpdateLdapBindDn(t *testing.T) {
 
        for n, c := range cases {
                // Mock functions.
-               var updatedLoginSource *models.LoginSource
+               var updatedLoginSource *login.Source
                service := &authService{
                        initDB: func() error {
                                return nil
                        },
-                       createLoginSource: func(loginSource *models.LoginSource) error {
+                       createLoginSource: func(loginSource *login.Source) error {
                                assert.FailNow(t, "case %d: should not call createLoginSource", n)
                                return nil
                        },
-                       updateLoginSource: func(loginSource *models.LoginSource) error {
+                       updateLoginSource: func(loginSource *login.Source) error {
                                updatedLoginSource = loginSource
                                return nil
                        },
-                       getLoginSourceByID: func(id int64) (*models.LoginSource, error) {
+                       getLoginSourceByID: func(id int64) (*login.Source, error) {
                                if c.id != 0 {
                                        assert.Equal(t, c.id, id, "case %d: wrong id", n)
                                }
                                if c.existingLoginSource != nil {
                                        return c.existingLoginSource, nil
                                }
-                               return &models.LoginSource{
-                                       Type: models.LoginLDAP,
+                               return &login.Source{
+                                       Type: login.LDAP,
                                        Cfg:  &ldap.Source{},
                                }, nil
                        },
@@ -908,8 +908,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
        var cases = []struct {
                args                []string
                id                  int64
-               existingLoginSource *models.LoginSource
-               loginSource         *models.LoginSource
+               existingLoginSource *login.Source
+               loginSource         *login.Source
                errMsg              string
        }{
                // case 0
@@ -935,8 +935,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--user-dn", "cn=%s,ou=Users,dc=full-domain-simple,dc=org",
                        },
                        id: 7,
-                       loginSource: &models.LoginSource{
-                               Type:     models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type:     login.DLDAP,
                                Name:     "ldap (simple auth) source full",
                                IsActive: false,
                                Cfg: &ldap.Source{
@@ -964,8 +964,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "ldap-test",
                                "--id", "1",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg:  &ldap.Source{},
                        },
                },
@@ -976,8 +976,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--name", "ldap (simple auth) source",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Name: "ldap (simple auth) source",
                                Cfg: &ldap.Source{
                                        Name: "ldap (simple auth) source",
@@ -991,13 +991,13 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--not-active",
                        },
-                       existingLoginSource: &models.LoginSource{
-                               Type:     models.LoginDLDAP,
+                       existingLoginSource: &login.Source{
+                               Type:     login.DLDAP,
                                IsActive: true,
                                Cfg:      &ldap.Source{},
                        },
-                       loginSource: &models.LoginSource{
-                               Type:     models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type:     login.DLDAP,
                                IsActive: false,
                                Cfg:      &ldap.Source{},
                        },
@@ -1009,8 +1009,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--security-protocol", "starttls",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        SecurityProtocol: ldap.SecurityProtocol(2),
                                },
@@ -1023,8 +1023,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--skip-tls-verify",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        SkipVerify: true,
                                },
@@ -1037,8 +1037,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--host", "ldap-server",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        Host: "ldap-server",
                                },
@@ -1051,8 +1051,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--port", "987",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        Port: 987,
                                },
@@ -1065,8 +1065,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--user-search-base", "ou=Users,dc=domain,dc=org",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        UserBase: "ou=Users,dc=domain,dc=org",
                                },
@@ -1079,8 +1079,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--user-filter", "(&(objectClass=posixAccount)(cn=%s))",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        Filter: "(&(objectClass=posixAccount)(cn=%s))",
                                },
@@ -1093,8 +1093,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--admin-filter", "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        AdminFilter: "(memberOf=cn=admin-group,ou=example,dc=domain,dc=org)",
                                },
@@ -1107,8 +1107,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--username-attribute", "uid",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        AttributeUsername: "uid",
                                },
@@ -1121,8 +1121,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--firstname-attribute", "givenName",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        AttributeName: "givenName",
                                },
@@ -1135,8 +1135,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--surname-attribute", "sn",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        AttributeSurname: "sn",
                                },
@@ -1149,8 +1149,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--email-attribute", "mail",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
 
                                        AttributeMail: "mail",
@@ -1164,8 +1164,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--public-ssh-key-attribute", "publickey",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        AttributeSSHPublicKey: "publickey",
                                },
@@ -1178,8 +1178,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "--id", "1",
                                "--user-dn", "cn=%s,ou=Users,dc=domain,dc=org",
                        },
-                       loginSource: &models.LoginSource{
-                               Type: models.LoginDLDAP,
+                       loginSource: &login.Source{
+                               Type: login.DLDAP,
                                Cfg: &ldap.Source{
                                        UserDN: "cn=%s,ou=Users,dc=domain,dc=org",
                                },
@@ -1207,8 +1207,8 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
                                "ldap-test",
                                "--id", "1",
                        },
-                       existingLoginSource: &models.LoginSource{
-                               Type: models.LoginPAM,
+                       existingLoginSource: &login.Source{
+                               Type: login.PAM,
                                Cfg:  &ldap.Source{},
                        },
                        errMsg: "Invalid authentication type. expected: LDAP (simple auth), actual: PAM",
@@ -1217,28 +1217,28 @@ func TestUpdateLdapSimpleAuth(t *testing.T) {
 
        for n, c := range cases {
                // Mock functions.
-               var updatedLoginSource *models.LoginSource
+               var updatedLoginSource *login.Source
                service := &authService{
                        initDB: func() error {
                                return nil
                        },
-                       createLoginSource: func(loginSource *models.LoginSource) error {
+                       createLoginSource: func(loginSource *login.Source) error {
                                assert.FailNow(t, "case %d: should not call createLoginSource", n)
                                return nil
                        },
-                       updateLoginSource: func(loginSource *models.LoginSource) error {
+                       updateLoginSource: func(loginSource *login.Source) error {
                                updatedLoginSource = loginSource
                                return nil
                        },
-                       getLoginSourceByID: func(id int64) (*models.LoginSource, error) {
+                       getLoginSourceByID: func(id int64) (*login.Source, error) {
                                if c.id != 0 {
                                        assert.Equal(t, c.id, id, "case %d: wrong id", n)
                                }
                                if c.existingLoginSource != nil {
                                        return c.existingLoginSource, nil
                                }
-                               return &models.LoginSource{
-                                       Type: models.LoginDLDAP,
+                               return &login.Source{
+                                       Type: login.DLDAP,
                                        Cfg:  &ldap.Source{},
                                }, nil
                        },
index 5408a005c663d681a6c0dec7e477ec1f7c990ef3..5e7dd39a78fb1b90ff6fa583e3b8dc057e3d0f25 100644 (file)
@@ -31,7 +31,9 @@ var (
 func main() {
        pathToGiteaRoot := "."
        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)
                os.Exit(1)
        }
index cba6d4d372da514b1b3a1fe22bda085ca6939438..d831ebdabdb2f29ad051dc8fdb27ae384210633b 100644 (file)
@@ -101,7 +101,9 @@ func runPR() {
        db.HasEngine = true
        //x.ShowSQL(true)
        err = db.InitFixtures(
-               path.Join(curDir, "models/fixtures/"),
+               db.FixturesOptions{
+                       Dir: path.Join(curDir, "models/fixtures/"),
+               },
        )
        if err != nil {
                fmt.Printf("Error initializing test database: %v\n", err)
index 4cda41755a6c1a6eb99be12f2c37f56dd0b657e0..6f0a46249f5156b948c39a9e2b9619c4410629ca 100644 (file)
@@ -11,6 +11,7 @@ import (
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        api "code.gitea.io/gitea/modules/structs"
 
        "github.com/stretchr/testify/assert"
@@ -46,7 +47,7 @@ func testAPICreateOAuth2Application(t *testing.T) {
        assert.Len(t, createdApp.ClientID, 36)
        assert.NotEmpty(t, createdApp.Created)
        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) {
@@ -54,13 +55,13 @@ func testAPIListOAuth2Applications(t *testing.T) {
        session := loginUser(t, user.Name)
        token := getTokenForLoggedInUser(t, session)
 
-       existApp := db.AssertExistsAndLoadBean(t, &models.OAuth2Application{
+       existApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{
                UID:  user.ID,
                Name: "test-app-1",
                RedirectURIs: []string{
                        "http://www.google.com",
                },
-       }).(*models.OAuth2Application)
+       }).(*login.OAuth2Application)
 
        urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2?token=%s", token)
        req := NewRequest(t, "GET", urlStr)
@@ -75,7 +76,7 @@ func testAPIListOAuth2Applications(t *testing.T) {
        assert.Len(t, expectedApp.ClientID, 36)
        assert.Empty(t, expectedApp.ClientSecret)
        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) {
@@ -83,16 +84,16 @@ func testAPIDeleteOAuth2Application(t *testing.T) {
        session := loginUser(t, user.Name)
        token := getTokenForLoggedInUser(t, session)
 
-       oldApp := db.AssertExistsAndLoadBean(t, &models.OAuth2Application{
+       oldApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{
                UID:  user.ID,
                Name: "test-app-1",
-       }).(*models.OAuth2Application)
+       }).(*login.OAuth2Application)
 
        urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", oldApp.ID, token)
        req := NewRequest(t, "DELETE", urlStr)
        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
        req = NewRequest(t, "DELETE", urlStr)
@@ -104,13 +105,13 @@ func testAPIGetOAuth2Application(t *testing.T) {
        session := loginUser(t, user.Name)
        token := getTokenForLoggedInUser(t, session)
 
-       existApp := db.AssertExistsAndLoadBean(t, &models.OAuth2Application{
+       existApp := db.AssertExistsAndLoadBean(t, &login.OAuth2Application{
                UID:  user.ID,
                Name: "test-app-1",
                RedirectURIs: []string{
                        "http://www.google.com",
                },
-       }).(*models.OAuth2Application)
+       }).(*login.OAuth2Application)
 
        urlStr := fmt.Sprintf("/api/v1/user/applications/oauth2/%d?token=%s", existApp.ID, token)
        req := NewRequest(t, "GET", urlStr)
@@ -126,19 +127,19 @@ func testAPIGetOAuth2Application(t *testing.T) {
        assert.Empty(t, expectedApp.ClientSecret)
        assert.Len(t, expectedApp.RedirectURIs, 1)
        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) {
        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,
                Name: "test-app-1",
                RedirectURIs: []string{
                        "http://www.google.com",
                },
-       }).(*models.OAuth2Application)
+       }).(*login.OAuth2Application)
 
        appBody := api.CreateOAuth2ApplicationOptions{
                Name: "test-app-1",
@@ -160,5 +161,5 @@ func testAPIUpdateOAuth2Application(t *testing.T) {
        assert.Len(t, expectedApp.RedirectURIs, 2)
        assert.EqualValues(t, expectedApp.RedirectURIs[0], appBody.RedirectURIs[0])
        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})
 }
index fac36320cf88213ad37f6a2d1892d19cfaa92e1b..1429893270b0ebb4d09e63fe1fa7b4553cd1b478 100644 (file)
@@ -113,7 +113,9 @@ func TestMain(m *testing.M) {
        }
 
        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 {
                fmt.Printf("Error initializing test database: %v\n", err)
index 88fbe8189fa8e812290ef984f774d1f449b21168..560234aae807e129a541999c54278ff959d302c5 100644 (file)
@@ -225,7 +225,7 @@ func (repo *Repository) refreshAccesses(e db.Engine, accessMap map[int64]*userAc
 
 // refreshCollaboratorAccesses retrieves repository collaborations with their access modes.
 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 {
                return fmt.Errorf("getCollaborations: %v", err)
        }
index ada94667ccccf5d8df4c006dc75995e1de9e96e6..a6ded049c31cd2bbb6ead8f154bdbe09fb663789 100644 (file)
@@ -163,7 +163,7 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
 
 // CommitStatusOptions holds the options for query commit statuses
 type CommitStatusOptions struct {
-       ListOptions
+       db.ListOptions
        State    string
        SortType string
 }
@@ -178,7 +178,7 @@ func GetCommitStatuses(repo *Repository, sha string, opts *CommitStatusOptions)
        }
 
        countSession := listCommitStatusesStatement(repo, sha, opts)
-       countSession = setSessionPagination(countSession, opts)
+       countSession = db.SetSessionPagination(countSession, opts)
        maxResults, err := countSession.Count(new(CommitStatus))
        if err != nil {
                log.Error("Count PRs: %v", err)
@@ -187,7 +187,7 @@ func GetCommitStatuses(repo *Repository, sha string, opts *CommitStatusOptions)
 
        statuses := make([]*CommitStatus, 0, opts.PageSize)
        findSession := listCommitStatusesStatement(repo, sha, opts)
-       findSession = setSessionPagination(findSession, opts)
+       findSession = db.SetSessionPagination(findSession, opts)
        sortCommitStatusesSession(findSession, opts.SortType)
        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.
-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)
 }
 
-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)
        sess := e.Table(&CommitStatus{}).
                Where("repo_id = ?", repoID).And("sha = ?", sha).
                Select("max( id ) as id").
                GroupBy("context_hash").OrderBy("max( id ) desc")
 
-       sess = setSessionPagination(sess, &listOptions)
+       sess = db.SetSessionPagination(sess, &listOptions)
 
        err := sess.Find(&ids)
        if err != nil {
@@ -336,7 +336,7 @@ func ParseCommitsWithStatus(oldCommits []*SignCommit, repo *Repository) []*SignC
                commit := &SignCommitWithStatuses{
                        SignCommit: c,
                }
-               statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{})
+               statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), db.ListOptions{})
                if err != nil {
                        log.Error("GetLatestCommitStatus: %v", err)
                } else {
index 0d8dbf264686e3435f902d2e5274d77feea61aef..7f4709144ceb3b35fb018a2df5e8506ed9d5cb7f 100644 (file)
@@ -19,7 +19,7 @@ func TestGetCommitStatuses(t *testing.T) {
 
        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.Equal(t, int(maxResults), 5)
        assert.Len(t, statuses, 5)
diff --git a/models/db/list_options.go b/models/db/list_options.go
new file mode 100644 (file)
index 0000000..f31febf
--- /dev/null
@@ -0,0 +1,100 @@
+// 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 db
+
+import (
+       "code.gitea.io/gitea/modules/setting"
+
+       "xorm.io/xorm"
+)
+
+// Paginator is the base for different ListOptions types
+type Paginator interface {
+       GetSkipTake() (skip, take int)
+       GetStartEnd() (start, end int)
+}
+
+// GetPaginatedSession creates a paginated database session
+func GetPaginatedSession(p Paginator) *xorm.Session {
+       skip, take := p.GetSkipTake()
+
+       return x.Limit(take, skip)
+}
+
+// SetSessionPagination sets pagination for a database session
+func SetSessionPagination(sess *xorm.Session, p Paginator) *xorm.Session {
+       skip, take := p.GetSkipTake()
+
+       return sess.Limit(take, skip)
+}
+
+// SetEnginePagination sets pagination for a database engine
+func SetEnginePagination(e Engine, p Paginator) Engine {
+       skip, take := p.GetSkipTake()
+
+       return e.Limit(take, skip)
+}
+
+// ListOptions options to paginate results
+type ListOptions struct {
+       PageSize int
+       Page     int // start from 1
+}
+
+// GetSkipTake returns the skip and take values
+func (opts *ListOptions) GetSkipTake() (skip, take int) {
+       opts.SetDefaultValues()
+       return (opts.Page - 1) * opts.PageSize, opts.PageSize
+}
+
+// GetStartEnd returns the start and end of the ListOptions
+func (opts *ListOptions) GetStartEnd() (start, end int) {
+       start, take := opts.GetSkipTake()
+       end = start + take
+       return
+}
+
+// SetDefaultValues sets default values
+func (opts *ListOptions) SetDefaultValues() {
+       if opts.PageSize <= 0 {
+               opts.PageSize = setting.API.DefaultPagingNum
+       }
+       if opts.PageSize > setting.API.MaxResponseItems {
+               opts.PageSize = setting.API.MaxResponseItems
+       }
+       if opts.Page <= 0 {
+               opts.Page = 1
+       }
+}
+
+// AbsoluteListOptions absolute options to paginate results
+type AbsoluteListOptions struct {
+       skip int
+       take int
+}
+
+// NewAbsoluteListOptions creates a list option with applied limits
+func NewAbsoluteListOptions(skip, take int) *AbsoluteListOptions {
+       if skip < 0 {
+               skip = 0
+       }
+       if take <= 0 {
+               take = setting.API.DefaultPagingNum
+       }
+       if take > setting.API.MaxResponseItems {
+               take = setting.API.MaxResponseItems
+       }
+       return &AbsoluteListOptions{skip, take}
+}
+
+// GetSkipTake returns the skip and take values
+func (opts *AbsoluteListOptions) GetSkipTake() (skip, take int) {
+       return opts.skip, opts.take
+}
+
+// GetStartEnd returns the start and end values
+func (opts *AbsoluteListOptions) GetStartEnd() (start, end int) {
+       return opts.skip, opts.skip + opts.take
+}
diff --git a/models/db/list_options_test.go b/models/db/list_options_test.go
new file mode 100644 (file)
index 0000000..2c860af
--- /dev/null
@@ -0,0 +1,62 @@
+// Copyright 2021 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 db
+
+import (
+       "testing"
+
+       "code.gitea.io/gitea/modules/setting"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestPaginator(t *testing.T) {
+       cases := []struct {
+               Paginator
+               Skip  int
+               Take  int
+               Start int
+               End   int
+       }{
+               {
+                       Paginator: &ListOptions{Page: -1, PageSize: -1},
+                       Skip:      0,
+                       Take:      setting.API.DefaultPagingNum,
+                       Start:     0,
+                       End:       setting.API.DefaultPagingNum,
+               },
+               {
+                       Paginator: &ListOptions{Page: 2, PageSize: 10},
+                       Skip:      10,
+                       Take:      10,
+                       Start:     10,
+                       End:       20,
+               },
+               {
+                       Paginator: NewAbsoluteListOptions(-1, -1),
+                       Skip:      0,
+                       Take:      setting.API.DefaultPagingNum,
+                       Start:     0,
+                       End:       setting.API.DefaultPagingNum,
+               },
+               {
+                       Paginator: NewAbsoluteListOptions(2, 10),
+                       Skip:      2,
+                       Take:      10,
+                       Start:     2,
+                       End:       12,
+               },
+       }
+
+       for _, c := range cases {
+               skip, take := c.Paginator.GetSkipTake()
+               start, end := c.Paginator.GetStartEnd()
+
+               assert.Equal(t, c.Skip, skip)
+               assert.Equal(t, c.Take, take)
+               assert.Equal(t, c.Start, start)
+               assert.Equal(t, c.End, end)
+       }
+}
index 172701513309bbee79aa3c5b5d6e4da37cde7c6b..2715b688ea35690ab2111683a3239b75e472098b 100644 (file)
@@ -17,13 +17,18 @@ import (
 var fixtures *testfixtures.Loader
 
 // 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
        if len(engine) == 1 {
                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"
        switch e.Dialect().URI().DBType {
        case schemas.POSTGRES:
index 781f0ecca20f366408e5bbc1cf44168e2fd40ddc..d81610df6b6bac00c9f32e7dc2335a920d80e44e 100644 (file)
@@ -44,11 +44,21 @@ func fatalTestError(fmtStr string, args ...interface{}) {
 
 // MainTest a reusable TestMain(..) function for unit tests that need to use a
 // 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
        giteaRoot = pathToGiteaRoot
        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)
        }
 
@@ -102,8 +112,14 @@ func MainTest(m *testing.M, pathToGiteaRoot string) {
        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
-func CreateTestEngine(fixturesDir string) error {
+func CreateTestEngine(opts FixturesOptions) error {
        var err error
        x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate")
        if err != nil {
@@ -123,7 +139,7 @@ func CreateTestEngine(fixturesDir string) error {
                e:       x,
        }
 
-       return InitFixtures(fixturesDir)
+       return InitFixtures(opts)
 }
 
 // PrepareTestDatabase load test fixtures into test database
index fd8f2771ae25d967ffac3f221acb5c72818d69b5..956b240097358d816708c162c3f4b0c272c6f87a 100644 (file)
@@ -1836,58 +1836,6 @@ func (err ErrAttachmentNotExist) Error() string {
        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.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)
-}
index c6a4b71b53e2db58e73d4f4c2537eed3f65077fe..6b023a4cb2a984192b32bd3a77023032cf459499 100644 (file)
@@ -8,6 +8,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/structs"
 
        "github.com/markbates/goth"
@@ -106,7 +107,7 @@ func GetUserIDByExternalUserID(provider, userID string) (int64, error) {
 
 // UpdateExternalUser updates external user's information
 func UpdateExternalUser(user *User, gothUser goth.User) error {
-       loginSource, err := GetActiveOAuth2LoginSourceByName(gothUser.Provider)
+       loginSource, err := login.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
        if err != nil {
                return err
        }
index d8dd79c28538518e365dafe60aef5991569776a3..a62ed61966eda16b5e56f5419d713536d25521a2 100644 (file)
@@ -62,14 +62,14 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) {
 }
 
 // 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)
 }
 
-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)
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
        }
 
        keys := make([]*GPGKey, 0, 2)
index a4c7d702850fa867c74b5fc54eee9270f0c37a62..f508303a0965c2736873d528df111dd559f9d0f4 100644 (file)
@@ -9,6 +9,7 @@ import (
        "hash"
        "strings"
 
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
        "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
        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
                        log.Error("ListGPGKeys: %v", err)
                        return &CommitVerification{
index cafd996ac51d60f652245bc7e1c64728153cc917..b8c7053b2d2a3632a1156cbb28f0a65972753c98 100644 (file)
@@ -1122,7 +1122,7 @@ func GetIssuesByIDs(issueIDs []int64) ([]*Issue, error) {
 
 // IssuesOptions represents options of an issue.
 type IssuesOptions struct {
-       ListOptions
+       db.ListOptions
        RepoIDs            []int64 // include all repos if empty
        AssigneeID         int64
        PosterID           int64
index d8f8e36df288e8130fee4a42d92c12a96241fd78..01e41814a47445cb9ded072de8de1f69f7935952 100644 (file)
@@ -964,7 +964,7 @@ func getCommentByID(e db.Engine, id int64) (*Comment, error) {
 
 // FindCommentsOptions describes the conditions to Find comments
 type FindCommentsOptions struct {
-       ListOptions
+       db.ListOptions
        RepoID   int64
        IssueID  int64
        ReviewID int64
@@ -1012,7 +1012,7 @@ func findComments(e db.Engine, opts *FindCommentsOptions) ([]*Comment, error) {
        }
 
        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
index 87d7eb9221233578c90db62b6f995880ef95bc27..293b7140f777353994b73f5185d1c7afb3d029c6 100644 (file)
@@ -447,7 +447,7 @@ func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
                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 {
                return nil, ErrRepoLabelNotExist{0, repoID}
        }
@@ -466,14 +466,14 @@ func getLabelsByRepoID(e db.Engine, repoID int64, sortType string, listOptions L
        }
 
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
        }
 
        return labels, sess.Find(&labels)
 }
 
 // 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)
 }
 
@@ -564,7 +564,7 @@ func GetLabelsInOrgByIDs(orgID int64, labelIDs []int64) ([]*Label, error) {
                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 {
                return nil, ErrOrgLabelNotExist{0, orgID}
        }
@@ -583,14 +583,14 @@ func getLabelsByOrgID(e db.Engine, orgID int64, sortType string, listOptions Lis
        }
 
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
        }
 
        return labels, sess.Find(&labels)
 }
 
 // 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)
 }
 
index 384965b846c2050f78d07a43c7965ade145d2026..93807a326f8080dd67bc7c7562933bdda59c2518 100644 (file)
@@ -123,7 +123,7 @@ func TestGetLabelsInRepoByIDs(t *testing.T) {
 func TestGetLabelsByRepoID(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
        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.Len(t, labels, len(expectedIssueIDs))
                for i, label := range labels {
@@ -214,7 +214,7 @@ func TestGetLabelsInOrgByIDs(t *testing.T) {
 func TestGetLabelsByOrgID(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
        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.Len(t, labels, len(expectedIssueIDs))
                for i, label := range labels {
@@ -227,10 +227,10 @@ func TestGetLabelsByOrgID(t *testing.T) {
        testSuccess(3, "default", []int64{3, 4})
 
        var err error
-       _, err = GetLabelsByOrgID(0, "leastissues", ListOptions{})
+       _, err = GetLabelsByOrgID(0, "leastissues", db.ListOptions{})
        assert.True(t, IsErrOrgLabelNotExist(err))
 
-       _, err = GetLabelsByOrgID(-1, "leastissues", ListOptions{})
+       _, err = GetLabelsByOrgID(-1, "leastissues", db.ListOptions{})
        assert.True(t, IsErrOrgLabelNotExist(err))
 }
 
index fb6ced5b41a37d90b8a40405f5168a236112a7ef..3898e5b3978566d518f4d720e183fc2f2202ff82 100644 (file)
@@ -378,7 +378,7 @@ func (milestones MilestoneList) getMilestoneIDs() []int64 {
 
 // GetMilestonesOption contain options to get milestones
 type GetMilestonesOption struct {
-       ListOptions
+       db.ListOptions
        RepoID   int64
        State    api.StateType
        Name     string
@@ -413,7 +413,7 @@ func GetMilestones(opts GetMilestonesOption) (MilestoneList, int64, error) {
        sess := db.GetEngine(db.DefaultContext).Where(opts.toCond())
 
        if opts.Page != 0 {
-               sess = setSessionPagination(sess, &opts)
+               sess = db.SetSessionPagination(sess, &opts)
        }
 
        switch opts.SortType {
index 519b65715d15fc193a6fef2b141e87cc34ecd3b0..099fe47c7c1caf5ff4bf37b76b16a19cdabe1b5b 100644 (file)
@@ -102,7 +102,7 @@ func TestGetMilestones(t *testing.T) {
        test := func(sortType string, sortCond func(*Milestone) int) {
                for _, page := range []int{0, 1} {
                        milestones, _, err := GetMilestones(GetMilestonesOption{
-                               ListOptions: ListOptions{
+                               ListOptions: db.ListOptions{
                                        Page:     page,
                                        PageSize: setting.UI.IssuePagingNum,
                                },
@@ -119,7 +119,7 @@ func TestGetMilestones(t *testing.T) {
                        assert.True(t, sort.IntsAreSorted(values))
 
                        milestones, _, err = GetMilestones(GetMilestonesOption{
-                               ListOptions: ListOptions{
+                               ListOptions: db.ListOptions{
                                        Page:     page,
                                        PageSize: setting.UI.IssuePagingNum,
                                },
index 4e49add5c2bea37df66a0a365191cb67432d0bff..423eb8b96cf69c1911c41d08cb57a539f98c2d8c 100644 (file)
@@ -35,7 +35,7 @@ func init() {
 
 // FindReactionsOptions describes the conditions to Find reactions
 type FindReactionsOptions struct {
-       ListOptions
+       db.ListOptions
        IssueID   int64
        CommentID int64
        UserID    int64
@@ -78,7 +78,7 @@ func FindCommentReactions(comment *Comment) (ReactionList, error) {
 }
 
 // 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{
                ListOptions: listOptions,
                IssueID:     issue.ID,
@@ -92,7 +92,7 @@ func findReactions(e db.Engine, opts FindReactionsOptions) ([]*Reaction, error)
                In("reaction.`type`", setting.UI.Reactions).
                Asc("reaction.issue_id", "reaction.comment_id", "reaction.created_unix", "reaction.id")
        if opts.Page != 0 {
-               e = setEnginePagination(e, &opts)
+               e = db.SetEnginePagination(e, &opts)
 
                reactions := make([]*Reaction, 0, opts.PageSize)
                return reactions, e.Find(&reactions)
index 157658e182df0c0ee89a3929433d6a9b63d25b90..e8f19dd738c4e4a284e3ef2ddb4fb8bcb4da7f8a 100644 (file)
@@ -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
-func GetUserStopwatches(userID int64, listOptions ListOptions) ([]*Stopwatch, error) {
+func GetUserStopwatches(userID int64, listOptions db.ListOptions) ([]*Stopwatch, error) {
        sws := make([]*Stopwatch, 0, 8)
        sess := db.GetEngine(db.DefaultContext).Where("stopwatch.user_id = ?", userID)
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
        }
 
        err := sess.Find(&sws)
index d5f6f36e9c6efc580bf19b2fa3bfe0a3486715a1..d726a2434476640ca18d90f8bcd91a5014d959ba 100644 (file)
@@ -151,7 +151,7 @@ func TestIssues(t *testing.T) {
                        IssuesOptions{
                                RepoIDs:  []int64{1, 3},
                                SortType: "oldest",
-                               ListOptions: ListOptions{
+                               ListOptions: db.ListOptions{
                                        Page:     1,
                                        PageSize: 4,
                                },
@@ -161,7 +161,7 @@ func TestIssues(t *testing.T) {
                {
                        IssuesOptions{
                                LabelIDs: []int64{1},
-                               ListOptions: ListOptions{
+                               ListOptions: db.ListOptions{
                                        Page:     1,
                                        PageSize: 4,
                                },
@@ -171,7 +171,7 @@ func TestIssues(t *testing.T) {
                {
                        IssuesOptions{
                                LabelIDs: []int64{1, 2},
-                               ListOptions: ListOptions{
+                               ListOptions: db.ListOptions{
                                        Page:     1,
                                        PageSize: 4,
                                },
index d024c6896f62050d72743d66f95f3ad94cd8a6df..79de8910194a5dbb0cc6390c6ba991ac52ca376c 100644 (file)
@@ -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.
 type FindTrackedTimesOptions struct {
-       ListOptions
+       db.ListOptions
        IssueID           int64
        UserID            int64
        RepositoryID      int64
@@ -118,7 +118,7 @@ func (opts *FindTrackedTimesOptions) toSession(e db.Engine) db.Engine {
        sess = sess.Where(opts.toCond())
 
        if opts.Page != 0 {
-               sess = setEnginePagination(sess, opts)
+               sess = db.SetEnginePagination(sess, opts)
        }
 
        return sess
index cc1edcba1b6658dae163e533a42a6f7a8f2ee02e..5bac406ad01bddf8bed23ffd764841f9f0db3c87 100644 (file)
@@ -103,11 +103,11 @@ func getIssueWatchersIDs(e db.Engine, issueID int64, watching bool) ([]int64, er
 }
 
 // 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)
 }
 
-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.
                Where("`issue_watch`.issue_id = ?", issueID).
                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")
 
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
                watches := make([]*IssueWatch, 0, listOptions.PageSize)
                return watches, sess.Find(&watches)
        }
index f85e7cef59a6d086a036dc1e9ddadde19da0f11b..139ed41cb608aa50eecea32818c97f5a57691ee1 100644 (file)
@@ -43,22 +43,22 @@ func TestGetIssueWatch(t *testing.T) {
 func TestGetIssueWatchers(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
 
-       iws, err := GetIssueWatchers(1, ListOptions{})
+       iws, err := GetIssueWatchers(1, db.ListOptions{})
        assert.NoError(t, err)
        // Watcher is inactive, thus 0
        assert.Len(t, iws, 0)
 
-       iws, err = GetIssueWatchers(2, ListOptions{})
+       iws, err = GetIssueWatchers(2, db.ListOptions{})
        assert.NoError(t, err)
        // Watcher is explicit not watching
        assert.Len(t, iws, 0)
 
-       iws, err = GetIssueWatchers(5, ListOptions{})
+       iws, err = GetIssueWatchers(5, db.ListOptions{})
        assert.NoError(t, err)
        // Issue has no Watchers
        assert.Len(t, iws, 0)
 
-       iws, err = GetIssueWatchers(7, ListOptions{})
+       iws, err = GetIssueWatchers(7, db.ListOptions{})
        assert.NoError(t, err)
        // Issue has one watcher
        assert.Len(t, iws, 1)
diff --git a/models/list_options.go b/models/list_options.go
deleted file mode 100644 (file)
index 25b9a05..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-// 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 models
-
-import (
-       "code.gitea.io/gitea/models/db"
-       "code.gitea.io/gitea/modules/setting"
-
-       "xorm.io/xorm"
-)
-
-// Paginator is the base for different ListOptions types
-type Paginator interface {
-       GetSkipTake() (skip, take int)
-       GetStartEnd() (start, end int)
-}
-
-// getPaginatedSession creates a paginated database session
-func getPaginatedSession(p Paginator) *xorm.Session {
-       skip, take := p.GetSkipTake()
-
-       return db.GetEngine(db.DefaultContext).Limit(take, skip)
-}
-
-// setSessionPagination sets pagination for a database session
-func setSessionPagination(sess *xorm.Session, p Paginator) *xorm.Session {
-       skip, take := p.GetSkipTake()
-
-       return sess.Limit(take, skip)
-}
-
-// setSessionPagination sets pagination for a database engine
-func setEnginePagination(e db.Engine, p Paginator) db.Engine {
-       skip, take := p.GetSkipTake()
-
-       return e.Limit(take, skip)
-}
-
-// ListOptions options to paginate results
-type ListOptions struct {
-       PageSize int
-       Page     int // start from 1
-}
-
-// GetSkipTake returns the skip and take values
-func (opts *ListOptions) GetSkipTake() (skip, take int) {
-       opts.setDefaultValues()
-       return (opts.Page - 1) * opts.PageSize, opts.PageSize
-}
-
-// GetStartEnd returns the start and end of the ListOptions
-func (opts *ListOptions) GetStartEnd() (start, end int) {
-       start, take := opts.GetSkipTake()
-       end = start + take
-       return
-}
-
-func (opts *ListOptions) setDefaultValues() {
-       if opts.PageSize <= 0 {
-               opts.PageSize = setting.API.DefaultPagingNum
-       }
-       if opts.PageSize > setting.API.MaxResponseItems {
-               opts.PageSize = setting.API.MaxResponseItems
-       }
-       if opts.Page <= 0 {
-               opts.Page = 1
-       }
-}
-
-// AbsoluteListOptions absolute options to paginate results
-type AbsoluteListOptions struct {
-       skip int
-       take int
-}
-
-// NewAbsoluteListOptions creates a list option with applied limits
-func NewAbsoluteListOptions(skip, take int) *AbsoluteListOptions {
-       if skip < 0 {
-               skip = 0
-       }
-       if take <= 0 {
-               take = setting.API.DefaultPagingNum
-       }
-       if take > setting.API.MaxResponseItems {
-               take = setting.API.MaxResponseItems
-       }
-       return &AbsoluteListOptions{skip, take}
-}
-
-// GetSkipTake returns the skip and take values
-func (opts *AbsoluteListOptions) GetSkipTake() (skip, take int) {
-       return opts.skip, opts.take
-}
-
-// GetStartEnd returns the start and end values
-func (opts *AbsoluteListOptions) GetStartEnd() (start, end int) {
-       return opts.skip, opts.skip + opts.take
-}
diff --git a/models/list_options_test.go b/models/list_options_test.go
deleted file mode 100644 (file)
index 3145aa7..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-// Copyright 2021 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 (
-       "testing"
-
-       "code.gitea.io/gitea/modules/setting"
-
-       "github.com/stretchr/testify/assert"
-)
-
-func TestPaginator(t *testing.T) {
-       cases := []struct {
-               Paginator
-               Skip  int
-               Take  int
-               Start int
-               End   int
-       }{
-               {
-                       Paginator: &ListOptions{Page: -1, PageSize: -1},
-                       Skip:      0,
-                       Take:      setting.API.DefaultPagingNum,
-                       Start:     0,
-                       End:       setting.API.DefaultPagingNum,
-               },
-               {
-                       Paginator: &ListOptions{Page: 2, PageSize: 10},
-                       Skip:      10,
-                       Take:      10,
-                       Start:     10,
-                       End:       20,
-               },
-               {
-                       Paginator: NewAbsoluteListOptions(-1, -1),
-                       Skip:      0,
-                       Take:      setting.API.DefaultPagingNum,
-                       Start:     0,
-                       End:       setting.API.DefaultPagingNum,
-               },
-               {
-                       Paginator: NewAbsoluteListOptions(2, 10),
-                       Skip:      2,
-                       Take:      10,
-                       Start:     2,
-                       End:       12,
-               },
-       }
-
-       for _, c := range cases {
-               skip, take := c.Paginator.GetSkipTake()
-               start, end := c.Paginator.GetStartEnd()
-
-               assert.Equal(t, c.Skip, skip)
-               assert.Equal(t, c.Take, take)
-               assert.Equal(t, c.Start, start)
-               assert.Equal(t, c.End, end)
-       }
-}
diff --git a/models/login/main_test.go b/models/login/main_test.go
new file mode 100644 (file)
index 0000000..ef4b590
--- /dev/null
@@ -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",
+       )
+}
diff --git a/models/login/oauth2.go b/models/login/oauth2.go
new file mode 100644 (file)
index 0000000..45ab59d
--- /dev/null
@@ -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
+}
diff --git a/models/login/oauth2_application.go b/models/login/oauth2_application.go
new file mode 100644 (file)
index 0000000..060bfe5
--- /dev/null
@@ -0,0 +1,511 @@
+// Copyright 2019 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 (
+       "crypto/sha256"
+       "encoding/base64"
+       "fmt"
+       "net/url"
+       "strings"
+
+       "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/modules/secret"
+       "code.gitea.io/gitea/modules/timeutil"
+       "code.gitea.io/gitea/modules/util"
+
+       uuid "github.com/google/uuid"
+       "golang.org/x/crypto/bcrypt"
+       "xorm.io/xorm"
+)
+
+// OAuth2Application represents an OAuth2 client (RFC 6749)
+type OAuth2Application struct {
+       ID           int64 `xorm:"pk autoincr"`
+       UID          int64 `xorm:"INDEX"`
+       Name         string
+       ClientID     string `xorm:"unique"`
+       ClientSecret string
+       RedirectURIs []string           `xorm:"redirect_uris JSON TEXT"`
+       CreatedUnix  timeutil.TimeStamp `xorm:"INDEX created"`
+       UpdatedUnix  timeutil.TimeStamp `xorm:"INDEX updated"`
+}
+
+func init() {
+       db.RegisterModel(new(OAuth2Application))
+       db.RegisterModel(new(OAuth2AuthorizationCode))
+       db.RegisterModel(new(OAuth2Grant))
+}
+
+// TableName sets the table name to `oauth2_application`
+func (app *OAuth2Application) TableName() string {
+       return "oauth2_application"
+}
+
+// PrimaryRedirectURI returns the first redirect uri or an empty string if empty
+func (app *OAuth2Application) PrimaryRedirectURI() string {
+       if len(app.RedirectURIs) == 0 {
+               return ""
+       }
+       return app.RedirectURIs[0]
+}
+
+// ContainsRedirectURI checks if redirectURI is allowed for app
+func (app *OAuth2Application) ContainsRedirectURI(redirectURI string) bool {
+       return util.IsStringInSlice(redirectURI, app.RedirectURIs, true)
+}
+
+// GenerateClientSecret will generate the client secret and returns the plaintext and saves the hash at the database
+func (app *OAuth2Application) GenerateClientSecret() (string, error) {
+       clientSecret, err := secret.New()
+       if err != nil {
+               return "", err
+       }
+       hashedSecret, err := bcrypt.GenerateFromPassword([]byte(clientSecret), bcrypt.DefaultCost)
+       if err != nil {
+               return "", err
+       }
+       app.ClientSecret = string(hashedSecret)
+       if _, err := db.GetEngine(db.DefaultContext).ID(app.ID).Cols("client_secret").Update(app); err != nil {
+               return "", err
+       }
+       return clientSecret, nil
+}
+
+// ValidateClientSecret validates the given secret by the hash saved in database
+func (app *OAuth2Application) ValidateClientSecret(secret []byte) bool {
+       return bcrypt.CompareHashAndPassword([]byte(app.ClientSecret), secret) == nil
+}
+
+// GetGrantByUserID returns a OAuth2Grant by its user and application ID
+func (app *OAuth2Application) GetGrantByUserID(userID int64) (*OAuth2Grant, error) {
+       return app.getGrantByUserID(db.GetEngine(db.DefaultContext), userID)
+}
+
+func (app *OAuth2Application) getGrantByUserID(e db.Engine, userID int64) (grant *OAuth2Grant, err error) {
+       grant = new(OAuth2Grant)
+       if has, err := e.Where("user_id = ? AND application_id = ?", userID, app.ID).Get(grant); err != nil {
+               return nil, err
+       } else if !has {
+               return nil, nil
+       }
+       return grant, nil
+}
+
+// CreateGrant generates a grant for an user
+func (app *OAuth2Application) CreateGrant(userID int64, scope string) (*OAuth2Grant, error) {
+       return app.createGrant(db.GetEngine(db.DefaultContext), userID, scope)
+}
+
+func (app *OAuth2Application) createGrant(e db.Engine, userID int64, scope string) (*OAuth2Grant, error) {
+       grant := &OAuth2Grant{
+               ApplicationID: app.ID,
+               UserID:        userID,
+               Scope:         scope,
+       }
+       _, err := e.Insert(grant)
+       if err != nil {
+               return nil, err
+       }
+       return grant, nil
+}
+
+// GetOAuth2ApplicationByClientID returns the oauth2 application with the given client_id. Returns an error if not found.
+func GetOAuth2ApplicationByClientID(clientID string) (app *OAuth2Application, err error) {
+       return getOAuth2ApplicationByClientID(db.GetEngine(db.DefaultContext), clientID)
+}
+
+func getOAuth2ApplicationByClientID(e db.Engine, clientID string) (app *OAuth2Application, err error) {
+       app = new(OAuth2Application)
+       has, err := e.Where("client_id = ?", clientID).Get(app)
+       if !has {
+               return nil, ErrOAuthClientIDInvalid{ClientID: clientID}
+       }
+       return
+}
+
+// GetOAuth2ApplicationByID returns the oauth2 application with the given id. Returns an error if not found.
+func GetOAuth2ApplicationByID(id int64) (app *OAuth2Application, err error) {
+       return getOAuth2ApplicationByID(db.GetEngine(db.DefaultContext), id)
+}
+
+func getOAuth2ApplicationByID(e db.Engine, id int64) (app *OAuth2Application, err error) {
+       app = new(OAuth2Application)
+       has, err := e.ID(id).Get(app)
+       if err != nil {
+               return nil, err
+       }
+       if !has {
+               return nil, ErrOAuthApplicationNotFound{ID: id}
+       }
+       return app, nil
+}
+
+// GetOAuth2ApplicationsByUserID returns all oauth2 applications owned by the user
+func GetOAuth2ApplicationsByUserID(userID int64) (apps []*OAuth2Application, err error) {
+       return getOAuth2ApplicationsByUserID(db.GetEngine(db.DefaultContext), userID)
+}
+
+func getOAuth2ApplicationsByUserID(e db.Engine, userID int64) (apps []*OAuth2Application, err error) {
+       apps = make([]*OAuth2Application, 0)
+       err = e.Where("uid = ?", userID).Find(&apps)
+       return
+}
+
+// CreateOAuth2ApplicationOptions holds options to create an oauth2 application
+type CreateOAuth2ApplicationOptions struct {
+       Name         string
+       UserID       int64
+       RedirectURIs []string
+}
+
+// CreateOAuth2Application inserts a new oauth2 application
+func CreateOAuth2Application(opts CreateOAuth2ApplicationOptions) (*OAuth2Application, error) {
+       return createOAuth2Application(db.GetEngine(db.DefaultContext), opts)
+}
+
+func createOAuth2Application(e db.Engine, opts CreateOAuth2ApplicationOptions) (*OAuth2Application, error) {
+       clientID := uuid.New().String()
+       app := &OAuth2Application{
+               UID:          opts.UserID,
+               Name:         opts.Name,
+               ClientID:     clientID,
+               RedirectURIs: opts.RedirectURIs,
+       }
+       if _, err := e.Insert(app); err != nil {
+               return nil, err
+       }
+       return app, nil
+}
+
+// UpdateOAuth2ApplicationOptions holds options to update an oauth2 application
+type UpdateOAuth2ApplicationOptions struct {
+       ID           int64
+       Name         string
+       UserID       int64
+       RedirectURIs []string
+}
+
+// UpdateOAuth2Application updates an oauth2 application
+func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) {
+       sess := db.NewSession(db.DefaultContext)
+       if err := sess.Begin(); err != nil {
+               return nil, err
+       }
+       defer sess.Close()
+
+       app, err := getOAuth2ApplicationByID(sess, opts.ID)
+       if err != nil {
+               return nil, err
+       }
+       if app.UID != opts.UserID {
+               return nil, fmt.Errorf("UID mismatch")
+       }
+
+       app.Name = opts.Name
+       app.RedirectURIs = opts.RedirectURIs
+
+       if err = updateOAuth2Application(sess, app); err != nil {
+               return nil, err
+       }
+       app.ClientSecret = ""
+
+       return app, sess.Commit()
+}
+
+func updateOAuth2Application(e db.Engine, app *OAuth2Application) error {
+       if _, err := e.ID(app.ID).Update(app); err != nil {
+               return err
+       }
+       return nil
+}
+
+func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
+       if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil {
+               return err
+       } else if deleted == 0 {
+               return ErrOAuthApplicationNotFound{ID: id}
+       }
+       codes := make([]*OAuth2AuthorizationCode, 0)
+       // delete correlating auth codes
+       if err := sess.Join("INNER", "oauth2_grant",
+               "oauth2_authorization_code.grant_id = oauth2_grant.id AND oauth2_grant.application_id = ?", id).Find(&codes); err != nil {
+               return err
+       }
+       codeIDs := make([]int64, 0)
+       for _, grant := range codes {
+               codeIDs = append(codeIDs, grant.ID)
+       }
+
+       if _, err := sess.In("id", codeIDs).Delete(new(OAuth2AuthorizationCode)); err != nil {
+               return err
+       }
+
+       if _, err := sess.Where("application_id = ?", id).Delete(new(OAuth2Grant)); err != nil {
+               return err
+       }
+       return nil
+}
+
+// DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app.
+func DeleteOAuth2Application(id, userid int64) error {
+       sess := db.NewSession(db.DefaultContext)
+       defer sess.Close()
+       if err := sess.Begin(); err != nil {
+               return err
+       }
+       if err := deleteOAuth2Application(sess, id, userid); err != nil {
+               return err
+       }
+       return sess.Commit()
+}
+
+// ListOAuth2Applications returns a list of oauth2 applications belongs to given user.
+func ListOAuth2Applications(uid int64, listOptions db.ListOptions) ([]*OAuth2Application, int64, error) {
+       sess := db.GetEngine(db.DefaultContext).
+               Where("uid=?", uid).
+               Desc("id")
+
+       if listOptions.Page != 0 {
+               sess = db.SetSessionPagination(sess, &listOptions)
+
+               apps := make([]*OAuth2Application, 0, listOptions.PageSize)
+               total, err := sess.FindAndCount(&apps)
+               return apps, total, err
+       }
+
+       apps := make([]*OAuth2Application, 0, 5)
+       total, err := sess.FindAndCount(&apps)
+       return apps, total, err
+}
+
+//////////////////////////////////////////////////////
+
+// OAuth2AuthorizationCode is a code to obtain an access token in combination with the client secret once. It has a limited lifetime.
+type OAuth2AuthorizationCode struct {
+       ID                  int64        `xorm:"pk autoincr"`
+       Grant               *OAuth2Grant `xorm:"-"`
+       GrantID             int64
+       Code                string `xorm:"INDEX unique"`
+       CodeChallenge       string
+       CodeChallengeMethod string
+       RedirectURI         string
+       ValidUntil          timeutil.TimeStamp `xorm:"index"`
+}
+
+// TableName sets the table name to `oauth2_authorization_code`
+func (code *OAuth2AuthorizationCode) TableName() string {
+       return "oauth2_authorization_code"
+}
+
+// GenerateRedirectURI generates a redirect URI for a successful authorization request. State will be used if not empty.
+func (code *OAuth2AuthorizationCode) GenerateRedirectURI(state string) (redirect *url.URL, err error) {
+       if redirect, err = url.Parse(code.RedirectURI); err != nil {
+               return
+       }
+       q := redirect.Query()
+       if state != "" {
+               q.Set("state", state)
+       }
+       q.Set("code", code.Code)
+       redirect.RawQuery = q.Encode()
+       return
+}
+
+// Invalidate deletes the auth code from the database to invalidate this code
+func (code *OAuth2AuthorizationCode) Invalidate() error {
+       return code.invalidate(db.GetEngine(db.DefaultContext))
+}
+
+func (code *OAuth2AuthorizationCode) invalidate(e db.Engine) error {
+       _, err := e.Delete(code)
+       return err
+}
+
+// ValidateCodeChallenge validates the given verifier against the saved code challenge. This is part of the PKCE implementation.
+func (code *OAuth2AuthorizationCode) ValidateCodeChallenge(verifier string) bool {
+       return code.validateCodeChallenge(verifier)
+}
+
+func (code *OAuth2AuthorizationCode) validateCodeChallenge(verifier string) bool {
+       switch code.CodeChallengeMethod {
+       case "S256":
+               // base64url(SHA256(verifier)) see https://tools.ietf.org/html/rfc7636#section-4.6
+               h := sha256.Sum256([]byte(verifier))
+               hashedVerifier := base64.RawURLEncoding.EncodeToString(h[:])
+               return hashedVerifier == code.CodeChallenge
+       case "plain":
+               return verifier == code.CodeChallenge
+       case "":
+               return true
+       default:
+               // unsupported method -> return false
+               return false
+       }
+}
+
+// GetOAuth2AuthorizationByCode returns an authorization by its code
+func GetOAuth2AuthorizationByCode(code string) (*OAuth2AuthorizationCode, error) {
+       return getOAuth2AuthorizationByCode(db.GetEngine(db.DefaultContext), code)
+}
+
+func getOAuth2AuthorizationByCode(e db.Engine, code string) (auth *OAuth2AuthorizationCode, err error) {
+       auth = new(OAuth2AuthorizationCode)
+       if has, err := e.Where("code = ?", code).Get(auth); err != nil {
+               return nil, err
+       } else if !has {
+               return nil, nil
+       }
+       auth.Grant = new(OAuth2Grant)
+       if has, err := e.ID(auth.GrantID).Get(auth.Grant); err != nil {
+               return nil, err
+       } else if !has {
+               return nil, nil
+       }
+       return auth, nil
+}
+
+//////////////////////////////////////////////////////
+
+// OAuth2Grant represents the permission of an user for a specific application to access resources
+type OAuth2Grant struct {
+       ID            int64              `xorm:"pk autoincr"`
+       UserID        int64              `xorm:"INDEX unique(user_application)"`
+       Application   *OAuth2Application `xorm:"-"`
+       ApplicationID int64              `xorm:"INDEX unique(user_application)"`
+       Counter       int64              `xorm:"NOT NULL DEFAULT 1"`
+       Scope         string             `xorm:"TEXT"`
+       Nonce         string             `xorm:"TEXT"`
+       CreatedUnix   timeutil.TimeStamp `xorm:"created"`
+       UpdatedUnix   timeutil.TimeStamp `xorm:"updated"`
+}
+
+// TableName sets the table name to `oauth2_grant`
+func (grant *OAuth2Grant) TableName() string {
+       return "oauth2_grant"
+}
+
+// GenerateNewAuthorizationCode generates a new authorization code for a grant and saves it to the database
+func (grant *OAuth2Grant) GenerateNewAuthorizationCode(redirectURI, codeChallenge, codeChallengeMethod string) (*OAuth2AuthorizationCode, error) {
+       return grant.generateNewAuthorizationCode(db.GetEngine(db.DefaultContext), redirectURI, codeChallenge, codeChallengeMethod)
+}
+
+func (grant *OAuth2Grant) generateNewAuthorizationCode(e db.Engine, redirectURI, codeChallenge, codeChallengeMethod string) (code *OAuth2AuthorizationCode, err error) {
+       var codeSecret string
+       if codeSecret, err = secret.New(); err != nil {
+               return &OAuth2AuthorizationCode{}, err
+       }
+       code = &OAuth2AuthorizationCode{
+               Grant:               grant,
+               GrantID:             grant.ID,
+               RedirectURI:         redirectURI,
+               Code:                codeSecret,
+               CodeChallenge:       codeChallenge,
+               CodeChallengeMethod: codeChallengeMethod,
+       }
+       if _, err := e.Insert(code); err != nil {
+               return nil, err
+       }
+       return code, nil
+}
+
+// IncreaseCounter increases the counter and updates the grant
+func (grant *OAuth2Grant) IncreaseCounter() error {
+       return grant.increaseCount(db.GetEngine(db.DefaultContext))
+}
+
+func (grant *OAuth2Grant) increaseCount(e db.Engine) error {
+       _, err := e.ID(grant.ID).Incr("counter").Update(new(OAuth2Grant))
+       if err != nil {
+               return err
+       }
+       updatedGrant, err := getOAuth2GrantByID(e, grant.ID)
+       if err != nil {
+               return err
+       }
+       grant.Counter = updatedGrant.Counter
+       return nil
+}
+
+// ScopeContains returns true if the grant scope contains the specified scope
+func (grant *OAuth2Grant) ScopeContains(scope string) bool {
+       for _, currentScope := range strings.Split(grant.Scope, " ") {
+               if scope == currentScope {
+                       return true
+               }
+       }
+       return false
+}
+
+// SetNonce updates the current nonce value of a grant
+func (grant *OAuth2Grant) SetNonce(nonce string) error {
+       return grant.setNonce(db.GetEngine(db.DefaultContext), nonce)
+}
+
+func (grant *OAuth2Grant) setNonce(e db.Engine, nonce string) error {
+       grant.Nonce = nonce
+       _, err := e.ID(grant.ID).Cols("nonce").Update(grant)
+       if err != nil {
+               return err
+       }
+       return nil
+}
+
+// GetOAuth2GrantByID returns the grant with the given ID
+func GetOAuth2GrantByID(id int64) (*OAuth2Grant, error) {
+       return getOAuth2GrantByID(db.GetEngine(db.DefaultContext), id)
+}
+
+func getOAuth2GrantByID(e db.Engine, id int64) (grant *OAuth2Grant, err error) {
+       grant = new(OAuth2Grant)
+       if has, err := e.ID(id).Get(grant); err != nil {
+               return nil, err
+       } else if !has {
+               return nil, nil
+       }
+       return
+}
+
+// GetOAuth2GrantsByUserID lists all grants of a certain user
+func GetOAuth2GrantsByUserID(uid int64) ([]*OAuth2Grant, error) {
+       return getOAuth2GrantsByUserID(db.GetEngine(db.DefaultContext), uid)
+}
+
+func getOAuth2GrantsByUserID(e db.Engine, uid int64) ([]*OAuth2Grant, error) {
+       type joinedOAuth2Grant struct {
+               Grant       *OAuth2Grant       `xorm:"extends"`
+               Application *OAuth2Application `xorm:"extends"`
+       }
+       var results *xorm.Rows
+       var err error
+       if results, err = e.
+               Table("oauth2_grant").
+               Where("user_id = ?", uid).
+               Join("INNER", "oauth2_application", "application_id = oauth2_application.id").
+               Rows(new(joinedOAuth2Grant)); err != nil {
+               return nil, err
+       }
+       defer results.Close()
+       grants := make([]*OAuth2Grant, 0)
+       for results.Next() {
+               joinedGrant := new(joinedOAuth2Grant)
+               if err := results.Scan(joinedGrant); err != nil {
+                       return nil, err
+               }
+               joinedGrant.Grant.Application = joinedGrant.Application
+               grants = append(grants, joinedGrant.Grant)
+       }
+       return grants, nil
+}
+
+// RevokeOAuth2Grant deletes the grant with grantID and userID
+func RevokeOAuth2Grant(grantID, userID int64) error {
+       return revokeOAuth2Grant(db.GetEngine(db.DefaultContext), grantID, userID)
+}
+
+func revokeOAuth2Grant(e db.Engine, grantID, userID int64) error {
+       _, err := e.Delete(&OAuth2Grant{ID: grantID, UserID: userID})
+       return err
+}
diff --git a/models/login/oauth2_application_test.go b/models/login/oauth2_application_test.go
new file mode 100644 (file)
index 0000000..cb064ce
--- /dev/null
@@ -0,0 +1,233 @@
+// Copyright 2019 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 (
+       "testing"
+
+       "code.gitea.io/gitea/models/db"
+
+       "github.com/stretchr/testify/assert"
+)
+
+//////////////////// Application
+
+func TestOAuth2Application_GenerateClientSecret(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
+       secret, err := app.GenerateClientSecret()
+       assert.NoError(t, err)
+       assert.True(t, len(secret) > 0)
+       db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1, ClientSecret: app.ClientSecret})
+}
+
+func BenchmarkOAuth2Application_GenerateClientSecret(b *testing.B) {
+       assert.NoError(b, db.PrepareTestDatabase())
+       app := db.AssertExistsAndLoadBean(b, &OAuth2Application{ID: 1}).(*OAuth2Application)
+       for i := 0; i < b.N; i++ {
+               _, _ = app.GenerateClientSecret()
+       }
+}
+
+func TestOAuth2Application_ContainsRedirectURI(t *testing.T) {
+       app := &OAuth2Application{
+               RedirectURIs: []string{"a", "b", "c"},
+       }
+       assert.True(t, app.ContainsRedirectURI("a"))
+       assert.True(t, app.ContainsRedirectURI("b"))
+       assert.True(t, app.ContainsRedirectURI("c"))
+       assert.False(t, app.ContainsRedirectURI("d"))
+}
+
+func TestOAuth2Application_ValidateClientSecret(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
+       secret, err := app.GenerateClientSecret()
+       assert.NoError(t, err)
+       assert.True(t, app.ValidateClientSecret([]byte(secret)))
+       assert.False(t, app.ValidateClientSecret([]byte("fewijfowejgfiowjeoifew")))
+}
+
+func TestGetOAuth2ApplicationByClientID(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       app, err := GetOAuth2ApplicationByClientID("da7da3ba-9a13-4167-856f-3899de0b0138")
+       assert.NoError(t, err)
+       assert.Equal(t, "da7da3ba-9a13-4167-856f-3899de0b0138", app.ClientID)
+
+       app, err = GetOAuth2ApplicationByClientID("invalid client id")
+       assert.Error(t, err)
+       assert.Nil(t, app)
+}
+
+func TestCreateOAuth2Application(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       app, err := CreateOAuth2Application(CreateOAuth2ApplicationOptions{Name: "newapp", UserID: 1})
+       assert.NoError(t, err)
+       assert.Equal(t, "newapp", app.Name)
+       assert.Len(t, app.ClientID, 36)
+       db.AssertExistsAndLoadBean(t, &OAuth2Application{Name: "newapp"})
+}
+
+func TestOAuth2Application_TableName(t *testing.T) {
+       assert.Equal(t, "oauth2_application", new(OAuth2Application).TableName())
+}
+
+func TestOAuth2Application_GetGrantByUserID(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
+       grant, err := app.GetGrantByUserID(1)
+       assert.NoError(t, err)
+       assert.Equal(t, int64(1), grant.UserID)
+
+       grant, err = app.GetGrantByUserID(34923458)
+       assert.NoError(t, err)
+       assert.Nil(t, grant)
+}
+
+func TestOAuth2Application_CreateGrant(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
+       grant, err := app.CreateGrant(2, "")
+       assert.NoError(t, err)
+       assert.NotNil(t, grant)
+       assert.Equal(t, int64(2), grant.UserID)
+       assert.Equal(t, int64(1), grant.ApplicationID)
+       assert.Equal(t, "", grant.Scope)
+}
+
+//////////////////// Grant
+
+func TestGetOAuth2GrantByID(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       grant, err := GetOAuth2GrantByID(1)
+       assert.NoError(t, err)
+       assert.Equal(t, int64(1), grant.ID)
+
+       grant, err = GetOAuth2GrantByID(34923458)
+       assert.NoError(t, err)
+       assert.Nil(t, grant)
+}
+
+func TestOAuth2Grant_IncreaseCounter(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 1}).(*OAuth2Grant)
+       assert.NoError(t, grant.IncreaseCounter())
+       assert.Equal(t, int64(2), grant.Counter)
+       db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 2})
+}
+
+func TestOAuth2Grant_ScopeContains(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Scope: "openid profile"}).(*OAuth2Grant)
+       assert.True(t, grant.ScopeContains("openid"))
+       assert.True(t, grant.ScopeContains("profile"))
+       assert.False(t, grant.ScopeContains("profil"))
+       assert.False(t, grant.ScopeContains("profile2"))
+}
+
+func TestOAuth2Grant_GenerateNewAuthorizationCode(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1}).(*OAuth2Grant)
+       code, err := grant.GenerateNewAuthorizationCode("https://example2.com/callback", "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg", "S256")
+       assert.NoError(t, err)
+       assert.NotNil(t, code)
+       assert.True(t, len(code.Code) > 32) // secret length > 32
+}
+
+func TestOAuth2Grant_TableName(t *testing.T) {
+       assert.Equal(t, "oauth2_grant", new(OAuth2Grant).TableName())
+}
+
+func TestGetOAuth2GrantsByUserID(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       result, err := GetOAuth2GrantsByUserID(1)
+       assert.NoError(t, err)
+       assert.Len(t, result, 1)
+       assert.Equal(t, int64(1), result[0].ID)
+       assert.Equal(t, result[0].ApplicationID, result[0].Application.ID)
+
+       result, err = GetOAuth2GrantsByUserID(34134)
+       assert.NoError(t, err)
+       assert.Empty(t, result)
+}
+
+func TestRevokeOAuth2Grant(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       assert.NoError(t, RevokeOAuth2Grant(1, 1))
+       db.AssertNotExistsBean(t, &OAuth2Grant{ID: 1, UserID: 1})
+}
+
+//////////////////// Authorization Code
+
+func TestGetOAuth2AuthorizationByCode(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       code, err := GetOAuth2AuthorizationByCode("authcode")
+       assert.NoError(t, err)
+       assert.NotNil(t, code)
+       assert.Equal(t, "authcode", code.Code)
+       assert.Equal(t, int64(1), code.ID)
+
+       code, err = GetOAuth2AuthorizationByCode("does not exist")
+       assert.NoError(t, err)
+       assert.Nil(t, code)
+}
+
+func TestOAuth2AuthorizationCode_ValidateCodeChallenge(t *testing.T) {
+       // test plain
+       code := &OAuth2AuthorizationCode{
+               CodeChallengeMethod: "plain",
+               CodeChallenge:       "test123",
+       }
+       assert.True(t, code.ValidateCodeChallenge("test123"))
+       assert.False(t, code.ValidateCodeChallenge("ierwgjoergjio"))
+
+       // test S256
+       code = &OAuth2AuthorizationCode{
+               CodeChallengeMethod: "S256",
+               CodeChallenge:       "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg",
+       }
+       assert.True(t, code.ValidateCodeChallenge("N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt"))
+       assert.False(t, code.ValidateCodeChallenge("wiogjerogorewngoenrgoiuenorg"))
+
+       // test unknown
+       code = &OAuth2AuthorizationCode{
+               CodeChallengeMethod: "monkey",
+               CodeChallenge:       "foiwgjioriogeiogjerger",
+       }
+       assert.False(t, code.ValidateCodeChallenge("foiwgjioriogeiogjerger"))
+
+       // test no code challenge
+       code = &OAuth2AuthorizationCode{
+               CodeChallengeMethod: "",
+               CodeChallenge:       "foierjiogerogerg",
+       }
+       assert.True(t, code.ValidateCodeChallenge(""))
+}
+
+func TestOAuth2AuthorizationCode_GenerateRedirectURI(t *testing.T) {
+       code := &OAuth2AuthorizationCode{
+               RedirectURI: "https://example.com/callback",
+               Code:        "thecode",
+       }
+
+       redirect, err := code.GenerateRedirectURI("thestate")
+       assert.NoError(t, err)
+       assert.Equal(t, "https://example.com/callback?code=thecode&state=thestate", redirect.String())
+
+       redirect, err = code.GenerateRedirectURI("")
+       assert.NoError(t, err)
+       assert.Equal(t, "https://example.com/callback?code=thecode", redirect.String())
+}
+
+func TestOAuth2AuthorizationCode_Invalidate(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+       code := db.AssertExistsAndLoadBean(t, &OAuth2AuthorizationCode{Code: "authcode"}).(*OAuth2AuthorizationCode)
+       assert.NoError(t, code.Invalidate())
+       db.AssertNotExistsBean(t, &OAuth2AuthorizationCode{Code: "authcode"})
+}
+
+func TestOAuth2AuthorizationCode_TableName(t *testing.T) {
+       assert.Equal(t, "oauth2_authorization_code", new(OAuth2AuthorizationCode).TableName())
+}
diff --git a/models/login/source.go b/models/login/source.go
new file mode 100644 (file)
index 0000000..1001d49
--- /dev/null
@@ -0,0 +1,411 @@
+// Copyright 2014 The Gogs Authors. All rights reserved.
+// Copyright 2019 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"
+       "reflect"
+       "strconv"
+
+       "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/modules/log"
+       "code.gitea.io/gitea/modules/timeutil"
+
+       "xorm.io/xorm"
+       "xorm.io/xorm/convert"
+)
+
+// Type represents an login type.
+type Type int
+
+// Note: new type must append to the end of list to maintain compatibility.
+const (
+       NoType Type = iota
+       Plain       // 1
+       LDAP        // 2
+       SMTP        // 3
+       PAM         // 4
+       DLDAP       // 5
+       OAuth2      // 6
+       SSPI        // 7
+)
+
+// String returns the string name of the LoginType
+func (typ Type) String() string {
+       return Names[typ]
+}
+
+// Int returns the int value of the LoginType
+func (typ Type) Int() int {
+       return int(typ)
+}
+
+// Names contains the name of LoginType values.
+var Names = map[Type]string{
+       LDAP:   "LDAP (via BindDN)",
+       DLDAP:  "LDAP (simple auth)", // Via direct bind
+       SMTP:   "SMTP",
+       PAM:    "PAM",
+       OAuth2: "OAuth2",
+       SSPI:   "SPNEGO with SSPI",
+}
+
+// Config represents login config as far as the db is concerned
+type Config interface {
+       convert.Conversion
+}
+
+// SkipVerifiable configurations provide a IsSkipVerify to check if SkipVerify is set
+type SkipVerifiable interface {
+       IsSkipVerify() bool
+}
+
+// HasTLSer configurations provide a HasTLS to check if TLS can be enabled
+type HasTLSer interface {
+       HasTLS() bool
+}
+
+// UseTLSer configurations provide a HasTLS to check if TLS is enabled
+type UseTLSer interface {
+       UseTLS() bool
+}
+
+// SSHKeyProvider configurations provide ProvidesSSHKeys to check if they provide SSHKeys
+type SSHKeyProvider interface {
+       ProvidesSSHKeys() bool
+}
+
+// RegisterableSource configurations provide RegisterSource which needs to be run on creation
+type RegisterableSource interface {
+       RegisterSource() error
+       UnregisterSource() error
+}
+
+// SourceSettable configurations can have their loginSource set on them
+type SourceSettable interface {
+       SetLoginSource(*Source)
+}
+
+// RegisterTypeConfig register a config for a provided type
+func RegisterTypeConfig(typ Type, exemplar Config) {
+       if reflect.TypeOf(exemplar).Kind() == reflect.Ptr {
+               // Pointer:
+               registeredConfigs[typ] = func() Config {
+                       return reflect.New(reflect.ValueOf(exemplar).Elem().Type()).Interface().(Config)
+               }
+               return
+       }
+
+       // Not a Pointer
+       registeredConfigs[typ] = func() Config {
+               return reflect.New(reflect.TypeOf(exemplar)).Elem().Interface().(Config)
+       }
+}
+
+var registeredConfigs = map[Type]func() Config{}
+
+// Source represents an external way for authorizing users.
+type Source struct {
+       ID            int64 `xorm:"pk autoincr"`
+       Type          Type
+       Name          string             `xorm:"UNIQUE"`
+       IsActive      bool               `xorm:"INDEX NOT NULL DEFAULT false"`
+       IsSyncEnabled bool               `xorm:"INDEX NOT NULL DEFAULT false"`
+       Cfg           convert.Conversion `xorm:"TEXT"`
+
+       CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
+       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() {
+       db.RegisterModel(new(Source))
+}
+
+// Cell2Int64 converts a xorm.Cell type to int64,
+// and handles possible irregular cases.
+func Cell2Int64(val xorm.Cell) int64 {
+       switch (*val).(type) {
+       case []uint8:
+               log.Trace("Cell2Int64 ([]uint8): %v", *val)
+
+               v, _ := strconv.ParseInt(string((*val).([]uint8)), 10, 64)
+               return v
+       }
+       return (*val).(int64)
+}
+
+// BeforeSet is invoked from XORM before setting the value of a field of this object.
+func (source *Source) BeforeSet(colName string, val xorm.Cell) {
+       if colName == "type" {
+               typ := Type(Cell2Int64(val))
+               constructor, ok := registeredConfigs[typ]
+               if !ok {
+                       return
+               }
+               source.Cfg = constructor()
+               if settable, ok := source.Cfg.(SourceSettable); ok {
+                       settable.SetLoginSource(source)
+               }
+       }
+}
+
+// TypeName return name of this login source type.
+func (source *Source) TypeName() string {
+       return Names[source.Type]
+}
+
+// IsLDAP returns true of this source is of the LDAP type.
+func (source *Source) IsLDAP() bool {
+       return source.Type == LDAP
+}
+
+// IsDLDAP returns true of this source is of the DLDAP type.
+func (source *Source) IsDLDAP() bool {
+       return source.Type == DLDAP
+}
+
+// IsSMTP returns true of this source is of the SMTP type.
+func (source *Source) IsSMTP() bool {
+       return source.Type == SMTP
+}
+
+// IsPAM returns true of this source is of the PAM type.
+func (source *Source) IsPAM() bool {
+       return source.Type == PAM
+}
+
+// IsOAuth2 returns true of this source is of the OAuth2 type.
+func (source *Source) IsOAuth2() bool {
+       return source.Type == OAuth2
+}
+
+// IsSSPI returns true of this source is of the SSPI type.
+func (source *Source) IsSSPI() bool {
+       return source.Type == SSPI
+}
+
+// HasTLS returns true of this source supports TLS.
+func (source *Source) HasTLS() bool {
+       hasTLSer, ok := source.Cfg.(HasTLSer)
+       return ok && hasTLSer.HasTLS()
+}
+
+// UseTLS returns true of this source is configured to use TLS.
+func (source *Source) UseTLS() bool {
+       useTLSer, ok := source.Cfg.(UseTLSer)
+       return ok && useTLSer.UseTLS()
+}
+
+// SkipVerify returns true if this source is configured to skip SSL
+// verification.
+func (source *Source) SkipVerify() bool {
+       skipVerifiable, ok := source.Cfg.(SkipVerifiable)
+       return ok && skipVerifiable.IsSkipVerify()
+}
+
+// CreateSource inserts a LoginSource in the DB if not already
+// existing with the given name.
+func CreateSource(source *Source) error {
+       has, err := db.GetEngine(db.DefaultContext).Where("name=?", source.Name).Exist(new(Source))
+       if err != nil {
+               return err
+       } else if has {
+               return ErrSourceAlreadyExist{source.Name}
+       }
+       // Synchronization is only available with LDAP for now
+       if !source.IsLDAP() {
+               source.IsSyncEnabled = false
+       }
+
+       _, err = db.GetEngine(db.DefaultContext).Insert(source)
+       if err != nil {
+               return err
+       }
+
+       if !source.IsActive {
+               return nil
+       }
+
+       if settable, ok := source.Cfg.(SourceSettable); ok {
+               settable.SetLoginSource(source)
+       }
+
+       registerableSource, ok := source.Cfg.(RegisterableSource)
+       if !ok {
+               return nil
+       }
+
+       err = registerableSource.RegisterSource()
+       if err != nil {
+               // remove the LoginSource in case of errors while registering configuration
+               if _, err := db.GetEngine(db.DefaultContext).Delete(source); err != nil {
+                       log.Error("CreateSource: Error while wrapOpenIDConnectInitializeError: %v", err)
+               }
+       }
+       return err
+}
+
+// Sources returns a slice of all login sources found in DB.
+func Sources() ([]*Source, error) {
+       auths := make([]*Source, 0, 6)
+       return auths, db.GetEngine(db.DefaultContext).Find(&auths)
+}
+
+// SourcesByType returns all sources of the specified type
+func SourcesByType(loginType Type) ([]*Source, error) {
+       sources := make([]*Source, 0, 1)
+       if err := db.GetEngine(db.DefaultContext).Where("type = ?", loginType).Find(&sources); err != nil {
+               return nil, err
+       }
+       return sources, nil
+}
+
+// AllActiveSources returns all active sources
+func AllActiveSources() ([]*Source, error) {
+       sources := make([]*Source, 0, 5)
+       if err := db.GetEngine(db.DefaultContext).Where("is_active = ?", true).Find(&sources); err != nil {
+               return nil, err
+       }
+       return sources, nil
+}
+
+// ActiveSources returns all active sources of the specified type
+func ActiveSources(tp Type) ([]*Source, error) {
+       sources := make([]*Source, 0, 1)
+       if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, tp).Find(&sources); err != nil {
+               return nil, err
+       }
+       return sources, nil
+}
+
+// IsSSPIEnabled returns true if there is at least one activated login
+// source of type LoginSSPI
+func IsSSPIEnabled() bool {
+       if !db.HasEngine {
+               return false
+       }
+       sources, err := ActiveSources(SSPI)
+       if err != nil {
+               log.Error("ActiveSources: %v", err)
+               return false
+       }
+       return len(sources) > 0
+}
+
+// GetSourceByID returns login source by given ID.
+func GetSourceByID(id int64) (*Source, error) {
+       source := new(Source)
+       if id == 0 {
+               source.Cfg = registeredConfigs[NoType]()
+               // Set this source to active
+               // FIXME: allow disabling of db based password authentication in future
+               source.IsActive = true
+               return source, nil
+       }
+
+       has, err := db.GetEngine(db.DefaultContext).ID(id).Get(source)
+       if err != nil {
+               return nil, err
+       } else if !has {
+               return nil, ErrSourceNotExist{id}
+       }
+       return source, nil
+}
+
+// UpdateSource updates a Source record in DB.
+func UpdateSource(source *Source) error {
+       var originalLoginSource *Source
+       if source.IsOAuth2() {
+               // keep track of the original values so we can restore in case of errors while registering OAuth2 providers
+               var err error
+               if originalLoginSource, err = GetSourceByID(source.ID); err != nil {
+                       return err
+               }
+       }
+
+       _, err := db.GetEngine(db.DefaultContext).ID(source.ID).AllCols().Update(source)
+       if err != nil {
+               return err
+       }
+
+       if !source.IsActive {
+               return nil
+       }
+
+       if settable, ok := source.Cfg.(SourceSettable); ok {
+               settable.SetLoginSource(source)
+       }
+
+       registerableSource, ok := source.Cfg.(RegisterableSource)
+       if !ok {
+               return nil
+       }
+
+       err = registerableSource.RegisterSource()
+       if err != nil {
+               // restore original values since we cannot update the provider it self
+               if _, err := db.GetEngine(db.DefaultContext).ID(source.ID).AllCols().Update(originalLoginSource); err != nil {
+                       log.Error("UpdateSource: Error while wrapOpenIDConnectInitializeError: %v", err)
+               }
+       }
+       return err
+}
+
+// CountSources returns number of login sources.
+func CountSources() int64 {
+       count, _ := db.GetEngine(db.DefaultContext).Count(new(Source))
+       return count
+}
+
+// ErrSourceNotExist represents a "SourceNotExist" kind of error.
+type ErrSourceNotExist struct {
+       ID int64
+}
+
+// IsErrSourceNotExist checks if an error is a ErrSourceNotExist.
+func IsErrSourceNotExist(err error) bool {
+       _, ok := err.(ErrSourceNotExist)
+       return ok
+}
+
+func (err ErrSourceNotExist) Error() string {
+       return fmt.Sprintf("login source does not exist [id: %d]", err.ID)
+}
+
+// ErrSourceAlreadyExist represents a "SourceAlreadyExist" kind of error.
+type ErrSourceAlreadyExist struct {
+       Name string
+}
+
+// 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)
+}
diff --git a/models/login/source_test.go b/models/login/source_test.go
new file mode 100644 (file)
index 0000000..d986090
--- /dev/null
@@ -0,0 +1,59 @@
+// Copyright 2019 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 (
+       "strings"
+       "testing"
+
+       "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/modules/json"
+
+       "github.com/stretchr/testify/assert"
+       "xorm.io/xorm/schemas"
+)
+
+type TestSource struct {
+       Provider                      string
+       ClientID                      string
+       ClientSecret                  string
+       OpenIDConnectAutoDiscoveryURL string
+       IconURL                       string
+}
+
+// FromDB fills up a LDAPConfig from serialized format.
+func (source *TestSource) FromDB(bs []byte) error {
+       return json.Unmarshal(bs, &source)
+}
+
+// ToDB exports a LDAPConfig to a serialized format.
+func (source *TestSource) ToDB() ([]byte, error) {
+       return json.Marshal(source)
+}
+
+func TestDumpLoginSource(t *testing.T) {
+       assert.NoError(t, db.PrepareTestDatabase())
+
+       loginSourceSchema, err := db.TableInfo(new(Source))
+       assert.NoError(t, err)
+
+       RegisterTypeConfig(OAuth2, new(TestSource))
+
+       CreateSource(&Source{
+               Type:     OAuth2,
+               Name:     "TestSource",
+               IsActive: false,
+               Cfg: &TestSource{
+                       Provider: "ConvertibleSourceName",
+                       ClientID: "42",
+               },
+       })
+
+       sb := new(strings.Builder)
+
+       db.DumpTables([]*schemas.Table{loginSourceSchema}, sb)
+
+       assert.Contains(t, sb.String(), `"Provider":"ConvertibleSourceName"`)
+}
diff --git a/models/login_source.go b/models/login_source.go
deleted file mode 100644 (file)
index e1f7a7e..0000000
+++ /dev/null
@@ -1,386 +0,0 @@
-// Copyright 2014 The Gogs Authors. All rights reserved.
-// Copyright 2019 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 (
-       "reflect"
-       "strconv"
-
-       "code.gitea.io/gitea/models/db"
-       "code.gitea.io/gitea/modules/log"
-       "code.gitea.io/gitea/modules/timeutil"
-
-       "xorm.io/xorm"
-       "xorm.io/xorm/convert"
-)
-
-// LoginType represents an login type.
-type LoginType int
-
-// Note: new type must append to the end of list to maintain compatibility.
-const (
-       LoginNoType LoginType = iota
-       LoginPlain            // 1
-       LoginLDAP             // 2
-       LoginSMTP             // 3
-       LoginPAM              // 4
-       LoginDLDAP            // 5
-       LoginOAuth2           // 6
-       LoginSSPI             // 7
-)
-
-// String returns the string name of the LoginType
-func (typ LoginType) String() string {
-       return LoginNames[typ]
-}
-
-// Int returns the int value of the LoginType
-func (typ LoginType) Int() int {
-       return int(typ)
-}
-
-// LoginNames contains the name of LoginType values.
-var LoginNames = map[LoginType]string{
-       LoginLDAP:   "LDAP (via BindDN)",
-       LoginDLDAP:  "LDAP (simple auth)", // Via direct bind
-       LoginSMTP:   "SMTP",
-       LoginPAM:    "PAM",
-       LoginOAuth2: "OAuth2",
-       LoginSSPI:   "SPNEGO with SSPI",
-}
-
-// LoginConfig represents login config as far as the db is concerned
-type LoginConfig interface {
-       convert.Conversion
-}
-
-// SkipVerifiable configurations provide a IsSkipVerify to check if SkipVerify is set
-type SkipVerifiable interface {
-       IsSkipVerify() bool
-}
-
-// HasTLSer configurations provide a HasTLS to check if TLS can be enabled
-type HasTLSer interface {
-       HasTLS() bool
-}
-
-// UseTLSer configurations provide a HasTLS to check if TLS is enabled
-type UseTLSer interface {
-       UseTLS() bool
-}
-
-// SSHKeyProvider configurations provide ProvidesSSHKeys to check if they provide SSHKeys
-type SSHKeyProvider interface {
-       ProvidesSSHKeys() bool
-}
-
-// RegisterableSource configurations provide RegisterSource which needs to be run on creation
-type RegisterableSource interface {
-       RegisterSource() error
-       UnregisterSource() error
-}
-
-// LoginSourceSettable configurations can have their loginSource set on them
-type LoginSourceSettable interface {
-       SetLoginSource(*LoginSource)
-}
-
-// RegisterLoginTypeConfig register a config for a provided type
-func RegisterLoginTypeConfig(typ LoginType, exemplar LoginConfig) {
-       if reflect.TypeOf(exemplar).Kind() == reflect.Ptr {
-               // Pointer:
-               registeredLoginConfigs[typ] = func() LoginConfig {
-                       return reflect.New(reflect.ValueOf(exemplar).Elem().Type()).Interface().(LoginConfig)
-               }
-               return
-       }
-
-       // Not a Pointer
-       registeredLoginConfigs[typ] = func() LoginConfig {
-               return reflect.New(reflect.TypeOf(exemplar)).Elem().Interface().(LoginConfig)
-       }
-}
-
-var registeredLoginConfigs = map[LoginType]func() LoginConfig{}
-
-// LoginSource represents an external way for authorizing users.
-type LoginSource struct {
-       ID            int64 `xorm:"pk autoincr"`
-       Type          LoginType
-       Name          string             `xorm:"UNIQUE"`
-       IsActive      bool               `xorm:"INDEX NOT NULL DEFAULT false"`
-       IsSyncEnabled bool               `xorm:"INDEX NOT NULL DEFAULT false"`
-       Cfg           convert.Conversion `xorm:"TEXT"`
-
-       CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
-       UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
-}
-
-func init() {
-       db.RegisterModel(new(LoginSource))
-}
-
-// Cell2Int64 converts a xorm.Cell type to int64,
-// and handles possible irregular cases.
-func Cell2Int64(val xorm.Cell) int64 {
-       switch (*val).(type) {
-       case []uint8:
-               log.Trace("Cell2Int64 ([]uint8): %v", *val)
-
-               v, _ := strconv.ParseInt(string((*val).([]uint8)), 10, 64)
-               return v
-       }
-       return (*val).(int64)
-}
-
-// BeforeSet is invoked from XORM before setting the value of a field of this object.
-func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
-       if colName == "type" {
-               typ := LoginType(Cell2Int64(val))
-               constructor, ok := registeredLoginConfigs[typ]
-               if !ok {
-                       return
-               }
-               source.Cfg = constructor()
-               if settable, ok := source.Cfg.(LoginSourceSettable); ok {
-                       settable.SetLoginSource(source)
-               }
-       }
-}
-
-// TypeName return name of this login source type.
-func (source *LoginSource) TypeName() string {
-       return LoginNames[source.Type]
-}
-
-// IsLDAP returns true of this source is of the LDAP type.
-func (source *LoginSource) IsLDAP() bool {
-       return source.Type == LoginLDAP
-}
-
-// IsDLDAP returns true of this source is of the DLDAP type.
-func (source *LoginSource) IsDLDAP() bool {
-       return source.Type == LoginDLDAP
-}
-
-// IsSMTP returns true of this source is of the SMTP type.
-func (source *LoginSource) IsSMTP() bool {
-       return source.Type == LoginSMTP
-}
-
-// IsPAM returns true of this source is of the PAM type.
-func (source *LoginSource) IsPAM() bool {
-       return source.Type == LoginPAM
-}
-
-// IsOAuth2 returns true of this source is of the OAuth2 type.
-func (source *LoginSource) IsOAuth2() bool {
-       return source.Type == LoginOAuth2
-}
-
-// IsSSPI returns true of this source is of the SSPI type.
-func (source *LoginSource) IsSSPI() bool {
-       return source.Type == LoginSSPI
-}
-
-// HasTLS returns true of this source supports TLS.
-func (source *LoginSource) HasTLS() bool {
-       hasTLSer, ok := source.Cfg.(HasTLSer)
-       return ok && hasTLSer.HasTLS()
-}
-
-// UseTLS returns true of this source is configured to use TLS.
-func (source *LoginSource) UseTLS() bool {
-       useTLSer, ok := source.Cfg.(UseTLSer)
-       return ok && useTLSer.UseTLS()
-}
-
-// SkipVerify returns true if this source is configured to skip SSL
-// verification.
-func (source *LoginSource) SkipVerify() bool {
-       skipVerifiable, ok := source.Cfg.(SkipVerifiable)
-       return ok && skipVerifiable.IsSkipVerify()
-}
-
-// CreateLoginSource inserts a LoginSource in the DB if not already
-// existing with the given name.
-func CreateLoginSource(source *LoginSource) error {
-       has, err := db.GetEngine(db.DefaultContext).Where("name=?", source.Name).Exist(new(LoginSource))
-       if err != nil {
-               return err
-       } else if has {
-               return ErrLoginSourceAlreadyExist{source.Name}
-       }
-       // Synchronization is only available with LDAP for now
-       if !source.IsLDAP() {
-               source.IsSyncEnabled = false
-       }
-
-       _, err = db.GetEngine(db.DefaultContext).Insert(source)
-       if err != nil {
-               return err
-       }
-
-       if !source.IsActive {
-               return nil
-       }
-
-       if settable, ok := source.Cfg.(LoginSourceSettable); ok {
-               settable.SetLoginSource(source)
-       }
-
-       registerableSource, ok := source.Cfg.(RegisterableSource)
-       if !ok {
-               return nil
-       }
-
-       err = registerableSource.RegisterSource()
-       if err != nil {
-               // remove the LoginSource in case of errors while registering configuration
-               if _, err := db.GetEngine(db.DefaultContext).Delete(source); err != nil {
-                       log.Error("CreateLoginSource: Error while wrapOpenIDConnectInitializeError: %v", err)
-               }
-       }
-       return err
-}
-
-// LoginSources returns a slice of all login sources found in DB.
-func LoginSources() ([]*LoginSource, error) {
-       auths := make([]*LoginSource, 0, 6)
-       return auths, db.GetEngine(db.DefaultContext).Find(&auths)
-}
-
-// LoginSourcesByType returns all sources of the specified type
-func LoginSourcesByType(loginType LoginType) ([]*LoginSource, error) {
-       sources := make([]*LoginSource, 0, 1)
-       if err := db.GetEngine(db.DefaultContext).Where("type = ?", loginType).Find(&sources); err != nil {
-               return nil, err
-       }
-       return sources, nil
-}
-
-// AllActiveLoginSources returns all active sources
-func AllActiveLoginSources() ([]*LoginSource, error) {
-       sources := make([]*LoginSource, 0, 5)
-       if err := db.GetEngine(db.DefaultContext).Where("is_active = ?", true).Find(&sources); err != nil {
-               return nil, err
-       }
-       return sources, nil
-}
-
-// ActiveLoginSources returns all active sources of the specified type
-func ActiveLoginSources(loginType LoginType) ([]*LoginSource, error) {
-       sources := make([]*LoginSource, 0, 1)
-       if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, loginType).Find(&sources); err != nil {
-               return nil, err
-       }
-       return sources, nil
-}
-
-// IsSSPIEnabled returns true if there is at least one activated login
-// source of type LoginSSPI
-func IsSSPIEnabled() bool {
-       if !db.HasEngine {
-               return false
-       }
-       sources, err := ActiveLoginSources(LoginSSPI)
-       if err != nil {
-               log.Error("ActiveLoginSources: %v", err)
-               return false
-       }
-       return len(sources) > 0
-}
-
-// GetLoginSourceByID returns login source by given ID.
-func GetLoginSourceByID(id int64) (*LoginSource, error) {
-       source := new(LoginSource)
-       if id == 0 {
-               source.Cfg = registeredLoginConfigs[LoginNoType]()
-               // Set this source to active
-               // FIXME: allow disabling of db based password authentication in future
-               source.IsActive = true
-               return source, nil
-       }
-
-       has, err := db.GetEngine(db.DefaultContext).ID(id).Get(source)
-       if err != nil {
-               return nil, err
-       } else if !has {
-               return nil, ErrLoginSourceNotExist{id}
-       }
-       return source, nil
-}
-
-// UpdateSource updates a LoginSource record in DB.
-func UpdateSource(source *LoginSource) error {
-       var originalLoginSource *LoginSource
-       if source.IsOAuth2() {
-               // keep track of the original values so we can restore in case of errors while registering OAuth2 providers
-               var err error
-               if originalLoginSource, err = GetLoginSourceByID(source.ID); err != nil {
-                       return err
-               }
-       }
-
-       _, err := db.GetEngine(db.DefaultContext).ID(source.ID).AllCols().Update(source)
-       if err != nil {
-               return err
-       }
-
-       if !source.IsActive {
-               return nil
-       }
-
-       if settable, ok := source.Cfg.(LoginSourceSettable); ok {
-               settable.SetLoginSource(source)
-       }
-
-       registerableSource, ok := source.Cfg.(RegisterableSource)
-       if !ok {
-               return nil
-       }
-
-       err = registerableSource.RegisterSource()
-       if err != nil {
-               // restore original values since we cannot update the provider it self
-               if _, err := db.GetEngine(db.DefaultContext).ID(source.ID).AllCols().Update(originalLoginSource); err != nil {
-                       log.Error("UpdateSource: Error while wrapOpenIDConnectInitializeError: %v", err)
-               }
-       }
-       return err
-}
-
-// DeleteSource deletes a LoginSource record in DB.
-func DeleteSource(source *LoginSource) error {
-       count, err := db.GetEngine(db.DefaultContext).Count(&User{LoginSource: source.ID})
-       if err != nil {
-               return err
-       } else if count > 0 {
-               return ErrLoginSourceInUse{source.ID}
-       }
-
-       count, err = db.GetEngine(db.DefaultContext).Count(&ExternalLoginUser{LoginSourceID: source.ID})
-       if err != nil {
-               return err
-       } else if count > 0 {
-               return ErrLoginSourceInUse{source.ID}
-       }
-
-       if registerableSource, ok := source.Cfg.(RegisterableSource); ok {
-               if err := registerableSource.UnregisterSource(); err != nil {
-                       return err
-               }
-       }
-
-       _, err = db.GetEngine(db.DefaultContext).ID(source.ID).Delete(new(LoginSource))
-       return err
-}
-
-// CountLoginSources returns number of login sources.
-func CountLoginSources() int64 {
-       count, _ := db.GetEngine(db.DefaultContext).Count(new(LoginSource))
-       return count
-}
diff --git a/models/login_source_test.go b/models/login_source_test.go
deleted file mode 100644 (file)
index ea18122..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-// Copyright 2019 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 (
-       "strings"
-       "testing"
-
-       "code.gitea.io/gitea/models/db"
-       "code.gitea.io/gitea/modules/json"
-
-       "github.com/stretchr/testify/assert"
-       "xorm.io/xorm/schemas"
-)
-
-type TestSource struct {
-       Provider                      string
-       ClientID                      string
-       ClientSecret                  string
-       OpenIDConnectAutoDiscoveryURL string
-       IconURL                       string
-}
-
-// FromDB fills up a LDAPConfig from serialized format.
-func (source *TestSource) FromDB(bs []byte) error {
-       return json.Unmarshal(bs, &source)
-}
-
-// ToDB exports a LDAPConfig to a serialized format.
-func (source *TestSource) ToDB() ([]byte, error) {
-       return json.Marshal(source)
-}
-
-func TestDumpLoginSource(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-
-       loginSourceSchema, err := db.TableInfo(new(LoginSource))
-       assert.NoError(t, err)
-
-       RegisterLoginTypeConfig(LoginOAuth2, new(TestSource))
-
-       CreateLoginSource(&LoginSource{
-               Type:     LoginOAuth2,
-               Name:     "TestSource",
-               IsActive: false,
-               Cfg: &TestSource{
-                       Provider: "ConvertibleSourceName",
-                       ClientID: "42",
-               },
-       })
-
-       sb := new(strings.Builder)
-
-       db.DumpTables([]*schemas.Table{loginSourceSchema}, sb)
-
-       assert.Contains(t, sb.String(), `"Provider":"ConvertibleSourceName"`)
-}
index fffc44be122e60bbdd869fc0fa1976e531aa680e..78624f1e27156640fa1995f91f12418e529853bd 100644 (file)
@@ -241,7 +241,10 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
 
        if _, err := os.Stat(fixturesDir); err == nil {
                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)
                        return x, deferFn
                }
index af24a6cf5a7ad99bb9d752ca3bebd3cfb3ff9000..bcbe8b0988e86931d198653e9a2758434aad95fd 100644 (file)
@@ -74,7 +74,7 @@ func init() {
 
 // FindNotificationOptions represent the filters for notifications. If an ID is 0 it will be ignored.
 type FindNotificationOptions struct {
-       ListOptions
+       db.ListOptions
        UserID            int64
        RepoID            int64
        IssueID           int64
@@ -115,7 +115,7 @@ func (opts *FindNotificationOptions) ToCond() builder.Cond {
 func (opts *FindNotificationOptions) ToSession(e db.Engine) *xorm.Session {
        sess := e.Where(opts.ToCond())
        if opts.Page != 0 {
-               sess = setSessionPagination(sess, opts)
+               sess = db.SetSessionPagination(sess, opts)
        }
        return sess
 }
diff --git a/models/oauth2.go b/models/oauth2.go
deleted file mode 100644 (file)
index 7fdd530..0000000
+++ /dev/null
@@ -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
-}
diff --git a/models/oauth2_application.go b/models/oauth2_application.go
deleted file mode 100644 (file)
index 0fd2e38..0000000
+++ /dev/null
@@ -1,524 +0,0 @@
-// Copyright 2019 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 (
-       "crypto/sha256"
-       "encoding/base64"
-       "fmt"
-       "net/url"
-       "strings"
-
-       "code.gitea.io/gitea/models/db"
-       "code.gitea.io/gitea/modules/secret"
-       "code.gitea.io/gitea/modules/timeutil"
-       "code.gitea.io/gitea/modules/util"
-
-       uuid "github.com/google/uuid"
-       "golang.org/x/crypto/bcrypt"
-       "xorm.io/xorm"
-)
-
-// OAuth2Application represents an OAuth2 client (RFC 6749)
-type OAuth2Application struct {
-       ID   int64 `xorm:"pk autoincr"`
-       UID  int64 `xorm:"INDEX"`
-       User *User `xorm:"-"`
-
-       Name string
-
-       ClientID     string `xorm:"unique"`
-       ClientSecret string
-
-       RedirectURIs []string `xorm:"redirect_uris JSON TEXT"`
-
-       CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
-       UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
-}
-
-func init() {
-       db.RegisterModel(new(OAuth2Application))
-       db.RegisterModel(new(OAuth2AuthorizationCode))
-       db.RegisterModel(new(OAuth2Grant))
-}
-
-// TableName sets the table name to `oauth2_application`
-func (app *OAuth2Application) TableName() string {
-       return "oauth2_application"
-}
-
-// PrimaryRedirectURI returns the first redirect uri or an empty string if empty
-func (app *OAuth2Application) PrimaryRedirectURI() string {
-       if len(app.RedirectURIs) == 0 {
-               return ""
-       }
-       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
-func (app *OAuth2Application) ContainsRedirectURI(redirectURI string) bool {
-       return util.IsStringInSlice(redirectURI, app.RedirectURIs, true)
-}
-
-// GenerateClientSecret will generate the client secret and returns the plaintext and saves the hash at the database
-func (app *OAuth2Application) GenerateClientSecret() (string, error) {
-       clientSecret, err := secret.New()
-       if err != nil {
-               return "", err
-       }
-       hashedSecret, err := bcrypt.GenerateFromPassword([]byte(clientSecret), bcrypt.DefaultCost)
-       if err != nil {
-               return "", err
-       }
-       app.ClientSecret = string(hashedSecret)
-       if _, err := db.GetEngine(db.DefaultContext).ID(app.ID).Cols("client_secret").Update(app); err != nil {
-               return "", err
-       }
-       return clientSecret, nil
-}
-
-// ValidateClientSecret validates the given secret by the hash saved in database
-func (app *OAuth2Application) ValidateClientSecret(secret []byte) bool {
-       return bcrypt.CompareHashAndPassword([]byte(app.ClientSecret), secret) == nil
-}
-
-// GetGrantByUserID returns a OAuth2Grant by its user and application ID
-func (app *OAuth2Application) GetGrantByUserID(userID int64) (*OAuth2Grant, error) {
-       return app.getGrantByUserID(db.GetEngine(db.DefaultContext), userID)
-}
-
-func (app *OAuth2Application) getGrantByUserID(e db.Engine, userID int64) (grant *OAuth2Grant, err error) {
-       grant = new(OAuth2Grant)
-       if has, err := e.Where("user_id = ? AND application_id = ?", userID, app.ID).Get(grant); err != nil {
-               return nil, err
-       } else if !has {
-               return nil, nil
-       }
-       return grant, nil
-}
-
-// CreateGrant generates a grant for an user
-func (app *OAuth2Application) CreateGrant(userID int64, scope string) (*OAuth2Grant, error) {
-       return app.createGrant(db.GetEngine(db.DefaultContext), userID, scope)
-}
-
-func (app *OAuth2Application) createGrant(e db.Engine, userID int64, scope string) (*OAuth2Grant, error) {
-       grant := &OAuth2Grant{
-               ApplicationID: app.ID,
-               UserID:        userID,
-               Scope:         scope,
-       }
-       _, err := e.Insert(grant)
-       if err != nil {
-               return nil, err
-       }
-       return grant, nil
-}
-
-// GetOAuth2ApplicationByClientID returns the oauth2 application with the given client_id. Returns an error if not found.
-func GetOAuth2ApplicationByClientID(clientID string) (app *OAuth2Application, err error) {
-       return getOAuth2ApplicationByClientID(db.GetEngine(db.DefaultContext), clientID)
-}
-
-func getOAuth2ApplicationByClientID(e db.Engine, clientID string) (app *OAuth2Application, err error) {
-       app = new(OAuth2Application)
-       has, err := e.Where("client_id = ?", clientID).Get(app)
-       if !has {
-               return nil, ErrOAuthClientIDInvalid{ClientID: clientID}
-       }
-       return
-}
-
-// GetOAuth2ApplicationByID returns the oauth2 application with the given id. Returns an error if not found.
-func GetOAuth2ApplicationByID(id int64) (app *OAuth2Application, err error) {
-       return getOAuth2ApplicationByID(db.GetEngine(db.DefaultContext), id)
-}
-
-func getOAuth2ApplicationByID(e db.Engine, id int64) (app *OAuth2Application, err error) {
-       app = new(OAuth2Application)
-       has, err := e.ID(id).Get(app)
-       if err != nil {
-               return nil, err
-       }
-       if !has {
-               return nil, ErrOAuthApplicationNotFound{ID: id}
-       }
-       return app, nil
-}
-
-// GetOAuth2ApplicationsByUserID returns all oauth2 applications owned by the user
-func GetOAuth2ApplicationsByUserID(userID int64) (apps []*OAuth2Application, err error) {
-       return getOAuth2ApplicationsByUserID(db.GetEngine(db.DefaultContext), userID)
-}
-
-func getOAuth2ApplicationsByUserID(e db.Engine, userID int64) (apps []*OAuth2Application, err error) {
-       apps = make([]*OAuth2Application, 0)
-       err = e.Where("uid = ?", userID).Find(&apps)
-       return
-}
-
-// CreateOAuth2ApplicationOptions holds options to create an oauth2 application
-type CreateOAuth2ApplicationOptions struct {
-       Name         string
-       UserID       int64
-       RedirectURIs []string
-}
-
-// CreateOAuth2Application inserts a new oauth2 application
-func CreateOAuth2Application(opts CreateOAuth2ApplicationOptions) (*OAuth2Application, error) {
-       return createOAuth2Application(db.GetEngine(db.DefaultContext), opts)
-}
-
-func createOAuth2Application(e db.Engine, opts CreateOAuth2ApplicationOptions) (*OAuth2Application, error) {
-       clientID := uuid.New().String()
-       app := &OAuth2Application{
-               UID:          opts.UserID,
-               Name:         opts.Name,
-               ClientID:     clientID,
-               RedirectURIs: opts.RedirectURIs,
-       }
-       if _, err := e.Insert(app); err != nil {
-               return nil, err
-       }
-       return app, nil
-}
-
-// UpdateOAuth2ApplicationOptions holds options to update an oauth2 application
-type UpdateOAuth2ApplicationOptions struct {
-       ID           int64
-       Name         string
-       UserID       int64
-       RedirectURIs []string
-}
-
-// UpdateOAuth2Application updates an oauth2 application
-func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) {
-       sess := db.NewSession(db.DefaultContext)
-       if err := sess.Begin(); err != nil {
-               return nil, err
-       }
-       defer sess.Close()
-
-       app, err := getOAuth2ApplicationByID(sess, opts.ID)
-       if err != nil {
-               return nil, err
-       }
-       if app.UID != opts.UserID {
-               return nil, fmt.Errorf("UID mismatch")
-       }
-
-       app.Name = opts.Name
-       app.RedirectURIs = opts.RedirectURIs
-
-       if err = updateOAuth2Application(sess, app); err != nil {
-               return nil, err
-       }
-       app.ClientSecret = ""
-
-       return app, sess.Commit()
-}
-
-func updateOAuth2Application(e db.Engine, app *OAuth2Application) error {
-       if _, err := e.ID(app.ID).Update(app); err != nil {
-               return err
-       }
-       return nil
-}
-
-func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
-       if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil {
-               return err
-       } else if deleted == 0 {
-               return ErrOAuthApplicationNotFound{ID: id}
-       }
-       codes := make([]*OAuth2AuthorizationCode, 0)
-       // delete correlating auth codes
-       if err := sess.Join("INNER", "oauth2_grant",
-               "oauth2_authorization_code.grant_id = oauth2_grant.id AND oauth2_grant.application_id = ?", id).Find(&codes); err != nil {
-               return err
-       }
-       codeIDs := make([]int64, 0)
-       for _, grant := range codes {
-               codeIDs = append(codeIDs, grant.ID)
-       }
-
-       if _, err := sess.In("id", codeIDs).Delete(new(OAuth2AuthorizationCode)); err != nil {
-               return err
-       }
-
-       if _, err := sess.Where("application_id = ?", id).Delete(new(OAuth2Grant)); err != nil {
-               return err
-       }
-       return nil
-}
-
-// DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app.
-func DeleteOAuth2Application(id, userid int64) error {
-       sess := db.NewSession(db.DefaultContext)
-       defer sess.Close()
-       if err := sess.Begin(); err != nil {
-               return err
-       }
-       if err := deleteOAuth2Application(sess, id, userid); err != nil {
-               return err
-       }
-       return sess.Commit()
-}
-
-// ListOAuth2Applications returns a list of oauth2 applications belongs to given user.
-func ListOAuth2Applications(uid int64, listOptions ListOptions) ([]*OAuth2Application, int64, error) {
-       sess := db.GetEngine(db.DefaultContext).
-               Where("uid=?", uid).
-               Desc("id")
-
-       if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
-
-               apps := make([]*OAuth2Application, 0, listOptions.PageSize)
-               total, err := sess.FindAndCount(&apps)
-               return apps, total, err
-       }
-
-       apps := make([]*OAuth2Application, 0, 5)
-       total, err := sess.FindAndCount(&apps)
-       return apps, total, err
-}
-
-//////////////////////////////////////////////////////
-
-// OAuth2AuthorizationCode is a code to obtain an access token in combination with the client secret once. It has a limited lifetime.
-type OAuth2AuthorizationCode struct {
-       ID                  int64        `xorm:"pk autoincr"`
-       Grant               *OAuth2Grant `xorm:"-"`
-       GrantID             int64
-       Code                string `xorm:"INDEX unique"`
-       CodeChallenge       string
-       CodeChallengeMethod string
-       RedirectURI         string
-       ValidUntil          timeutil.TimeStamp `xorm:"index"`
-}
-
-// TableName sets the table name to `oauth2_authorization_code`
-func (code *OAuth2AuthorizationCode) TableName() string {
-       return "oauth2_authorization_code"
-}
-
-// GenerateRedirectURI generates a redirect URI for a successful authorization request. State will be used if not empty.
-func (code *OAuth2AuthorizationCode) GenerateRedirectURI(state string) (redirect *url.URL, err error) {
-       if redirect, err = url.Parse(code.RedirectURI); err != nil {
-               return
-       }
-       q := redirect.Query()
-       if state != "" {
-               q.Set("state", state)
-       }
-       q.Set("code", code.Code)
-       redirect.RawQuery = q.Encode()
-       return
-}
-
-// Invalidate deletes the auth code from the database to invalidate this code
-func (code *OAuth2AuthorizationCode) Invalidate() error {
-       return code.invalidate(db.GetEngine(db.DefaultContext))
-}
-
-func (code *OAuth2AuthorizationCode) invalidate(e db.Engine) error {
-       _, err := e.Delete(code)
-       return err
-}
-
-// ValidateCodeChallenge validates the given verifier against the saved code challenge. This is part of the PKCE implementation.
-func (code *OAuth2AuthorizationCode) ValidateCodeChallenge(verifier string) bool {
-       return code.validateCodeChallenge(verifier)
-}
-
-func (code *OAuth2AuthorizationCode) validateCodeChallenge(verifier string) bool {
-       switch code.CodeChallengeMethod {
-       case "S256":
-               // base64url(SHA256(verifier)) see https://tools.ietf.org/html/rfc7636#section-4.6
-               h := sha256.Sum256([]byte(verifier))
-               hashedVerifier := base64.RawURLEncoding.EncodeToString(h[:])
-               return hashedVerifier == code.CodeChallenge
-       case "plain":
-               return verifier == code.CodeChallenge
-       case "":
-               return true
-       default:
-               // unsupported method -> return false
-               return false
-       }
-}
-
-// GetOAuth2AuthorizationByCode returns an authorization by its code
-func GetOAuth2AuthorizationByCode(code string) (*OAuth2AuthorizationCode, error) {
-       return getOAuth2AuthorizationByCode(db.GetEngine(db.DefaultContext), code)
-}
-
-func getOAuth2AuthorizationByCode(e db.Engine, code string) (auth *OAuth2AuthorizationCode, err error) {
-       auth = new(OAuth2AuthorizationCode)
-       if has, err := e.Where("code = ?", code).Get(auth); err != nil {
-               return nil, err
-       } else if !has {
-               return nil, nil
-       }
-       auth.Grant = new(OAuth2Grant)
-       if has, err := e.ID(auth.GrantID).Get(auth.Grant); err != nil {
-               return nil, err
-       } else if !has {
-               return nil, nil
-       }
-       return auth, nil
-}
-
-//////////////////////////////////////////////////////
-
-// OAuth2Grant represents the permission of an user for a specific application to access resources
-type OAuth2Grant struct {
-       ID            int64              `xorm:"pk autoincr"`
-       UserID        int64              `xorm:"INDEX unique(user_application)"`
-       Application   *OAuth2Application `xorm:"-"`
-       ApplicationID int64              `xorm:"INDEX unique(user_application)"`
-       Counter       int64              `xorm:"NOT NULL DEFAULT 1"`
-       Scope         string             `xorm:"TEXT"`
-       Nonce         string             `xorm:"TEXT"`
-       CreatedUnix   timeutil.TimeStamp `xorm:"created"`
-       UpdatedUnix   timeutil.TimeStamp `xorm:"updated"`
-}
-
-// TableName sets the table name to `oauth2_grant`
-func (grant *OAuth2Grant) TableName() string {
-       return "oauth2_grant"
-}
-
-// GenerateNewAuthorizationCode generates a new authorization code for a grant and saves it to the database
-func (grant *OAuth2Grant) GenerateNewAuthorizationCode(redirectURI, codeChallenge, codeChallengeMethod string) (*OAuth2AuthorizationCode, error) {
-       return grant.generateNewAuthorizationCode(db.GetEngine(db.DefaultContext), redirectURI, codeChallenge, codeChallengeMethod)
-}
-
-func (grant *OAuth2Grant) generateNewAuthorizationCode(e db.Engine, redirectURI, codeChallenge, codeChallengeMethod string) (code *OAuth2AuthorizationCode, err error) {
-       var codeSecret string
-       if codeSecret, err = secret.New(); err != nil {
-               return &OAuth2AuthorizationCode{}, err
-       }
-       code = &OAuth2AuthorizationCode{
-               Grant:               grant,
-               GrantID:             grant.ID,
-               RedirectURI:         redirectURI,
-               Code:                codeSecret,
-               CodeChallenge:       codeChallenge,
-               CodeChallengeMethod: codeChallengeMethod,
-       }
-       if _, err := e.Insert(code); err != nil {
-               return nil, err
-       }
-       return code, nil
-}
-
-// IncreaseCounter increases the counter and updates the grant
-func (grant *OAuth2Grant) IncreaseCounter() error {
-       return grant.increaseCount(db.GetEngine(db.DefaultContext))
-}
-
-func (grant *OAuth2Grant) increaseCount(e db.Engine) error {
-       _, err := e.ID(grant.ID).Incr("counter").Update(new(OAuth2Grant))
-       if err != nil {
-               return err
-       }
-       updatedGrant, err := getOAuth2GrantByID(e, grant.ID)
-       if err != nil {
-               return err
-       }
-       grant.Counter = updatedGrant.Counter
-       return nil
-}
-
-// ScopeContains returns true if the grant scope contains the specified scope
-func (grant *OAuth2Grant) ScopeContains(scope string) bool {
-       for _, currentScope := range strings.Split(grant.Scope, " ") {
-               if scope == currentScope {
-                       return true
-               }
-       }
-       return false
-}
-
-// SetNonce updates the current nonce value of a grant
-func (grant *OAuth2Grant) SetNonce(nonce string) error {
-       return grant.setNonce(db.GetEngine(db.DefaultContext), nonce)
-}
-
-func (grant *OAuth2Grant) setNonce(e db.Engine, nonce string) error {
-       grant.Nonce = nonce
-       _, err := e.ID(grant.ID).Cols("nonce").Update(grant)
-       if err != nil {
-               return err
-       }
-       return nil
-}
-
-// GetOAuth2GrantByID returns the grant with the given ID
-func GetOAuth2GrantByID(id int64) (*OAuth2Grant, error) {
-       return getOAuth2GrantByID(db.GetEngine(db.DefaultContext), id)
-}
-
-func getOAuth2GrantByID(e db.Engine, id int64) (grant *OAuth2Grant, err error) {
-       grant = new(OAuth2Grant)
-       if has, err := e.ID(id).Get(grant); err != nil {
-               return nil, err
-       } else if !has {
-               return nil, nil
-       }
-       return
-}
-
-// GetOAuth2GrantsByUserID lists all grants of a certain user
-func GetOAuth2GrantsByUserID(uid int64) ([]*OAuth2Grant, error) {
-       return getOAuth2GrantsByUserID(db.GetEngine(db.DefaultContext), uid)
-}
-
-func getOAuth2GrantsByUserID(e db.Engine, uid int64) ([]*OAuth2Grant, error) {
-       type joinedOAuth2Grant struct {
-               Grant       *OAuth2Grant       `xorm:"extends"`
-               Application *OAuth2Application `xorm:"extends"`
-       }
-       var results *xorm.Rows
-       var err error
-       if results, err = e.
-               Table("oauth2_grant").
-               Where("user_id = ?", uid).
-               Join("INNER", "oauth2_application", "application_id = oauth2_application.id").
-               Rows(new(joinedOAuth2Grant)); err != nil {
-               return nil, err
-       }
-       defer results.Close()
-       grants := make([]*OAuth2Grant, 0)
-       for results.Next() {
-               joinedGrant := new(joinedOAuth2Grant)
-               if err := results.Scan(joinedGrant); err != nil {
-                       return nil, err
-               }
-               joinedGrant.Grant.Application = joinedGrant.Application
-               grants = append(grants, joinedGrant.Grant)
-       }
-       return grants, nil
-}
-
-// RevokeOAuth2Grant deletes the grant with grantID and userID
-func RevokeOAuth2Grant(grantID, userID int64) error {
-       return revokeOAuth2Grant(db.GetEngine(db.DefaultContext), grantID, userID)
-}
-
-func revokeOAuth2Grant(e db.Engine, grantID, userID int64) error {
-       _, err := e.Delete(&OAuth2Grant{ID: grantID, UserID: userID})
-       return err
-}
diff --git a/models/oauth2_application_test.go b/models/oauth2_application_test.go
deleted file mode 100644 (file)
index b01ef96..0000000
+++ /dev/null
@@ -1,239 +0,0 @@
-// Copyright 2019 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 (
-       "testing"
-
-       "code.gitea.io/gitea/models/db"
-       "github.com/stretchr/testify/assert"
-)
-
-//////////////////// Application
-
-func TestOAuth2Application_GenerateClientSecret(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
-       secret, err := app.GenerateClientSecret()
-       assert.NoError(t, err)
-       assert.True(t, len(secret) > 0)
-       db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1, ClientSecret: app.ClientSecret})
-}
-
-func BenchmarkOAuth2Application_GenerateClientSecret(b *testing.B) {
-       assert.NoError(b, db.PrepareTestDatabase())
-       app := db.AssertExistsAndLoadBean(b, &OAuth2Application{ID: 1}).(*OAuth2Application)
-       for i := 0; i < b.N; i++ {
-               _, _ = app.GenerateClientSecret()
-       }
-}
-
-func TestOAuth2Application_ContainsRedirectURI(t *testing.T) {
-       app := &OAuth2Application{
-               RedirectURIs: []string{"a", "b", "c"},
-       }
-       assert.True(t, app.ContainsRedirectURI("a"))
-       assert.True(t, app.ContainsRedirectURI("b"))
-       assert.True(t, app.ContainsRedirectURI("c"))
-       assert.False(t, app.ContainsRedirectURI("d"))
-}
-
-func TestOAuth2Application_ValidateClientSecret(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
-       secret, err := app.GenerateClientSecret()
-       assert.NoError(t, err)
-       assert.True(t, app.ValidateClientSecret([]byte(secret)))
-       assert.False(t, app.ValidateClientSecret([]byte("fewijfowejgfiowjeoifew")))
-}
-
-func TestGetOAuth2ApplicationByClientID(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       app, err := GetOAuth2ApplicationByClientID("da7da3ba-9a13-4167-856f-3899de0b0138")
-       assert.NoError(t, err)
-       assert.Equal(t, "da7da3ba-9a13-4167-856f-3899de0b0138", app.ClientID)
-
-       app, err = GetOAuth2ApplicationByClientID("invalid client id")
-       assert.Error(t, err)
-       assert.Nil(t, app)
-}
-
-func TestCreateOAuth2Application(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       app, err := CreateOAuth2Application(CreateOAuth2ApplicationOptions{Name: "newapp", UserID: 1})
-       assert.NoError(t, err)
-       assert.Equal(t, "newapp", app.Name)
-       assert.Len(t, app.ClientID, 36)
-       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) {
-       assert.Equal(t, "oauth2_application", new(OAuth2Application).TableName())
-}
-
-func TestOAuth2Application_GetGrantByUserID(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
-       grant, err := app.GetGrantByUserID(1)
-       assert.NoError(t, err)
-       assert.Equal(t, int64(1), grant.UserID)
-
-       grant, err = app.GetGrantByUserID(34923458)
-       assert.NoError(t, err)
-       assert.Nil(t, grant)
-}
-
-func TestOAuth2Application_CreateGrant(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       app := db.AssertExistsAndLoadBean(t, &OAuth2Application{ID: 1}).(*OAuth2Application)
-       grant, err := app.CreateGrant(2, "")
-       assert.NoError(t, err)
-       assert.NotNil(t, grant)
-       assert.Equal(t, int64(2), grant.UserID)
-       assert.Equal(t, int64(1), grant.ApplicationID)
-       assert.Equal(t, "", grant.Scope)
-}
-
-//////////////////// Grant
-
-func TestGetOAuth2GrantByID(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       grant, err := GetOAuth2GrantByID(1)
-       assert.NoError(t, err)
-       assert.Equal(t, int64(1), grant.ID)
-
-       grant, err = GetOAuth2GrantByID(34923458)
-       assert.NoError(t, err)
-       assert.Nil(t, grant)
-}
-
-func TestOAuth2Grant_IncreaseCounter(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 1}).(*OAuth2Grant)
-       assert.NoError(t, grant.IncreaseCounter())
-       assert.Equal(t, int64(2), grant.Counter)
-       db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Counter: 2})
-}
-
-func TestOAuth2Grant_ScopeContains(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1, Scope: "openid profile"}).(*OAuth2Grant)
-       assert.True(t, grant.ScopeContains("openid"))
-       assert.True(t, grant.ScopeContains("profile"))
-       assert.False(t, grant.ScopeContains("profil"))
-       assert.False(t, grant.ScopeContains("profile2"))
-}
-
-func TestOAuth2Grant_GenerateNewAuthorizationCode(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       grant := db.AssertExistsAndLoadBean(t, &OAuth2Grant{ID: 1}).(*OAuth2Grant)
-       code, err := grant.GenerateNewAuthorizationCode("https://example2.com/callback", "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg", "S256")
-       assert.NoError(t, err)
-       assert.NotNil(t, code)
-       assert.True(t, len(code.Code) > 32) // secret length > 32
-}
-
-func TestOAuth2Grant_TableName(t *testing.T) {
-       assert.Equal(t, "oauth2_grant", new(OAuth2Grant).TableName())
-}
-
-func TestGetOAuth2GrantsByUserID(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       result, err := GetOAuth2GrantsByUserID(1)
-       assert.NoError(t, err)
-       assert.Len(t, result, 1)
-       assert.Equal(t, int64(1), result[0].ID)
-       assert.Equal(t, result[0].ApplicationID, result[0].Application.ID)
-
-       result, err = GetOAuth2GrantsByUserID(34134)
-       assert.NoError(t, err)
-       assert.Empty(t, result)
-}
-
-func TestRevokeOAuth2Grant(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       assert.NoError(t, RevokeOAuth2Grant(1, 1))
-       db.AssertNotExistsBean(t, &OAuth2Grant{ID: 1, UserID: 1})
-}
-
-//////////////////// Authorization Code
-
-func TestGetOAuth2AuthorizationByCode(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       code, err := GetOAuth2AuthorizationByCode("authcode")
-       assert.NoError(t, err)
-       assert.NotNil(t, code)
-       assert.Equal(t, "authcode", code.Code)
-       assert.Equal(t, int64(1), code.ID)
-
-       code, err = GetOAuth2AuthorizationByCode("does not exist")
-       assert.NoError(t, err)
-       assert.Nil(t, code)
-}
-
-func TestOAuth2AuthorizationCode_ValidateCodeChallenge(t *testing.T) {
-       // test plain
-       code := &OAuth2AuthorizationCode{
-               CodeChallengeMethod: "plain",
-               CodeChallenge:       "test123",
-       }
-       assert.True(t, code.ValidateCodeChallenge("test123"))
-       assert.False(t, code.ValidateCodeChallenge("ierwgjoergjio"))
-
-       // test S256
-       code = &OAuth2AuthorizationCode{
-               CodeChallengeMethod: "S256",
-               CodeChallenge:       "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg",
-       }
-       assert.True(t, code.ValidateCodeChallenge("N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt"))
-       assert.False(t, code.ValidateCodeChallenge("wiogjerogorewngoenrgoiuenorg"))
-
-       // test unknown
-       code = &OAuth2AuthorizationCode{
-               CodeChallengeMethod: "monkey",
-               CodeChallenge:       "foiwgjioriogeiogjerger",
-       }
-       assert.False(t, code.ValidateCodeChallenge("foiwgjioriogeiogjerger"))
-
-       // test no code challenge
-       code = &OAuth2AuthorizationCode{
-               CodeChallengeMethod: "",
-               CodeChallenge:       "foierjiogerogerg",
-       }
-       assert.True(t, code.ValidateCodeChallenge(""))
-}
-
-func TestOAuth2AuthorizationCode_GenerateRedirectURI(t *testing.T) {
-       code := &OAuth2AuthorizationCode{
-               RedirectURI: "https://example.com/callback",
-               Code:        "thecode",
-       }
-
-       redirect, err := code.GenerateRedirectURI("thestate")
-       assert.NoError(t, err)
-       assert.Equal(t, "https://example.com/callback?code=thecode&state=thestate", redirect.String())
-
-       redirect, err = code.GenerateRedirectURI("")
-       assert.NoError(t, err)
-       assert.Equal(t, "https://example.com/callback?code=thecode", redirect.String())
-}
-
-func TestOAuth2AuthorizationCode_Invalidate(t *testing.T) {
-       assert.NoError(t, db.PrepareTestDatabase())
-       code := db.AssertExistsAndLoadBean(t, &OAuth2AuthorizationCode{Code: "authcode"}).(*OAuth2AuthorizationCode)
-       assert.NoError(t, code.Invalidate())
-       db.AssertNotExistsBean(t, &OAuth2AuthorizationCode{Code: "authcode"})
-}
-
-func TestOAuth2AuthorizationCode_TableName(t *testing.T) {
-       assert.Equal(t, "oauth2_authorization_code", new(OAuth2AuthorizationCode).TableName())
-}
index bc6c47fd456ccd06d80cd91df923ec8d8e39c655..94939d2c74b812a88f397372e921b71db92882f3 100644 (file)
@@ -78,7 +78,7 @@ func (org *User) GetMembers() (err error) {
 
 // FindOrgMembersOpts represensts find org members conditions
 type FindOrgMembersOpts struct {
-       ListOptions
+       db.ListOptions
        OrgID      int64
        PublicOnly bool
 }
@@ -574,7 +574,7 @@ func GetOrgUsersByUserID(uid int64, opts *SearchOrganizationsOptions) ([]*OrgUse
        }
 
        if opts.PageSize != 0 {
-               sess = setSessionPagination(sess, opts)
+               sess = db.SetSessionPagination(sess, opts)
        }
 
        err := sess.
@@ -594,7 +594,7 @@ func getOrgUsersByOrgID(e db.Engine, opts *FindOrgMembersOpts) ([]*OrgUser, erro
                sess.And("is_public = ?", true)
        }
        if opts.ListOptions.PageSize > 0 {
-               sess = setSessionPagination(sess, opts)
+               sess = db.SetSessionPagination(sess, opts)
 
                ous := make([]*OrgUser, 0, opts.PageSize)
                return ous, sess.Find(&ous)
index 7ca715bb78991c3e4c068ec16130b5f22e5a0c85..fc6a5f2c3b186d42db9dab324b9ee8e1c75c981e 100644 (file)
@@ -47,7 +47,7 @@ func init() {
 
 // SearchTeamOptions holds the search options
 type SearchTeamOptions struct {
-       ListOptions
+       db.ListOptions
        UserID      int64
        Keyword     string
        OrgID       int64
@@ -56,7 +56,7 @@ type SearchTeamOptions struct {
 
 // SearchMembersOptions holds the search options
 type SearchMembersOptions struct {
-       ListOptions
+       db.ListOptions
 }
 
 // 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(getPaginatedSession(opts))
+       return t.getRepositories(db.GetPaginatedSession(opts))
 }
 
 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(getPaginatedSession(opts))
+       return t.getMembers(db.GetPaginatedSession(opts))
 }
 
 // AddMember adds new membership of the team to the organization,
index 75dfc4262d5cd8c658e53e860ee1e450ba241f43..2df89b2afcac3585c02d16213d18250d2f06dcc4 100644 (file)
@@ -399,7 +399,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
 
        orgUsers, err := GetOrgUsersByOrgID(&FindOrgMembersOpts{
-               ListOptions: ListOptions{},
+               ListOptions: db.ListOptions{},
                OrgID:       3,
                PublicOnly:  false,
        })
@@ -420,7 +420,7 @@ func TestGetOrgUsersByOrgID(t *testing.T) {
        }
 
        orgUsers, err = GetOrgUsersByOrgID(&FindOrgMembersOpts{
-               ListOptions: ListOptions{},
+               ListOptions: db.ListOptions{},
                OrgID:       db.NonexistentID,
                PublicOnly:  false,
        })
index 57e2f9c85f7e329300576435195f3e1ed62adb9b..57ef210213950f5dbd01ec444017c878ddc09a82 100644 (file)
@@ -17,7 +17,7 @@ import (
 
 // PullRequestsOptions holds the options for PRs
 type PullRequestsOptions struct {
-       ListOptions
+       db.ListOptions
        State       string
        SortType    string
        Labels      []string
@@ -101,7 +101,7 @@ func PullRequests(baseRepoID int64, opts *PullRequestsOptions) ([]*PullRequest,
                log.Error("listPullRequestStatement: %v", err)
                return nil, maxResults, err
        }
-       findSession = setSessionPagination(findSession, opts)
+       findSession = db.SetSessionPagination(findSession, opts)
        prs := make([]*PullRequest, 0, opts.PageSize)
        return prs, maxResults, findSession.Find(&prs)
 }
index e7cf4ab666ee8632095690e365235be33b9a1a31..2e7cbff48b4316d354594aafa43c271fe4dade4c 100644 (file)
@@ -5,6 +5,7 @@
 package models
 
 import (
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/setting"
@@ -35,7 +36,7 @@ Loop:
                case always:
                        break Loop
                case pubkey:
-                       keys, err := ListGPGKeys(u.ID, ListOptions{})
+                       keys, err := ListGPGKeys(u.ID, db.ListOptions{})
                        if err != nil {
                                return false, "", nil, err
                        }
index 6543d0ec9660de917363403c50641cace117247e..2b7ef2f664afc3f38b412e07e89b5b9aa1789a85 100644 (file)
@@ -56,7 +56,7 @@ func TestPullRequest_LoadHeadRepo(t *testing.T) {
 func TestPullRequestsNewest(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
        prs, count, err := PullRequests(1, &PullRequestsOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        Page: 1,
                },
                State:    "open",
@@ -75,7 +75,7 @@ func TestPullRequestsNewest(t *testing.T) {
 func TestPullRequestsOldest(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
        prs, count, err := PullRequests(1, &PullRequestsOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        Page: 1,
                },
                State:    "open",
index d6b629cfe839db7a2e5f04163c46bd1961a9f5a2..4624791b8ff41d89ad37a49dddeeeb23e22f531b 100644 (file)
@@ -177,7 +177,7 @@ func GetReleaseByID(id int64) (*Release, error) {
 
 // FindReleasesOptions describes the conditions to Find releases
 type FindReleasesOptions struct {
-       ListOptions
+       db.ListOptions
        IncludeDrafts bool
        IncludeTags   bool
        IsPreRelease  util.OptionalBool
@@ -214,7 +214,7 @@ func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions) ([]*Release, er
                Where(opts.toConds(repoID))
 
        if opts.PageSize != 0 {
-               sess = setSessionPagination(sess, &opts.ListOptions)
+               sess = db.SetSessionPagination(sess, &opts.ListOptions)
        }
 
        rels := make([]*Release, 0, opts.PageSize)
index ae149f467d6d2050c407c8c0482554c7f568bd4b..efd78c6042d6910aeee1c1321eec14635702d372 100644 (file)
@@ -1772,7 +1772,7 @@ func GetUserRepositories(opts *SearchRepoOptions) ([]*Repository, int64, error)
 
        sess.Where(cond).OrderBy(opts.OrderBy.String())
        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.
@@ -2057,13 +2057,13 @@ func CopyLFS(ctx context.Context, newRepo, oldRepo *Repository) error {
 }
 
 // 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 {
                forks := make([]*Repository, 0, repo.NumForks)
                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)
        return forks, sess.Find(&forks, &Repository{ForkID: repo.ID})
 }
index 08d2062dbba0094876872bce77b8c59ec8a38723..08360c102d8a36df3794a793e7f424608389a9c9 100644 (file)
@@ -64,13 +64,13 @@ func (repo *Repository) AddCollaborator(u *User) error {
        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 {
                collaborations := make([]*Collaboration, 0, 8)
                return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
        }
 
-       e = setEnginePagination(e, &listOptions)
+       e = db.SetEnginePagination(e, &listOptions)
 
        collaborations := make([]*Collaboration, 0, listOptions.PageSize)
        return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
@@ -82,7 +82,7 @@ type Collaborator struct {
        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)
        if err != nil {
                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
-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)
 }
 
index 5a3ffef5fae18b1a6bc98d799233fb443c100fa7..326fb4dbf7a879baa86a6f30b7d3cf60dd82e653 100644 (file)
@@ -30,7 +30,7 @@ func TestRepository_GetCollaborators(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
        test := func(repoID int64) {
                repo := db.AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
-               collaborators, err := repo.GetCollaborators(ListOptions{})
+               collaborators, err := repo.GetCollaborators(db.ListOptions{})
                assert.NoError(t, err)
                expectedLen, err := db.GetEngine(db.DefaultContext).Count(&Collaboration{RepoID: repoID})
                assert.NoError(t, err)
index cb8bf45184f905c364c74eab4253b4e7bf3d7193..650da711a347294b7a6d547e44788230ce71d964 100644 (file)
@@ -151,7 +151,7 @@ func GenerateAvatar(ctx context.Context, templateRepo, generateRepo *Repository)
 
 // GenerateIssueLabels generates issue labels from a template repository
 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 {
                return err
        }
index 7179114f46729b58acaeadae7bd7803e70782933..6804a997c845c68c92125c4c3f1d264da7be427c 100644 (file)
@@ -135,7 +135,7 @@ func (repos MirrorRepositoryList) LoadAttributes() error {
 
 // SearchRepoOptions holds the search options
 type SearchRepoOptions struct {
-       ListOptions
+       db.ListOptions
        Actor           *User
        Keyword         string
        OwnerID         int64
index a1fd454c106144b3628f877edc35d6c9cd5723a9..3c30cad564d670a984767ff0d8fef98624dcd52a 100644 (file)
@@ -18,7 +18,7 @@ func TestSearchRepository(t *testing.T) {
 
        // test search public repository on explore page
        repos, count, err := SearchRepositoryByName(&SearchRepoOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        Page:     1,
                        PageSize: 10,
                },
@@ -33,7 +33,7 @@ func TestSearchRepository(t *testing.T) {
        assert.Equal(t, int64(1), count)
 
        repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        Page:     1,
                        PageSize: 10,
                },
@@ -47,7 +47,7 @@ func TestSearchRepository(t *testing.T) {
 
        // test search private repository on explore page
        repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        Page:     1,
                        PageSize: 10,
                },
@@ -63,7 +63,7 @@ func TestSearchRepository(t *testing.T) {
        assert.Equal(t, int64(1), count)
 
        repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        Page:     1,
                        PageSize: 10,
                },
@@ -85,7 +85,7 @@ func TestSearchRepository(t *testing.T) {
 
        // Test search within description
        repos, count, err = SearchRepository(&SearchRepoOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        Page:     1,
                        PageSize: 10,
                },
@@ -102,7 +102,7 @@ func TestSearchRepository(t *testing.T) {
 
        // Test NOT search within description
        repos, count, err = SearchRepository(&SearchRepoOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        Page:     1,
                        PageSize: 10,
                },
@@ -122,142 +122,142 @@ func TestSearchRepository(t *testing.T) {
        }{
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
                {
                        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,
                },
        }
index be9309ed4eac15f89126442e35fad9370f010b0f..ae0895df764662d7740354949bfe64ffcbe7d226 100644 (file)
@@ -7,6 +7,7 @@ package models
 import (
        "strings"
 
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/process"
@@ -120,7 +121,7 @@ Loop:
                case always:
                        break Loop
                case pubkey:
-                       keys, err := ListGPGKeys(u.ID, ListOptions{})
+                       keys, err := ListGPGKeys(u.ID, db.ListOptions{})
                        if err != nil {
                                return false, "", nil, err
                        }
@@ -156,7 +157,7 @@ Loop:
                case always:
                        break Loop
                case pubkey:
-                       keys, err := ListGPGKeys(u.ID, ListOptions{})
+                       keys, err := ListGPGKeys(u.ID, db.ListOptions{})
                        if err != nil {
                                return false, "", nil, err
                        }
@@ -209,7 +210,7 @@ Loop:
                case always:
                        break Loop
                case pubkey:
-                       keys, err := ListGPGKeys(u.ID, ListOptions{})
+                       keys, err := ListGPGKeys(u.ID, db.ListOptions{})
                        if err != nil {
                                return false, "", nil, err
                        }
index e3eb756eb405ea1b71d89eefd102434f8eed2703..fe50c1cc04601bc1317c830df3c59a07769a4e86 100644 (file)
@@ -266,7 +266,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) (err e
        }
 
        // Remove redundant collaborators.
-       collaborators, err := repo.getCollaborators(sess, ListOptions{})
+       collaborators, err := repo.getCollaborators(sess, db.ListOptions{})
        if err != nil {
                return fmt.Errorf("getCollaborators: %v", err)
        }
index c35312be604f87444e0cdc4c8e8695ba4070c837..7061119bd85173e0cd21214bfe033ac8f17dfdef 100644 (file)
@@ -8,6 +8,7 @@ import (
        "fmt"
 
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/json"
        "code.gitea.io/gitea/modules/timeutil"
 
@@ -153,7 +154,7 @@ func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int {
 func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
        switch colName {
        case "type":
-               switch UnitType(Cell2Int64(val)) {
+               switch UnitType(login.Cell2Int64(val)) {
                case UnitTypeCode, UnitTypeReleases, UnitTypeWiki, UnitTypeProjects:
                        r.Config = new(UnitConfig)
                case UnitTypeExternalWiki:
index d3720fe857a45f84c061da50147847a93cfc349f..b37d47874e3431f799b8657e0161c908caa2872f 100644 (file)
@@ -165,12 +165,12 @@ func getRepoWatchersIDs(e db.Engine, repoID int64) ([]int64, error) {
 }
 
 // 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).
                Join("LEFT", "watch", "`user`.id=`watch`.user_id").
                And("`watch`.mode<>?", RepoWatchModeDont)
        if opts.Page > 0 {
-               sess = setSessionPagination(sess, &opts)
+               sess = db.SetSessionPagination(sess, &opts)
                users := make([]*User, 0, opts.PageSize)
 
                return users, sess.Find(&users)
index 1a94b8ad3092334ab7c83a69d835a98bb9445666..52222af2ca1438f7a1b780398336cc74ac7c0a20 100644 (file)
@@ -60,7 +60,7 @@ func TestRepository_GetWatchers(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
 
        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.Len(t, watchers, repo.NumWatches)
        for _, watcher := range watchers {
@@ -68,7 +68,7 @@ func TestRepository_GetWatchers(t *testing.T) {
        }
 
        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.Len(t, watchers, 0)
 }
@@ -114,7 +114,7 @@ func TestWatchIfAuto(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
 
        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.Len(t, watchers, repo.NumWatches)
 
@@ -124,13 +124,13 @@ func TestWatchIfAuto(t *testing.T) {
 
        // Must not add watch
        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.Len(t, watchers, prevCount)
 
        // Should not add watch
        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.Len(t, watchers, prevCount)
 
@@ -138,31 +138,31 @@ func TestWatchIfAuto(t *testing.T) {
 
        // Must not add watch
        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.Len(t, watchers, prevCount)
 
        // Should not add watch
        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.Len(t, watchers, prevCount)
 
        // Should add watch
        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.Len(t, watchers, prevCount+1)
 
        // Should remove watch, inhibit from adding auto
        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.Len(t, watchers, prevCount)
 
        // Must not add watch
        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.Len(t, watchers, prevCount)
 }
index 3f81c39b2fc8cce39f409555a46be45c078d8778..5b12e6ffa29e884f7dd25ba1243d3ea384a8af1a 100644 (file)
@@ -172,7 +172,7 @@ func GetReviewByID(id int64) (*Review, error) {
 
 // FindReviewOptions represent possible filters to find reviews
 type FindReviewOptions struct {
-       ListOptions
+       db.ListOptions
        Type         ReviewType
        IssueID      int64
        ReviewerID   int64
@@ -200,7 +200,7 @@ func findReviews(e db.Engine, opts FindReviewOptions) ([]*Review, error) {
        reviews := make([]*Review, 0, 10)
        sess := e.Where(opts.toCond())
        if opts.Page > 0 {
-               sess = setSessionPagination(sess, &opts)
+               sess = db.SetSessionPagination(sess, &opts)
        }
        return reviews, sess.
                Asc("created_unix").
index 41016537eb65f6466de5fc1b55e552372ce59655..c08fb72e7513335df0363948638c3e1f60537b74 100644 (file)
@@ -11,6 +11,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/timeutil"
        "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.
-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)
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
 
                keys := make([]*PublicKey, 0, listOptions.PageSize)
                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
 func PublicKeysAreExternallyManaged(keys []*PublicKey) ([]bool, error) {
-       sources := make([]*LoginSource, 0, 5)
+       sources := make([]*login.Source, 0, 5)
        externals := make([]bool, len(keys))
 keyloop:
        for i, key := range keys {
@@ -264,7 +265,7 @@ keyloop:
                        continue keyloop
                }
 
-               var source *LoginSource
+               var source *login.Source
 
        sourceloop:
                for _, s := range sources {
@@ -276,11 +277,11 @@ keyloop:
 
                if source == nil {
                        var err error
-                       source, err = GetLoginSourceByID(key.LoginSourceID)
+                       source, err = login.GetSourceByID(key.LoginSourceID)
                        if err != nil {
-                               if IsErrLoginSourceNotExist(err) {
+                               if login.IsErrSourceNotExist(err) {
                                        externals[i] = false
-                                       sources[i] = &LoginSource{
+                                       sources[i] = &login.Source{
                                                ID: key.LoginSourceID,
                                        }
                                        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
                        externals[i] = true
                }
@@ -307,14 +308,14 @@ func PublicKeyIsExternallyManaged(id int64) (bool, error) {
        if key.LoginSourceID == 0 {
                return false, nil
        }
-       source, err := GetLoginSourceByID(key.LoginSourceID)
+       source, err := login.GetSourceByID(key.LoginSourceID)
        if err != nil {
-               if IsErrLoginSourceNotExist(err) {
+               if login.IsErrSourceNotExist(err) {
                        return false, nil
                }
                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
                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.
-func AddPublicKeysBySource(usr *User, s *LoginSource, sshPublicKeys []string) bool {
+func AddPublicKeysBySource(usr *User, s *login.Source, sshPublicKeys []string) bool {
        var sshKeysNeedUpdate bool
        for _, sshKey := range sshPublicKeys {
                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.
-func SynchronizePublicKeys(usr *User, s *LoginSource, sshPublicKeys []string) bool {
+func SynchronizePublicKeys(usr *User, s *login.Source, sshPublicKeys []string) bool {
        var sshKeysNeedUpdate bool
 
        log.Trace("synchronizePublicKeys[%s]: Handling Public SSH Key synchronization for user %s", s.Name, usr.Name)
index 3b9a168280740cbae3a7a212b8b3a685c23ff70d..34cf03e9251869041aff0dfc3f1e2c8351e19fa7 100644 (file)
@@ -271,7 +271,7 @@ func deleteDeployKey(sess db.Engine, doer *User, id int64) error {
 
 // ListDeployKeysOptions are options for ListDeployKeys
 type ListDeployKeysOptions struct {
-       ListOptions
+       db.ListOptions
        RepoID      int64
        KeyID       int64
        Fingerprint string
@@ -300,7 +300,7 @@ func listDeployKeys(e db.Engine, opts *ListDeployKeysOptions) ([]*DeployKey, err
        sess := e.Where(opts.toCond())
 
        if opts.Page != 0 {
-               sess = setSessionPagination(sess, opts)
+               sess = db.SetSessionPagination(sess, opts)
 
                keys := make([]*DeployKey, 0, opts.PageSize)
                return keys, sess.Find(&keys)
index 383693e14ed7da73e976545998e2ca83d68adc4c..44b2ee0bb4e60b64c5907617eafaaf2ecd9a08d1 100644 (file)
@@ -112,10 +112,10 @@ func CheckPrincipalKeyString(user *User, content string) (_ string, err error) {
 }
 
 // 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)
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
 
                keys := make([]*PublicKey, 0, listOptions.PageSize)
                return keys, sess.Find(&keys)
index ad583f19985a7d36cf194997249bacd4683ac69d..ee7791513d97167e0dd0ffead7eecffb43e5062b 100644 (file)
@@ -74,11 +74,11 @@ func isStaring(e db.Engine, userID, repoID int64) bool {
 }
 
 // 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).
                Join("LEFT", "star", "`user`.id = star.uid")
        if opts.Page > 0 {
-               sess = setSessionPagination(sess, &opts)
+               sess = db.SetSessionPagination(sess, &opts)
 
                users := make([]*User, 0, opts.PageSize)
                return users, sess.Find(&users)
index c0c0a607be3530b3a43eeacd988b54a2beae3c5b..326da8a861d945331d9aa5f394a591dbcc7d64df 100644 (file)
@@ -34,7 +34,7 @@ func TestRepository_GetStargazers(t *testing.T) {
        // repo with stargazers
        assert.NoError(t, db.PrepareTestDatabase())
        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)
        if assert.Len(t, gazers, 1) {
                assert.Equal(t, int64(2), gazers[0].ID)
@@ -45,7 +45,7 @@ func TestRepository_GetStargazers2(t *testing.T) {
        // repo with stargazers
        assert.NoError(t, db.PrepareTestDatabase())
        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.Len(t, gazers, 0)
 }
index d192a971f59fad89aa4c0ab79c6da5a606cac909..43b1afbc48173fd483ff02c9bbb1639be5fec9aa 100644 (file)
@@ -4,7 +4,10 @@
 
 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
 type Statistic struct {
@@ -52,7 +55,7 @@ func GetStatistic() (stats Statistic) {
        stats.Counter.Follow, _ = db.GetEngine(db.DefaultContext).Count(new(Follow))
        stats.Counter.Mirror, _ = db.GetEngine(db.DefaultContext).Count(new(Mirror))
        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.Milestone, _ = db.GetEngine(db.DefaultContext).Count(new(Milestone))
        stats.Counter.Label, _ = db.GetEngine(db.DefaultContext).Count(new(Label))
index 48ae795424614045ec2c2b76f48420cb58f134ca..07d013ac8ed452b862a6bafdafa4c92bc0554481 100644 (file)
@@ -147,7 +147,7 @@ func AccessTokenByNameExists(token *AccessToken) (bool, error) {
 
 // ListAccessTokensOptions contain filter options
 type ListAccessTokensOptions struct {
-       ListOptions
+       db.ListOptions
        Name   string
        UserID int64
 }
@@ -163,7 +163,7 @@ func ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
        sess = sess.Desc("id")
 
        if opts.Page != 0 {
-               sess = setSessionPagination(sess, &opts)
+               sess = db.SetSessionPagination(sess, &opts)
 
                tokens := make([]*AccessToken, 0, opts.PageSize)
                return tokens, sess.Find(&tokens)
index cf563e9b11c5335ca82c2b7037c9a1aa8c5deb6b..6eb8c67b8dd858ea5625bfc3fced7afc4034570e 100644 (file)
@@ -164,7 +164,7 @@ func removeTopicsFromRepo(e db.Engine, repoID int64) error {
 
 // FindTopicOptions represents the options when fdin topics
 type FindTopicOptions struct {
-       ListOptions
+       db.ListOptions
        RepoID  int64
        Keyword string
 }
@@ -189,7 +189,7 @@ func FindTopics(opts *FindTopicOptions) ([]*Topic, int64, error) {
                sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
        }
        if opts.PageSize != 0 && opts.Page != 0 {
-               sess = setSessionPagination(sess, opts)
+               sess = db.SetSessionPagination(sess, opts)
        }
        topics := make([]*Topic, 0, 10)
        total, err := sess.Desc("topic.repo_count").FindAndCount(&topics)
index 9f6352e7e753524ce76a6d8580f0fb47077e979a..b069deaba3c20a8e187b65c7a42b5f86d6e02a86 100644 (file)
@@ -23,7 +23,7 @@ func TestAddTopic(t *testing.T) {
        assert.Len(t, topics, totalNrOfTopics)
 
        topics, total, err := FindTopics(&FindTopicOptions{
-               ListOptions: ListOptions{Page: 1, PageSize: 2},
+               ListOptions: db.ListOptions{Page: 1, PageSize: 2},
        })
        assert.NoError(t, err)
        assert.Len(t, topics, 2)
index fc5d417d360901e453017e721e1dc8ee9bfcd7c8..fb40a0acc8f2ebd805402764fef1ed5cbb0bdc93 100644 (file)
@@ -21,6 +21,7 @@ import (
        "unicode/utf8"
 
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
@@ -106,7 +107,7 @@ type User struct {
        // is to change his/her password after registration.
        MustChangePassword bool `xorm:"NOT NULL DEFAULT false"`
 
-       LoginType   LoginType
+       LoginType   login.Type
        LoginSource int64 `xorm:"NOT NULL DEFAULT 0"`
        LoginName   string
        Type        UserType
@@ -169,7 +170,7 @@ func init() {
 
 // SearchOrganizationsOptions options to filter organizations
 type SearchOrganizationsOptions struct {
-       ListOptions
+       db.ListOptions
        All bool
 }
 
@@ -241,12 +242,12 @@ func GetAllUsers() ([]*User, error) {
 
 // IsLocal returns true if user login type is LoginPlain.
 func (u *User) IsLocal() bool {
-       return u.LoginType <= LoginPlain
+       return u.LoginType <= login.Plain
 }
 
 // IsOAuth2 returns true if user login type is LoginOAuth2.
 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.
@@ -331,13 +332,13 @@ func (u *User) GenerateEmailActivateCode(email string) string {
 }
 
 // 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).
                Where("follow.follow_id=?", u.ID).
                Join("LEFT", "follow", "`user`.id=follow.user_id")
 
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
 
                users := make([]*User, 0, listOptions.PageSize)
                return users, sess.Find(&users)
@@ -353,13 +354,13 @@ func (u *User) IsFollowing(followID int64) bool {
 }
 
 // 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).
                Where("follow.user_id=?", u.ID).
                Join("LEFT", "follow", "`user`.id=follow.follow_id")
 
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
 
                users := make([]*User, 0, listOptions.PageSize)
                return users, sess.Find(&users)
@@ -542,7 +543,7 @@ func (u *User) GetOrganizationCount() (int64, error) {
 }
 
 // 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})
        return err
 }
@@ -1252,7 +1253,7 @@ func deleteUser(e db.Engine, u *User) error {
        // ***** END: PublicKey *****
 
        // ***** START: GPGPublicKey *****
-       keys, err := listGPGKeys(e, u.ID, ListOptions{})
+       keys, err := listGPGKeys(e, u.ID, db.ListOptions{})
        if err != nil {
                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
-func GetUsersBySource(s *LoginSource) ([]*User, error) {
+func GetUsersBySource(s *login.Source) ([]*User, error) {
        var users []*User
        err := db.GetEngine(db.DefaultContext).Where("login_type = ? AND login_source = ?", s.Type, s.ID).Find(&users)
        return users, err
@@ -1592,7 +1593,7 @@ func GetUser(user *User) (bool, error) {
 
 // SearchUserOptions contains the options for searching
 type SearchUserOptions struct {
-       ListOptions
+       db.ListOptions
        Keyword       string
        Type          UserType
        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())
        if opts.Page != 0 {
-               sess = setSessionPagination(sess, opts)
+               sess = db.SetSessionPagination(sess, opts)
        }
 
        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
-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).
                Join("LEFT", "star", "`repository`.id=`star`.repo_id")
        if !private {
@@ -1691,7 +1692,7 @@ func GetStarredRepos(userID int64, private bool, listOptions ListOptions) ([]*Re
        }
 
        if listOptions.Page != 0 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
 
                repos := make([]*Repository, 0, listOptions.PageSize)
                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
-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).
                And("`watch`.mode<>?", RepoWatchModeDont).
                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 {
-               sess = setSessionPagination(sess, &listOptions)
+               sess = db.SetSessionPagination(sess, &listOptions)
 
                repos := make([]*Repository, 0, listOptions.PageSize)
                total, err := sess.FindAndCount(&repos)
index 51d34d26826a0a0adae1a31a1d0f43912f00f52d..caa931788d5bb2827a9f050d6390c7b337150f81 100644 (file)
@@ -301,7 +301,7 @@ const (
 
 // SearchEmailOptions are options to search e-mail addresses for the admin panel
 type SearchEmailOptions struct {
-       ListOptions
+       db.ListOptions
        Keyword     string
        SortType    SearchEmailOrderBy
        IsPrimary   util.OptionalBool
@@ -357,7 +357,7 @@ func SearchEmails(opts *SearchEmailOptions) ([]*SearchEmailResult, int64, error)
                orderby = SearchEmailOrderByEmail.String()
        }
 
-       opts.setDefaultValues()
+       opts.SetDefaultValues()
 
        emails := make([]*SearchEmailResult, 0, opts.PageSize)
        err = db.GetEngine(db.DefaultContext).Table("email_address").
index 22e5f786bf911a95fa520180c38ff7a3df239180..384f28b7bf4edd20112aa2ba78c0ceb2bd89611d 100644 (file)
@@ -189,7 +189,7 @@ func TestListEmails(t *testing.T) {
 
        // Must find all users and their emails
        opts := &SearchEmailOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        PageSize: 10000,
                },
        }
@@ -241,7 +241,7 @@ func TestListEmails(t *testing.T) {
 
        // Must find more than one page, but retrieve only one
        opts = &SearchEmailOptions{
-               ListOptions: ListOptions{
+               ListOptions: db.ListOptions{
                        PageSize: 5,
                        Page:     1,
                },
index 6c616a60a902aab631c42b78c996629e80351380..bf796a8c6252f23850ffca1c1c9ff9e7cf65182b 100644 (file)
@@ -11,6 +11,7 @@ import (
        "testing"
 
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/setting"
        "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/util"
@@ -18,6 +19,14 @@ import (
        "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) {
        assert.NoError(t, db.PrepareTestDatabase())
 
@@ -116,19 +125,19 @@ func TestSearchUsers(t *testing.T) {
                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})
 
-       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})
 
-       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})
 
-       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})
 
-       testOrgSuccess(&SearchUserOptions{ListOptions: ListOptions{Page: 5, PageSize: 2}},
+       testOrgSuccess(&SearchUserOptions{ListOptions: db.ListOptions{Page: 5, PageSize: 2}},
                []int64{})
 
        // test users
@@ -137,20 +146,20 @@ func TestSearchUsers(t *testing.T) {
                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})
 
-       testUserSuccess(&SearchUserOptions{ListOptions: ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse},
+       testUserSuccess(&SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse},
                []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})
 
-       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})
 
        // 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})
 }
 
@@ -407,7 +416,7 @@ func TestAddLdapSSHPublicKeys(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
 
        user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
-       s := &LoginSource{ID: 1}
+       s := &login.Source{ID: 1}
 
        testCases := []struct {
                keyString   string
index 034b37263aadac267603dbebaa71181df0c49635..9d04f8f5e4fb44b761c481272158ea46a64360dd 100644 (file)
@@ -397,7 +397,7 @@ func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
 
 // ListWebhookOptions are options to filter webhooks on ListWebhooksByOpts
 type ListWebhookOptions struct {
-       ListOptions
+       db.ListOptions
        RepoID   int64
        OrgID    int64
        IsActive util.OptionalBool
@@ -421,7 +421,7 @@ func listWebhooksByOpts(e db.Engine, opts *ListWebhookOptions) ([]*Webhook, erro
        sess := e.Where(opts.toCond())
 
        if opts.Page != 0 {
-               sess = setSessionPagination(sess, opts)
+               sess = db.SetSessionPagination(sess, opts)
                webhooks := make([]*Webhook, 0, opts.PageSize)
                err := sess.Find(&webhooks)
                return webhooks, err
index 404786ec9c9ad290cc36044b585d04b4a6cfdc38..3c309a82f861dde5fa71866850f4cd7aa54ba00c 100644 (file)
@@ -12,6 +12,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
        "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
-func ToOAuth2Application(app *models.OAuth2Application) *api.OAuth2Application {
+// ToOAuth2Application convert from login.OAuth2Application to api.OAuth2Application
+func ToOAuth2Application(app *login.OAuth2Application) *api.OAuth2Application {
        return &api.OAuth2Application{
                ID:           app.ID,
                Name:         app.Name,
index ec47f0ad84aca2e6e05c2c68dddef5ce08e9d79c..86bd8cb237bf82022ca767d216b9de94728f1302 100644 (file)
@@ -10,6 +10,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
 )
@@ -114,7 +115,7 @@ func (graph *Graph) LoadAndProcessCommits(repository *models.Repository, gitRepo
 
                _ = 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 {
                        log.Error("GetLatestCommitStatus: %v", err)
                } else {
index 676b6686ea5b2aa714dbaab97b7ebccaea267ee1..4e133b4dd39310095956e70d9069a01760de3480 100644 (file)
@@ -12,6 +12,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/graceful"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/queue"
@@ -241,7 +242,7 @@ func populateIssueIndexer(ctx context.Context) {
                default:
                }
                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,
                        Private:     true,
                        Collaborate: util.OptionalBoolFalse,
index 73293f1f85400e12f4b62d1cca5694efc09abb2b..b8b947961f4b70e83964c95c5d59bfcd049c57e1 100644 (file)
@@ -69,12 +69,12 @@ func TestGiteaUploadRepo(t *testing.T) {
        assert.NoError(t, err)
        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.Len(t, labels, 12)
 
        releases, err := models.GetReleasesByRepoID(repo.ID, models.FindReleasesOptions{
-               ListOptions: models.ListOptions{
+               ListOptions: db.ListOptions{
                        PageSize: 10,
                        Page:     0,
                },
@@ -84,7 +84,7 @@ func TestGiteaUploadRepo(t *testing.T) {
        assert.Len(t, releases, 8)
 
        releases, err = models.GetReleasesByRepoID(repo.ID, models.FindReleasesOptions{
-               ListOptions: models.ListOptions{
+               ListOptions: db.ListOptions{
                        PageSize: 10,
                        Page:     0,
                },
index daefee9c74603216ce2d3ddf42f6bc4d5243d83c..c5c059f4718b4d045207b879100e82961187e844 100644 (file)
@@ -122,7 +122,7 @@ func DeleteUnadoptedRepository(doer, u *models.User, repoName string) error {
 }
 
 // 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("*")
        globRepo, _ := glob.Compile("*")
 
@@ -165,10 +165,13 @@ func ListUnadoptedRepositories(query string, opts *models.ListOptions) ([]string
 
                        // Clean up old repoNamesToCheck
                        if len(repoNamesToCheck) > 0 {
-                               repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: models.ListOptions{
-                                       Page:     1,
-                                       PageSize: opts.PageSize,
-                               }, LowerNames: repoNamesToCheck})
+                               repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
+                                       Actor:   ctxUser,
+                                       Private: true,
+                                       ListOptions: db.ListOptions{
+                                               Page:     1,
+                                               PageSize: opts.PageSize,
+                                       }, LowerNames: repoNamesToCheck})
                                if err != nil {
                                        return err
                                }
@@ -219,10 +222,13 @@ func ListUnadoptedRepositories(query string, opts *models.ListOptions) ([]string
                if count < end {
                        repoNamesToCheck = append(repoNamesToCheck, name)
                        if len(repoNamesToCheck) >= opts.PageSize {
-                               repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: models.ListOptions{
-                                       Page:     1,
-                                       PageSize: opts.PageSize,
-                               }, LowerNames: repoNamesToCheck})
+                               repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
+                                       Actor:   ctxUser,
+                                       Private: true,
+                                       ListOptions: db.ListOptions{
+                                               Page:     1,
+                                               PageSize: opts.PageSize,
+                                       }, LowerNames: repoNamesToCheck})
                                if err != nil {
                                        return err
                                }
@@ -254,10 +260,13 @@ func ListUnadoptedRepositories(query string, opts *models.ListOptions) ([]string
        }
 
        if len(repoNamesToCheck) > 0 {
-               repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{Actor: ctxUser, Private: true, ListOptions: models.ListOptions{
-                       Page:     1,
-                       PageSize: opts.PageSize,
-               }, LowerNames: repoNamesToCheck})
+               repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
+                       Actor:   ctxUser,
+                       Private: true,
+                       ListOptions: db.ListOptions{
+                               Page:     1,
+                               PageSize: opts.PageSize,
+                       }, LowerNames: repoNamesToCheck})
                if err != nil {
                        return nil, 0, err
                }
index ee970fd711ee2159e4de691e8b58512602342ae6..6b40a894fb8b2731b879bea807e568c1b7eeb95b 100644 (file)
@@ -224,7 +224,11 @@ func CleanUpMigrateInfo(repo *models.Repository) (*models.Repository, error) {
 // SyncReleasesWithTags synchronizes release table with repository tags
 func SyncReleasesWithTags(repo *models.Repository, gitRepo *git.Repository) error {
        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++ {
                opts.Page = page
                rels, err := models.GetReleasesByRepoID(repo.ID, opts)
index e5a75da759ea211873df7ef09b0ec614748e18fc..2d585b60401ba26eaf9e06e4edab2ff25f3ff0ec 100644 (file)
@@ -11,6 +11,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
        "code.gitea.io/gitea/modules/log"
@@ -27,12 +28,12 @@ func parseLoginSource(ctx *context.APIContext, u *models.User, sourceID int64, l
                return
        }
 
-       source, err := models.GetLoginSourceByID(sourceID)
+       source, err := login.GetSourceByID(sourceID)
        if err != nil {
-               if models.IsErrLoginSourceNotExist(err) {
+               if login.IsErrSourceNotExist(err) {
                        ctx.Error(http.StatusUnprocessableEntity, "", err)
                } else {
-                       ctx.Error(http.StatusInternalServerError, "GetLoginSourceByID", err)
+                       ctx.Error(http.StatusInternalServerError, "login.GetSourceByID", err)
                }
                return
        }
@@ -74,7 +75,7 @@ func CreateUser(ctx *context.APIContext) {
                Passwd:             form.Password,
                MustChangePassword: true,
                IsActive:           true,
-               LoginType:          models.LoginPlain,
+               LoginType:          login.Plain,
        }
        if form.MustChangePassword != nil {
                u.MustChangePassword = *form.MustChangePassword
index c576d18e5fedb663c0631d3301ded7f9668e38c7..7b97a5683a36bbcd8ff029121b9db8349f36ccde 100644 (file)
@@ -13,6 +13,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
        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.
        if len(keyword) == 0 || len(issueIDs) > 0 || len(includedLabelNames) > 0 || len(includedMilestones) > 0 {
                issuesOpt := &models.IssuesOptions{
-                       ListOptions: models.ListOptions{
+                       ListOptions: db.ListOptions{
                                Page:     ctx.FormInt("page"),
                                PageSize: limit,
                        },
@@ -261,7 +262,7 @@ func SearchIssues(ctx *context.APIContext) {
                        return
                }
 
-               issuesOpt.ListOptions = models.ListOptions{
+               issuesOpt.ListOptions = db.ListOptions{
                        Page: -1,
                }
                if filteredCount, err = models.CountIssues(issuesOpt); err != nil {
@@ -470,7 +471,7 @@ func ListIssues(ctx *context.APIContext) {
                        return
                }
 
-               issuesOpt.ListOptions = models.ListOptions{
+               issuesOpt.ListOptions = db.ListOptions{
                        Page: -1,
                }
                if filteredCount, err = models.CountIssues(issuesOpt); err != nil {
index f0f7cb4159b65d09b596b27cb3843ff73bc9d8cb..bf45bf4dd5a65311a28b15ea1bc9596c336afc66 100644 (file)
@@ -12,6 +12,7 @@ import (
        "strconv"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
        api "code.gitea.io/gitea/modules/structs"
@@ -212,7 +213,7 @@ func CreateOauth2Application(ctx *context.APIContext) {
 
        data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions)
 
-       app, err := models.CreateOAuth2Application(models.CreateOAuth2ApplicationOptions{
+       app, err := login.CreateOAuth2Application(login.CreateOAuth2ApplicationOptions{
                Name:         data.Name,
                UserID:       ctx.User.ID,
                RedirectURIs: data.RedirectURIs,
@@ -251,7 +252,7 @@ func ListOauth2Applications(ctx *context.APIContext) {
        //   "200":
        //     "$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 {
                ctx.Error(http.StatusInternalServerError, "ListOAuth2Applications", err)
                return
@@ -287,8 +288,8 @@ func DeleteOauth2Application(ctx *context.APIContext) {
        //   "404":
        //     "$ref": "#/responses/notFound"
        appID := ctx.ParamsInt64(":id")
-       if err := models.DeleteOAuth2Application(appID, ctx.User.ID); err != nil {
-               if models.IsErrOAuthApplicationNotFound(err) {
+       if err := login.DeleteOAuth2Application(appID, ctx.User.ID); err != nil {
+               if login.IsErrOAuthApplicationNotFound(err) {
                        ctx.NotFound()
                } else {
                        ctx.Error(http.StatusInternalServerError, "DeleteOauth2ApplicationByID", err)
@@ -319,9 +320,9 @@ func GetOauth2Application(ctx *context.APIContext) {
        //   "404":
        //     "$ref": "#/responses/notFound"
        appID := ctx.ParamsInt64(":id")
-       app, err := models.GetOAuth2ApplicationByID(appID)
+       app, err := login.GetOAuth2ApplicationByID(appID)
        if err != nil {
-               if models.IsErrOauthClientIDInvalid(err) || models.IsErrOAuthApplicationNotFound(err) {
+               if login.IsErrOauthClientIDInvalid(err) || login.IsErrOAuthApplicationNotFound(err) {
                        ctx.NotFound()
                } else {
                        ctx.Error(http.StatusInternalServerError, "GetOauth2ApplicationByID", err)
@@ -362,14 +363,14 @@ func UpdateOauth2Application(ctx *context.APIContext) {
 
        data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions)
 
-       app, err := models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{
+       app, err := login.UpdateOAuth2Application(login.UpdateOAuth2ApplicationOptions{
                Name:         data.Name,
                UserID:       ctx.User.ID,
                ID:           appID,
                RedirectURIs: data.RedirectURIs,
        })
        if err != nil {
-               if models.IsErrOauthClientIDInvalid(err) || models.IsErrOAuthApplicationNotFound(err) {
+               if login.IsErrOauthClientIDInvalid(err) || login.IsErrOAuthApplicationNotFound(err) {
                        ctx.NotFound()
                } else {
                        ctx.Error(http.StatusInternalServerError, "UpdateOauth2ApplicationByID", err)
index f32d60d03816a0552cff044b5507abd540f41c61..9066268bba293204a70234dd894f7d7bd19adcac 100644 (file)
@@ -9,6 +9,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
        api "code.gitea.io/gitea/modules/structs"
@@ -16,7 +17,7 @@ import (
        "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)
        if err != nil {
                ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
index 8ee167685639c480c4afaaf8cab9871821aa5d07..f067722bfa821cd75215fd9e39a29b5b1d7b2604 100644 (file)
@@ -9,6 +9,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
        api "code.gitea.io/gitea/modules/structs"
@@ -17,7 +18,7 @@ import (
 
 // getStarredRepos returns the repos that the user with the specified userID has
 // 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)
        if err != nil {
                return nil, err
index f32ce7359864a2b9f5e538e7daa2b8357f05e2eb..3c6f8b30704f385a8cc00e49702e6f3e18699e5d 100644 (file)
@@ -8,6 +8,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
        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
-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)
        if err != nil {
                return nil, 0, err
index 81f5086c9611c88e4670afc3c1dc686b3b154029..75648571157154739fd5540e399c7c8f938140a5 100644 (file)
@@ -9,7 +9,7 @@ import (
        "strings"
        "time"
 
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/context"
        "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
-func GetListOptions(ctx *context.APIContext) models.ListOptions {
-       return models.ListOptions{
+func GetListOptions(ctx *context.APIContext) db.ListOptions {
+       return db.ListOptions{
                Page:     ctx.FormInt("page"),
                PageSize: convert.ToCorrectPageSize(ctx.FormInt("limit")),
        }
index 2937190a1f5010d4498e3a9a3518ac59ab973a0e..1b005e5c7bcee8c92d91d9110de9aed727ef9b90 100644 (file)
@@ -11,6 +11,7 @@ import (
        "regexp"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/auth/pam"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
@@ -18,6 +19,7 @@ import (
        "code.gitea.io/gitea/modules/setting"
        "code.gitea.io/gitea/modules/util"
        "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/oauth2"
        pamService "code.gitea.io/gitea/services/auth/source/pam"
@@ -46,13 +48,13 @@ func Authentications(ctx *context.Context) {
        ctx.Data["PageIsAdminAuthentications"] = true
 
        var err error
-       ctx.Data["Sources"], err = models.LoginSources()
+       ctx.Data["Sources"], err = login.Sources()
        if err != nil {
-               ctx.ServerError("LoginSources", err)
+               ctx.ServerError("login.Sources", err)
                return
        }
 
-       ctx.Data["Total"] = models.CountLoginSources()
+       ctx.Data["Total"] = login.CountSources()
        ctx.HTML(http.StatusOK, tplAuths)
 }
 
@@ -64,14 +66,14 @@ type dropdownItem struct {
 var (
        authSources = func() []dropdownItem {
                items := []dropdownItem{
-                       {models.LoginNames[models.LoginLDAP], models.LoginLDAP},
-                       {models.LoginNames[models.LoginDLDAP], models.LoginDLDAP},
-                       {models.LoginNames[models.LoginSMTP], models.LoginSMTP},
-                       {models.LoginNames[models.LoginOAuth2], models.LoginOAuth2},
-                       {models.LoginNames[models.LoginSSPI], models.LoginSSPI},
+                       {login.LDAP.String(), login.LDAP},
+                       {login.DLDAP.String(), login.DLDAP},
+                       {login.SMTP.String(), login.SMTP},
+                       {login.OAuth2.String(), login.OAuth2},
+                       {login.SSPI.String(), login.SSPI},
                }
                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
        }()
@@ -89,8 +91,8 @@ func NewAuthSource(ctx *context.Context) {
        ctx.Data["PageIsAdmin"] = true
        ctx.Data["PageIsAdminAuthentications"] = true
 
-       ctx.Data["type"] = models.LoginLDAP
-       ctx.Data["CurrentTypeName"] = models.LoginNames[models.LoginLDAP]
+       ctx.Data["type"] = login.LDAP
+       ctx.Data["CurrentTypeName"] = login.Names[login.LDAP]
        ctx.Data["CurrentSecurityProtocol"] = ldap.SecurityProtocolNames[ldap.SecurityProtocolUnencrypted]
        ctx.Data["smtp_auth"] = "PLAIN"
        ctx.Data["is_active"] = true
@@ -217,7 +219,7 @@ func NewAuthSourcePost(ctx *context.Context) {
        ctx.Data["PageIsAdmin"] = 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["AuthSources"] = authSources
        ctx.Data["SecurityProtocols"] = securityProtocols
@@ -233,28 +235,28 @@ func NewAuthSourcePost(ctx *context.Context) {
 
        hasTLS := false
        var config convert.Conversion
-       switch models.LoginType(form.Type) {
-       case models.LoginLDAP, models.LoginDLDAP:
+       switch login.Type(form.Type) {
+       case login.LDAP, login.DLDAP:
                config = parseLDAPConfig(form)
                hasTLS = ldap.SecurityProtocol(form.SecurityProtocol) > ldap.SecurityProtocolUnencrypted
-       case models.LoginSMTP:
+       case login.SMTP:
                config = parseSMTPConfig(form)
                hasTLS = true
-       case models.LoginPAM:
+       case login.PAM:
                config = &pamService.Source{
                        ServiceName: form.PAMServiceName,
                        EmailDomain: form.PAMEmailDomain,
                }
-       case models.LoginOAuth2:
+       case login.OAuth2:
                config = parseOAuth2Config(form)
-       case models.LoginSSPI:
+       case login.SSPI:
                var err error
                config, err = parseSSPIConfig(ctx, form)
                if err != nil {
                        ctx.RenderWithErr(err.Error(), tplAuthNew, form)
                        return
                }
-               existing, err := models.LoginSourcesByType(models.LoginSSPI)
+               existing, err := login.SourcesByType(login.SSPI)
                if err != nil || len(existing) > 0 {
                        ctx.Data["Err_Type"] = true
                        ctx.RenderWithErr(ctx.Tr("admin.auths.login_source_of_type_exist"), tplAuthNew, form)
@@ -271,18 +273,18 @@ func NewAuthSourcePost(ctx *context.Context) {
                return
        }
 
-       if err := models.CreateLoginSource(&models.LoginSource{
-               Type:          models.LoginType(form.Type),
+       if err := login.CreateSource(&login.Source{
+               Type:          login.Type(form.Type),
                Name:          form.Name,
                IsActive:      form.IsActive,
                IsSyncEnabled: form.IsSyncEnabled,
                Cfg:           config,
        }); err != nil {
-               if models.IsErrLoginSourceAlreadyExist(err) {
+               if login.IsErrSourceAlreadyExist(err) {
                        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 {
-                       ctx.ServerError("CreateSource", err)
+                       ctx.ServerError("login.CreateSource", err)
                }
                return
        }
@@ -304,9 +306,9 @@ func EditAuthSource(ctx *context.Context) {
        oauth2providers := oauth2.GetOAuth2Providers()
        ctx.Data["OAuth2Providers"] = oauth2providers
 
-       source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
+       source, err := login.GetSourceByID(ctx.ParamsInt64(":authid"))
        if err != nil {
-               ctx.ServerError("GetLoginSourceByID", err)
+               ctx.ServerError("login.GetSourceByID", err)
                return
        }
        ctx.Data["Source"] = source
@@ -339,9 +341,9 @@ func EditAuthSourcePost(ctx *context.Context) {
        oauth2providers := oauth2.GetOAuth2Providers()
        ctx.Data["OAuth2Providers"] = oauth2providers
 
-       source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
+       source, err := login.GetSourceByID(ctx.ParamsInt64(":authid"))
        if err != nil {
-               ctx.ServerError("GetLoginSourceByID", err)
+               ctx.ServerError("login.GetSourceByID", err)
                return
        }
        ctx.Data["Source"] = source
@@ -353,19 +355,19 @@ func EditAuthSourcePost(ctx *context.Context) {
        }
 
        var config convert.Conversion
-       switch models.LoginType(form.Type) {
-       case models.LoginLDAP, models.LoginDLDAP:
+       switch login.Type(form.Type) {
+       case login.LDAP, login.DLDAP:
                config = parseLDAPConfig(form)
-       case models.LoginSMTP:
+       case login.SMTP:
                config = parseSMTPConfig(form)
-       case models.LoginPAM:
+       case login.PAM:
                config = &pamService.Source{
                        ServiceName: form.PAMServiceName,
                        EmailDomain: form.PAMEmailDomain,
                }
-       case models.LoginOAuth2:
+       case login.OAuth2:
                config = parseOAuth2Config(form)
-       case models.LoginSSPI:
+       case login.SSPI:
                config, err = parseSSPIConfig(ctx, form)
                if err != nil {
                        ctx.RenderWithErr(err.Error(), tplAuthEdit, form)
@@ -380,7 +382,7 @@ func EditAuthSourcePost(ctx *context.Context) {
        source.IsActive = form.IsActive
        source.IsSyncEnabled = form.IsSyncEnabled
        source.Cfg = config
-       if err := models.UpdateSource(source); err != nil {
+       if err := login.UpdateSource(source); err != nil {
                if models.IsErrOpenIDConnectInitialize(err) {
                        ctx.Flash.Error(err.Error(), true)
                        ctx.HTML(http.StatusOK, tplAuthEdit)
@@ -397,17 +399,17 @@ func EditAuthSourcePost(ctx *context.Context) {
 
 // DeleteAuthSource response for deleting an auth source
 func DeleteAuthSource(ctx *context.Context) {
-       source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
+       source, err := login.GetSourceByID(ctx.ParamsInt64(":authid"))
        if err != nil {
-               ctx.ServerError("GetLoginSourceByID", err)
+               ctx.ServerError("login.GetSourceByID", err)
                return
        }
 
-       if err = models.DeleteSource(source); err != nil {
-               if models.IsErrLoginSourceInUse(err) {
+       if err = auth_service.DeleteLoginSource(source); err != nil {
+               if login.IsErrSourceInUse(err) {
                        ctx.Flash.Error(ctx.Tr("admin.auths.still_in_used"))
                } else {
-                       ctx.Flash.Error(fmt.Sprintf("DeleteSource: %v", err))
+                       ctx.Flash.Error(fmt.Sprintf("DeleteLoginSource: %v", err))
                }
                ctx.JSON(http.StatusOK, map[string]interface{}{
                        "redirect": setting.AppSubURL + "/admin/auths/" + ctx.Params(":authid"),
index 017d696e202de43616030464d191eecc64527591..5cbe70020b703683cf168421cd9b43a08f7f4a69 100644 (file)
@@ -10,6 +10,7 @@ import (
        "net/url"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/log"
@@ -28,7 +29,7 @@ func Emails(ctx *context.Context) {
        ctx.Data["PageIsAdminEmails"] = true
 
        opts := &models.SearchEmailOptions{
-               ListOptions: models.ListOptions{
+               ListOptions: db.ListOptions{
                        PageSize: setting.UI.Admin.UserPagingNum,
                        Page:     ctx.FormInt("page"),
                },
index a2b3ed1bcc0fe6038707cd0832b7530e5fb8ce53..df3118b60f22ceeb880c9b83c82434caa6b2ea08 100644 (file)
@@ -7,6 +7,7 @@ package admin
 
 import (
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/setting"
@@ -27,7 +28,7 @@ func Organizations(ctx *context.Context) {
        explore.RenderUserSearch(ctx, &models.SearchUserOptions{
                Actor: ctx.User,
                Type:  models.UserTypeOrganization,
-               ListOptions: models.ListOptions{
+               ListOptions: db.ListOptions{
                        PageSize: setting.UI.Admin.OrgPagingNum,
                },
                Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
index 4c3f2ad614ee8fd93c588a8b424023fd927bfc2d..2f4d182af8fe4d856479b5b5374708efeb21a421 100644 (file)
@@ -10,6 +10,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/log"
@@ -68,7 +69,7 @@ func UnadoptedRepos(ctx *context.Context) {
        ctx.Data["PageIsAdmin"] = true
        ctx.Data["PageIsAdminRepositories"] = true
 
-       opts := models.ListOptions{
+       opts := db.ListOptions{
                PageSize: setting.UI.Admin.UserPagingNum,
                Page:     ctx.FormInt("page"),
        }
index acccc516bb4566922d0dfa92d149b3f84f7839c5..2556cae3a87a2bae831d4db7ff4efa28867f42ed 100644 (file)
@@ -12,6 +12,8 @@ import (
        "strings"
 
        "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/context"
        "code.gitea.io/gitea/modules/log"
@@ -39,7 +41,7 @@ func Users(ctx *context.Context) {
        explore.RenderUserSearch(ctx, &models.SearchUserOptions{
                Actor: ctx.User,
                Type:  models.UserTypeIndividual,
-               ListOptions: models.ListOptions{
+               ListOptions: db.ListOptions{
                        PageSize: setting.UI.Admin.UserPagingNum,
                },
                SearchByEmail: true,
@@ -56,9 +58,9 @@ func NewUser(ctx *context.Context) {
 
        ctx.Data["login_type"] = "0-0"
 
-       sources, err := models.LoginSources()
+       sources, err := login.Sources()
        if err != nil {
-               ctx.ServerError("LoginSources", err)
+               ctx.ServerError("login.Sources", err)
                return
        }
        ctx.Data["Sources"] = sources
@@ -75,9 +77,9 @@ func NewUserPost(ctx *context.Context) {
        ctx.Data["PageIsAdminUsers"] = true
        ctx.Data["DefaultUserVisibilityMode"] = setting.Service.DefaultUserVisibilityMode
 
-       sources, err := models.LoginSources()
+       sources, err := login.Sources()
        if err != nil {
-               ctx.ServerError("LoginSources", err)
+               ctx.ServerError("login.Sources", err)
                return
        }
        ctx.Data["Sources"] = sources
@@ -94,19 +96,19 @@ func NewUserPost(ctx *context.Context) {
                Email:     form.Email,
                Passwd:    form.Password,
                IsActive:  true,
-               LoginType: models.LoginPlain,
+               LoginType: login.Plain,
        }
 
        if len(form.LoginType) > 0 {
                fields := strings.Split(form.LoginType, "-")
                if len(fields) == 2 {
                        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.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 {
                        ctx.Data["Err_Password"] = true
                        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
 
        if u.LoginSource > 0 {
-               ctx.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource)
+               ctx.Data["LoginSource"], err = login.GetSourceByID(u.LoginSource)
                if err != nil {
-                       ctx.ServerError("GetLoginSourceByID", err)
+                       ctx.ServerError("login.GetSourceByID", err)
                        return nil
                }
        } else {
-               ctx.Data["LoginSource"] = &models.LoginSource{}
+               ctx.Data["LoginSource"] = &login.Source{}
        }
 
-       sources, err := models.LoginSources()
+       sources, err := login.Sources()
        if err != nil {
-               ctx.ServerError("LoginSources", err)
+               ctx.ServerError("login.Sources", err)
                return nil
        }
        ctx.Data["Sources"] = sources
@@ -247,7 +249,7 @@ func EditUserPost(ctx *context.Context) {
 
                if u.LoginSource != loginSource {
                        u.LoginSource = loginSource
-                       u.LoginType = models.LoginType(loginType)
+                       u.LoginType = login.Type(loginType)
                }
        }
 
index a630d9c224e8eca5ee8bda4bd9987e4af109e49e..974aa755d16ced87a2d05530e85cec2aba2ac87b 100644 (file)
@@ -9,6 +9,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
        "code.gitea.io/gitea/modules/eventsource"
@@ -93,7 +94,7 @@ loop:
                        go unregister()
                        break loop
                case <-stopwatchTimer.C:
-                       sws, err := models.GetUserStopwatches(ctx.User.ID, models.ListOptions{})
+                       sws, err := models.GetUserStopwatches(ctx.User.ID, db.ListOptions{})
                        if err != nil {
                                log.Error("Unable to GetUserStopwatches: %v", err)
                                continue
index 470e0eb8530b10a166de82a7de3aab6cd96abd9a..d005cfa50322da0890b71dde379d0d85ef290541 100644 (file)
@@ -6,6 +6,7 @@ package explore
 
 import (
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/setting"
@@ -33,7 +34,7 @@ func Organizations(ctx *context.Context) {
        RenderUserSearch(ctx, &models.SearchUserOptions{
                Actor:       ctx.User,
                Type:        models.UserTypeOrganization,
-               ListOptions: models.ListOptions{PageSize: setting.UI.ExplorePagingNum},
+               ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum},
                Visible:     visibleTypes,
        }, tplExploreOrganizations)
 }
index dfc6261b33ed1e55e8c2ecb5fda20d5d483df65f..78035037e510550cdd152117a314b1af9224edfb 100644 (file)
@@ -8,6 +8,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/setting"
@@ -77,7 +78,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
        ctx.Data["TopicOnly"] = topicOnly
 
        repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
-               ListOptions: models.ListOptions{
+               ListOptions: db.ListOptions{
                        Page:     page,
                        PageSize: opts.PageSize,
                },
index aeaaf92c12216b37c8aa6a743ed080b62d5aa5ab..4ddb90132d165abe4a36b654f9dd1ec53b0a199c 100644 (file)
@@ -9,6 +9,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/setting"
@@ -99,7 +100,7 @@ func Users(ctx *context.Context) {
        RenderUserSearch(ctx, &models.SearchUserOptions{
                Actor:       ctx.User,
                Type:        models.UserTypeIndividual,
-               ListOptions: models.ListOptions{PageSize: setting.UI.ExplorePagingNum},
+               ListOptions: db.ListOptions{PageSize: setting.UI.ExplorePagingNum},
                IsActive:    util.OptionalBoolTrue,
                Visible:     []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate},
        }, tplExploreUsers)
index f682dc5cb697f5fa8d2ec6b886aa36ad648c56f1..89bd12a18f78f0759956938d5a7a24b4185803c1 100644 (file)
@@ -8,6 +8,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/markup"
@@ -91,7 +92,7 @@ func Home(ctx *context.Context) {
                err   error
        )
        repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
-               ListOptions: models.ListOptions{
+               ListOptions: db.ListOptions{
                        PageSize: setting.UI.User.RepoPagingNum,
                        Page:     page,
                },
@@ -110,7 +111,7 @@ func Home(ctx *context.Context) {
        var opts = &models.FindOrgMembersOpts{
                OrgID:       org.ID,
                PublicOnly:  true,
-               ListOptions: models.ListOptions{Page: 1, PageSize: 25},
+               ListOptions: db.ListOptions{Page: 1, PageSize: 25},
        }
 
        if ctx.User != nil {
index 13728a31b30a0456dda0c9ca84155466aae3881d..5079d9baa71d74ba1353ce730845f9d64035e327 100644 (file)
@@ -16,7 +16,7 @@ import (
 
 // RetrieveLabels find all the labels of an organization
 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 {
                ctx.ServerError("RetrieveLabels.GetLabels", err)
                return
index 7e6fc5bf4cd95f79fc3aa8189d0acf9c859c57dc..277ff9d97359b1c1cccaebf8a41692c29591035c 100644 (file)
@@ -10,6 +10,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/log"
@@ -103,7 +104,7 @@ func SettingsPost(ctx *context.Context) {
 
        // update forks visibility
        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)
                        return
                }
index 810581640cc2768d50bcbbba616e3f6238bddad0..61435527da1b9ce495a57d37116ffa8f71e0d833 100644 (file)
@@ -12,6 +12,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/charset"
        "code.gitea.io/gitea/modules/context"
@@ -287,7 +288,7 @@ func Diff(ctx *context.Context) {
                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 {
                log.Error("GetLatestCommitStatus: %v", err)
        }
index 013286f2de90af7557b8021e6f64db2202cdbc26..7498830d94ccc20145bfc057254d1db17b7d6bc4 100644 (file)
@@ -16,6 +16,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
@@ -216,7 +217,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti
                issues = []*models.Issue{}
        } else {
                issues, err = models.Issues(&models.IssuesOptions{
-                       ListOptions: models.ListOptions{
+                       ListOptions: db.ListOptions{
                                Page:     pager.Paginater.Current(),
                                PageSize: setting.UI.IssuePagingNum,
                        },
@@ -278,14 +279,14 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti
                return
        }
 
-       labels, err := models.GetLabelsByRepoID(repo.ID, "", models.ListOptions{})
+       labels, err := models.GetLabelsByRepoID(repo.ID, "", db.ListOptions{})
        if err != nil {
                ctx.ServerError("GetLabelsByRepoID", err)
                return
        }
 
        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 {
                        ctx.ServerError("GetLabelsByOrgID", err)
                        return
@@ -645,14 +646,14 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository, isPull boo
                return nil
        }
 
-       labels, err := models.GetLabelsByRepoID(repo.ID, "", models.ListOptions{})
+       labels, err := models.GetLabelsByRepoID(repo.ID, "", db.ListOptions{})
        if err != nil {
                ctx.ServerError("GetLabelsByRepoID", err)
                return nil
        }
        ctx.Data["Labels"] = labels
        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 {
                        return nil
                }
@@ -735,10 +736,10 @@ func setTemplateIfExists(ctx *context.Context, ctxDataKey string, possibleDirs [
                        ctx.Data[issueTemplateTitleKey] = meta.Title
                        ctx.Data[ctxDataKey] = templateBody
                        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
                                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
                                                repoLabels = append(repoLabels, orgLabels...)
                                        }
@@ -1164,7 +1165,7 @@ func ViewIssue(ctx *context.Context) {
        for i := range issue.Labels {
                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 {
                ctx.ServerError("GetLabelsByRepoID", err)
                return
@@ -1172,7 +1173,7 @@ func ViewIssue(ctx *context.Context) {
        ctx.Data["Labels"] = labels
 
        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 {
                        ctx.ServerError("GetLabelsByOrgID", err)
                        return
index 0ce511448547002ee522888991eee1d1f3cb5d77..b97f57175b4cd85e460899a9d766d5a386f99b02 100644 (file)
@@ -54,7 +54,7 @@ func InitializeLabels(ctx *context.Context) {
 
 // RetrieveLabels find all the labels of a repository and organization
 func RetrieveLabels(ctx *context.Context) {
-       labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.FormString("sort"), models.ListOptions{})
+       labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.FormString("sort"), db.ListOptions{})
        if err != nil {
                ctx.ServerError("RetrieveLabels.GetLabels", err)
                return
@@ -67,7 +67,7 @@ func RetrieveLabels(ctx *context.Context) {
        ctx.Data["Labels"] = labels
 
        if ctx.Repo.Owner.IsOrganization() {
-               orgLabels, err := models.GetLabelsByOrgID(ctx.Repo.Owner.ID, ctx.FormString("sort"), models.ListOptions{})
+               orgLabels, err := models.GetLabelsByOrgID(ctx.Repo.Owner.ID, ctx.FormString("sort"), db.ListOptions{})
                if err != nil {
                        ctx.ServerError("GetLabelsByOrgID", err)
                        return
index 80f1eb52318a07b834bf5003e514a5eb6d8e8456..21e1fb2eab8d9b9a161ffd240e10ee58c74168cc 100644 (file)
@@ -9,6 +9,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/markup"
@@ -59,7 +60,7 @@ func Milestones(ctx *context.Context) {
        }
 
        miles, total, err := models.GetMilestones(models.GetMilestonesOption{
-               ListOptions: models.ListOptions{
+               ListOptions: db.ListOptions{
                        Page:     page,
                        PageSize: setting.UI.IssuePagingNum,
                },
index 6b369195de35601347099cbdd469ba4a65f3c4a8..c370e7f04d63d97d03e9608ebc4bbeddc7d7d19b 100644 (file)
@@ -16,6 +16,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/git"
@@ -335,7 +336,7 @@ func PrepareMergedViewPullInfo(ctx *context.Context, issue *models.Issue) *git.C
 
        if len(compareInfo.Commits) != 0 {
                sha := compareInfo.Commits[0].ID.String()
-               commitStatuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, sha, models.ListOptions{})
+               commitStatuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, sha, db.ListOptions{})
                if err != nil {
                        ctx.ServerError("GetLatestCommitStatus", err)
                        return nil
@@ -389,7 +390,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
                        ctx.ServerError(fmt.Sprintf("GetRefCommitID(%s)", pull.GetGitRefName()), err)
                        return nil
                }
-               commitStatuses, err := models.GetLatestCommitStatus(repo.ID, sha, models.ListOptions{})
+               commitStatuses, err := models.GetLatestCommitStatus(repo.ID, sha, db.ListOptions{})
                if err != nil {
                        ctx.ServerError("GetLatestCommitStatus", err)
                        return nil
@@ -478,7 +479,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
                return nil
        }
 
-       commitStatuses, err := models.GetLatestCommitStatus(repo.ID, sha, models.ListOptions{})
+       commitStatuses, err := models.GetLatestCommitStatus(repo.ID, sha, db.ListOptions{})
        if err != nil {
                ctx.ServerError("GetLatestCommitStatus", err)
                return nil
index 0603f0ee9745cda323b8c21ea19bd0c3d1c298b4..df1fd745d86771a97cbff36f2d74279a42c5ce00 100644 (file)
@@ -11,6 +11,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/log"
@@ -83,7 +84,7 @@ func releasesOrTags(ctx *context.Context, isTagList bool) {
                ctx.Data["PageIsTagList"] = false
        }
 
-       listOptions := models.ListOptions{
+       listOptions := db.ListOptions{
                Page:     ctx.FormInt("page"),
                PageSize: ctx.FormInt("limit"),
        }
index ed82c2eeb5f8c7f99f39136a74fba20f43fa4e13..e71a5bf482bbfc769c709f244b779cca2d1dafac 100644 (file)
@@ -15,6 +15,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/git"
@@ -768,7 +769,7 @@ func Collaboration(ctx *context.Context) {
        ctx.Data["Title"] = ctx.Tr("repo.settings")
        ctx.Data["PageIsSettingsCollaboration"] = true
 
-       users, err := ctx.Repo.Repository.GetCollaborators(models.ListOptions{})
+       users, err := ctx.Repo.Repository.GetCollaborators(db.ListOptions{})
        if err != nil {
                ctx.ServerError("GetCollaborators", err)
                return
index addde15de153fab6464ae4c5ee98a079cc3b0ac4..c0a35bcb4f05f71a704ab000967f86a223d6f571 100644 (file)
@@ -18,6 +18,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/cache"
        "code.gitea.io/gitea/modules/charset"
@@ -377,7 +378,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
 
        ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
 
-       statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), models.ListOptions{})
+       statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), db.ListOptions{})
        if err != nil {
                log.Error("GetLatestCommitStatus: %v", err)
        }
@@ -758,7 +759,7 @@ func renderCode(ctx *context.Context) {
 }
 
 // RenderUserCards render a page show users according the input template
-func RenderUserCards(ctx *context.Context, total int, getter func(opts models.ListOptions) ([]*models.User, error), tpl base.TplName) {
+func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*models.User, error), tpl base.TplName) {
        page := ctx.FormInt("page")
        if page <= 0 {
                page = 1
@@ -766,7 +767,7 @@ func RenderUserCards(ctx *context.Context, total int, getter func(opts models.Li
        pager := context.NewPagination(total, models.ItemsPerPage, page, 5)
        ctx.Data["Page"] = pager
 
-       items, err := getter(models.ListOptions{
+       items, err := getter(db.ListOptions{
                Page:     pager.Paginater.Current(),
                PageSize: models.ItemsPerPage,
        })
@@ -801,7 +802,7 @@ func Forks(ctx *context.Context) {
        ctx.Data["Title"] = ctx.Tr("repos.forks")
 
        // TODO: need pagination
-       forks, err := ctx.Repo.Repository.GetForks(models.ListOptions{})
+       forks, err := ctx.Repo.Repository.GetForks(db.ListOptions{})
        if err != nil {
                ctx.ServerError("GetForks", err)
                return
index 9785ca68d51c13702c3f31c6aac499765d22cf3a..733ace81b02a5490a627f861c85ae06b4c0232a0 100644 (file)
@@ -14,6 +14,7 @@ import (
 
        "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/context"
        "code.gitea.io/gitea/modules/eventsource"
@@ -147,7 +148,7 @@ func SignIn(ctx *context.Context) {
        ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login"
        ctx.Data["PageIsSignIn"] = true
        ctx.Data["PageIsLogin"] = true
-       ctx.Data["EnableSSPI"] = models.IsSSPIEnabled()
+       ctx.Data["EnableSSPI"] = login.IsSSPIEnabled()
 
        ctx.HTML(http.StatusOK, tplSignIn)
 }
@@ -167,7 +168,7 @@ func SignInPost(ctx *context.Context) {
        ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login"
        ctx.Data["PageIsSignIn"] = true
        ctx.Data["PageIsLogin"] = true
-       ctx.Data["EnableSSPI"] = models.IsSSPIEnabled()
+       ctx.Data["EnableSSPI"] = login.IsSSPIEnabled()
 
        if ctx.HasError() {
                ctx.HTML(http.StatusOK, tplSignIn)
@@ -573,7 +574,7 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
 func SignInOAuth(ctx *context.Context) {
        provider := ctx.Params(":provider")
 
-       loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
+       loginSource, err := login.GetActiveOAuth2LoginSourceByName(provider)
        if err != nil {
                ctx.ServerError("SignIn", err)
                return
@@ -608,7 +609,7 @@ func SignInOAuthCallback(ctx *context.Context) {
        provider := ctx.Params(":provider")
 
        // first look if the provider is still active
-       loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
+       loginSource, err := login.GetActiveOAuth2LoginSourceByName(provider)
        if err != nil {
                ctx.ServerError("SignIn", err)
                return
@@ -653,7 +654,7 @@ func SignInOAuthCallback(ctx *context.Context) {
                                FullName:    gothUser.Name,
                                Email:       gothUser.Email,
                                IsActive:    !setting.OAuth2Client.RegisterEmailConfirm,
-                               LoginType:   models.LoginOAuth2,
+                               LoginType:   login.OAuth2,
                                LoginSource: loginSource.ID,
                                LoginName:   gothUser.UserID,
                        }
@@ -711,7 +712,7 @@ func updateAvatarIfNeed(url string, u *models.User) {
        }
 }
 
-func handleOAuth2SignIn(ctx *context.Context, source *models.LoginSource, u *models.User, gothUser goth.User) {
+func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *models.User, gothUser goth.User) {
        updateAvatarIfNeed(gothUser.AvatarURL, u)
 
        needs2FA := false
@@ -785,7 +786,7 @@ func handleOAuth2SignIn(ctx *context.Context, source *models.LoginSource, u *mod
 
 // OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
 // login the user
-func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
+func oAuth2UserLoginCallback(loginSource *login.Source, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
        gothUser, err := loginSource.Cfg.(*oauth2.Source).Callback(request, response)
        if err != nil {
                if err.Error() == "securecookie: the value is too long" {
@@ -797,7 +798,7 @@ func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Requ
 
        user := &models.User{
                LoginName:   gothUser.UserID,
-               LoginType:   models.LoginOAuth2,
+               LoginType:   login.OAuth2,
                LoginSource: loginSource.ID,
        }
 
@@ -1068,7 +1069,7 @@ func LinkAccountPostRegister(ctx *context.Context) {
                }
        }
 
-       loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
+       loginSource, err := login.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
        if err != nil {
                ctx.ServerError("CreateUser", err)
        }
@@ -1078,7 +1079,7 @@ func LinkAccountPostRegister(ctx *context.Context) {
                Email:       form.Email,
                Passwd:      form.Password,
                IsActive:    !(setting.Service.RegisterEmailConfirm || setting.Service.RegisterManualConfirm),
-               LoginType:   models.LoginOAuth2,
+               LoginType:   login.OAuth2,
                LoginSource: loginSource.ID,
                LoginName:   gothUser.UserID,
        }
index bb75558dc85a2d8b809fc14eb0646e2a0ff1c456..2f1fca45271155090cf77c33b097b7bdb88fcc50 100644 (file)
@@ -15,6 +15,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
@@ -846,7 +847,7 @@ func repoIDMap(ctxUser *models.User, issueCountByRepo map[int64]int64, unitType
 
 // ShowSSHKeys output all the ssh keys of user by uid
 func ShowSSHKeys(ctx *context.Context, uid int64) {
-       keys, err := models.ListPublicKeys(uid, models.ListOptions{})
+       keys, err := models.ListPublicKeys(uid, db.ListOptions{})
        if err != nil {
                ctx.ServerError("ListPublicKeys", err)
                return
@@ -862,7 +863,7 @@ func ShowSSHKeys(ctx *context.Context, uid int64) {
 
 // ShowGPGKeys output all the public GPG keys of user by uid
 func ShowGPGKeys(ctx *context.Context, uid int64) {
-       keys, err := models.ListGPGKeys(uid, models.ListOptions{})
+       keys, err := models.ListGPGKeys(uid, db.ListOptions{})
        if err != nil {
                ctx.ServerError("ListGPGKeys", err)
                return
index cec6a92bbea45290a3227b5ea32930fcdcfec3e6..d9fc5eeaf9232443969af52a0d105ad4a5496d77 100644 (file)
@@ -13,6 +13,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/json"
@@ -115,7 +116,7 @@ type AccessTokenResponse struct {
        IDToken      string    `json:"id_token,omitempty"`
 }
 
-func newAccessTokenResponse(grant *models.OAuth2Grant, serverKey, clientKey oauth2.JWTSigningKey) (*AccessTokenResponse, *AccessTokenError) {
+func newAccessTokenResponse(grant *login.OAuth2Grant, serverKey, clientKey oauth2.JWTSigningKey) (*AccessTokenResponse, *AccessTokenError) {
        if setting.OAuth2.InvalidateRefreshTokens {
                if err := grant.IncreaseCounter(); err != nil {
                        return nil, &AccessTokenError{
@@ -162,7 +163,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, serverKey, clientKey oaut
        // generate OpenID Connect id_token
        signedIDToken := ""
        if grant.ScopeContains("openid") {
-               app, err := models.GetOAuth2ApplicationByID(grant.ApplicationID)
+               app, err := login.GetOAuth2ApplicationByID(grant.ApplicationID)
                if err != nil {
                        return nil, &AccessTokenError{
                                ErrorCode:        AccessTokenErrorCodeInvalidRequest,
@@ -268,9 +269,9 @@ func IntrospectOAuth(ctx *context.Context) {
        token, err := oauth2.ParseToken(form.Token, oauth2.DefaultSigningKey)
        if err == nil {
                if token.Valid() == nil {
-                       grant, err := models.GetOAuth2GrantByID(token.GrantID)
+                       grant, err := login.GetOAuth2GrantByID(token.GrantID)
                        if err == nil && grant != nil {
-                               app, err := models.GetOAuth2ApplicationByID(grant.ApplicationID)
+                               app, err := login.GetOAuth2ApplicationByID(grant.ApplicationID)
                                if err == nil && app != nil {
                                        response.Active = true
                                        response.Scope = grant.Scope
@@ -299,9 +300,9 @@ func AuthorizeOAuth(ctx *context.Context) {
                return
        }
 
-       app, err := models.GetOAuth2ApplicationByClientID(form.ClientID)
+       app, err := login.GetOAuth2ApplicationByClientID(form.ClientID)
        if err != nil {
-               if models.IsErrOauthClientIDInvalid(err) {
+               if login.IsErrOauthClientIDInvalid(err) {
                        handleAuthorizeError(ctx, AuthorizeError{
                                ErrorCode:        ErrorCodeUnauthorizedClient,
                                ErrorDescription: "Client ID not registered",
@@ -312,8 +313,10 @@ func AuthorizeOAuth(ctx *context.Context) {
                ctx.ServerError("GetOAuth2ApplicationByClientID", err)
                return
        }
-       if err := app.LoadUser(); err != nil {
-               ctx.ServerError("LoadUser", err)
+
+       user, err := models.GetUserByID(app.UID)
+       if err != nil {
+               ctx.ServerError("GetUserByID", err)
                return
        }
 
@@ -406,7 +409,7 @@ func AuthorizeOAuth(ctx *context.Context) {
        ctx.Data["State"] = form.State
        ctx.Data["Scope"] = form.Scope
        ctx.Data["Nonce"] = form.Nonce
-       ctx.Data["ApplicationUserLink"] = "<a href=\"" + html.EscapeString(setting.AppURL) + html.EscapeString(url.PathEscape(app.User.LowerName)) + "\">@" + html.EscapeString(app.User.Name) + "</a>"
+       ctx.Data["ApplicationUserLink"] = "<a href=\"" + html.EscapeString(setting.AppURL) + html.EscapeString(url.PathEscape(user.LowerName)) + "\">@" + html.EscapeString(user.Name) + "</a>"
        ctx.Data["ApplicationRedirectDomainHTML"] = "<strong>" + html.EscapeString(form.RedirectURI) + "</strong>"
        // TODO document SESSION <=> FORM
        err = ctx.Session.Set("client_id", app.ClientID)
@@ -443,7 +446,7 @@ func GrantApplicationOAuth(ctx *context.Context) {
                ctx.Error(http.StatusBadRequest)
                return
        }
-       app, err := models.GetOAuth2ApplicationByClientID(form.ClientID)
+       app, err := login.GetOAuth2ApplicationByClientID(form.ClientID)
        if err != nil {
                ctx.ServerError("GetOAuth2ApplicationByClientID", err)
                return
@@ -581,7 +584,7 @@ func handleRefreshToken(ctx *context.Context, form forms.AccessTokenForm, server
                return
        }
        // get grant before increasing counter
-       grant, err := models.GetOAuth2GrantByID(token.GrantID)
+       grant, err := login.GetOAuth2GrantByID(token.GrantID)
        if err != nil || grant == nil {
                handleAccessTokenError(ctx, AccessTokenError{
                        ErrorCode:        AccessTokenErrorCodeInvalidGrant,
@@ -608,7 +611,7 @@ func handleRefreshToken(ctx *context.Context, form forms.AccessTokenForm, server
 }
 
 func handleAuthorizationCode(ctx *context.Context, form forms.AccessTokenForm, serverKey, clientKey oauth2.JWTSigningKey) {
-       app, err := models.GetOAuth2ApplicationByClientID(form.ClientID)
+       app, err := login.GetOAuth2ApplicationByClientID(form.ClientID)
        if err != nil {
                handleAccessTokenError(ctx, AccessTokenError{
                        ErrorCode:        AccessTokenErrorCodeInvalidClient,
@@ -630,7 +633,7 @@ func handleAuthorizationCode(ctx *context.Context, form forms.AccessTokenForm, s
                })
                return
        }
-       authorizationCode, err := models.GetOAuth2AuthorizationByCode(form.Code)
+       authorizationCode, err := login.GetOAuth2AuthorizationByCode(form.Code)
        if err != nil || authorizationCode == nil {
                handleAccessTokenError(ctx, AccessTokenError{
                        ErrorCode:        AccessTokenErrorCodeUnauthorizedClient,
index 09abf1ee2a6879fbd96f24d3ef3b3464772ccffd..27d339b778ea39e93aeddde9459e6bf7ceee6295 100644 (file)
@@ -9,13 +9,14 @@ import (
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth/source/oauth2"
 
        "github.com/golang-jwt/jwt"
        "github.com/stretchr/testify/assert"
 )
 
-func createAndParseToken(t *testing.T, grant *models.OAuth2Grant) *oauth2.OIDCToken {
+func createAndParseToken(t *testing.T, grant *login.OAuth2Grant) *oauth2.OIDCToken {
        signingKey, err := oauth2.CreateJWTSigningKey("HS256", make([]byte, 32))
        assert.NoError(t, err)
        assert.NotNil(t, signingKey)
@@ -42,7 +43,7 @@ func createAndParseToken(t *testing.T, grant *models.OAuth2Grant) *oauth2.OIDCTo
 func TestNewAccessTokenResponse_OIDCToken(t *testing.T) {
        assert.NoError(t, db.PrepareTestDatabase())
 
-       grants, err := models.GetOAuth2GrantsByUserID(3)
+       grants, err := login.GetOAuth2GrantsByUserID(3)
        assert.NoError(t, err)
        assert.Len(t, grants, 1)
 
@@ -58,7 +59,7 @@ func TestNewAccessTokenResponse_OIDCToken(t *testing.T) {
        assert.False(t, oidcToken.EmailVerified)
 
        user := db.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
-       grants, err = models.GetOAuth2GrantsByUserID(user.ID)
+       grants, err = login.GetOAuth2GrantsByUserID(user.ID)
        assert.NoError(t, err)
        assert.Len(t, grants, 1)
 
index 9ecdc2345c5ea16b92e8b44a8cb15b41feef61da..d64d5621dead29a7e99f64be51a02d07cb85918b 100644 (file)
@@ -12,6 +12,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/markup"
        "code.gitea.io/gitea/modules/markup/markdown"
@@ -192,7 +193,7 @@ func Profile(ctx *context.Context) {
        ctx.Data["Keyword"] = keyword
        switch tab {
        case "followers":
-               items, err := ctxUser.GetFollowers(models.ListOptions{
+               items, err := ctxUser.GetFollowers(db.ListOptions{
                        PageSize: setting.UI.User.RepoPagingNum,
                        Page:     page,
                })
@@ -204,7 +205,7 @@ func Profile(ctx *context.Context) {
 
                total = ctxUser.NumFollowers
        case "following":
-               items, err := ctxUser.GetFollowing(models.ListOptions{
+               items, err := ctxUser.GetFollowing(db.ListOptions{
                        PageSize: setting.UI.User.RepoPagingNum,
                        Page:     page,
                })
@@ -229,7 +230,7 @@ func Profile(ctx *context.Context) {
        case "stars":
                ctx.Data["PageIsProfileStarList"] = true
                repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
-                       ListOptions: models.ListOptions{
+                       ListOptions: db.ListOptions{
                                PageSize: setting.UI.User.RepoPagingNum,
                                Page:     page,
                        },
@@ -260,7 +261,7 @@ func Profile(ctx *context.Context) {
                }
        case "watching":
                repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
-                       ListOptions: models.ListOptions{
+                       ListOptions: db.ListOptions{
                                PageSize: setting.UI.User.RepoPagingNum,
                                Page:     page,
                        },
@@ -281,7 +282,7 @@ func Profile(ctx *context.Context) {
                total = int(count)
        default:
                repos, count, err = models.SearchRepository(&models.SearchRepoOptions{
-                       ListOptions: models.ListOptions{
+                       ListOptions: db.ListOptions{
                                PageSize: setting.UI.User.RepoPagingNum,
                                Page:     page,
                        },
index 5e208afafea3d088cbe97ed76b1ca6918493327c..9976337bfab90ad6b699547e6043ccdbc7f814c3 100644 (file)
@@ -9,6 +9,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/setting"
@@ -92,12 +93,12 @@ func loadApplicationsData(ctx *context.Context) {
        ctx.Data["Tokens"] = tokens
        ctx.Data["EnableOAuth2"] = setting.OAuth2.Enable
        if setting.OAuth2.Enable {
-               ctx.Data["Applications"], err = models.GetOAuth2ApplicationsByUserID(ctx.User.ID)
+               ctx.Data["Applications"], err = login.GetOAuth2ApplicationsByUserID(ctx.User.ID)
                if err != nil {
                        ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
                        return
                }
-               ctx.Data["Grants"], err = models.GetOAuth2GrantsByUserID(ctx.User.ID)
+               ctx.Data["Grants"], err = login.GetOAuth2GrantsByUserID(ctx.User.ID)
                if err != nil {
                        ctx.ServerError("GetOAuth2GrantsByUserID", err)
                        return
index 24b9a9e205a0c8d21f89307d9b6e61c869e753e7..bb7a50841bb8a5771ce207b772052d45acdba5b0 100644 (file)
@@ -9,6 +9,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/setting"
@@ -233,7 +234,7 @@ func DeleteKey(ctx *context.Context) {
 }
 
 func loadKeysData(ctx *context.Context) {
-       keys, err := models.ListPublicKeys(ctx.User.ID, models.ListOptions{})
+       keys, err := models.ListPublicKeys(ctx.User.ID, db.ListOptions{})
        if err != nil {
                ctx.ServerError("ListPublicKeys", err)
                return
@@ -247,7 +248,7 @@ func loadKeysData(ctx *context.Context) {
        }
        ctx.Data["ExternalKeys"] = externalKeys
 
-       gpgkeys, err := models.ListGPGKeys(ctx.User.ID, models.ListOptions{})
+       gpgkeys, err := models.ListGPGKeys(ctx.User.ID, db.ListOptions{})
        if err != nil {
                ctx.ServerError("ListGPGKeys", err)
                return
@@ -258,7 +259,7 @@ func loadKeysData(ctx *context.Context) {
        // generate a new aes cipher using the csrfToken
        ctx.Data["TokenToSign"] = tokenToSign
 
-       principals, err := models.ListPrincipalKeys(ctx.User.ID, models.ListOptions{})
+       principals, err := models.ListPrincipalKeys(ctx.User.ID, db.ListOptions{})
        if err != nil {
                ctx.ServerError("ListPrincipalKeys", err)
                return
index 8de0720b510577aad8176050a63940bdb4dda82e..0f338ab5d1cbbc97ff6944d1a77031ebac7373d9 100644 (file)
@@ -8,7 +8,7 @@ import (
        "fmt"
        "net/http"
 
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/log"
@@ -34,7 +34,7 @@ func OAuthApplicationsPost(ctx *context.Context) {
                return
        }
        // TODO validate redirect URI
-       app, err := models.CreateOAuth2Application(models.CreateOAuth2ApplicationOptions{
+       app, err := login.CreateOAuth2Application(login.CreateOAuth2ApplicationOptions{
                Name:         form.Name,
                RedirectURIs: []string{form.RedirectURI},
                UserID:       ctx.User.ID,
@@ -67,7 +67,7 @@ func OAuthApplicationsEdit(ctx *context.Context) {
        }
        // TODO validate redirect URI
        var err error
-       if ctx.Data["App"], err = models.UpdateOAuth2Application(models.UpdateOAuth2ApplicationOptions{
+       if ctx.Data["App"], err = login.UpdateOAuth2Application(login.UpdateOAuth2ApplicationOptions{
                ID:           ctx.ParamsInt64("id"),
                Name:         form.Name,
                RedirectURIs: []string{form.RedirectURI},
@@ -85,9 +85,9 @@ func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
        ctx.Data["Title"] = ctx.Tr("settings")
        ctx.Data["PageIsSettingsApplications"] = true
 
-       app, err := models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
+       app, err := login.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
        if err != nil {
-               if models.IsErrOAuthApplicationNotFound(err) {
+               if login.IsErrOAuthApplicationNotFound(err) {
                        ctx.NotFound("Application not found", err)
                        return
                }
@@ -110,9 +110,9 @@ func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
 
 // OAuth2ApplicationShow displays the given application
 func OAuth2ApplicationShow(ctx *context.Context) {
-       app, err := models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
+       app, err := login.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
        if err != nil {
-               if models.IsErrOAuthApplicationNotFound(err) {
+               if login.IsErrOAuthApplicationNotFound(err) {
                        ctx.NotFound("Application not found", err)
                        return
                }
@@ -129,7 +129,7 @@ func OAuth2ApplicationShow(ctx *context.Context) {
 
 // DeleteOAuth2Application deletes the given oauth2 application
 func DeleteOAuth2Application(ctx *context.Context) {
-       if err := models.DeleteOAuth2Application(ctx.FormInt64("id"), ctx.User.ID); err != nil {
+       if err := login.DeleteOAuth2Application(ctx.FormInt64("id"), ctx.User.ID); err != nil {
                ctx.ServerError("DeleteOAuth2Application", err)
                return
        }
@@ -147,7 +147,7 @@ func RevokeOAuth2Grant(ctx *context.Context) {
                ctx.ServerError("RevokeOAuth2Grant", fmt.Errorf("user id or grant id is zero"))
                return
        }
-       if err := models.RevokeOAuth2Grant(ctx.FormInt64("id"), ctx.User.ID); err != nil {
+       if err := login.RevokeOAuth2Grant(ctx.FormInt64("id"), ctx.User.ID); err != nil {
                ctx.ServerError("RevokeOAuth2Grant", err)
                return
        }
index bd967af32b56d6d35f3ef0426ea8a416dbcedc68..d75149b8fc717f2a85ea9b1a30dc1b1634b60289 100644 (file)
@@ -15,6 +15,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/log"
@@ -235,7 +236,7 @@ func Repos(ctx *context.Context) {
        ctx.Data["allowAdopt"] = ctx.IsUserSiteAdmin() || setting.Repository.AllowAdoptionOfUnadoptedRepositories
        ctx.Data["allowDelete"] = ctx.IsUserSiteAdmin() || setting.Repository.AllowDeleteOfUnadoptedRepositories
 
-       opts := models.ListOptions{
+       opts := db.ListOptions{
                PageSize: setting.UI.Admin.UserPagingNum,
                Page:     ctx.FormInt("page"),
        }
@@ -284,7 +285,7 @@ func Repos(ctx *context.Context) {
                        return
                }
 
-               if err := ctxUser.GetRepositories(models.ListOptions{Page: 1, PageSize: setting.UI.Admin.UserPagingNum}, repoNames...); err != nil {
+               if err := ctxUser.GetRepositories(db.ListOptions{Page: 1, PageSize: setting.UI.Admin.UserPagingNum}, repoNames...); err != nil {
                        ctx.ServerError("GetRepositories", err)
                        return
                }
index 3406194015f65bb52b0ad4f8f3466d9c3924d0a3..d4abe84d9601c4ccd4bd3353eff8ec7cf4781342 100644 (file)
@@ -9,6 +9,7 @@ import (
        "net/http"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/setting"
@@ -87,9 +88,9 @@ func loadSecurityData(ctx *context.Context) {
        }
 
        // map the provider display name with the LoginSource
-       sources := make(map[*models.LoginSource]string)
+       sources := make(map[*login.Source]string)
        for _, externalAccount := range accountLinks {
-               if loginSource, err := models.GetLoginSourceByID(externalAccount.LoginSourceID); err == nil {
+               if loginSource, err := login.GetSourceByID(externalAccount.LoginSourceID); err == nil {
                        var providerDisplayName string
 
                        type DisplayNamed interface {
diff --git a/services/auth/login_source.go b/services/auth/login_source.go
new file mode 100644 (file)
index 0000000..723dd2b
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright 2021 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 auth
+
+import (
+       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
+)
+
+// DeleteLoginSource deletes a LoginSource record in DB.
+func DeleteLoginSource(source *login.Source) error {
+       count, err := db.GetEngine(db.DefaultContext).Count(&models.User{LoginSource: source.ID})
+       if err != nil {
+               return err
+       } else if count > 0 {
+               return login.ErrSourceInUse{
+                       ID: source.ID,
+               }
+       }
+
+       count, err = db.GetEngine(db.DefaultContext).Count(&models.ExternalLoginUser{LoginSourceID: source.ID})
+       if err != nil {
+               return err
+       } else if count > 0 {
+               return login.ErrSourceInUse{
+                       ID: source.ID,
+               }
+       }
+
+       if registerableSource, ok := source.Cfg.(login.RegisterableSource); ok {
+               if err := registerableSource.UnregisterSource(); err != nil {
+                       return err
+               }
+       }
+
+       _, err = db.GetEngine(db.DefaultContext).ID(source.ID).Delete(new(login.Source))
+       return err
+}
index e79b640ce46de36ced98bf91ef0fcf9ea572b157..9b342f3458f60f7a0a8e48eb620c11b8f29ecb40 100644 (file)
@@ -12,6 +12,7 @@ import (
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/timeutil"
        "code.gitea.io/gitea/modules/web/middleware"
@@ -35,8 +36,8 @@ func CheckOAuthAccessToken(accessToken string) int64 {
                log.Trace("oauth2.ParseToken: %v", err)
                return 0
        }
-       var grant *models.OAuth2Grant
-       if grant, err = models.GetOAuth2GrantByID(token.GrantID); err != nil || grant == nil {
+       var grant *login.OAuth2Grant
+       if grant, err = login.GetOAuth2GrantByID(token.GrantID); err != nil || grant == nil {
                return 0
        }
        if token.Type != oauth2.TypeAccessToken {
index a7acb95ba2505273706fd5ddc36da679a1a77f12..a7ad029456dfa522492fbe4369231b05bc7a1609 100644 (file)
@@ -9,6 +9,7 @@ import (
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/log"
 
        // Register the sources
@@ -21,7 +22,7 @@ import (
 )
 
 // UserSignIn validates user name and password.
-func UserSignIn(username, password string) (*models.User, *models.LoginSource, error) {
+func UserSignIn(username, password string) (*models.User, *login.Source, error) {
        var user *models.User
        if strings.Contains(username, "@") {
                user = &models.User{Email: strings.ToLower(strings.TrimSpace(username))}
@@ -50,7 +51,7 @@ func UserSignIn(username, password string) (*models.User, *models.LoginSource, e
        }
 
        if hasUser {
-               source, err := models.GetLoginSourceByID(user.LoginSource)
+               source, err := login.GetSourceByID(user.LoginSource)
                if err != nil {
                        return nil, nil, err
                }
@@ -78,7 +79,7 @@ func UserSignIn(username, password string) (*models.User, *models.LoginSource, e
                return user, source, nil
        }
 
-       sources, err := models.AllActiveLoginSources()
+       sources, err := login.AllActiveSources()
        if err != nil {
                return nil, nil, err
        }
index 2e0fa9ba2247aa9a474fe562e051dedd9a2c1eab..a8b137ec4817610f23eb4ec3329ae2244ac207b7 100644 (file)
@@ -5,7 +5,7 @@
 package db_test
 
 import (
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth"
        "code.gitea.io/gitea/services/auth/source/db"
 )
@@ -15,7 +15,7 @@ import (
 
 type sourceInterface interface {
        auth.PasswordAuthenticator
-       models.LoginConfig
+       login.Config
 }
 
 var _ (sourceInterface) = &db.Source{}
index 182c05f0dfcc5f8053b6903f8fe74693f973040d..2fedff3a7ea85271cc6b6957b9e8757414452f09 100644 (file)
@@ -4,7 +4,10 @@
 
 package db
 
-import "code.gitea.io/gitea/models"
+import (
+       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
+)
 
 // Source is a password authentication service
 type Source struct{}
@@ -26,6 +29,6 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
 }
 
 func init() {
-       models.RegisterLoginTypeConfig(models.LoginNoType, &Source{})
-       models.RegisterLoginTypeConfig(models.LoginPlain, &Source{})
+       login.RegisterTypeConfig(login.NoType, &Source{})
+       login.RegisterTypeConfig(login.Plain, &Source{})
 }
index a0425d2f763c1089b442794af7cb84c40eac60aa..c480119cd3fe6b5e9ca7fc5926a6895c26aeff8e 100644 (file)
@@ -5,7 +5,7 @@
 package ldap_test
 
 import (
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth"
        "code.gitea.io/gitea/services/auth/source/ldap"
 )
@@ -17,12 +17,12 @@ type sourceInterface interface {
        auth.PasswordAuthenticator
        auth.SynchronizableSource
        auth.LocalTwoFASkipper
-       models.SSHKeyProvider
-       models.LoginConfig
-       models.SkipVerifiable
-       models.HasTLSer
-       models.UseTLSer
-       models.LoginSourceSettable
+       login.SSHKeyProvider
+       login.Config
+       login.SkipVerifiable
+       login.HasTLSer
+       login.UseTLSer
+       login.SourceSettable
 }
 
 var _ (sourceInterface) = &ldap.Source{}
index d1228d41aeb1160a9ee9a57517fcfa0538046671..82ff7313b288a474cb1117dd33f3d9764f71c9d0 100644 (file)
@@ -8,6 +8,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/json"
        "code.gitea.io/gitea/modules/secret"
        "code.gitea.io/gitea/modules/setting"
@@ -55,7 +56,7 @@ type Source struct {
        SkipLocalTwoFA        bool   // Skip Local 2fa for users authenticated with this source
 
        // reference to the loginSource
-       loginSource *models.LoginSource
+       loginSource *login.Source
 }
 
 // FromDB fills up a LDAPConfig from serialized format.
@@ -109,11 +110,11 @@ func (source *Source) ProvidesSSHKeys() bool {
 }
 
 // SetLoginSource sets the related LoginSource
-func (source *Source) SetLoginSource(loginSource *models.LoginSource) {
+func (source *Source) SetLoginSource(loginSource *login.Source) {
        source.loginSource = loginSource
 }
 
 func init() {
-       models.RegisterLoginTypeConfig(models.LoginLDAP, &Source{})
-       models.RegisterLoginTypeConfig(models.LoginDLDAP, &Source{})
+       login.RegisterTypeConfig(login.LDAP, &Source{})
+       login.RegisterTypeConfig(login.DLDAP, &Source{})
 }
index 46478e60296edacc920feb0ae804fee9e81c2e9e..f302a9d5837fb434feb9bf42f08f63fc80986515 100644 (file)
@@ -9,16 +9,17 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/mailer"
 )
 
 // Authenticate queries if login/password is valid against the LDAP directory pool,
 // and create a local user if success when enabled.
-func (source *Source) Authenticate(user *models.User, login, password string) (*models.User, error) {
-       sr := source.SearchEntry(login, password, source.loginSource.Type == models.LoginDLDAP)
+func (source *Source) Authenticate(user *models.User, userName, password string) (*models.User, error) {
+       sr := source.SearchEntry(userName, password, source.loginSource.Type == login.DLDAP)
        if sr == nil {
                // User not in LDAP, do nothing
-               return nil, models.ErrUserNotExist{Name: login}
+               return nil, models.ErrUserNotExist{Name: userName}
        }
 
        isAttributeSSHPublicKeySet := len(strings.TrimSpace(source.AttributeSSHPublicKey)) > 0
@@ -64,7 +65,7 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
 
        // Fallback.
        if len(sr.Username) == 0 {
-               sr.Username = login
+               sr.Username = userName
        }
 
        if len(sr.Mail) == 0 {
@@ -78,7 +79,7 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
                Email:        sr.Mail,
                LoginType:    source.loginSource.Type,
                LoginSource:  source.loginSource.ID,
-               LoginName:    login,
+               LoginName:    userName,
                IsActive:     true,
                IsAdmin:      sr.IsAdmin,
                IsRestricted: sr.IsRestricted,
index 4157427ff2f7696807ae936f051c8ae8231eed3f..0a1986a3b29bc7e43c7d2e59ef57abbcb7bf69c0 100644 (file)
@@ -5,7 +5,7 @@
 package oauth2_test
 
 import (
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth"
        "code.gitea.io/gitea/services/auth/source/oauth2"
 )
@@ -14,9 +14,9 @@ import (
 // It tightly binds the interfaces and implementation without breaking go import cycles
 
 type sourceInterface interface {
-       models.LoginConfig
-       models.LoginSourceSettable
-       models.RegisterableSource
+       login.Config
+       login.SourceSettable
+       login.RegisterableSource
        auth.PasswordAuthenticator
 }
 
index be31503eeff5e2c4ef1ad233715755596c1b6f9f..343b24cf6f58717ab86a11aaf4abe57b3baf7c8e 100644 (file)
@@ -8,8 +8,8 @@ import (
        "net/http"
        "sync"
 
-       "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/models/db"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/setting"
 
@@ -74,7 +74,7 @@ func ResetOAuth2() error {
 
 // initOAuth2LoginSources is used to load and register all active OAuth2 providers
 func initOAuth2LoginSources() error {
-       loginSources, _ := models.GetActiveOAuth2ProviderLoginSources()
+       loginSources, _ := login.GetActiveOAuth2ProviderLoginSources()
        for _, source := range loginSources {
                oauth2Source, ok := source.Cfg.(*Source)
                if !ok {
index 2196e304928e86c9ac1f20d2d967ee5198fb685f..0fd57a8dbd5a0cc43480ddf2d0daf5d54cc01f5a 100644 (file)
@@ -9,6 +9,7 @@ import (
        "sort"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/setting"
 
@@ -87,7 +88,7 @@ func GetOAuth2Providers() []Provider {
 func GetActiveOAuth2Providers() ([]string, map[string]Provider, error) {
        // Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type
 
-       loginSources, err := models.GetActiveOAuth2ProviderLoginSources()
+       loginSources, err := login.GetActiveOAuth2ProviderLoginSources()
        if err != nil {
                return nil, nil, err
        }
index 7b22383d7ed6dac169f10ed334dadf32356a3c7e..49bb9a0148fb4a62a98a038f62e18bd58aadce97 100644 (file)
@@ -6,6 +6,7 @@ package oauth2
 
 import (
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/json"
 )
 
@@ -27,7 +28,7 @@ type Source struct {
        SkipLocalTwoFA                bool
 
        // reference to the loginSource
-       loginSource *models.LoginSource
+       loginSource *login.Source
 }
 
 // FromDB fills up an OAuth2Config from serialized format.
@@ -41,10 +42,10 @@ func (source *Source) ToDB() ([]byte, error) {
 }
 
 // SetLoginSource sets the related LoginSource
-func (source *Source) SetLoginSource(loginSource *models.LoginSource) {
+func (source *Source) SetLoginSource(loginSource *login.Source) {
        source.loginSource = loginSource
 }
 
 func init() {
-       models.RegisterLoginTypeConfig(models.LoginOAuth2, &Source{})
+       login.RegisterTypeConfig(login.OAuth2, &Source{})
 }
index a0bebdf9c6799c0dc457cc0fd931ec7ab452f1b2..a151c2f52e6e6165ac333c63dee862c3ecbd526d 100644 (file)
@@ -5,7 +5,7 @@
 package pam_test
 
 import (
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth"
        "code.gitea.io/gitea/services/auth/source/pam"
 )
@@ -15,8 +15,8 @@ import (
 
 type sourceInterface interface {
        auth.PasswordAuthenticator
-       models.LoginConfig
-       models.LoginSourceSettable
+       login.Config
+       login.SourceSettable
 }
 
 var _ (sourceInterface) = &pam.Source{}
index 75aa99e45fd485d5f8b4fa9df5f5e6c572cbdcc7..0bfa7cdb06b876741ba11a1c277272051b0270a2 100644 (file)
@@ -6,6 +6,7 @@ package pam
 
 import (
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/json"
 )
 
@@ -22,7 +23,7 @@ type Source struct {
        EmailDomain string
 
        // reference to the loginSource
-       loginSource *models.LoginSource
+       loginSource *login.Source
 }
 
 // FromDB fills up a PAMConfig from serialized format.
@@ -36,10 +37,10 @@ func (source *Source) ToDB() ([]byte, error) {
 }
 
 // SetLoginSource sets the related LoginSource
-func (source *Source) SetLoginSource(loginSource *models.LoginSource) {
+func (source *Source) SetLoginSource(loginSource *login.Source) {
        source.loginSource = loginSource
 }
 
 func init() {
-       models.RegisterLoginTypeConfig(models.LoginPAM, &Source{})
+       login.RegisterTypeConfig(login.PAM, &Source{})
 }
index 8241aed7256f4af97c70e6d5f0ecc9f1d812a130..ad6fbb5cce8f92cc7361681dfb8950941fc4097f 100644 (file)
@@ -9,6 +9,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/auth/pam"
        "code.gitea.io/gitea/modules/setting"
        "code.gitea.io/gitea/services/mailer"
@@ -18,11 +19,11 @@ import (
 
 // Authenticate queries if login/password is valid against the PAM,
 // and create a local user if success when enabled.
-func (source *Source) Authenticate(user *models.User, login, password string) (*models.User, error) {
-       pamLogin, err := pam.Auth(source.ServiceName, login, password)
+func (source *Source) Authenticate(user *models.User, userName, password string) (*models.User, error) {
+       pamLogin, err := pam.Auth(source.ServiceName, userName, password)
        if err != nil {
                if strings.Contains(err.Error(), "Authentication failure") {
-                       return nil, models.ErrUserNotExist{Name: login}
+                       return nil, models.ErrUserNotExist{Name: userName}
                }
                return nil, err
        }
@@ -54,9 +55,9 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
                Name:        username,
                Email:       email,
                Passwd:      password,
-               LoginType:   models.LoginPAM,
+               LoginType:   login.PAM,
                LoginSource: source.loginSource.ID,
-               LoginName:   login, // This is what the user typed in
+               LoginName:   userName, // This is what the user typed in
                IsActive:    true,
        }
 
index bc2042e069965243892783b128272b119af9df79..d1c982472fc630eee36b938c7454b25bfc9a7a1f 100644 (file)
@@ -5,7 +5,7 @@
 package smtp_test
 
 import (
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth"
        "code.gitea.io/gitea/services/auth/source/smtp"
 )
@@ -15,11 +15,11 @@ import (
 
 type sourceInterface interface {
        auth.PasswordAuthenticator
-       models.LoginConfig
-       models.SkipVerifiable
-       models.HasTLSer
-       models.UseTLSer
-       models.LoginSourceSettable
+       login.Config
+       login.SkipVerifiable
+       login.HasTLSer
+       login.UseTLSer
+       login.SourceSettable
 }
 
 var _ (sourceInterface) = &smtp.Source{}
index 39c9851ede233f646b780de6b4bd87d1603f0c3d..487375c3044b209443266d92769ddb00cf764c36 100644 (file)
@@ -6,6 +6,7 @@ package smtp
 
 import (
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/json"
 )
 
@@ -28,7 +29,7 @@ type Source struct {
        DisableHelo    bool
 
        // reference to the loginSource
-       loginSource *models.LoginSource
+       loginSource *login.Source
 }
 
 // FromDB fills up an SMTPConfig from serialized format.
@@ -57,10 +58,10 @@ func (source *Source) UseTLS() bool {
 }
 
 // SetLoginSource sets the related LoginSource
-func (source *Source) SetLoginSource(loginSource *models.LoginSource) {
+func (source *Source) SetLoginSource(loginSource *login.Source) {
        source.loginSource = loginSource
 }
 
 func init() {
-       models.RegisterLoginTypeConfig(models.LoginSMTP, &Source{})
+       login.RegisterTypeConfig(login.SMTP, &Source{})
 }
index cff64c69d2f99678f9aa5cd12768a1cb6951b8ec..f50baa56a25375cde4efe9130f479d586d6b402c 100644 (file)
@@ -11,31 +11,32 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/util"
        "code.gitea.io/gitea/services/mailer"
 )
 
 // Authenticate queries if the provided login/password is authenticates against the SMTP server
 // Users will be autoregistered as required
-func (source *Source) Authenticate(user *models.User, login, password string) (*models.User, error) {
+func (source *Source) Authenticate(user *models.User, userName, password string) (*models.User, error) {
        // Verify allowed domains.
        if len(source.AllowedDomains) > 0 {
-               idx := strings.Index(login, "@")
+               idx := strings.Index(userName, "@")
                if idx == -1 {
-                       return nil, models.ErrUserNotExist{Name: login}
-               } else if !util.IsStringInSlice(login[idx+1:], strings.Split(source.AllowedDomains, ","), true) {
-                       return nil, models.ErrUserNotExist{Name: login}
+                       return nil, models.ErrUserNotExist{Name: userName}
+               } else if !util.IsStringInSlice(userName[idx+1:], strings.Split(source.AllowedDomains, ","), true) {
+                       return nil, models.ErrUserNotExist{Name: userName}
                }
        }
 
        var auth smtp.Auth
        switch source.Auth {
        case PlainAuthentication:
-               auth = smtp.PlainAuth("", login, password, source.Host)
+               auth = smtp.PlainAuth("", userName, password, source.Host)
        case LoginAuthentication:
-               auth = &loginAuthenticator{login, password}
+               auth = &loginAuthenticator{userName, password}
        case CRAMMD5Authentication:
-               auth = smtp.CRAMMD5Auth(login, password)
+               auth = smtp.CRAMMD5Auth(userName, password)
        default:
                return nil, errors.New("unsupported SMTP auth type")
        }
@@ -46,11 +47,11 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
                tperr, ok := err.(*textproto.Error)
                if (ok && tperr.Code == 535) ||
                        strings.Contains(err.Error(), "Username and Password not accepted") {
-                       return nil, models.ErrUserNotExist{Name: login}
+                       return nil, models.ErrUserNotExist{Name: userName}
                }
                if (ok && tperr.Code == 534) ||
                        strings.Contains(err.Error(), "Application-specific password required") {
-                       return nil, models.ErrUserNotExist{Name: login}
+                       return nil, models.ErrUserNotExist{Name: userName}
                }
                return nil, err
        }
@@ -59,20 +60,20 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
                return user, nil
        }
 
-       username := login
-       idx := strings.Index(login, "@")
+       username := userName
+       idx := strings.Index(userName, "@")
        if idx > -1 {
-               username = login[:idx]
+               username = userName[:idx]
        }
 
        user = &models.User{
                LowerName:   strings.ToLower(username),
                Name:        strings.ToLower(username),
-               Email:       login,
+               Email:       userName,
                Passwd:      password,
-               LoginType:   models.LoginSMTP,
+               LoginType:   login.SMTP,
                LoginSource: source.loginSource.ID,
-               LoginName:   login,
+               LoginName:   userName,
                IsActive:    true,
        }
 
index 605a6ec6c541a2a3a84ba39bb8435dcd42b1d9c6..1efa69c05ba64bcd80647aef300a8d951f2eb9c9 100644 (file)
@@ -5,7 +5,7 @@
 package sspi_test
 
 import (
-       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/services/auth/source/sspi"
 )
 
@@ -13,7 +13,7 @@ import (
 // It tightly binds the interfaces and implementation without breaking go import cycles
 
 type sourceInterface interface {
-       models.LoginConfig
+       login.Config
 }
 
 var _ (sourceInterface) = &sspi.Source{}
index 58cb10de1df91f5d596af8a2abf48e1594d772fc..68fd6a6079485557e86c1e6412017746926554b6 100644 (file)
@@ -6,6 +6,7 @@ package sspi
 
 import (
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/json"
 )
 
@@ -36,5 +37,5 @@ func (cfg *Source) ToDB() ([]byte, error) {
 }
 
 func init() {
-       models.RegisterLoginTypeConfig(models.LoginSSPI, &Source{})
+       login.RegisterTypeConfig(login.SSPI, &Source{})
 }
index d7e0f55242aa7dee7804fc76f5069da12c62168c..a4c39b00646d5bbbb6d91aacde5fa17a338e28dc 100644 (file)
@@ -10,6 +10,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/setting"
@@ -152,7 +153,7 @@ func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore,
 
 // getConfig retrieves the SSPI configuration from login sources
 func (s *SSPI) getConfig() (*sspi.Source, error) {
-       sources, err := models.ActiveLoginSources(models.LoginSSPI)
+       sources, err := login.ActiveSources(login.SSPI)
        if err != nil {
                return nil, err
        }
@@ -248,7 +249,7 @@ func sanitizeUsername(username string, cfg *sspi.Source) string {
 // fails (or if negotiation should continue), which would prevent other authentication methods
 // to execute at all.
 func specialInit() {
-       if models.IsSSPIEnabled() {
+       if login.IsSSPIEnabled() {
                Register(&SSPI{})
        }
 }
index a34b4d1d2694ff2a62859d97d28a13131e637976..6d69650e5baa23d08bab3325e3688f720decb1ba 100644 (file)
@@ -8,6 +8,7 @@ import (
        "context"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/log"
 )
 
@@ -15,7 +16,7 @@ import (
 func SyncExternalUsers(ctx context.Context, updateExisting bool) error {
        log.Trace("Doing: SyncExternalUsers")
 
-       ls, err := models.LoginSources()
+       ls, err := login.Sources()
        if err != nil {
                log.Error("SyncExternalUsers: %v", err)
                return err
index 45773fdb127d23f511159e7d0ac43f9c362f4921..e43b3ca7c5f5bb8a08ea8b8109444cd3f010d0c1 100644 (file)
@@ -8,6 +8,7 @@ import (
        "strings"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/login"
        "code.gitea.io/gitea/modules/structs"
 
        "github.com/markbates/goth"
@@ -15,7 +16,7 @@ import (
 
 // LinkAccountToUser link the gothUser to the user
 func LinkAccountToUser(user *models.User, gothUser goth.User) error {
-       loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
+       loginSource, err := login.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
        if err != nil {
                return err
        }
index c5c930ee0d8b36d59ec7a51872b583bb601cb139..f1f351138b8e19ba08b4672e8e950c0a5f89c177 100644 (file)
@@ -7,6 +7,7 @@ package pull
 
 import (
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/db"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/structs"
 
@@ -129,7 +130,7 @@ func GetPullRequestCommitStatusState(pr *models.PullRequest) (structs.CommitStat
                return "", errors.Wrap(err, "LoadBaseRepo")
        }
 
-       commitStatuses, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, sha, models.ListOptions{})
+       commitStatuses, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, sha, db.ListOptions{})
        if err != nil {
                return "", errors.Wrap(err, "GetLatestCommitStatus")
        }
index bd5551b6dcc0f03caec609f507343e3dd0913b07..f7e231379b7f9b9cd1f884f2b14f7afcc8e071b9 100644 (file)
@@ -780,7 +780,7 @@ func getLastCommitStatus(gitRepo *git.Repository, pr *models.PullRequest) (statu
                return nil, err
        }
 
-       statusList, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, sha, models.ListOptions{})
+       statusList, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, sha, db.ListOptions{})
        if err != nil {
                return nil, err
        }
index f65314c45d10e9164b3228438f6f6a844e6d8704..081b17cd83e83e34f3037ece127553e33a47bf08 100644 (file)
@@ -138,7 +138,7 @@ func createCodeComment(doer *models.User, repo *models.Repository, issue *models
                                Line:     line,
                                TreePath: treePath,
                                Type:     models.CommentTypeCode,
-                               ListOptions: models.ListOptions{
+                               ListOptions: db.ListOptions{
                                        PageSize: 1,
                                        Page:     1,
                                },