login.go 2.5 KB

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