refactor: ldap登录配置迁移至系统配置

This commit is contained in:
meilin.huang
2023-08-25 23:26:14 +08:00
parent 3634c902d0
commit 649b2bb165
7 changed files with 143 additions and 183 deletions

View File

@@ -3,12 +3,14 @@ package entity
import (
"encoding/json"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/stringx"
"strconv"
)
const (
ConfigKeyAccountLoginSecurity string = "AccountLoginSecurity" // 账号登录安全配置
ConfigKeyOauth2Login string = "Oauth2Login" // oauth2认证登录配置
ConfigKeyLdapLogin string = "LdapLogin" // ldap登录配置
ConfigKeyDbQueryMaxCount string = "DbQueryMaxCount" // 数据库查询的最大数量
ConfigKeyDbSaveQuerySQL string = "DbSaveQuerySQL" // 数据库是否记录查询相关sql
ConfigUseWartermark string = "UseWartermark" // 是否使用水印
@@ -105,13 +107,46 @@ func (c *Config) ToOauth2Login() *ConfigOauth2Login {
ol.AuthorizationURL = jm["authorizationURL"]
ol.AccessTokenURL = jm["accessTokenURL"]
ol.RedirectURL = jm["redirectURL"]
ol.Scopes = jm["scopes"]
ol.Scopes = stringx.Trim(jm["scopes"])
ol.ResourceURL = jm["resourceURL"]
ol.UserIdentifier = jm["userIdentifier"]
ol.AutoRegister = c.ConvBool(jm["autoRegister"], true)
return ol
}
type ConfigLdapLogin struct {
Enable bool // 是否启用
Host string
Port string `json:"port"`
SkipTLSVerify bool `json:"skipTLSVerify"` // 客户端是否跳过 TLS 证书验证
SecurityProtocol string `json:"securityProtocol"` // 安全协议为Null不使用安全协议如: StartTLS, LDAPS
BindDN string `json:"bindDn"` // LDAP 服务的管理员账号,如: "cn=admin,dc=example,dc=com"
BindPwd string `json:"bindPwd"` // LDAP 服务的管理员密码
BaseDN string `json:"baseDN"` // 用户所在的 base DN, 如: "ou=users,dc=example,dc=com"
UserFilter string `json:"userFilter"` // 过滤用户的方式, 如: "(uid=%s)"
UidMap string `json:"UidMap"` // 用户id和 LDAP 字段名之间的映射关系
UdnMap string `json:"UdnMap"` // 用户姓名(dispalyName)和 LDAP 字段名之间的映射关系
EmailMap string `json:"emailMap"` // 用户email和 LDAP 字段名之间的映射关系
}
// 转换为LdapLogin结构体
func (c *Config) ToLdapLogin() *ConfigLdapLogin {
jm := c.GetJsonMap()
ll := new(ConfigLdapLogin)
ll.Enable = c.ConvBool(jm["enable"], false)
ll.Host = jm["host"]
ll.SkipTLSVerify = c.ConvBool(jm["skipTLSVerify"], true)
ll.SecurityProtocol = jm["securityProtocol"]
ll.BindDN = stringx.Trim(jm["bindDN"])
ll.BindPwd = stringx.Trim(jm["bindPwd"])
ll.BaseDN = stringx.Trim(jm["baseDN"])
ll.UserFilter = stringx.Trim(jm["userFilter"])
ll.UidMap = stringx.Trim(jm["uidMap"])
ll.UdnMap = stringx.Trim(jm["udnMap"])
ll.EmailMap = stringx.Trim(jm["emailMap"])
return ll
}
// 转换配置中的值为bool类型默认"1"或"true"为true其他为false
func (c *Config) ConvBool(value string, defaultValue bool) bool {
if value == "" {