summaryrefslogtreecommitdiffstats
path: root/models/login.go
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-06-08 17:53:53 -0400
committerUnknown <joe2010xtmf@163.com>2014-06-08 17:53:53 -0400
commitf160b4f33ca69df13b071648aad09e561dafec26 (patch)
treedc630164a3c25c70e4ce5f2257b1a4a69a488d7a /models/login.go
parent1f58d6f5d912187653db442e17d20261fc970b7b (diff)
downloadgitea-f160b4f33ca69df13b071648aad09e561dafec26.tar.gz
gitea-f160b4f33ca69df13b071648aad09e561dafec26.zip
Add tar.gz download button and other mirror updates
Diffstat (limited to 'models/login.go')
-rw-r--r--models/login.go83
1 files changed, 38 insertions, 45 deletions
diff --git a/models/login.go b/models/login.go
index 984a9f8c30..863ba44e20 100644
--- a/models/login.go
+++ b/models/login.go
@@ -1,4 +1,4 @@
-// Copyright github.com/juju2013. All rights reserved.
+// 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.
@@ -20,12 +20,13 @@ import (
"github.com/gogits/gogs/modules/log"
)
-// Login types.
+type LoginType int
+
const (
- LT_NOTYPE = iota
- LT_PLAIN
- LT_LDAP
- LT_SMTP
+ NOTYPE LoginType = iota
+ PLAIN
+ LDAP
+ SMTP
)
var (
@@ -34,9 +35,9 @@ var (
ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users")
)
-var LoginTypes = map[int]string{
- LT_LDAP: "LDAP",
- LT_SMTP: "SMTP",
+var LoginTypes = map[LoginType]string{
+ LDAP: "LDAP",
+ SMTP: "SMTP",
}
// Ensure structs implmented interface.
@@ -49,7 +50,6 @@ type LDAPConfig struct {
ldap.Ldapsource
}
-// implement
func (cfg *LDAPConfig) FromDB(bs []byte) error {
return json.Unmarshal(bs, &cfg.Ldapsource)
}
@@ -65,7 +65,6 @@ type SMTPConfig struct {
TLS bool
}
-// implement
func (cfg *SMTPConfig) FromDB(bs []byte) error {
return json.Unmarshal(bs, cfg)
}
@@ -76,13 +75,13 @@ func (cfg *SMTPConfig) ToDB() ([]byte, error) {
type LoginSource struct {
Id int64
- Type int
- Name string `xorm:"unique"`
- IsActived bool `xorm:"not null default false"`
+ Type LoginType
+ Name string `xorm:"UNIQUE"`
+ IsActived bool `xorm:"NOT NULL DEFAULT false"`
Cfg core.Conversion `xorm:"TEXT"`
- Created time.Time `xorm:"created"`
- Updated time.Time `xorm:"updated"`
- AllowAutoRegister bool `xorm:"not null default false"`
+ AllowAutoRegister bool `xorm:"NOT NULL DEFAULT false"`
+ Created time.Time `xorm:"CREATED"`
+ Updated time.Time `xorm:"UPDATED"`
}
func (source *LoginSource) TypeString() string {
@@ -97,21 +96,25 @@ func (source *LoginSource) SMTP() *SMTPConfig {
return source.Cfg.(*SMTPConfig)
}
-// for xorm callback
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
if colName == "type" {
ty := (*val).(int64)
- switch ty {
- case LT_LDAP:
+ switch LoginType(ty) {
+ case LDAP:
source.Cfg = new(LDAPConfig)
- case LT_SMTP:
+ case SMTP:
source.Cfg = new(SMTPConfig)
}
}
}
+func CreateSource(source *LoginSource) error {
+ _, err := orm.Insert(source)
+ return err
+}
+
func GetAuths() ([]*LoginSource, error) {
- var auths = make([]*LoginSource, 0)
+ var auths = make([]*LoginSource, 0, 5)
err := orm.Find(&auths)
return auths, err
}
@@ -121,18 +124,12 @@ func GetLoginSourceById(id int64) (*LoginSource, error) {
has, err := orm.Id(id).Get(source)
if err != nil {
return nil, err
- }
- if !has {
+ } else if !has {
return nil, ErrAuthenticationNotExist
}
return source, nil
}
-func AddSource(source *LoginSource) error {
- _, err := orm.Insert(source)
- return err
-}
-
func UpdateSource(source *LoginSource) error {
_, err := orm.Id(source.Id).AllCols().Update(source)
return err
@@ -164,14 +161,14 @@ func UserSignIn(uname, passwd string) (*User, error) {
return nil, err
}
- if u.LoginType == LT_NOTYPE {
+ if u.LoginType == NOTYPE {
if has {
- u.LoginType = LT_PLAIN
+ u.LoginType = PLAIN
}
}
// for plain login, user must have existed.
- if u.LoginType == LT_PLAIN {
+ if u.LoginType == PLAIN {
if !has {
return nil, ErrUserNotExist
}
@@ -191,22 +188,20 @@ func UserSignIn(uname, passwd string) (*User, error) {
}
for _, source := range sources {
- if source.Type == LT_LDAP {
+ if source.Type == LDAP {
u, err := LoginUserLdapSource(nil, uname, passwd,
source.Id, source.Cfg.(*LDAPConfig), true)
if err == nil {
return u, nil
- } else {
- log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err)
}
- } else if source.Type == LT_SMTP {
+ log.Warn("Fail to login(%s) by LDAP(%s): %v", uname, source.Name, err)
+ } else if source.Type == SMTP {
u, err := LoginUserSMTPSource(nil, uname, passwd,
source.Id, source.Cfg.(*SMTPConfig), true)
if err == nil {
return u, nil
- } else {
- log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err)
}
+ log.Warn("Fail to login(%s) by SMTP(%s): %v", uname, source.Name, err)
}
}
@@ -224,10 +219,10 @@ func UserSignIn(uname, passwd string) (*User, error) {
}
switch u.LoginType {
- case LT_LDAP:
+ case LDAP:
return LoginUserLdapSource(u, u.LoginName, passwd,
source.Id, source.Cfg.(*LDAPConfig), false)
- case LT_SMTP:
+ case SMTP:
return LoginUserSMTPSource(u, u.LoginName, passwd,
source.Id, source.Cfg.(*SMTPConfig), false)
}
@@ -252,7 +247,7 @@ func LoginUserLdapSource(user *User, name, passwd string, sourceId int64, cfg *L
user = &User{
LowerName: strings.ToLower(name),
Name: strings.ToLower(name),
- LoginType: LT_LDAP,
+ LoginType: LDAP,
LoginSource: sourceId,
LoginName: name,
IsActive: true,
@@ -320,9 +315,8 @@ func SmtpAuth(host string, port int, a smtp.Auth, useTls bool) error {
return err
}
return nil
- } else {
- return ErrUnsupportedLoginType
}
+ return ErrUnsupportedLoginType
}
// Query if name/passwd can login against the LDAP direcotry pool
@@ -358,13 +352,12 @@ func LoginUserSMTPSource(user *User, name, passwd string, sourceId int64, cfg *S
user = &User{
LowerName: strings.ToLower(loginName),
Name: strings.ToLower(loginName),
- LoginType: LT_SMTP,
+ LoginType: SMTP,
LoginSource: sourceId,
LoginName: name,
IsActive: true,
Passwd: passwd,
Email: name,
}
-
return RegisterUser(user)
}