You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v189.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2021 The Gitea Authors. 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 migrations
  5. import (
  6. "encoding/binary"
  7. "fmt"
  8. "code.gitea.io/gitea/modules/json"
  9. "xorm.io/xorm"
  10. )
  11. func unwrapLDAPSourceCfg(x *xorm.Engine) error {
  12. jsonUnmarshalHandleDoubleEncode := func(bs []byte, v interface{}) error {
  13. err := json.Unmarshal(bs, v)
  14. if err != nil {
  15. ok := true
  16. rs := []byte{}
  17. temp := make([]byte, 2)
  18. for _, rn := range string(bs) {
  19. if rn > 0xffff {
  20. ok = false
  21. break
  22. }
  23. binary.LittleEndian.PutUint16(temp, uint16(rn))
  24. rs = append(rs, temp...)
  25. }
  26. if ok {
  27. if rs[0] == 0xff && rs[1] == 0xfe {
  28. rs = rs[2:]
  29. }
  30. err = json.Unmarshal(rs, v)
  31. }
  32. }
  33. if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
  34. err = json.Unmarshal(bs[2:], v)
  35. }
  36. return err
  37. }
  38. // LoginSource represents an external way for authorizing users.
  39. type LoginSource struct {
  40. ID int64 `xorm:"pk autoincr"`
  41. Type int
  42. IsActived bool `xorm:"INDEX NOT NULL DEFAULT false"`
  43. IsActive bool `xorm:"INDEX NOT NULL DEFAULT false"`
  44. Cfg string `xorm:"TEXT"`
  45. }
  46. const ldapType = 2
  47. const dldapType = 5
  48. type WrappedSource struct {
  49. Source map[string]interface{}
  50. }
  51. // change lower_email as unique
  52. if err := x.Sync2(new(LoginSource)); err != nil {
  53. return err
  54. }
  55. sess := x.NewSession()
  56. defer sess.Close()
  57. const batchSize = 100
  58. for start := 0; ; start += batchSize {
  59. sources := make([]*LoginSource, 0, batchSize)
  60. if err := sess.Limit(batchSize, start).Where("`type` = ? OR `type` = ?", ldapType, dldapType).Find(&sources); err != nil {
  61. return err
  62. }
  63. if len(sources) == 0 {
  64. break
  65. }
  66. for _, source := range sources {
  67. wrapped := &WrappedSource{
  68. Source: map[string]interface{}{},
  69. }
  70. err := jsonUnmarshalHandleDoubleEncode([]byte(source.Cfg), &wrapped)
  71. if err != nil {
  72. return fmt.Errorf("failed to unmarshal %s: %w", string(source.Cfg), err)
  73. }
  74. if wrapped.Source != nil && len(wrapped.Source) > 0 {
  75. bs, err := json.Marshal(wrapped.Source)
  76. if err != nil {
  77. return err
  78. }
  79. source.Cfg = string(bs)
  80. if _, err := sess.ID(source.ID).Cols("cfg").Update(source); err != nil {
  81. return err
  82. }
  83. }
  84. }
  85. }
  86. if _, err := x.SetExpr("is_active", "is_actived").Update(&LoginSource{}); err != nil {
  87. return fmt.Errorf("SetExpr Update failed: %w", err)
  88. }
  89. if err := sess.Begin(); err != nil {
  90. return err
  91. }
  92. if err := dropTableColumns(sess, "login_source", "is_actived"); err != nil {
  93. return err
  94. }
  95. return sess.Commit()
  96. }