This commit is contained in:
Unknown 2014-05-05 05:32:47 -04:00
parent d8136c9c3c
commit 3b7465f817
9 changed files with 155 additions and 95 deletions

View file

@ -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 package models
import ( import (
@ -7,6 +11,7 @@ import (
"github.com/go-xorm/core" "github.com/go-xorm/core"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/auth/ldap" "github.com/gogits/gogs/modules/auth/ldap"
) )
@ -19,7 +24,7 @@ const (
var ( var (
ErrAuthenticationAlreadyExist = errors.New("Authentication already exist") 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") ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users")
) )

View file

@ -20,8 +20,8 @@ type AdminEditUserForm struct {
Website string `form:"website" binding:"MaxSize(50)"` Website string `form:"website" binding:"MaxSize(50)"`
Location string `form:"location" binding:"MaxSize(50)"` Location string `form:"location" binding:"MaxSize(50)"`
Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"` Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
Active string `form:"active"` Active bool `form:"active"`
Admin string `form:"admin"` Admin bool `form:"admin"`
LoginType int `form:"login_type"` LoginType int `form:"login_type"`
} }

View file

@ -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 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 { type AuthenticationForm struct {
Id int64 `form:"id"` Id int64 `form:"id"`
Type int `form:"type"` Type int `form:"type"`
Name string `form:"name" binding:"MaxSize(50)"` AuthName string `form:"name" binding:"Required;MaxSize(50)"`
Domain string `form:"domain"` Domain string `form:"domain" binding:"Required"`
Host string `form:"host"` Host string `form:"host" binding:"Required"`
Port int `form:"port"` Port int `form:"port" binding:"Required"`
BaseDN string `form:"base_dn"` BaseDN string `form:"base_dn" binding:"Required"`
Attributes string `form:"attributes"` Attributes string `form:"attributes" binding:"Required"`
Filter string `form:"filter"` Filter string `form:"filter" binding:"Required"`
MsAdSA string `form:"ms_ad_sa"` MsAdSA string `form:"ms_ad_sa" binding:"Required"`
IsActived bool `form:"is_actived"` 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)
}

View file

