Skip to content

(Preview) Fix no same naming convention between template and form #22846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions models/auth/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@ func (typ Type) Int() int {
return int(typ)
}

// IsLDAP returns true of this type is of the LDAP type.
func (typ Type) IsLDAP() bool {
return typ == LDAP
}

// IsDLDAP returns true of this type is of the DLDAP type.
func (typ Type) IsDLDAP() bool {
return typ == DLDAP
}

// IsSMTP returns true of this type is of the SMTP type.
func (typ Type) IsSMTP() bool {
return typ == SMTP
}

// IsPAM returns true of this type is of the PAM type.
func (typ Type) IsPAM() bool {
return typ == PAM
}

// IsOAuth2 returns true of this type is of the OAuth2 type.
func (typ Type) IsOAuth2() bool {
return typ == OAuth2
}

// IsSSPI returns true of this type is of the SSPI type.
func (typ Type) IsSSPI() bool {
return typ == SSPI
}

// Names contains the name of LoginType values.
var Names = map[Type]string{
LDAP: "LDAP (via BindDN)",
Expand Down Expand Up @@ -150,32 +180,32 @@ func (source *Source) TypeName() string {

// IsLDAP returns true of this source is of the LDAP type.
func (source *Source) IsLDAP() bool {
return source.Type == LDAP
return source.Type.IsLDAP()
}

// IsDLDAP returns true of this source is of the DLDAP type.
func (source *Source) IsDLDAP() bool {
return source.Type == DLDAP
return source.Type.IsDLDAP()
}

// IsSMTP returns true of this source is of the SMTP type.
func (source *Source) IsSMTP() bool {
return source.Type == SMTP
return source.Type.IsSMTP()
}

// IsPAM returns true of this source is of the PAM type.
func (source *Source) IsPAM() bool {
return source.Type == PAM
return source.Type.IsPAM()
}

// IsOAuth2 returns true of this source is of the OAuth2 type.
func (source *Source) IsOAuth2() bool {
return source.Type == OAuth2
return source.Type.IsOAuth2()
}

// IsSSPI returns true of this source is of the SSPI type.
func (source *Source) IsSSPI() bool {
return source.Type == SSPI
return source.Type.IsSSPI()
}

// HasTLS returns true of this source supports TLS.
Expand Down
23 changes: 2 additions & 21 deletions modules/web/middleware/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,8 @@ func init() {

// AssignForm assign form values back to the template data.
func AssignForm(form interface{}, data map[string]interface{}) {
typ := reflect.TypeOf(form)
val := reflect.ValueOf(form)

for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}

for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)

fieldName := field.Tag.Get("form")
// Allow ignored fields in the struct
if fieldName == "-" {
continue
} else if len(fieldName) == 0 {
fieldName = util.ToSnakeCase(field.Name)
}

data[fieldName] = val.Field(i).Interface()
}
// TODO: Allow ignored fields in the struct?
data["Form"] = form
}

