v13.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2016 The Gogs 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. "fmt"
  7. "strings"
  8. "github.com/Unknwon/com"
  9. "github.com/go-xorm/xorm"
  10. "github.com/json-iterator/go"
  11. )
  12. func ldapUseSSLToSecurityProtocol(x *xorm.Engine) error {
  13. results, err := x.Query("SELECT `id`,`cfg` FROM `login_source` WHERE `type` = 2 OR `type` = 5")
  14. if err != nil {
  15. if strings.Contains(err.Error(), "no such column") {
  16. return nil
  17. }
  18. return fmt.Errorf("select LDAP login sources: %v", err)
  19. }
  20. sess := x.NewSession()
  21. defer sess.Close()
  22. if err = sess.Begin(); err != nil {
  23. return err
  24. }
  25. for _, result := range results {
  26. cfg := map[string]interface{}{}
  27. if err = jsoniter.Unmarshal(result["cfg"], &cfg); err != nil {
  28. return fmt.Errorf("unmarshal JSON config: %v", err)
  29. }
  30. if com.ToStr(cfg["UseSSL"]) == "true" {
  31. cfg["SecurityProtocol"] = 1 // LDAPS
  32. }
  33. delete(cfg, "UseSSL")
  34. data, err := jsoniter.Marshal(&cfg)
  35. if err != nil {
  36. return fmt.Errorf("marshal JSON config: %v", err)
  37. }
  38. if _, err = sess.Exec("UPDATE `login_source` SET `cfg`=? WHERE `id`=?",
  39. string(data), com.StrTo(result["id"]).MustInt64()); err != nil {
  40. return fmt.Errorf("update config column: %v", err)
  41. }
  42. }
  43. return sess.Commit()
  44. }