mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
finish new edit auth UI
This commit is contained in:
@@ -65,6 +65,37 @@ func NewAuthSource(ctx *middleware.Context) {
|
||||
ctx.HTML(200, AUTH_NEW)
|
||||
}
|
||||
|
||||
func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
|
||||
return &models.LDAPConfig{
|
||||
Ldapsource: ldap.Ldapsource{
|
||||
Name: form.Name,
|
||||
Host: form.Host,
|
||||
Port: form.Port,
|
||||
UseSSL: form.TLS,
|
||||
BindDN: form.BindDN,
|
||||
UserDN: form.UserDN,
|
||||
BindPassword: form.BindPassword,
|
||||
UserBase: form.UserBase,
|
||||
AttributeName: form.AttributeName,
|
||||
AttributeSurname: form.AttributeSurname,
|
||||
AttributeMail: form.AttributeMail,
|
||||
Filter: form.Filter,
|
||||
AdminFilter: form.AdminFilter,
|
||||
Enabled: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func parseSMTPConfig(form auth.AuthenticationForm) *models.SMTPConfig {
|
||||
return &models.SMTPConfig{
|
||||
Auth: form.SMTPAuth,
|
||||
Host: form.SMTPHost,
|
||||
Port: form.SMTPPort,
|
||||
TLS: form.TLS,
|
||||
SkipVerify: form.SkipVerify,
|
||||
}
|
||||
}
|
||||
|
||||
func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
|
||||
ctx.Data["Title"] = ctx.Tr("admin.auths.new")
|
||||
ctx.Data["PageIsAdmin"] = true
|
||||
@@ -79,126 +110,12 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
|
||||
return
|
||||
}
|
||||
|
||||
var u core.Conversion
|
||||
switch models.LoginType(form.Type) {
|
||||
case models.LDAP, models.DLDAP:
|
||||
u = &models.LDAPConfig{
|
||||
Ldapsource: ldap.Ldapsource{
|
||||
Name: form.Name,
|
||||
Host: form.Host,
|
||||
Port: form.Port,
|
||||
UseSSL: form.UseSSL,
|
||||
BindDN: form.BindDN,
|
||||
UserDN: form.UserDN,
|
||||
BindPassword: form.BindPassword,
|
||||
UserBase: form.UserBase,
|
||||
AttributeName: form.AttributeName,
|
||||
AttributeSurname: form.AttributeSurname,
|
||||
AttributeMail: form.AttributeMail,
|
||||
Filter: form.Filter,
|
||||
AdminFilter: form.AdminFilter,
|
||||
Enabled: true,
|
||||
},
|
||||
}
|
||||
case models.SMTP:
|
||||
u = &models.SMTPConfig{
|
||||
Auth: form.SMTPAuth,
|
||||
Host: form.SMTPHost,
|
||||
Port: form.SMTPPort,
|
||||
TLS: form.TLS,
|
||||
SkipVerify: form.SkipVerify,
|
||||
}
|
||||
case models.PAM:
|
||||
u = &models.PAMConfig{
|
||||
ServiceName: form.PAMServiceName,
|
||||
}
|
||||
default:
|
||||
ctx.Error(400)
|
||||
return
|
||||
}
|
||||
|
||||
var source = &models.LoginSource{
|
||||
Type: models.LoginType(form.Type),
|
||||
Name: form.Name,
|
||||
IsActived: form.IsActive,
|
||||
AllowAutoRegister: form.AllowAutoRegister,
|
||||
Cfg: u,
|
||||
}
|
||||
|
||||
if err := models.CreateSource(source); err != nil {
|
||||
ctx.Handle(500, "CreateSource", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
|
||||
ctx.Redirect(setting.AppSubUrl + "/admin/auths")
|
||||
}
|
||||
|
||||
func EditAuthSource(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
|
||||
ctx.Data["PageIsAdmin"] = true
|
||||
ctx.Data["PageIsAdminAuthentications"] = true
|
||||
// ctx.Data["LoginTypes"] = models.LoginTypes
|
||||
ctx.Data["SMTPAuths"] = models.SMTPAuths
|
||||
|
||||
id := com.StrTo(ctx.Params(":authid")).MustInt64()
|
||||
if id == 0 {
|
||||
ctx.Handle(404, "EditAuthSource", nil)
|
||||
return
|
||||
}
|
||||
u, err := models.GetLoginSourceByID(id)
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetLoginSourceById", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Source"] = u
|
||||
ctx.HTML(200, AUTH_EDIT)
|
||||
}
|
||||
|
||||
func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
|
||||
ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
|
||||
ctx.Data["PageIsAdmin"] = true
|
||||
ctx.Data["PageIsAdminAuthentications"] = true
|
||||
ctx.Data["PageIsAuths"] = true
|
||||
// ctx.Data["LoginTypes"] = models.LoginTypes
|
||||
ctx.Data["SMTPAuths"] = models.SMTPAuths
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, AUTH_EDIT)
|
||||
return
|
||||
}
|
||||
|
||||
var config core.Conversion
|
||||
switch models.LoginType(form.Type) {
|
||||
case models.LDAP:
|
||||
fallthrough
|
||||
case models.DLDAP:
|
||||
config = &models.LDAPConfig{
|
||||
Ldapsource: ldap.Ldapsource{
|
||||
Name: form.Name,
|
||||
Host: form.Host,
|
||||
Port: form.Port,
|
||||
UseSSL: form.UseSSL,
|
||||
BindDN: form.BindDN,
|
||||
UserDN: form.UserDN,
|
||||
BindPassword: form.BindPassword,
|
||||
UserBase: form.UserBase,
|
||||
AttributeName: form.AttributeName,
|
||||
AttributeSurname: form.AttributeSurname,
|
||||
AttributeMail: form.AttributeMail,
|
||||
Filter: form.Filter,
|
||||
AdminFilter: form.AdminFilter,
|
||||
Enabled: true,
|
||||
},
|
||||
}
|
||||
case models.LDAP, models.DLDAP:
|
||||
config = parseLDAPConfig(form)
|
||||
case models.SMTP:
|
||||
config = &models.SMTPConfig{
|
||||
Auth: form.SMTPAuth,
|
||||
Host: form.SMTPHost,
|
||||
Port: form.SMTPPort,
|
||||
TLS: form.TLS,
|
||||
SkipVerify: form.SkipVerify,
|
||||
}
|
||||
config = parseSMTPConfig(form)
|
||||
case models.PAM:
|
||||
config = &models.PAMConfig{
|
||||
ServiceName: form.PAMServiceName,
|
||||
@@ -208,48 +125,108 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
|
||||
return
|
||||
}
|
||||
|
||||
u := models.LoginSource{
|
||||
ID: form.ID,
|
||||
if err := models.CreateSource(&models.LoginSource{
|
||||
Type: models.LoginType(form.Type),
|
||||
Name: form.Name,
|
||||
IsActived: form.IsActive,
|
||||
Type: models.LoginType(form.Type),
|
||||
AllowAutoRegister: form.AllowAutoRegister,
|
||||
Cfg: config,
|
||||
}); err != nil {
|
||||
ctx.Handle(500, "CreateSource", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.UpdateSource(&u); err != nil {
|
||||
log.Trace("Authentication created by admin(%s): %s", ctx.User.Name, form.Name)
|
||||
|
||||
ctx.Flash.Success(ctx.Tr("admin.auths.new_success", form.Name))
|
||||
ctx.Redirect(setting.AppSubUrl + "/admin/auths")
|
||||
}
|
||||
|
||||
func EditAuthSource(ctx *middleware.Context) {
|
||||
ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
|
||||
ctx.Data["PageIsAdmin"] = true
|
||||
ctx.Data["PageIsAdminAuthentications"] = true
|
||||
|
||||
ctx.Data["SMTPAuths"] = models.SMTPAuths
|
||||
|
||||
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetLoginSourceByID", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Source"] = source
|
||||
ctx.HTML(200, AUTH_EDIT)
|
||||
}
|
||||
|
||||
func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
|
||||
ctx.Data["Title"] = ctx.Tr("admin.auths.edit")
|
||||
ctx.Data["PageIsAdmin"] = true
|
||||
ctx.Data["PageIsAdminAuthentications"] = true
|
||||
|
||||
ctx.Data["SMTPAuths"] = models.SMTPAuths
|
||||
|
||||
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetLoginSourceByID", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["Source"] = source
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(200, AUTH_EDIT)
|
||||
return
|
||||
}
|
||||
|
||||
var config core.Conversion
|
||||
switch models.LoginType(form.Type) {
|
||||
case models.LDAP, models.DLDAP:
|
||||
config = parseLDAPConfig(form)
|
||||
case models.SMTP:
|
||||
config = parseSMTPConfig(form)
|
||||
case models.PAM:
|
||||
config = &models.PAMConfig{
|
||||
ServiceName: form.PAMServiceName,
|
||||
}
|
||||
default:
|
||||
ctx.Error(400)
|
||||
return
|
||||
}
|
||||
|
||||
source.Name = form.Name
|
||||
source.IsActived = form.IsActive
|
||||
source.AllowAutoRegister = form.AllowAutoRegister
|
||||
source.Cfg = config
|
||||
if err := models.UpdateSource(source); err != nil {
|
||||
ctx.Handle(500, "UpdateSource", err)
|
||||
return
|
||||
}
|
||||
log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, source.ID)
|
||||
|
||||
log.Trace("Authentication changed by admin(%s): %s", ctx.User.Name, form.Name)
|
||||
ctx.Flash.Success(ctx.Tr("admin.auths.update_success"))
|
||||
ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
|
||||
ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + com.ToStr(form.ID))
|
||||
}
|
||||
|
||||
func DeleteAuthSource(ctx *middleware.Context) {
|
||||
id := com.StrTo(ctx.Params(":authid")).MustInt64()
|
||||
if id == 0 {
|
||||
ctx.Handle(404, "DeleteAuthSource", nil)
|
||||
return
|
||||
}
|
||||
|
||||
a, err := models.GetLoginSourceByID(id)
|
||||
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
|
||||
if err != nil {
|
||||
ctx.Handle(500, "GetLoginSourceById", err)
|
||||
ctx.Handle(500, "GetLoginSourceByID", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = models.DelLoginSource(a); err != nil {
|
||||
if err = models.DeleteSource(source); err != nil {
|
||||
switch err {
|
||||
case models.ErrAuthenticationUserUsed:
|
||||
ctx.Flash.Error("form.still_own_user")
|
||||
ctx.Redirect(setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"))
|
||||
default:
|
||||
ctx.Handle(500, "DelLoginSource", err)
|
||||
ctx.Handle(500, "DeleteSource", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Trace("Authentication deleted by admin(%s): %s", ctx.User.Name, a.Name)
|
||||
ctx.Redirect(setting.AppSubUrl + "/admin/auths")
|
||||
log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID)
|
||||
|
||||
ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success"))
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"redirect": setting.AppSubUrl + "/admin/auths",
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user