@ -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 package admin
import ( import (
"strings" "strings"
"github.com/go-martini/martini" "github.com/go-martini/martini"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/auth/ldap" "github.com/gogits/gogs/modules/auth/ldap"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/middleware"
"github.com/gpmgo/gopm/log"
) )
func NewAuthSource(ctx *middleware.Context) { func NewAuthSource(ctx *middleware.Context) {
@ -37,11 +42,11 @@ func NewAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
Filter: form.Filter, Filter: form.Filter,
MsAdSAFormat: form.MsAdSA, MsAdSAFormat: form.MsAdSA,
Enabled: true, 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 { switch err {
default: default:
ctx.Handle(500, "admin.auths.NewAuth", err) 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, 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") ctx.Redirect("/admin/auths")
} }
@ -83,7 +88,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
} }
u := models.LoginSource{ u := models.LoginSource{
Name: form.Name, Name: form.AuthName,
IsActived: form.IsActived, IsActived: form.IsActived,
Type: models.LT_LDAP, Type: models.LT_LDAP,
Cfg: &models.LDAPConfig{ Cfg: &models.LDAPConfig{
@ -95,7 +100,7 @@ func EditAuthSourcePost(ctx *middleware.Context, form auth.AuthenticationForm) {
Filter: form.Filter, Filter: form.Filter,
MsAdSAFormat: form.MsAdSA, MsAdSAFormat: form.MsAdSA,
Enabled: true, 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, 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") ctx.Redirect("/admin/auths")
} }

View file

@ -130,9 +130,8 @@ func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.Admi
u.Location = form.Location u.Location = form.Location
u.Avatar = base.EncodeMd5(form.Avatar) u.Avatar = base.EncodeMd5(form.Avatar)
u.AvatarEmail = form.Avatar u.AvatarEmail = form.Avatar
u.IsActive = form.Active == "on" u.IsActive = form.Active
u.IsAdmin = form.Admin == "on" u.IsAdmin = form.Admin
u.LoginType = form.LoginType
if err := models.UpdateUser(u); err != nil { if err := models.UpdateUser(u); err != nil {
ctx.Handle(500, "admin.user.EditUser", err) ctx.Handle(500, "admin.user.EditUser", err)
return return

View file

@ -15,69 +15,70 @@
{{template "base/alert" .}} {{template "base/alert" .}}
<input type="hidden" value="{{.Source.Id}}" name="id"/> <input type="hidden" value="{{.Source.Id}}" name="id"/>
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label">Auth Type: </label> <label class="col-md-3 control-label">Auth Type: </label>
<div class="col-md-7"> <div class="col-md-7">
<select class="form-control"> <select class="form-control">
{{$type := .Source.Type}} {{$type := .Source.Type}}
{{range $key, $val := .LoginTypes}} {{range $key, $val := .LoginTypes}}
<option value="{{$key}}" {{if eq $key $type}}selected{{end}}>{{$val}}</option> <option value="{{$key}}" {{if eq $key $type}}selected{{end}}>{{$val}}</option>
{{end}} {{end}}
</select> </select>
</div> </div>
</div> </div>
<div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}">
<div class="form-group {{if .Err_AuthName}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Name: </label> <label class="col-md-3 control-label">Name: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="name" class="form-control" placeholder="Type account's username" value="{{.Source.Name}}" required="required"> <input name="name" class="form-control" placeholder="Type authentication's name" value="{{.Source.Name}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Domain}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Domain: </label> <label class="col-md-3 control-label">Domain: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="domain" class="form-control" placeholder="Type account's e-mail address" value="{{.Source.LDAP.Name}}" required="required" title="Email is not valid"> <input name="domain" class="form-control" placeholder="Type domain name" value="{{.Source.LDAP.Name}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Host}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Host: </label> <label class="col-md-3 control-label">Host: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="host" class="form-control" placeholder="Type account's e-mail address" value="{{.Source.LDAP.Host}}" required="required" title="Email is not valid"> <input name="host" class="form-control" placeholder="Type host address" value="{{.Source.LDAP.Host}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Port}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Port: </label> <label class="col-md-3 control-label">Port: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="port" class="form-control" placeholder="Type account's e-mail address" value="{{.Source.LDAP.Port}}" required="required" title="Email is not valid"> <input name="port" class="form-control" placeholder="Type port number" value="{{.Source.LDAP.Port}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_BaseDN}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Base DN: </label> <label class="col-md-3 control-label">Base DN: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="base_dn" class="form-control" placeholder="Type account's e-mail address" value="{{.Source.LDAP.BaseDN}}" required="required" title="Email is not valid"> <input name="base_dn" class="form-control" placeholder="Type base DN" value="{{.Source.LDAP.BaseDN}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Attributes}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Search Attributes: </label> <label class="col-md-3 control-label">Search Attributes: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="attributes" class="form-control" placeholder="Type account's e-mail address" value="{{.Source.LDAP.Attributes}}" required="required" title="Email is not valid"> <input name="attributes" class="form-control" placeholder="Type search attributes" value="{{.Source.LDAP.Attributes}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Filter}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Search Filter: </label> <label class="col-md-3 control-label">Search Filter: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="filter" class="form-control" placeholder="Type account's e-mail address" value="{{.Source.LDAP.Filter}}" required="required" title="Email is not valid"> <input name="filter" class="form-control" placeholder="Type search filter" value="{{.Source.LDAP.Filter}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_MsAdSA}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Ms Ad SA: </label> <label class="col-md-3 control-label">Ms Ad SA: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="ms_ad_sa" class="form-control" placeholder="Type account's e-mail address" value="{{.Source.LDAP.MsAdSAFormat}}" required="required" title="Email is not valid"> <input name="ms_ad_sa" class="form-control" placeholder="Type Ms Ad SA" value="{{.Source.LDAP.MsAdSAFormat}}" required="required">
</div> </div>
</div> </div>

View file

@ -14,68 +14,69 @@
{{.CsrfTokenHtml}} {{.CsrfTokenHtml}}
{{template "base/alert" .}} {{template "base/alert" .}}
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label">Auth Type: </label> <label class="col-md-3 control-label">Auth Type: </label>
<div class="col-md-7"> <div class="col-md-7">
<select class="form-control"> <select class="form-control">
{{range $key, $val := .LoginTypes}} {{range $key, $val := .LoginTypes}}
<option value="{{$key}}">{{$val}}</option> <option value="{{$key}}">{{$val}}</option>
{{end}} {{end}}
</select> </select>
</div> </div>
</div> </div>
<div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}">
<div class="form-group {{if .Err_AuthName}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Name: </label> <label class="col-md-3 control-label">Name: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="name" class="form-control" placeholder="Authentication's name" required="required"> <input name="name" class="form-control" placeholder="Type authentication's name" value="{{.name}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Domain}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Domain: </label> <label class="col-md-3 control-label">Domain: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="domain" class="form-control" placeholder="Domain name" value="{{.domain}}" required="required" title="Email is not valid"> <input name="domain" class="form-control" placeholder="Type domain name" value="{{.domain}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Host}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Host: </label> <label class="col-md-3 control-label">Host: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="host" class="form-control" placeholder="Type account's e-mail address" value="{{.email}}" required="required" title="Email is not valid"> <input name="host" class="form-control" placeholder="Type host address" value="{{.host}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Port}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Port: </label> <label class="col-md-3 control-label">Port: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="port" class="form-control" placeholder="Type account's e-mail address" value="{{.email}}" required="required" title="Email is not valid"> <input name="port" class="form-control" placeholder="Type port number" value="{{.port}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_BaseDN}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Base DN: </label> <label class="col-md-3 control-label">Base DN: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="base_dn" class="form-control" placeholder="Type account's e-mail address" value="{{.email}}" required="required" title="Email is not valid"> <input name="base_dn" class="form-control" placeholder="Type base DN" value="{{.base_dn}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Attributes}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Search Attributes: </label> <label class="col-md-3 control-label">Search Attributes: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="attributes" class="form-control" placeholder="Type account's e-mail address" value="{{.email}}" required="required" title="Email is not valid"> <input name="attributes" class="form-control" placeholder="Type search attributes" value="{{.attributes}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_Filter}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Search Filter: </label> <label class="col-md-3 control-label">Search Filter: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="filter" class="form-control" placeholder="Type account's e-mail address" value="{{.email}}" required="required" title="Email is not valid"> <input name="filter" class="form-control" placeholder="Type search filter" value="{{.filter}}" required="required">
</div> </div>
</div> </div>
<div class="form-group {{if .Err_Email}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_MsAdSA}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Ms Ad SA: </label> <label class="col-md-3 control-label">Ms Ad SA: </label>
<div class="col-md-7"> <div class="col-md-7">
<input name="ms_ad_sa" class="form-control" placeholder="Type account's e-mail address" value="{{.email}}" required="required" title="Email is not valid"> <input name="ms_ad_sa" class="form-control" placeholder="Type Ms Ad SA" value="{{.ms_ad_sa}}" required="required">
</div> </div>
</div> </div>

View file

@ -13,19 +13,19 @@
<form action="/admin/users/{{.User.Id}}" method="post" class="form-horizontal"> <form action="/admin/users/{{.User.Id}}" method="post" class="form-horizontal">
{{.CsrfTokenHtml}} {{.CsrfTokenHtml}}
{{template "base/alert" .}} {{template "base/alert" .}}
<input type="hidden" value="{{.User.Id}}" name="userId"/> <div class="form-group">
<div class="form-group"> <label class="col-md-3 control-label">Auth Source: </label>
<label class="col-md-3 control-label">Auth Source: </label> <div class="col-md-7">
<div class="col-md-7"> <select name="logintype" class="form-control">
<select name="logintype" class="form-control"> <option value="0-0"{{if eq 0 .User.LoginSource}} selected{{end}}>Local</option>
<option value="0-0"{{if eq 0 .User.LoginSource}} selected{{end}}>Local</option> {{$tp := .User.LoginSource}}
{{$tp := .User.LoginSource}} {{range $key, $val := .LoginSources}}
{{range $key, $val := .LoginSources}} <option value="{{$val.Type}}-{{$val.Id}}"{{if eq $val.Id $tp}} selected{{end}}>{{$val.Name}}</option>
<option value="{{$val.Type}}-{{$val.Id}}"{{if eq $val.Id $tp}} selected{{end}}>{{$val.Name}}</option> {{end}}
{{end}} </select>
</select> </div>
</div> </div>
</div>
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label">Username: </label> <label class="col-md-3 control-label">Username: </label>
<label class="control-label">{{.User.Name}}</label> <label class="control-label">{{.User.Name}}</label>

View file

@ -14,16 +14,17 @@
{{.CsrfTokenHtml}} {{.CsrfTokenHtml}}
{{template "base/alert" .}} {{template "base/alert" .}}
<div class="form-group"> <div class="form-group">
<label class="col-md-3 control-label">Auth Source: </label> <label class="col-md-3 control-label">Auth Source: </label>
<div class="col-md-7"> <div class="col-md-7">
<select name="logintype" class="form-control"> <select name="logintype" class="form-control">
<option value="0-0">Local</option> <option value="0-0">Local</option>
{{range $key, $val := .LoginSources}} {{range $key, $val := .LoginSources}}
<option value="{{$val.Type}}-{{$val.Id}}">{{$val.Name}}</option> <option value="{{$val.Type}}-{{$val.Id}}">{{$val.Name}}</option>
{{end}} {{end}}
</select> </select>
</div> </div>
</div> </div>
<div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}"> <div class="form-group {{if .Err_UserName}}has-error has-feedback{{end}}">
<label class="col-md-3 control-label">Username: </label> <label class="col-md-3 control-label">Username: </label>
<div class="col-md-7"> <div class="col-md-7">