summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2014-05-05 16:40:25 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2014-05-05 16:40:25 +0800
commit1652dd5068f2f3ae1851bc2321832c88af85d570 (patch)
treed8bee76f5c1016c7ee432334fe02390387b9a8e9 /models
parent79ea34e70ebe989f1a5f8fbd71cfe3109c6f8a58 (diff)
downloadgitea-1652dd5068f2f3ae1851bc2321832c88af85d570.tar.gz
gitea-1652dd5068f2f3ae1851bc2321832c88af85d570.zip
basic authentications
Diffstat (limited to 'models')
-rw-r--r--models/login.go73
-rw-r--r--models/user.go7
2 files changed, 61 insertions, 19 deletions
diff --git a/models/login.go b/models/login.go
index 6d9e54943f..d5905eb36b 100644
--- a/models/login.go
+++ b/models/login.go
@@ -2,17 +2,31 @@ package models
import (
"encoding/json"
+ "errors"
"time"
"github.com/go-xorm/core"
+ "github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/auth/ldap"
)
-/*const (
+// Login types.
+const (
LT_PLAIN = iota + 1
LT_LDAP
LT_SMTP
-)*/
+)
+
+var (
+ ErrAuthenticationAlreadyExist = errors.New("Authentication already exist")
+ ErrAuthenticationNotExist = errors.New("Authentication is not exist")
+ ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users")
+)
+
+var LoginTypes = map[int]string{
+ LT_LDAP: "LDAP",
+ LT_SMTP: "SMTP",
+}
var _ core.Conversion = &LDAPConfig{}
@@ -32,19 +46,50 @@ func (cfg *LDAPConfig) ToDB() ([]byte, error) {
type LoginSource struct {
Id int64
Type int
- Name string
- IsActived bool
+ 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"`
}
+func (source *LoginSource) TypeString() string {
+ return LoginTypes[source.Type]
+}
+
+func (source *LoginSource) LDAP() *LDAPConfig {
+ return source.Cfg.(*LDAPConfig)
+}
+
+// for xorm callback
+func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
+ if colName == "type" {
+ ty := (*val).(int64)
+ switch ty {
+ case LT_LDAP:
+ source.Cfg = new(LDAPConfig)
+ }
+ }
+}
+
func GetAuths() ([]*LoginSource, error) {
var auths = make([]*LoginSource, 0)
err := orm.Find(&auths)
return auths, err
}
+func GetLoginSourceById(id int64) (*LoginSource, error) {
+ source := new(LoginSource)
+ has, err := orm.Id(id).Get(source)
+ if err != nil {
+ return nil, err
+ }
+ if !has {
+ return nil, ErrAuthenticationNotExist
+ }
+ return source, nil
+}
+
func AddLDAPSource(name string, cfg *LDAPConfig) error {
_, err := orm.Insert(&LoginSource{Type: LT_LDAP,
Name: name,
@@ -54,17 +99,19 @@ func AddLDAPSource(name string, cfg *LDAPConfig) error {
return err
}
-func UpdateLDAPSource(id int64, name string, cfg *LDAPConfig) error {
- _, err := orm.AllCols().Id(id).Update(&LoginSource{
- Id: id,
- Type: LT_LDAP,
- Name: name,
- Cfg: cfg,
- })
+func UpdateLDAPSource(source *LoginSource) error {
+ _, err := orm.AllCols().Id(source.Id).Update(source)
return err
}
-func DelLoginSource(id int64) error {
- _, err := orm.Id(id).Delete(&LoginSource{})
+func DelLoginSource(source *LoginSource) error {
+ cnt, err := orm.Count(&User{LoginSource: source.Id})
+ if err != nil {
+ return err
+ }
+ if cnt > 0 {
+ return ErrAuthenticationUserUsed
+ }
+ _, err = orm.Id(source.Id).Delete(&LoginSource{})
return err
}
diff --git a/models/user.go b/models/user.go
index df1eb985c2..3372d75497 100644
--- a/models/user.go
+++ b/models/user.go
@@ -26,12 +26,6 @@ const (
UT_ORGANIZATION
)
-// Login types.
-const (
- LT_PLAIN = iota + 1
- LT_LDAP
-)
-
var (
ErrUserOwnRepos = errors.New("User still have ownership of repositories")
ErrUserAlreadyExist = errors.New("User already exist")
@@ -49,6 +43,7 @@ type User struct {
Email string `xorm:"unique not null"`
Passwd string `xorm:"not null"`
LoginType int
+ LoginSource int64 `xorm:"not null default 0"`
Type int
NumFollowers int
NumFollowings int