diff options
Diffstat (limited to 'internal/route')
-rw-r--r-- | internal/route/admin/auths.go | 65 | ||||
-rw-r--r-- | internal/route/admin/users.go | 6 | ||||
-rw-r--r-- | internal/route/api/v1/admin/user.go | 3 | ||||
-rw-r--r-- | internal/route/install.go | 1 | ||||
-rw-r--r-- | internal/route/user/auth.go | 4 |
5 files changed, 44 insertions, 35 deletions
diff --git a/internal/route/admin/auths.go b/internal/route/admin/auths.go index c5c1e480..d2967e29 100644 --- a/internal/route/admin/auths.go +++ b/internal/route/admin/auths.go @@ -11,7 +11,6 @@ import ( "github.com/unknwon/com" log "unknwon.dev/clog/v2" - "xorm.io/core" "gogs.io/gogs/internal/auth/ldap" "gogs.io/gogs/internal/conf" @@ -32,13 +31,13 @@ func Authentications(c *context.Context) { c.PageIs("AdminAuthentications") var err error - c.Data["Sources"], err = db.ListLoginSources() + c.Data["Sources"], err = db.LoginSources.List(db.ListLoginSourceOpts{}) if err != nil { c.Error(err, "list login sources") return } - c.Data["Total"] = db.CountLoginSources() + c.Data["Total"] = db.LoginSources.Count() c.Success(AUTHS) } @@ -56,9 +55,9 @@ var ( {db.LoginNames[db.LoginGitHub], db.LoginGitHub}, } securityProtocols = []dropdownItem{ - {db.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED], ldap.SECURITY_PROTOCOL_UNENCRYPTED}, - {db.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_LDAPS], ldap.SECURITY_PROTOCOL_LDAPS}, - {db.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_START_TLS], ldap.SECURITY_PROTOCOL_START_TLS}, + {db.SecurityProtocolNames[ldap.SecurityProtocolUnencrypted], ldap.SecurityProtocolUnencrypted}, + {db.SecurityProtocolNames[ldap.SecurityProtocolLDAPS], ldap.SecurityProtocolLDAPS}, + {db.SecurityProtocolNames[ldap.SecurityProtocolStartTLS], ldap.SecurityProtocolStartTLS}, } ) @@ -69,7 +68,7 @@ func NewAuthSource(c *context.Context) { c.Data["type"] = db.LoginLDAP c.Data["CurrentTypeName"] = db.LoginNames[db.LoginLDAP] - c.Data["CurrentSecurityProtocol"] = db.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED] + c.Data["CurrentSecurityProtocol"] = db.SecurityProtocolNames[ldap.SecurityProtocolUnencrypted] c.Data["smtp_auth"] = "PLAIN" c.Data["is_active"] = true c.Data["is_default"] = true @@ -81,7 +80,7 @@ func NewAuthSource(c *context.Context) { func parseLDAPConfig(f form.Authentication) *db.LDAPConfig { return &db.LDAPConfig{ - Source: &ldap.Source{ + Source: ldap.Source{ Host: f.Host, Port: f.Port, SecurityProtocol: ldap.SecurityProtocol(f.SecurityProtocol), @@ -129,11 +128,11 @@ func NewAuthSourcePost(c *context.Context, f form.Authentication) { c.Data["SMTPAuths"] = db.SMTPAuths hasTLS := false - var config core.Conversion + var config interface{} switch db.LoginType(f.Type) { case db.LoginLDAP, db.LoginDLDAP: config = parseLDAPConfig(f) - hasTLS = ldap.SecurityProtocol(f.SecurityProtocol) > ldap.SECURITY_PROTOCOL_UNENCRYPTED + hasTLS = ldap.SecurityProtocol(f.SecurityProtocol) > ldap.SecurityProtocolUnencrypted case db.LoginSMTP: config = parseSMTPConfig(f) hasTLS = true @@ -156,22 +155,31 @@ func NewAuthSourcePost(c *context.Context, f form.Authentication) { return } - if err := db.CreateLoginSource(&db.LoginSource{ + source, err := db.LoginSources.Create(db.CreateLoginSourceOpts{ Type: db.LoginType(f.Type), Name: f.Name, - IsActived: f.IsActive, - IsDefault: f.IsDefault, - Cfg: config, - }); err != nil { + Activated: f.IsActive, + Default: f.IsDefault, + Config: config, + }) + if err != nil { if db.IsErrLoginSourceAlreadyExist(err) { c.FormErr("Name") - c.RenderWithErr(c.Tr("admin.auths.login_source_exist", err.(db.ErrLoginSourceAlreadyExist).Name), AUTH_NEW, f) + c.RenderWithErr(c.Tr("admin.auths.login_source_exist", f.Name), AUTH_NEW, f) } else { c.Error(err, "create login source") } return } + if source.IsDefault { + err = db.LoginSources.ResetNonDefault(source) + if err != nil { + c.Error(err, "reset non-default login sources") + return + } + } + log.Trace("Authentication created by admin(%s): %s", c.User.Name, f.Name) c.Flash.Success(c.Tr("admin.auths.new_success", f.Name)) @@ -217,7 +225,7 @@ func EditAuthSourcePost(c *context.Context, f form.Authentication) { return } - var config core.Conversion + var config interface{} switch db.LoginType(f.Type) { case db.LoginLDAP, db.LoginDLDAP: config = parseLDAPConfig(f) @@ -239,12 +247,20 @@ func EditAuthSourcePost(c *context.Context, f form.Authentication) { source.Name = f.Name source.IsActived = f.IsActive source.IsDefault = f.IsDefault - source.Cfg = config - if err := db.UpdateLoginSource(source); err != nil { + source.Config = config + if err := db.LoginSources.Save(source); err != nil { c.Error(err, "update login source") return } + if source.IsDefault { + err = db.LoginSources.ResetNonDefault(source) + if err != nil { + c.Error(err, "reset non-default login sources") + return + } + } + log.Trace("Authentication changed by admin '%s': %d", c.User.Name, source.ID) c.Flash.Success(c.Tr("admin.auths.update_success")) @@ -252,13 +268,8 @@ func EditAuthSourcePost(c *context.Context, f form.Authentication) { } func DeleteAuthSource(c *context.Context) { - source, err := db.LoginSources.GetByID(c.ParamsInt64(":authid")) - if err != nil { - c.Error(err, "get login source by ID") - return - } - - if err = db.DeleteSource(source); err != nil { + id := c.ParamsInt64(":authid") + if err := db.LoginSources.DeleteByID(id); err != nil { if db.IsErrLoginSourceInUse(err) { c.Flash.Error(c.Tr("admin.auths.still_in_used")) } else { @@ -269,7 +280,7 @@ func DeleteAuthSource(c *context.Context) { }) return } - log.Trace("Authentication deleted by admin(%s): %d", c.User.Name, source.ID) + log.Trace("Authentication deleted by admin(%s): %d", c.User.Name, id) c.Flash.Success(c.Tr("admin.auths.deletion_success")) c.JSONSuccess(map[string]interface{}{ diff --git a/internal/route/admin/users.go b/internal/route/admin/users.go index 96257c59..caf2109e 100644 --- a/internal/route/admin/users.go +++ b/internal/route/admin/users.go @@ -46,7 +46,7 @@ func NewUser(c *context.Context) { c.Data["login_type"] = "0-0" - sources, err := db.ListLoginSources() + sources, err := db.LoginSources.List(db.ListLoginSourceOpts{}) if err != nil { c.Error(err, "list login sources") return @@ -62,7 +62,7 @@ func NewUserPost(c *context.Context, f form.AdminCrateUser) { c.Data["PageIsAdmin"] = true c.Data["PageIsAdminUsers"] = true - sources, err := db.ListLoginSources() + sources, err := db.LoginSources.List(db.ListLoginSourceOpts{}) if err != nil { c.Error(err, "list login sources") return @@ -141,7 +141,7 @@ func prepareUserInfo(c *context.Context) *db.User { c.Data["LoginSource"] = &db.LoginSource{} } - sources, err := db.ListLoginSources() + sources, err := db.LoginSources.List(db.ListLoginSourceOpts{}) if err != nil { c.Error(err, "list login sources") return nil diff --git a/internal/route/api/v1/admin/user.go b/internal/route/api/v1/admin/user.go index 06c6569f..0593475a 100644 --- a/internal/route/api/v1/admin/user.go +++ b/internal/route/api/v1/admin/user.go @@ -13,7 +13,6 @@ import ( "gogs.io/gogs/internal/conf" "gogs.io/gogs/internal/context" "gogs.io/gogs/internal/db" - "gogs.io/gogs/internal/db/errors" "gogs.io/gogs/internal/email" "gogs.io/gogs/internal/route/api/v1/user" ) @@ -25,7 +24,7 @@ func parseLoginSource(c *context.APIContext, u *db.User, sourceID int64, loginNa source, err := db.LoginSources.GetByID(sourceID) if err != nil { - if errors.IsLoginSourceNotExist(err) { + if db.IsErrLoginSourceNotExist(err) { c.ErrorStatus(http.StatusUnprocessableEntity, err) } else { c.Error(err, "get login source by ID") diff --git a/internal/route/install.go b/internal/route/install.go index fbb605f0..5bbf943a 100644 --- a/internal/route/install.go +++ b/internal/route/install.go @@ -76,7 +76,6 @@ func GlobalInit(customConf string) error { } db.HasEngine = true - db.LoadAuthSources() db.LoadRepoConfig() db.NewRepoContext() diff --git a/internal/route/user/auth.go b/internal/route/user/auth.go index 22e8176c..be23b78d 100644 --- a/internal/route/user/auth.go +++ b/internal/route/user/auth.go @@ -101,7 +101,7 @@ func Login(c *context.Context) { } // Display normal login page - loginSources, err := db.ActivatedLoginSources() + loginSources, err := db.LoginSources.List(db.ListLoginSourceOpts{OnlyActivated: true}) if err != nil { c.Error(err, "list activated login sources") return @@ -148,7 +148,7 @@ func afterLogin(c *context.Context, u *db.User, remember bool) { func LoginPost(c *context.Context, f form.SignIn) { c.Title("sign_in") - loginSources, err := db.ActivatedLoginSources() + loginSources, err := db.LoginSources.List(db.ListLoginSourceOpts{OnlyActivated: true}) if err != nil { c.Error(err, "list activated login sources") return |