login.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "time"
  6. "github.com/go-xorm/core"
  7. "github.com/go-xorm/xorm"
  8. "github.com/gogits/gogs/modules/auth/ldap"
  9. )
  10. // Login types.
  11. const (
  12. LT_PLAIN = iota + 1
  13. LT_LDAP
  14. LT_SMTP
  15. )
  16. var (
  17. ErrAuthenticationAlreadyExist = errors.New("Authentication already exist")
  18. ErrAuthenticationNotExist = errors.New("Authentication is not exist")
  19. ErrAuthenticationUserUsed = errors.New("Authentication has been used by some users")
  20. )
  21. var LoginTypes = map[int]string{
  22. LT_LDAP: "LDAP",
  23. LT_SMTP: "SMTP",
  24. }
  25. var _ core.Conversion = &LDAPConfig{}
  26. type LDAPConfig struct {
  27. ldap.Ldapsource
  28. }
  29. // implement
  30. func (cfg *LDAPConfig) FromDB(bs []byte) error {
  31. return json.Unmarshal(bs, &cfg.Ldapsource)
  32. }
  33. func (cfg *LDAPConfig) ToDB() ([]byte, error) {
  34. return json.Marshal(cfg.Ldapsource)
  35. }
  36. type LoginSource struct {
  37. Id int64
  38. Type int
  39. Name string `xorm:"unique"`
  40. IsActived bool `xorm:"not null default false"`
  41. Cfg core.Conversion `xorm:"TEXT"`
  42. Created time.Time `xorm:"created"`
  43. Updated time.Time `xorm:"updated"`
  44. }
  45. func (source *LoginSource) TypeString() string {
  46. return LoginTypes[source.Type]
  47. }
  48. func (source *LoginSource) LDAP() *LDAPConfig {
  49. return source.Cfg.(*LDAPConfig)
  50. }
  51. // for xorm callback
  52. func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
  53. if colName == "type" {
  54. ty := (*val).(int64)
  55. switch ty {
  56. case LT_LDAP:
  57. source.Cfg = new(LDAPConfig)
  58. }
  59. }
  60. }
  61. func GetAuths() ([]*LoginSource, error) {
  62. var auths = make([]*LoginSource, 0)
  63. err := orm.Find(&auths)
  64. return auths, err
  65. }
  66. func GetLoginSourceById(id int64) (*LoginSource, error) {
  67. source := new(LoginSource)
  68. has, err := orm.Id(id).Get(source)
  69. if err != nil {
  70. return nil, err
  71. }
  72. if !has {
  73. return nil, ErrAuthenticationNotExist
  74. }
  75. return source, nil
  76. }
  77. func AddLDAPSource(name string, cfg *LDAPConfig) error {
  78. _, err := orm.Insert(&LoginSource{Type: LT_LDAP,
  79. Name: name,
  80. IsActived: true,
  81. Cfg: cfg,
  82. })
  83. return err
  84. }
  85. func UpdateLDAPSource(source *LoginSource) error {
  86. _, err := orm.AllCols().Id(source.Id).Update(source)
  87. return err
  88. }
  89. func DelLoginSource(source *LoginSource) error {
  90. cnt, err := orm.Count(&User{LoginSource: source.Id})
  91. if err != nil {
  92. return err
  93. }
  94. if cnt > 0 {
  95. return ErrAuthenticationUserUsed
  96. }
  97. _, err = orm.Id(source.Id).Delete(&LoginSource{})
  98. return err
  99. }