2021-07-24 06:16:34 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-24 06:16:34 -04:00
|
|
|
|
2022-11-02 04:54:36 -04:00
|
|
|
package v1_16 //nolint
|
2021-07-24 06:16:34 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
|
2022-11-02 04:54:36 -04:00
|
|
|
"code.gitea.io/gitea/models/migrations/base"
|
2021-07-24 12:03:58 -04:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
|
2021-07-24 06:16:34 -04:00
|
|
|
"xorm.io/xorm"
|
|
|
|
)
|
|
|
|
|
2022-11-02 04:54:36 -04:00
|
|
|
func UnwrapLDAPSourceCfg(x *xorm.Engine) error {
|
2023-07-04 14:36:08 -04:00
|
|
|
jsonUnmarshalHandleDoubleEncode := func(bs []byte, v any) error {
|
2021-07-24 06:16:34 -04:00
|
|
|
err := json.Unmarshal(bs, v)
|
|
|
|
if err != nil {
|
|
|
|
ok := true
|
|
|
|
rs := []byte{}
|
|
|
|
temp := make([]byte, 2)
|
|
|
|
for _, rn := range string(bs) {
|
|
|
|
if rn > 0xffff {
|
|
|
|
ok = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
binary.LittleEndian.PutUint16(temp, uint16(rn))
|
|
|
|
rs = append(rs, temp...)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
if rs[0] == 0xff && rs[1] == 0xfe {
|
|
|
|
rs = rs[2:]
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(rs, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
|
|
|
|
err = json.Unmarshal(bs[2:], v)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoginSource represents an external way for authorizing users.
|
|
|
|
type LoginSource struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
Type int
|
|
|
|
IsActived bool `xorm:"INDEX NOT NULL DEFAULT false"`
|
|
|
|
IsActive bool `xorm:"INDEX NOT NULL DEFAULT false"`
|
|
|
|
Cfg string `xorm:"TEXT"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const ldapType = 2
|
|
|
|
const dldapType = 5
|
|
|
|
|
|
|
|
type WrappedSource struct {
|
2023-07-04 14:36:08 -04:00
|
|
|
Source map[string]any
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// change lower_email as unique
|
2023-08-13 15:17:21 -04:00
|
|
|
if err := x.Sync(new(LoginSource)); err != nil {
|
2021-07-24 06:16:34 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
const batchSize = 100
|
|
|
|
for start := 0; ; start += batchSize {
|
|
|
|
sources := make([]*LoginSource, 0, batchSize)
|
|
|
|
if err := sess.Limit(batchSize, start).Where("`type` = ? OR `type` = ?", ldapType, dldapType).Find(&sources); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(sources) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, source := range sources {
|
|
|
|
wrapped := &WrappedSource{
|
2023-07-04 14:36:08 -04:00
|
|
|
Source: map[string]any{},
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
|
|
|
err := jsonUnmarshalHandleDoubleEncode([]byte(source.Cfg), &wrapped)
|
|
|
|
if err != nil {
|
2022-06-20 06:02:49 -04:00
|
|
|
return fmt.Errorf("failed to unmarshal %s: %w", source.Cfg, err)
|
2021-07-24 06:16:34 -04:00
|
|
|
}
|
2024-09-09 22:23:07 -04:00
|
|
|
if len(wrapped.Source) > 0 {
|
2021-07-24 12:03:58 -04:00
|
|
|
bs, err := json.Marshal(wrapped.Source)
|
2021-07-24 06:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
source.Cfg = string(bs)
|
|
|
|
if _, err := sess.ID(source.ID).Cols("cfg").Update(source); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := x.SetExpr("is_active", "is_actived").Update(&LoginSource{}); err != nil {
|
|
|
|
return fmt.Errorf("SetExpr Update failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-02 04:54:36 -04:00
|
|
|
if err := base.DropTableColumns(sess, "login_source", "is_actived"); err != nil {
|
2021-07-24 06:16:34 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|