login.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package models
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/go-xorm/core"
  6. "github.com/gogits/gogs/modules/auth/ldap"
  7. )
  8. /*const (
  9. LT_PLAIN = iota + 1
  10. LT_LDAP
  11. LT_SMTP
  12. )*/
  13. var _ core.Conversion = &LDAPConfig{}
  14. type LDAPConfig struct {
  15. ldap.Ldapsource
  16. }
  17. // implement
  18. func (cfg *LDAPConfig) FromDB(bs []byte) error {
  19. return json.Unmarshal(bs, &cfg.Ldapsource)
  20. }
  21. func (cfg *LDAPConfig) ToDB() ([]byte, error) {
  22. return json.Marshal(cfg.Ldapsource)
  23. }
  24. type LoginSource struct {
  25. Id int64
  26. Type int
  27. Name string
  28. IsActived bool
  29. Cfg core.Conversion `xorm:"TEXT"`
  30. Created time.Time `xorm:"created"`
  31. Updated time.Time `xorm:"updated"`
  32. }
  33. func GetAuths() ([]*LoginSource, error) {
  34. var auths = make([]*LoginSource, 0)
  35. err := orm.Find(&auths)
  36. return auths, err
  37. }
  38. func AddLDAPSource(name string, cfg *LDAPConfig) error {
  39. _, err := orm.Insert(&LoginSource{Type: LT_LDAP,
  40. Name: name,
  41. IsActived: true,
  42. Cfg: cfg,
  43. })
  44. return err
  45. }
  46. func UpdateLDAPSource(id int64, name string, cfg *LDAPConfig) error {
  47. _, err := orm.AllCols().Id(id).Update(&LoginSource{
  48. Id: id,
  49. Type: LT_LDAP,
  50. Name: name,
  51. Cfg: cfg,
  52. })
  53. return err
  54. }
  55. func DelLoginSource(id int64) error {
  56. _, err := orm.Id(id).Delete(&LoginSource{})
  57. return err
  58. }