Skip to content

Commit

Permalink
Move login related structs and functions to models/login (go-gitea#17093
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
lunny authored Sep 24, 2021
1 parent 4a26550 commit 5842a55
Show file tree
Hide file tree
Showing 142 changed files with 1,046 additions and 903 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,3 @@ issues:
linters:
- staticcheck
text: "svc.IsAnInteractiveSession is deprecated: Use IsWindowsService instead."

21 changes: 12 additions & 9 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ 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"
pwd "code.gitea.io/gitea/modules/password"
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"
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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),
Expand All @@ -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
}
Expand Down Expand Up @@ -705,15 +708,15 @@ 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 {
if err := initDB(); err != nil {
return err
}

loginSources, err := models.LoginSources()
loginSources, err := login.Sources()

if err != nil {
return err
Expand All @@ -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()

Expand All @@ -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)
}
32 changes: 16 additions & 16 deletions cmd/admin_auth_ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
)

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 5842a55

Please sign in to comment.