diff options
author | Unknwon <u@gogs.io> | 2016-07-08 07:25:09 +0800 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-07-08 07:25:09 +0800 |
commit | 401bf944ef4b09e7d4ca85d7272dbd32e7e950a0 (patch) | |
tree | 7e9af9199616f4704d4e1e9e0bfcf97d537693e4 /models/migrations/v13.go | |
parent | 326c98266040a69ceec51c3804c372c7af47e027 (diff) | |
download | gitea-401bf944ef4b09e7d4ca85d7272dbd32e7e950a0.tar.gz gitea-401bf944ef4b09e7d4ca85d7272dbd32e7e950a0.zip |
Use SecurityProtocol to replace UseSSL in LDAP config
Initially proposed by #2376 and fixes #3068 as well.
Diffstat (limited to 'models/migrations/v13.go')
-rw-r--r-- | models/migrations/v13.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/models/migrations/v13.go b/models/migrations/v13.go new file mode 100644 index 0000000000..c9a50a992e --- /dev/null +++ b/models/migrations/v13.go @@ -0,0 +1,52 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "encoding/json" + "fmt" + "strings" + + "github.com/Unknwon/com" + "github.com/go-xorm/xorm" +) + +func ldapUseSSLToSecurityProtocol(x *xorm.Engine) error { + results, err := x.Query("SELECT `id`,`cfg` FROM `login_source` WHERE `type` = 2 OR `type` = 5") + if err != nil { + if strings.Contains(err.Error(), "no such column") { + return nil + } + return fmt.Errorf("select LDAP login sources: %v", err) + } + + sess := x.NewSession() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { + return err + } + + for _, result := range results { + cfg := map[string]interface{}{} + if err = json.Unmarshal(result["cfg"], &cfg); err != nil { + return fmt.Errorf("decode JSON config: %v", err) + } + if com.ToStr(cfg["UseSSL"]) == "true" { + cfg["SecurityProtocol"] = 1 // LDAPS + } + delete(cfg, "UseSSL") + + data, err := json.Marshal(&cfg) + if err != nil { + return fmt.Errorf("encode JSON config: %v", err) + } + + if _, err = sess.Exec("UPDATE `login_source` SET `cfg`=? WHERE `id`=?", + string(data), com.StrTo(result["id"]).MustInt64()); err != nil { + return fmt.Errorf("update config column: %v", err) + } + } + return sess.Commit() +} |