v13.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "encoding/json"
  7. "fmt"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. "github.com/go-xorm/xorm"
  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 = json.Unmarshal(result["cfg"], &cfg); err != nil {
  28. return fmt.Errorf("decode 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 := json.Marshal(&cfg)
  35. if err != nil {
  36. return fmt.Errorf("encode 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. }