diff options
author | Unknwon <u@gogs.io> | 2015-09-11 12:03:08 -0400 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-09-11 12:03:08 -0400 |
commit | 121a81a2c554b2de0de89ab7749fe691b5d1aba7 (patch) | |
tree | cc0206c03f546734545c3355b09a3e060eeef7e7 /routers | |
parent | 06174482826e81fb7c4675efff092e2986c4dac0 (diff) | |
download | gitea-121a81a2c554b2de0de89ab7749fe691b5d1aba7.tar.gz gitea-121a81a2c554b2de0de89ab7749fe691b5d1aba7.zip |
finish new edit auth UI
Diffstat (limited to 'routers')
-rw-r--r-- | routers/admin/auths.go | 167 |
1 files changed, 72 insertions, 95 deletions
diff --git a/routers/admin/auths.go b/routers/admin/auths.go index f62a9e3c2b..5e9d3be140 100644 --- a/routers/admin/auths.go +++ b/routers/admin/auths.go @@ -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,37 +110,14 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { return } - var u core.Conversion + var config 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, - }, - } + config = parseLDAPConfig(form) case models.SMTP: - u = &models.SMTPConfig{ - Auth: form.SMTPAuth, - Host: form.SMTPHost, - Port: form.SMTPPort, - TLS: form.TLS, - SkipVerify: form.SkipVerify, - } + config = parseSMTPConfig(form) case models.PAM: - u = &models.PAMConfig{ + config = &models.PAMConfig{ ServiceName: form.PAMServiceName, } default: @@ -117,20 +125,20 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { return } - var source = &models.LoginSource{ + if err := models.CreateSource(&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 { + Cfg: config, + }); err != nil { ctx.Handle(500, "CreateSource", err) return } 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") } @@ -138,20 +146,15 @@ 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) + source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid")) if err != nil { - ctx.Handle(500, "GetLoginSourceById", err) + ctx.Handle(500, "GetLoginSourceByID", err) return } - ctx.Data["Source"] = u + ctx.Data["Source"] = source ctx.HTML(200, AUTH_EDIT) } @@ -159,10 +162,16 @@ 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 + 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 @@ -170,35 +179,10 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { 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 +192,41 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { return } - u := models.LoginSource{ - ID: form.ID, - Name: form.Name, - IsActived: form.IsActive, - Type: models.LoginType(form.Type), - AllowAutoRegister: form.AllowAutoRegister, - Cfg: config, - } - - if err := models.UpdateSource(&u); err != nil { + 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", + }) } |