@ -133,6 +133,7 @@ type LoginSource struct {
Type LoginType
Name string ` xorm:"UNIQUE" `
IsActived bool ` xorm:"NOT NULL DEFAULT false" `
IsDefault bool ` xorm:"DEFAULT false" `
Cfg core . Conversion ` xorm:"TEXT" `
Created time . Time ` xorm:"-" json:"-" `
@ -257,7 +258,12 @@ func CreateLoginSource(source *LoginSource) error {
}
_ , err = x . Insert ( source )
if err != nil {
return err
} else if source . IsDefault {
return ResetNonDefaultLoginSources ( source )
}
return nil
}
// LoginSources returns all login sources defined.
@ -291,23 +297,48 @@ func GetLoginSourceByID(id int64) (*LoginSource, error) {
return source , nil
}
// ResetNonDefaultLoginSources clean other default source flag
func ResetNonDefaultLoginSources ( source * LoginSource ) error {
// update changes to DB
if _ , err := x . NotIn ( "id" , [ ] int64 { source . ID } ) . Cols ( "is_default" ) . Update ( & LoginSource { IsDefault : false } ) ; err != nil {
return err
}
// write changes to local authentications
for i := range localLoginSources . sources {
if localLoginSources . sources [ i ] . LocalFile != nil && localLoginSources . sources [ i ] . ID != source . ID {
localLoginSources . sources [ i ] . LocalFile . SetGeneral ( "is_default" , "false" )
if err := localLoginSources . sources [ i ] . LocalFile . SetConfig ( source . Cfg ) ; err != nil {
return fmt . Errorf ( "LocalFile.SetConfig: %v" , err )
} else if err = localLoginSources . sources [ i ] . LocalFile . Save ( ) ; err != nil {
return fmt . Errorf ( "LocalFile.Save: %v" , err )
}
}
}
// flush memory so that web page can show the same behaviors
localLoginSources . UpdateLoginSource ( source )
return nil
}
// UpdateLoginSource updates information of login source to database or local file.
func UpdateLoginSource ( source * LoginSource ) error {
if source . LocalFile == nil {
_ , err := x . Id ( source . ID ) . AllCols ( ) . Update ( source )
if _ , err := x . Id ( source . ID ) . AllCols ( ) . Update ( source ) ; err != nil {
return err
} else {
return ResetNonDefaultLoginSources ( source )
}
}
source . LocalFile . SetGeneral ( "name" , source . Name )
source . LocalFile . SetGeneral ( "is_activated" , com . ToStr ( source . IsActived ) )
source . LocalFile . SetGeneral ( "is_default" , com . ToStr ( source . IsDefault ) )
if err := source . LocalFile . SetConfig ( source . Cfg ) ; err != nil {
return fmt . Errorf ( "LocalFile.SetConfig: %v" , err )
} else if err = source . LocalFile . Save ( ) ; err != nil {
return fmt . Errorf ( "LocalFile.Save: %v" , err )
}
localLoginSources . UpdateLoginSource ( source )
return nil
return ResetNonDefaultLoginSources ( source )
}
func DeleteSource ( source * LoginSource ) error {
@ -361,7 +392,6 @@ func (s *LocalLoginSources) ActivatedList() []*LoginSource {
if ! s . sources [ i ] . IsActived {
continue
}
source := & LoginSource { }
* source = * s . sources [ i ]
list = append ( list , source )
@ -394,7 +424,8 @@ func (s *LocalLoginSources) UpdateLoginSource(source *LoginSource) {
for i := range s . sources {
if s . sources [ i ] . ID == source . ID {
* s . sources [ i ] = * source
break
} else if source . IsDefault {
s . sources [ i ] . IsDefault = false
}
}
}
@ -429,6 +460,7 @@ func LoadAuthSources() {
ID : s . Key ( "id" ) . MustInt64 ( ) ,
Name : s . Key ( "name" ) . String ( ) ,
IsActived : s . Key ( "is_activated" ) . MustBool ( ) ,
IsDefault : s . Key ( "is_default" ) . MustBool ( ) ,
LocalFile : & AuthSourceFile {
abspath : fpath ,
file : authSource ,