1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

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

* Move login related structs and functions to models/login

* Fix test

* Fix lint

* Fix lint

* Fix lint of windows

* Fix lint

* Fix test

* Fix test

* Only load necessary fixtures when preparing unit tests envs

* Fix lint

* Fix test

* Fix test

* Fix error log

* Fix error log

* Fix error log

* remove unnecessary change

* fix error log

* merge main branch
This commit is contained in:
Lunny Xiao
2021-09-24 19:32:56 +08:00
committed by GitHub
parent 4a2655098f
commit 5842a55b31
142 changed files with 1050 additions and 907 deletions

View 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"),

View 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"),
},

View 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},

View 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"),
}

View 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)
}
}