func getRuleBody(field reflect.StructField, prefix string) string {
Expand Down
179 changes: 129 additions & 50 deletions routers/web/admin/auths.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,24 @@ func NewAuthSource(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminAuthentications"] = true

ctx.Data["type"] = auth.LDAP.Int()
ctx.Data["CurrentTypeName"] = auth.Names[auth.LDAP]
ctx.Data["CurrentSecurityProtocol"] = ldap.SecurityProtocolNames[ldap.SecurityProtocolUnencrypted]
ctx.Data["smtp_auth"] = "PLAIN"
ctx.Data["is_active"] = true
ctx.Data["is_sync_enabled"] = true
ctx.Data["TypeNames"] = auth.Names
ctx.Data["AuthSources"] = authSources
ctx.Data["SecurityProtocols"] = securityProtocols
ctx.Data["SMTPAuths"] = smtp.Authenticators
oauth2providers := oauth2.GetOAuth2Providers()
ctx.Data["OAuth2Providers"] = oauth2providers

ctx.Data["SSPIAutoCreateUsers"] = true
ctx.Data["SSPIAutoActivateUsers"] = true
ctx.Data["SSPIStripDomainNames"] = true
ctx.Data["SSPISeparatorReplacement"] = "_"
ctx.Data["SSPIDefaultLanguage"] = ""

// only the first as default
ctx.Data["oauth2_provider"] = oauth2providers[0].Name()
ctx.Data["Form"] = forms.AuthenticationForm{
Type: auth.LDAP.Int(),
SMTPAuth: "PLAIN",
IsActive: true,
IsSyncEnabled: true,
Oauth2Provider: oauth2providers[0].Name(), // only the first as default
SSPIAutoCreateUsers: true,
SSPIAutoActivateUsers: true,
SSPIStripDomainNames: true,
SSPISeparatorReplacement: "_",
}

ctx.HTML(http.StatusOK, tplAuthNew)
}
Expand Down Expand Up @@ -240,20 +238,12 @@ func NewAuthSourcePost(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminAuthentications"] = true

ctx.Data["CurrentTypeName"] = auth.Type(form.Type).String()
ctx.Data["CurrentSecurityProtocol"] = ldap.SecurityProtocolNames[ldap.SecurityProtocol(form.SecurityProtocol)]
ctx.Data["AuthSources"] = authSources
ctx.Data["SecurityProtocols"] = securityProtocols
ctx.Data["SMTPAuths"] = smtp.Authenticators
oauth2providers := oauth2.GetOAuth2Providers()
ctx.Data["OAuth2Providers"] = oauth2providers

ctx.Data["SSPIAutoCreateUsers"] = true
ctx.Data["SSPIAutoActivateUsers"] = true
ctx.Data["SSPIStripDomainNames"] = true
ctx.Data["SSPISeparatorReplacement"] = "_"
ctx.Data["SSPIDefaultLanguage"] = ""

hasTLS := false
var config convert.Conversion
switch auth.Type(form.Type) {
Expand Down Expand Up @@ -330,6 +320,115 @@ func NewAuthSourcePost(ctx *context.Context) {
ctx.Redirect(setting.AppSubURL + "/admin/auths")
}

func parseSource(ctx *context.Context) *auth.Source {
source, err := auth.GetSourceByID(ctx.ParamsInt64(":authid"))
if err != nil {
ctx.ServerError("auth.GetSourceByID", err)
return nil
}
ctx.Data["HasTLS"] = source.HasTLS()
ctx.Data["Type"] = source.Type

form := forms.AuthenticationForm{
ID: source.ID,
Type: source.Type.Int(),
Name: source.Name,
IsActive: source.IsActive,
IsSyncEnabled: source.IsSyncEnabled,
}

if source.Cfg != nil {
switch source.Type {
case auth.LDAP, auth.DLDAP:
cfg := source.Cfg.(*ldap.Source)
if cfg.SearchPageSize > 0 {
form.UsePagedSearch = true
}
form.Host = cfg.Host
form.Port = cfg.Port
form.SecurityProtocol = cfg.SecurityProtocol.Int()
form.SkipVerify = cfg.SkipVerify
form.BindDN = cfg.BindDN
form.BindPassword = cfg.BindPassword
form.UserBase = cfg.UserBase
form.AttributeUsername = cfg.AttributeUsername
form.AttributeName = cfg.AttributeName
form.AttributeSurname = cfg.AttributeSurname
form.AttributeMail = cfg.AttributeMail
form.AttributesInBind = cfg.AttributesInBind
form.AttributeSSHPublicKey = cfg.AttributeSSHPublicKey
form.AttributeAvatar = cfg.AttributeAvatar
form.SearchPageSize = int(cfg.SearchPageSize)
form.Filter = cfg.Filter
form.GroupsEnabled = cfg.GroupsEnabled
form.GroupDN = cfg.GroupDN
form.GroupFilter = cfg.GroupFilter
form.GroupMemberUID = cfg.GroupMemberUID
form.GroupTeamMap = cfg.GroupTeamMap
form.GroupTeamMapRemoval = cfg.GroupTeamMapRemoval
form.UserUID = cfg.UserUID
form.AdminFilter = cfg.AdminFilter
form.RestrictedFilter = cfg.RestrictedFilter
form.AllowDeactivateAll = cfg.AllowDeactivateAll
//form.Enabled=cfg.Enabled
form.SkipLocalTwoFA = cfg.SkipLocalTwoFA
case auth.SMTP:
cfg := source.Cfg.(*smtp.Source)
form.SMTPAuth = cfg.Auth
form.SMTPHost = cfg.Host
form.SMTPPort = cfg.Port
form.AllowedDomains = cfg.AllowedDomains
form.ForceSMTPS = cfg.ForceSMTPS
form.SkipVerify = cfg.SkipVerify
form.HeloHostname = cfg.HeloHostname
form.DisableHelo = cfg.DisableHelo
form.SkipLocalTwoFA = cfg.SkipLocalTwoFA
case auth.PAM:
cfg := source.Cfg.(*pam_service.Source)
form.PAMServiceName = cfg.ServiceName
form.PAMEmailDomain = cfg.EmailDomain
form.SkipLocalTwoFA = cfg.SkipLocalTwoFA
case auth.OAuth2:
cfg := source.Cfg.(*oauth2.Source)
form.Oauth2Provider = cfg.Provider
form.Oauth2Key = cfg.ClientID
form.Oauth2Secret = cfg.ClientSecret
form.OpenIDConnectAutoDiscoveryURL = cfg.OpenIDConnectAutoDiscoveryURL
if cfg.CustomURLMapping != nil {
form.Oauth2UseCustomURL = true
form.Oauth2TokenURL = cfg.CustomURLMapping.TokenURL
form.Oauth2AuthURL = cfg.CustomURLMapping.AuthURL
form.Oauth2ProfileURL = cfg.CustomURLMapping.ProfileURL
form.Oauth2EmailURL = cfg.CustomURLMapping.EmailURL
form.Oauth2Tenant = cfg.CustomURLMapping.Tenant
}
form.Oauth2IconURL = cfg.IconURL
form.Oauth2Scopes = strings.Join(cfg.Scopes, ",")
form.Oauth2RequiredClaimName = cfg.RequiredClaimName
form.Oauth2RequiredClaimValue = cfg.RequiredClaimValue
form.SkipLocalTwoFA = cfg.SkipLocalTwoFA
form.Oauth2GroupClaimName = cfg.GroupClaimName
form.Oauth2RestrictedGroup = cfg.RestrictedGroup
form.Oauth2AdminGroup = cfg.AdminGroup
form.Oauth2GroupTeamMap = cfg.GroupTeamMap
form.Oauth2GroupTeamMapRemoval = cfg.GroupTeamMapRemoval
case auth.SSPI:
cfg := source.Cfg.(*sspi.Source)
form.SSPIAutoCreateUsers = cfg.AutoCreateUsers
form.SSPIAutoActivateUsers = cfg.AutoActivateUsers
form.SSPIStripDomainNames = cfg.StripDomainNames
form.SSPISeparatorReplacement = cfg.SeparatorReplacement
form.SSPIDefaultLanguage = cfg.DefaultLanguage
default:
ctx.Error(http.StatusBadRequest)
return nil
}
}
ctx.Data["Form"] = form

return source
}

// EditAuthSource render editing auth source page
func EditAuthSource(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
Expand All @@ -340,27 +439,12 @@ func EditAuthSource(ctx *context.Context) {
ctx.Data["SMTPAuths"] = smtp.Authenticators
oauth2providers := oauth2.GetOAuth2Providers()
ctx.Data["OAuth2Providers"] = oauth2providers
ctx.Data["SourceTypeNames"] = auth.Names

source, err := auth.GetSourceByID(ctx.ParamsInt64(":authid"))
if err != nil {
ctx.ServerError("auth.GetSourceByID", err)
parseSource(ctx)
if ctx.Written() {
return
}
ctx.Data["Source"] = source
ctx.Data["HasTLS"] = source.HasTLS()

if source.IsOAuth2() {
type Named interface {
Name() string
}

for _, provider := range oauth2providers {
if provider.Name() == source.Cfg.(Named).Name() {
ctx.Data["CurrentOAuth2Provider"] = provider
break
}
}
}

ctx.HTML(http.StatusOK, tplAuthEdit)
}
Expand All @@ -372,23 +456,18 @@ func EditAuthSourcePost(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminAuthentications"] = true

ctx.Data["SecurityProtocols"] = securityProtocols
ctx.Data["SMTPAuths"] = smtp.Authenticators
oauth2providers := oauth2.GetOAuth2Providers()
ctx.Data["OAuth2Providers"] = oauth2providers
ctx.Data["SourceTypeNames"] = auth.Names

source, err := auth.GetSourceByID(ctx.ParamsInt64(":authid"))
if err != nil {
ctx.ServerError("auth.GetSourceByID", err)
return
}
ctx.Data["Source"] = source
ctx.Data["HasTLS"] = source.HasTLS()

if ctx.HasError() {
ctx.HTML(http.StatusOK, tplAuthEdit)
source := parseSource(ctx)
if ctx.Written() {
return
}

var err error
var config convert.Conversion
switch auth.Type(form.Type) {
case auth.LDAP, auth.DLDAP:
Expand Down
Loading