From 3b7465f8171ddcc992498e14d8b608cad34c26da Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 5 May 2014 05:32:47 -0400 Subject: [PATCH] fix code --- models/login.go | 7 +++- modules/auth/admin.go | 4 +-- modules/auth/authentication.go | 64 ++++++++++++++++++++++++++++----- routers/admin/auths.go | 19 ++++++---- routers/admin/user.go | 5 ++- templates/admin/auths/edit.tmpl | 53 +++++++++++++-------------- templates/admin/auths/new.tmpl | 51 +++++++++++++------------- templates/admin/users/edit.tmpl | 26 +++++++------- templates/admin/users/new.tmpl | 21 +++++------ 9 files changed, 155 insertions(+), 95 deletions(-) diff --git a/models/login.go b/models/login.go index d5905eb36..21e1ce686 100644 --- a/models/login.go +++ b/models/login.go @@ -1,3 +1,7 @@ +// Copyright github.com/juju2013. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package models import ( @@ -7,6 +11,7 @@ import ( "github.com/go-xorm/core" "github.com/go-xorm/xorm" + "github.com/gogits/gogs/modules/auth/ldap" ) @@ -19,7 +24,7 @@ const ( var ( ErrAuthenticationAlreadyExist = errors.New("Authentication already exist") - ErrAuthenticationNotExist = errors.New("Authentication is not exist") + ErrAuthenticationNotExist = errors.New("Authentication does not exist") ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users") ) diff --git a/modules/auth/admin.go b/modules/auth/admin.go index c82fa7815..39d2d0962 100644 --- a/modules/auth/admin.go +++ b/modules/auth/admin.go @@ -20,8 +20,8 @@ type AdminEditUserForm struct { Website string `form:"website" binding:"MaxSize(50)"` Location string `form:"location" binding:"MaxSize(50)"` Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"` - Active string `form:"active"` - Admin string `form:"admin"` + Active bool `form:"active"` + Admin bool `form:"admin"` LoginType int `form:"login_type"` } diff --git a/modules/auth/authentication.go b/modules/auth/authentication.go index d9c61b9d7..376a52e9a 100644 --- a/modules/auth/authentication.go +++ b/modules/auth/authentication.go @@ -1,15 +1,63 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package auth +import ( + "net/http" + "reflect" + + "github.com/go-martini/martini" + + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" + "github.com/gogits/gogs/modules/middleware/binding" +) + type AuthenticationForm struct { Id int64 `form:"id"` Type int `form:"type"` - Name string `form:"name" binding:"MaxSize(50)"` - Domain string `form:"domain"` - Host string `form:"host"` - Port int `form:"port"` - BaseDN string `form:"base_dn"` - Attributes string `form:"attributes"` - Filter string `form:"filter"` - MsAdSA string `form:"ms_ad_sa"` + AuthName string `form:"name" binding:"Required;MaxSize(50)"` + Domain string `form:"domain" binding:"Required"` + Host string `form:"host" binding:"Required"` + Port int `form:"port" binding:"Required"` + BaseDN string `form:"base_dn" binding:"Required"` + Attributes string `form:"attributes" binding:"Required"` + Filter string `form:"filter" binding:"Required"` + MsAdSA string `form:"ms_ad_sa" binding:"Required"` IsActived bool `form:"is_actived"` } + +func (f *AuthenticationForm) Name(field string) string { + names := map[string]string{ + "AuthName": "Authentication's name", + "Domain": "Domain name", + "Host": "Host address", + "Port": "Port Number", + "BaseDN": "Base DN", + "Attributes": "Search attributes", + "Filter": "Search filter", + "MsAdSA": "Ms Ad SA", + } + return names[field] +} + +func (f *AuthenticationForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) { + if req.Method == "GET" || errors.Count() == 0 { + return + } + + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + data["HasError"] = true + AssignForm(f, data) + + if len(errors.Overall) > 0 { + for _, err := range errors.Overall { + log.Error("AuthenticationForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} diff --git a/routers/admin/auths.go b/routers/admin/auths.go index 892413049..40318c3d2 100644 --- a/routers/admin/auths.go +++ b/routers/admin/auths.go @@ -1,15 +1,20 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package admin import ( "strings" "github.com/go-martini/martini" + "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth/ldap" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" - "github.com/gpmgo/gopm/log" ) func NewAuthSource(ctx *middleware.Context) { @@ -37,11 +42,11 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { Filter: form.Filter, MsAdSAFormat: form.MsAdSA, Enabled: true, - Name: form.Name, + Name: form.AuthName, }, } - if err := models.AddLDAPSource(form.Name, u); err != nil { + if err := models.AddLDAPSource(form.AuthName, u); err != nil { switch err { default: ctx.Handle(500, "admin.auths.NewAuth", err) @@ -50,7 +55,7 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { } log.Trace("%s Authentication created by admin(%s): %s", ctx.Req.RequestURI, - ctx.User.LowerName, strings.ToLower(form.Name)) + ctx.User.LowerName, strings.ToLower(form.AuthName)) ctx.Redirect("/admin/auths") } @@ -83,7 +88,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { } u := models.LoginSource{ - Name: form.Name, + Name: form.AuthName, IsActived: form.IsActived, Type: models.LT_LDAP, Cfg: &models.LDAPConfig{ @@ -95,7 +100,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { Filter: form.Filter, MsAdSAFormat: form.MsAdSA, Enabled: true, - Name: form.Name, + Name: form.AuthName, }, }, } @@ -109,7 +114,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) { } log.Trace("%s Authentication changed by admin(%s): %s", ctx.Req.RequestURI, - ctx.User.LowerName, strings.ToLower(form.Name)) + ctx.User.LowerName, strings.ToLower(form.AuthName)) ctx.Redirect("/admin/auths") } diff --git a/routers/admin/user.go b/routers/admin/user.go index 14c17e975..f2e1b0473 100644 --- a/routers/admin/user.go +++ b/routers/admin/user.go @@ -130,9 +130,8 @@ func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.Admi u.Location = form.Location u.Avatar = base.EncodeMd5(form.Avatar) u.AvatarEmail = form.Avatar - u.IsActive = form.Active == "on" - u.IsAdmin = form.Admin == "on" - u.LoginType = form.LoginType + u.IsActive = form.Active + u.IsAdmin = form.Admin if err := models.UpdateUser(u); err != nil { ctx.Handle(500, "admin.user.EditUser", err) return diff --git a/templates/admin/auths/edit.tmpl b/templates/admin/auths/edit.tmpl index 1a2548fbe..e040ea8f7 100644 --- a/templates/admin/auths/edit.tmpl +++ b/templates/admin/auths/edit.tmpl @@ -15,69 +15,70 @@ {{template "base/alert" .}}
- -
- -
-
-
+ +
+ +
+
+ +
- +
-
+
- +
-
+
- +
-
+
- +
-
+
- +
-
+
- +
-
+
- +
-
+
- +
diff --git a/templates/admin/auths/new.tmpl b/templates/admin/auths/new.tmpl index a5da93bd2..1ed23569e 100644 --- a/templates/admin/auths/new.tmpl +++ b/templates/admin/auths/new.tmpl @@ -14,68 +14,69 @@ {{.CsrfTokenHtml}} {{template "base/alert" .}}
- -
- -
-
-
+ +
+ +
+
+ +
- +
-
+
- +
-
+
- +
-
+
- +
-
+
- +
-
+
- +
-
+
- +
-
+
- +
diff --git a/templates/admin/users/edit.tmpl b/templates/admin/users/edit.tmpl index 9c9c36a28..1fb29234e 100644 --- a/templates/admin/users/edit.tmpl +++ b/templates/admin/users/edit.tmpl @@ -13,19 +13,19 @@
{{.CsrfTokenHtml}} {{template "base/alert" .}} - -
- -
- -
-
+
+ +
+ +
+
+
diff --git a/templates/admin/users/new.tmpl b/templates/admin/users/new.tmpl index c19cd53c0..88da16aa4 100644 --- a/templates/admin/users/new.tmpl +++ b/templates/admin/users/new.tmpl @@ -14,16 +14,17 @@ {{.CsrfTokenHtml}} {{template "base/alert" .}}
- -
- -
-
+ +
+ +
+
+