-
Notifications
You must be signed in to change notification settings - Fork 105
ldap支持控制台配置 #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ldap支持控制台配置 #275
Conversation
📝 WalkthroughSummary by CodeRabbit
Walkthrough本次变更引入了LDAP配置的完整管理功能,包括后端的CRUD接口、数据库模型、密码加密处理、连接测试API,以及前端管理页面和菜单入口。同时,登录流程和用户服务逻辑也更新以适配新的LDAP配置存储方式,并移除了原有的LDAP相关命令行参数和环境变量支持。 Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~40 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (2)
🧰 Additional context used🧬 Code Graph Analysis (2)main.go (1)
pkg/controller/sso/route.go (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
🔇 Additional comments (4)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
pkg/service/user.go (1)
391-397
: LDAP 密码解密错误处理密码解密失败时返回了通用错误信息,建议保留更多调试信息用于问题排查。
// 解密管理员密码 bindPassword, err := utils.AesDecrypt(config.BindPassword) if err != nil { - klog.Errorf("LDAP密码解密失败: %v", err) - return nil, errors.New("LDAP配置错误") + klog.Errorf("LDAP密码解密失败 (配置ID: %d): %v", config.ID, err) + return nil, fmt.Errorf("LDAP密码解密失败: %w", err) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (6)
.DS_Store
is excluded by!**/.DS_Store
ui/.DS_Store
is excluded by!**/.DS_Store
ui/public/pages/.DS_Store
is excluded by!**/.DS_Store
ui/src/.DS_Store
is excluded by!**/.DS_Store
ui/src/components/.DS_Store
is excluded by!**/.DS_Store
ui/src/pages/.DS_Store
is excluded by!**/.DS_Store
📒 Files selected for processing (13)
main.go
(3 hunks)pkg/controller/admin/config/ldap.go
(1 hunks)pkg/controller/login/login.go
(1 hunks)pkg/flag/flag.go
(0 hunks)pkg/models/ldap_config.go
(1 hunks)pkg/models/migrate.go
(1 hunks)pkg/models/user.go
(1 hunks)pkg/service/user.go
(4 hunks)swagger/docs.go
(1 hunks)swagger/swagger.yaml
(1 hunks)ui/public/pages/admin/config/ldap_config.json
(1 hunks)ui/src/components/Sidebar/menu.tsx
(1 hunks)ui/src/pages/Login/index.tsx
(1 hunks)
💤 Files with no reviewable changes (1)
- pkg/flag/flag.go
🧰 Additional context used
🧬 Code Graph Analysis (2)
pkg/controller/admin/config/ldap.go (5)
internal/dao/param.go (1)
BuildParams
(30-72)pkg/models/ldap_config.go (1)
LDAPConfig
(14-28)internal/dao/conn.go (1)
DB
(176-182)pkg/comm/utils/crypto.go (2)
AesEncrypt
(34-52)AesDecrypt
(55-82)pkg/comm/utils/strings.go (1)
ToUInt
(56-62)
pkg/models/ldap_config.go (4)
internal/dao/param.go (1)
Params
(11-19)internal/dao/conn.go (1)
DB
(176-182)internal/dao/base.go (4)
GenericQuery
(13-77)GenericSave
(110-136)GenericDelete
(139-167)GenericGetOne
(79-101)pkg/comm/utils/strings.go (1)
ToInt64Slice
(80-94)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Analyze (go)
🔇 Additional comments (24)
pkg/models/migrate.go (1)
97-99
: LGTM! LDAP配置模型迁移添加正确新增的
LDAPConfig
模型迁移遵循了现有的数据库迁移模式,包括适当的错误处理。这与PR目标中的LDAP配置数据库化管理需求一致。swagger/docs.go (1)
886-886
: LGTM! Swagger文档更新正确用户来源字段描述从 "ldap" 更新为 "ldap_config.json",与新的LDAP配置管理方式保持一致。
ui/src/pages/Login/index.tsx (2)
44-46
: LGTM! 防御性编程改进添加
data.data
存在性检查避免了潜在的空引用错误,提高了代码的健壮性。
48-51
: LGTM! 错误处理优化合理将LDAP配置获取失败时的处理从显示用户错误消息改为控制台日志记录并明确设置
ldapEnabled
为false
,避免了配置获取失败对登录流程的干扰。这种处理方式更加用户友好。swagger/swagger.yaml (1)
18-18
: LGTM! API文档描述更新一致用户来源字段的描述与
swagger/docs.go
中的更新保持一致,正确反映了新的LDAP配置管理方式。ui/src/components/Sidebar/menu.tsx (1)
795-800
: LGTM! LDAP配置菜单项添加正确新增的LDAP配置菜单项遵循了现有的菜单结构模式,正确地限制为平台管理员可见,并且在逻辑上放置在单点登录配置之后。路径配置和图标选择都很合适。
pkg/models/user.go (1)
18-18
: 注释更新准确反映了新的LDAP配置管理方式将来源示例从"ldap"更新为"ldap_config.json"准确反映了从环境变量配置到数据库配置的转变,注释更新合理。
pkg/controller/login/login.go (2)
148-161
: LDAP配置查询逻辑实现正确从数据库获取启用的LDAP配置以获取默认用户组的实现是合理的。查询逻辑正确,错误处理恰当,能够优雅降级到空默认组。
162-185
: 用户存在性检查和创建逻辑实现完善新的用户检查和创建流程更加健壮:
- 正确检查用户是否存在
- 对禁用用户返回适当的错误
- 使用正确的来源标识"ldap_config"创建新用户
- 错误处理覆盖了各种数据库异常情况
实现符合安全最佳实践。
main.go (3)
234-234
: LDAP配置获取路由更新合理从
sso.GetLdapEnabled
更改为config.GetLdapConfig
符合新的LDAP配置管理架构,能够返回数据库中的LDAP配置状态而非静态标志。
388-388
: 注释简化合理将"节点污点"简化为"污点"使注释更加简洁,不影响功能理解。
562-568
: LDAP管理路由配置完整且安全新增的LDAP配置管理路由包含了完整的CRUD操作:
- 列表查询、详情获取
- 保存和删除操作
- 快速状态切换
- 连接测试功能
所有路由都正确地放在了
/admin
组下并使用了PlatformAuthMiddleware()
进行权限控制,确保只有平台管理员可以访问。pkg/controller/admin/config/ldap.go (7)
18-34
: LDAP配置列表接口实现完善列表接口正确使用了分页参数构建和通用查询方法,并且出于安全考虑隐藏了密码字段,这是很好的安全实践。
36-49
: 配置详情获取接口实现正确详情接口正确处理了ID参数,使用了适当的查询条件,错误处理完善。返回格式符合前端期望。
51-103
: LDAP配置保存逻辑实现安全且完善保存逻辑处理得很好:
- 正确区分新建和编辑操作
- 编辑时保留原密码的逻辑合理,避免不必要的重新加密
- 密码加密使用AES + Base64编码,安全性良好
- 使用Select指定更新字段,避免意外覆盖
- 错误处理完善
这是一个很好的密码管理实践示例。
105-117
: 删除接口支持批量操作删除接口正确支持批量删除,使用了通用的删除方法,实现简洁有效。
119-139
: 快速状态切换接口实现合理快速启用/禁用状态切换功能实现正确,使用了Select指定字段更新,避免了不必要的数据修改。参数解析和布尔值转换处理得当。
141-157
: LDAP启用状态查询接口逻辑正确查询启用状态的逻辑合理,通过检查是否存在启用的配置来确定LDAP功能状态。响应格式符合前端期望。
159-197
: LDAP连接测试实现安全且实用连接测试功能实现得很好:
- 请求结构体定义清晰
- 正确处理加密密码的解密
- LDAP连接和绑定测试逻辑正确
- 错误处理详细,便于调试
- 资源清理(defer conn.Close())正确
这为管理员提供了很好的配置验证工具。
pkg/models/ldap_config.go (2)
14-28
: LDAP配置模型定义完善模型结构设计合理:
- 字段定义完整,包含了LDAP连接所需的所有参数
- GORM注解正确,字段长度限制合理
- JSON标签正确,
bind_password
字段使用omitempty
是好的实践- 包含了必要的时间戳字段
- 默认值设置合理(Enabled默认为true,LOGIN2AUTHCLOSE默认为true)
30-48
: CRUD方法实现标准化所有CRUD方法都正确使用了通用DAO函数:
List
方法支持分页和自定义查询Save
方法支持创建和更新Delete
方法支持批量删除,正确使用了ID转换工具GetOne
方法支持单条记录查询实现遵循了项目的代码规范,具有良好的一致性。
ui/public/pages/admin/config/ldap_config.json (1)
328-335
: 无需修改:后端已支持批量删除后端
LDAPConfig.Delete
方法接收路径参数ids
(如 “1,2,3”),通过utils.ToInt64Slice(ids)
将逗号分隔的字符串转为[]int64
,再交给dao.GenericDelete
执行WHERE id IN (?)
批量删除。前端可继续使用"api": "post:/admin/config/ldap/delete/${ids}"并以逗号分隔的 ID 列表调用,无需额外改动。
pkg/service/user.go (2)
334-340
: 正确使用 UpdateColumn 避免数据丢失使用
UpdateColumn
仅更新group_names
字段的做法是正确的,避免了使用Save
方法时可能清空其他字段(如密码)的问题。
430-448
: LDAP 配置查询逻辑正确从数据库获取启用的 LDAP 配置的实现正确,使用
Order("id desc")
确保获取最新的配置。建议添加注释说明为什么选择最新的配置:
queryFunc := func(db *gorm.DB) *gorm.DB { + // 获取最新的启用配置(当有多个启用配置时) return db.Where("enabled = ?", true).Order("id desc").Limit(1) }
{ | ||
"type": "input-text", | ||
"name": "port", | ||
"label": "服务器端口", | ||
"required": true, | ||
"placeholder": "默认389", | ||
"value": 389 | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
端口字段类型和验证优化
端口字段使用了 input-text
类型,建议改为 input-number
并添加端口范围验证。
- "type": "input-text",
+ "type": "input-number",
"name": "port",
"label": "服务器端口",
"required": true,
"placeholder": "默认389",
- "value": 389
+ "value": 389,
+ "min": 1,
+ "max": 65535,
+ "validations": {
+ "minimum": 1,
+ "maximum": 65535
+ },
+ "validationErrors": {
+ "minimum": "端口号不能小于1",
+ "maximum": "端口号不能大于65535"
+ }
Also applies to: 222-229
🤖 Prompt for AI Agents
In ui/public/pages/admin/config/ldap_config.json at lines 54-61 and also lines
222-229, the port field is currently defined as type "input-text" without
validation. Change the field type to "input-number" and add validation rules to
ensure the port value is within the valid range (typically 1 to 65535). This
will enforce proper numeric input and prevent invalid port entries.
No description provided.