ldap-1-backend/controller/user_controller.go

321 lines
11 KiB
Go
Raw Normal View History

2025-08-26 00:43:50 +08:00
/*
Package controller 包含所有 HTTP 请求的控制器
用户控制器负责处理用户管理相关的 HTTP 请求包括
- 用户的增删改查操作
- 用户密码管理
- 用户状态管理
- 用户信息查询
控制器层的职责
1. 接收和验证 HTTP 请求参数
2. 调用业务逻辑层处理请求
3. 返回统一格式的响应结果
4. 处理请求过程中的异常情况
*/
2022-05-18 17:57:03 +08:00
package controller
import (
2022-05-29 10:06:21 +08:00
"github.com/eryajf/go-ldap-admin/logic"
"github.com/eryajf/go-ldap-admin/model/request"
2022-05-18 17:57:03 +08:00
"github.com/gin-gonic/gin"
)
2025-08-26 00:43:50 +08:00
// UserController 用户管理控制器
// 负责处理所有与用户相关的 HTTP 请求
2022-05-18 17:57:03 +08:00
type UserController struct{}
// Add 添加用户记录
2025-08-26 00:43:50 +08:00
// 该接口用于创建新用户,会同时在 MySQL 和 LDAP 中创建用户记录
//
// 业务流程:
// 1. 验证用户输入参数(用户名、邮箱、手机号等唯一性)
// 2. 检查当前登录用户权限
// 3. 构建用户对象并设置默认值
// 4. 先在 MySQL 中创建用户记录
// 5. 再在 LDAP 中创建用户条目
// 6. 处理用户与部门的关联关系
//
// @Summary 添加用户记录
2025-08-26 00:43:50 +08:00
// @Description 创建新用户同时在数据库和LDAP中添加用户信息
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
2025-08-26 00:43:50 +08:00
// @Param data body request.UserAddReq true "用户信息"
// @Success 200 {object} response.ResponseBody "创建成功"
// @Failure 400 {object} response.ResponseBody "参数错误"
// @Failure 401 {object} response.ResponseBody "未授权"
// @Failure 500 {object} response.ResponseBody "服务器错误"
// @Router /user/add [post]
// @Security ApiKeyAuth
2022-05-18 17:57:03 +08:00
func (m *UserController) Add(c *gin.Context) {
req := new(request.UserAddReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.User.Add(c, req)
})
}
// Update 更新用户记录
2025-08-26 00:43:50 +08:00
// 该接口用于更新现有用户信息,会同时更新 MySQL 和 LDAP 中的用户数据
//
// 业务流程:
// 1. 验证用户输入参数和权限
// 2. 检查用户名、邮箱、手机号等字段的唯一性(排除当前用户)
// 3. 先更新 MySQL 中的用户记录
// 4. 再更新 LDAP 中的用户条目
// 5. 处理用户部门关系的变更
//
// @Summary 更新用户记录
2025-08-26 00:43:50 +08:00
// @Description 更新用户基本信息同时同步到数据库和LDAP
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
2025-08-26 00:43:50 +08:00
// @Param data body request.UserUpdateReq true "用户更新信息"
// @Success 200 {object} response.ResponseBody "更新成功"
// @Failure 400 {object} response.ResponseBody "参数错误"
// @Failure 401 {object} response.ResponseBody "未授权"
// @Failure 500 {object} response.ResponseBody "服务器错误"
// @Router /user/update [post]
// @Security ApiKeyAuth
2022-05-18 17:57:03 +08:00
func (m *UserController) Update(c *gin.Context) {
req := new(request.UserUpdateReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.User.Update(c, req)
})
}
2025-08-26 00:43:50 +08:00
// List 获取用户列表
// 该接口支持分页查询和多条件筛选,返回用户基本信息列表
//
// 支持的查询条件:
// - 用户名模糊搜索
// - 部门筛选
// - 状态筛选
// - 分页参数
//
// @Summary 获取用户列表
// @Description 分页查询用户列表,支持多条件筛选
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
2025-08-26 00:43:50 +08:00
// @Param pageNum query int false "页码"
// @Param pageSize query int false "每页数量"
// @Param username query string false "用户名"
// @Param nickname query string false "昵称"
// @Param status query int false "状态"
// @Success 200 {object} response.ResponseBody "查询成功"
// @Failure 401 {object} response.ResponseBody "未授权"
// @Failure 500 {object} response.ResponseBody "服务器错误"
// @Router /user/list [get]
// @Security ApiKeyAuth
2022-05-18 17:57:03 +08:00
func (m *UserController) List(c *gin.Context) {
req := new(request.UserListReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.User.List(c, req)
})
}
// Delete 删除用户记录
2025-08-26 00:43:50 +08:00
// 该接口用于批量删除用户,会同时从 MySQL 和 LDAP 中删除用户数据
//
// 安全限制:
// 1. 用户不能删除自己
// 2. 用户不能删除比自己角色等级高的用户
// 3. 只有管理员才能执行删除操作
//
// 业务流程:
// 1. 验证删除权限和目标用户
// 2. 先从 LDAP 中删除用户条目
// 3. 再从 MySQL 中删除用户记录
// 4. 清理相关的角色关系和部门关系
//
// @Summary 删除用户记录
2025-08-26 00:43:50 +08:00
// @Description 批量删除用户同时从数据库和LDAP中移除
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
2025-08-26 00:43:50 +08:00
// @Param data body request.UserDeleteReq true "要删除的用户ID列表"
// @Success 200 {object} response.ResponseBody "删除成功"
// @Failure 400 {object} response.ResponseBody "参数错误"
// @Failure 401 {object} response.ResponseBody "未授权"
// @Failure 403 {object} response.ResponseBody "权限不足"
// @Failure 500 {object} response.ResponseBody "服务器错误"
// @Router /user/delete [post]
// @Security ApiKeyAuth
2022-05-18 17:57:03 +08:00
func (m UserController) Delete(c *gin.Context) {
req := new(request.UserDeleteReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.User.Delete(c, req)
})
}
2025-08-26 00:43:50 +08:00
// ChangePwd 修改用户密码
// 该接口用于修改用户密码,支持管理员修改其他用户密码和用户修改自己的密码
//
// 权限控制:
// 1. 用户可以修改自己的密码
// 2. 管理员可以修改任何用户的密码
// 3. 普通用户不能修改其他用户的密码
//
// 业务流程:
// 1. 验证当前用户权限
// 2. 验证旧密码(如果是修改自己的密码)
// 3. 加密新密码
// 4. 同时更新 MySQL 和 LDAP 中的密码
//
// @Summary 修改用户密码
// @Description 修改指定用户的登录密码同时更新数据库和LDAP
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
2025-08-26 00:43:50 +08:00
// @Param data body request.UserChangePwdReq true "密码修改信息"
// @Success 200 {object} response.ResponseBody "修改成功"
// @Failure 400 {object} response.ResponseBody "参数错误"
// @Failure 401 {object} response.ResponseBody "未授权"
// @Failure 403 {object} response.ResponseBody "权限不足"
// @Failure 500 {object} response.ResponseBody "服务器错误"
// @Router /user/changePwd [post]
// @Security ApiKeyAuth
2022-05-18 17:57:03 +08:00
func (m UserController) ChangePwd(c *gin.Context) {
req := new(request.UserChangePwdReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.User.ChangePwd(c, req)
})
}
2025-08-26 00:43:50 +08:00
// ChangeUserStatus 修改用户状态
// 该接口用于启用或禁用用户账户,只有管理员才能执行此操作
//
// 用户状态说明:
// - 1: 正常状态,用户可以正常登录和使用系统
// - 2: 禁用状态,用户无法登录系统
//
// 业务流程:
// 1. 验证当前用户是否为管理员
// 2. 根据状态决定在 LDAP 中添加或删除用户条目
// 3. 更新 MySQL 中的用户状态
//
// 注意:禁用用户时会从 LDAP 中删除用户条目,启用时会重新添加
//
// @Summary 修改用户状态
// @Description 启用或禁用用户账户,只有管理员可操作
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
2025-08-26 00:43:50 +08:00
// @Param data body request.UserChangeUserStatusReq true "用户状态修改信息"
// @Success 200 {object} response.ResponseBody "修改成功"
// @Failure 400 {object} response.ResponseBody "参数错误"
// @Failure 401 {object} response.ResponseBody "未授权"
// @Failure 403 {object} response.ResponseBody "权限不足"
// @Failure 500 {object} response.ResponseBody "服务器错误"
// @Router /user/changeUserStatus [post]
// @Security ApiKeyAuth
2022-05-18 17:57:03 +08:00
func (m UserController) ChangeUserStatus(c *gin.Context) {
req := new(request.UserChangeUserStatusReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.User.ChangeUserStatus(c, req)
})
}
// GetUserInfo 获取当前登录用户信息
// @Summary 获取当前登录用户信息
// @Description 获取当前登录用户信息
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.ResponseBody
// @Router /user/info [get]
// @Security ApiKeyAuth
2022-05-18 17:57:03 +08:00
func (uc UserController) GetUserInfo(c *gin.Context) {
req := new(request.UserGetUserInfoReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.User.GetUserInfo(c, req)
})
}
// SyncDingTalkUsers 同步钉钉用户信息
// @Summary 同步钉钉用户信息
// @Description 同步钉钉用户信息
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
// @Param data body request.SyncDingUserReq true "同步钉钉用户信息"
// @Success 200 {object} response.ResponseBody
// @Router /user/syncDingTalkUsers [post]
// @Security ApiKeyAuth
func (uc UserController) SyncDingTalkUsers(c *gin.Context) {
req := new(request.SyncDingUserReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.DingTalk.SyncDingTalkUsers(c, req)
})
}
// SyncWeComUsers 同步企业微信用户信息
// @Summary 同步企业微信用户信息
// @Description 同步企业微信用户信息
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
// @Param data body request.SyncWeComUserReq true "同步企业微信用户信息"
// @Success 200 {object} response.ResponseBody
// @Router /user/syncWeComUsers [post]
// @Security ApiKeyAuth
func (uc UserController) SyncWeComUsers(c *gin.Context) {
req := new(request.SyncWeComUserReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.WeCom.SyncWeComUsers(c, req)
})
}
2022-06-21 20:50:38 +08:00
// SyncFeiShuUsers 同步飞书用户信息
// @Summary 同步飞书用户信息
// @Description 同步飞书用户信息
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
// @Param data body request.SyncFeiShuUserReq true "同步飞书用户信息"
// @Success 200 {object} response.ResponseBody
// @Router /user/syncFeiShuUsers [post]
// @Security ApiKeyAuth
2022-06-21 20:50:38 +08:00
func (uc UserController) SyncFeiShuUsers(c *gin.Context) {
req := new(request.SyncFeiShuUserReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.FeiShu.SyncFeiShuUsers(c, req)
})
}
// SyncOpenLdapUsers 同步ldap用户信息
// @Summary 同步ldap用户信息
// @Description 同步ldap用户信息
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
// @Param data body request.SyncOpenLdapUserReq true "同步ldap用户信息"
// @Success 200 {object} response.ResponseBody
// @Router /user/syncOpenLdapUsers [post]
// @Security ApiKeyAuth
func (uc UserController) SyncOpenLdapUsers(c *gin.Context) {
req := new(request.SyncOpenLdapUserReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.OpenLdap.SyncOpenLdapUsers(c, req)
})
}
// SyncSqlUsers 同步sql用户信息到ldap
// @Summary 同步sql用户信息到ldap
// @Description 同步sql用户信息到ldap
// @Tags 用户管理
// @Accept application/json
// @Produce application/json
// @Param data body request.SyncSqlUserReq true "更改用户状态的结构体"
// @Success 200 {object} response.ResponseBody
// @Router /user/syncSqlUsers [post]
// @Security ApiKeyAuth
func (uc UserController) SyncSqlUsers(c *gin.Context) {
req := new(request.SyncSqlUserReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.Sql.SyncSqlUsers(c, req)
})
}