2025-08-26 00:43:50 +08:00
|
|
|
|
/*
|
|
|
|
|
Package logic 包含所有业务逻辑处理
|
|
|
|
|
|
|
|
|
|
用户业务逻辑层负责处理用户管理的核心业务逻辑,包括:
|
|
|
|
|
- 用户数据的验证和处理
|
|
|
|
|
- MySQL 和 LDAP 的双重数据操作
|
|
|
|
|
- 用户权限和角色管理
|
|
|
|
|
- 密码加密和验证
|
|
|
|
|
- 部门关系处理
|
|
|
|
|
|
|
|
|
|
业务逻辑层的职责:
|
|
|
|
|
1. 接收控制器层传递的请求参数
|
|
|
|
|
2. 执行业务规则验证
|
|
|
|
|
3. 协调数据访问层完成数据操作
|
|
|
|
|
4. 处理复杂的业务流程
|
|
|
|
|
5. 返回处理结果给控制器层
|
|
|
|
|
*/
|
2022-05-18 17:57:03 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2023-11-12 13:59:14 +08:00
|
|
|
|
"strings"
|
2022-05-18 17:57:03 +08:00
|
|
|
|
|
2022-05-29 10:06:21 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/config"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/model"
|
2022-06-14 11:17:38 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/model/request"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/model/response"
|
2022-05-29 10:06:21 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/public/tools"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/service/ildap"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/service/isql"
|
2022-05-18 17:57:03 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/thoas/go-funk"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-26 00:43:50 +08:00
|
|
|
|
// UserLogic 用户业务逻辑处理器
|
|
|
|
|
// 负责处理所有与用户相关的业务逻辑
|
2022-05-18 17:57:03 +08:00
|
|
|
|
type UserLogic struct{}
|
|
|
|
|
|
2025-08-26 00:43:50 +08:00
|
|
|
|
// Add 添加用户业务逻辑
|
|
|
|
|
// 该方法处理用户创建的完整业务流程,包括数据验证、密码处理、
|
|
|
|
|
// MySQL 和 LDAP 双重存储、部门关系建立等
|
|
|
|
|
//
|
|
|
|
|
// 业务流程:
|
|
|
|
|
// 1. 参数类型断言和基础验证
|
|
|
|
|
// 2. 检查用户名、手机号、工号、邮箱的唯一性
|
|
|
|
|
// 3. RSA 解密用户密码并验证强度
|
|
|
|
|
// 4. 获取当前登录用户信息
|
|
|
|
|
// 5. 验证角色权限
|
|
|
|
|
// 6. 构建用户对象并设置默认值
|
|
|
|
|
// 7. 获取用户所属部门信息
|
|
|
|
|
// 8. 调用通用添加用户方法完成创建
|
2022-05-18 17:57:03 +08:00
|
|
|
|
func (l UserLogic) Add(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
2025-08-26 00:43:50 +08:00
|
|
|
|
// 参数类型断言
|
2022-05-18 17:57:03 +08:00
|
|
|
|
r, ok := req.(*request.UserAddReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
2025-08-26 00:43:50 +08:00
|
|
|
|
// ==================== 数据唯一性验证 ====================
|
|
|
|
|
|
|
|
|
|
// 检查用户名是否已存在
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if isql.User.Exist(tools.H{"username": r.Username}) {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("用户名已存在,请勿重复添加"))
|
|
|
|
|
}
|
2025-08-26 00:43:50 +08:00
|
|
|
|
// 检查手机号是否已存在
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if isql.User.Exist(tools.H{"mobile": r.Mobile}) {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("手机号已存在,请勿重复添加"))
|
|
|
|
|
}
|
2025-08-26 00:43:50 +08:00
|
|
|
|
// 检查工号是否已存在
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if isql.User.Exist(tools.H{"job_number": r.JobNumber}) {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("工号已存在,请勿重复添加"))
|
|
|
|
|
}
|
2025-08-26 00:43:50 +08:00
|
|
|
|
// 检查邮箱是否已存在
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if isql.User.Exist(tools.H{"mail": r.Mail}) {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("邮箱已存在,请勿重复添加"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 密码通过RSA解密
|
|
|
|
|
// 密码不为空就解密
|
|
|
|
|
if r.Password != "" {
|
|
|
|
|
decodeData, err := tools.RSADecrypt([]byte(r.Password), config.Conf.System.RSAPrivateBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("密码解密失败"))
|
|
|
|
|
}
|
|
|
|
|
r.Password = string(decodeData)
|
|
|
|
|
if len(r.Password) < 6 {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("密码长度至少为6位"))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-06-14 12:08:16 +08:00
|
|
|
|
r.Password = config.Conf.Ldap.UserInitPassword
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 当前登陆用户角色排序最小值(最高等级角色)以及当前登陆的用户
|
|
|
|
|
currentRoleSortMin, ctxUser, err := isql.User.GetCurrentUserMinRoleSort(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("获取当前登陆用户角色排序最小值失败"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据角色id获取角色
|
|
|
|
|
if r.RoleIds == nil || len(r.RoleIds) == 0 {
|
|
|
|
|
r.RoleIds = []uint{2} // 默认添加为普通用户角色
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
roles, err := isql.Role.GetRolesByIds(r.RoleIds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("根据角色ID获取角色信息失败"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var reqRoleSorts []int
|
|
|
|
|
for _, role := range roles {
|
|
|
|
|
reqRoleSorts = append(reqRoleSorts, int(role.Sort))
|
|
|
|
|
}
|
|
|
|
|
// 前端传来用户角色排序最小值(最高等级角色)
|
|
|
|
|
reqRoleSortMin := uint(funk.MinInt(reqRoleSorts).(int))
|
|
|
|
|
|
2022-07-20 09:43:00 +08:00
|
|
|
|
// 如果登录用户的角色ID为1,亦即为管理员,则直接放行,保障管理员拥有最大权限
|
|
|
|
|
if currentRoleSortMin != 1 {
|
|
|
|
|
// 当前用户的角色排序最小值 需要小于 前端传来的角色排序最小值(用户不能创建比自己等级高的或者相同等级的用户)
|
|
|
|
|
if currentRoleSortMin >= reqRoleSortMin {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("用户不能创建比自己等级高的或者相同等级的用户"))
|
|
|
|
|
}
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
user := model.User{
|
|
|
|
|
Username: r.Username,
|
|
|
|
|
Password: r.Password,
|
|
|
|
|
Nickname: r.Nickname,
|
|
|
|
|
GivenName: r.GivenName,
|
|
|
|
|
Mail: r.Mail,
|
|
|
|
|
JobNumber: r.JobNumber,
|
|
|
|
|
Mobile: r.Mobile,
|
|
|
|
|
Avatar: r.Avatar,
|
|
|
|
|
PostalAddress: r.PostalAddress,
|
|
|
|
|
Departments: r.Departments,
|
|
|
|
|
Position: r.Position,
|
|
|
|
|
Introduction: r.Introduction,
|
|
|
|
|
Status: r.Status,
|
|
|
|
|
Creator: ctxUser.Username,
|
2022-06-14 11:17:38 +08:00
|
|
|
|
DepartmentId: tools.SliceToString(r.DepartmentId, ","),
|
2022-05-28 22:22:36 +08:00
|
|
|
|
Source: r.Source,
|
2022-05-18 17:57:03 +08:00
|
|
|
|
Roles: roles,
|
2022-06-14 12:08:16 +08:00
|
|
|
|
UserDN: fmt.Sprintf("uid=%s,%s", r.Username, config.Conf.Ldap.UserDN),
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
2022-05-28 22:22:36 +08:00
|
|
|
|
if user.Source == "" {
|
|
|
|
|
user.Source = "platform"
|
|
|
|
|
}
|
2022-08-25 22:11:26 +08:00
|
|
|
|
|
2022-07-15 17:56:45 +08:00
|
|
|
|
// 获取用户将要添加的分组
|
|
|
|
|
groups, err := isql.Group.GetGroupByIds(tools.StringToSlice(user.DepartmentId, ","))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("根据部门ID获取部门信息失败" + err.Error()))
|
|
|
|
|
}
|
2022-08-25 22:11:26 +08:00
|
|
|
|
|
2022-07-15 17:56:45 +08:00
|
|
|
|
err = CommonAddUser(&user, groups)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
2022-06-14 11:17:38 +08:00
|
|
|
|
return nil, tools.NewOperationError(fmt.Errorf("添加用户失败" + err.Error()))
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List 数据列表
|
|
|
|
|
func (l UserLogic) List(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.UserListReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
users, err := isql.User.List(r)
|
|
|
|
|
if err != nil {
|
2022-05-28 22:22:36 +08:00
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取用户列表失败:" + err.Error()))
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rets := make([]model.User, 0)
|
|
|
|
|
for _, user := range users {
|
|
|
|
|
rets = append(rets, *user)
|
|
|
|
|
}
|
2022-05-28 22:22:36 +08:00
|
|
|
|
count, err := isql.User.ListCount(r)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
2022-05-28 22:22:36 +08:00
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取用户总数失败:" + err.Error()))
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.UserListRsp{
|
|
|
|
|
Total: int(count),
|
|
|
|
|
Users: rets,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update 更新数据
|
|
|
|
|
func (l UserLogic) Update(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.UserUpdateReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
if !isql.User.Exist(tools.H{"id": r.ID}) {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("该记录不存在"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前登陆用户
|
|
|
|
|
ctxUser, err := isql.User.GetCurrentLoginUser(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取当前登陆用户失败"))
|
|
|
|
|
}
|
2022-07-20 09:43:00 +08:00
|
|
|
|
|
|
|
|
|
// 获取当前登陆用户角色ID集合
|
2022-05-18 17:57:03 +08:00
|
|
|
|
var currentRoleSorts []int
|
2022-07-20 09:43:00 +08:00
|
|
|
|
for _, role := range ctxUser.Roles {
|
2022-05-18 17:57:03 +08:00
|
|
|
|
currentRoleSorts = append(currentRoleSorts, int(role.Sort))
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 09:43:00 +08:00
|
|
|
|
// 获取将要操作的用户角色ID集合
|
|
|
|
|
var reqRoleSorts []int
|
2023-02-08 15:31:44 +08:00
|
|
|
|
roles, _ := isql.Role.GetRolesByIds(r.RoleIds)
|
|
|
|
|
if len(roles) == 0 {
|
2022-05-18 17:57:03 +08:00
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("根据角色ID获取角色信息失败"))
|
|
|
|
|
}
|
|
|
|
|
for _, role := range roles {
|
|
|
|
|
reqRoleSorts = append(reqRoleSorts, int(role.Sort))
|
|
|
|
|
}
|
2022-07-20 09:43:00 +08:00
|
|
|
|
|
|
|
|
|
// 当前登陆用户角色排序最小值(最高等级角色)
|
|
|
|
|
currentRoleSortMin := funk.MinInt(currentRoleSorts).(int)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
// 前端传来用户角色排序最小值(最高等级角色)
|
|
|
|
|
reqRoleSortMin := funk.MinInt(reqRoleSorts).(int)
|
|
|
|
|
|
2022-07-20 09:43:00 +08:00
|
|
|
|
// 如果登录用户的角色ID为1,亦即为管理员,则直接放行,保障管理员拥有最大权限
|
|
|
|
|
if currentRoleSortMin != 1 {
|
|
|
|
|
// 判断是更新自己还是更新别人,如果操作的ID与登陆用户的ID一致,则说明操作的是自己
|
|
|
|
|
if int(r.ID) == int(ctxUser.ID) {
|
|
|
|
|
// 不能更改自己的角色
|
|
|
|
|
reqDiff, currentDiff := funk.Difference(reqRoleSorts, currentRoleSorts)
|
|
|
|
|
if len(reqDiff.([]int)) > 0 || len(currentDiff.([]int)) > 0 {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("不能更改自己的角色"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是更新别人,操作者不能更新比自己角色等级高的或者相同等级的用户
|
|
|
|
|
minRoleSorts, err := isql.User.GetUserMinRoleSortsByIds([]uint{uint(r.ID)}) // 根据userIdID获取用户角色排序最小值
|
|
|
|
|
if err != nil || len(minRoleSorts) == 0 {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("根据用户ID获取用户角色排序最小值失败"))
|
|
|
|
|
}
|
|
|
|
|
if currentRoleSortMin >= minRoleSorts[0] || currentRoleSortMin >= reqRoleSortMin {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("用户不能更新比自己角色等级高的或者相同等级的用户"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 先获取用户信息
|
2022-05-18 17:57:03 +08:00
|
|
|
|
oldData := new(model.User)
|
|
|
|
|
err = isql.User.Find(tools.H{"id": r.ID}, oldData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(err)
|
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
2023-11-15 23:32:51 +08:00
|
|
|
|
// 过滤掉前端会选择到的 请选择部门信息 这个选项
|
2023-11-12 13:59:14 +08:00
|
|
|
|
var (
|
|
|
|
|
depts string
|
|
|
|
|
deptids []uint
|
|
|
|
|
)
|
2023-11-15 23:32:51 +08:00
|
|
|
|
for _, v := range strings.Split(r.Departments, ",") {
|
|
|
|
|
if v != "请选择部门信息" {
|
|
|
|
|
depts += v + ","
|
2023-11-12 13:59:14 +08:00
|
|
|
|
}
|
2023-11-15 23:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
for _, j := range r.DepartmentId {
|
|
|
|
|
if j != 0 {
|
|
|
|
|
deptids = append(deptids, j)
|
2023-11-12 13:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-20 09:43:00 +08:00
|
|
|
|
// 拼装新的用户信息
|
2022-05-18 17:57:03 +08:00
|
|
|
|
user := model.User{
|
|
|
|
|
Model: oldData.Model,
|
2022-05-28 22:22:36 +08:00
|
|
|
|
Username: r.Username,
|
2022-05-18 17:57:03 +08:00
|
|
|
|
Nickname: r.Nickname,
|
|
|
|
|
GivenName: r.GivenName,
|
|
|
|
|
Mail: r.Mail,
|
|
|
|
|
JobNumber: r.JobNumber,
|
|
|
|
|
Mobile: r.Mobile,
|
|
|
|
|
Avatar: r.Avatar,
|
|
|
|
|
PostalAddress: r.PostalAddress,
|
2023-11-12 13:59:14 +08:00
|
|
|
|
Departments: depts,
|
2022-05-18 17:57:03 +08:00
|
|
|
|
Position: r.Position,
|
|
|
|
|
Introduction: r.Introduction,
|
|
|
|
|
Creator: ctxUser.Username,
|
2023-11-12 13:59:14 +08:00
|
|
|
|
DepartmentId: tools.SliceToString(deptids, ","),
|
2022-05-28 22:22:36 +08:00
|
|
|
|
Source: oldData.Source,
|
2022-05-18 17:57:03 +08:00
|
|
|
|
Roles: roles,
|
2022-06-14 11:17:38 +08:00
|
|
|
|
UserDN: oldData.UserDN,
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
if err = CommonUpdateUser(oldData, &user, r.DepartmentId); err != nil {
|
|
|
|
|
return nil, tools.NewOperationError(fmt.Errorf("更新用户失败" + err.Error()))
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete 删除数据
|
|
|
|
|
func (l UserLogic) Delete(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.UserDeleteReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
for _, id := range r.UserIds {
|
|
|
|
|
filter := tools.H{"id": int(id)}
|
|
|
|
|
if !isql.User.Exist(filter) {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("有用户不存在"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 17:57:03 +08:00
|
|
|
|
// 根据用户ID获取用户角色排序最小值
|
2022-07-20 09:43:00 +08:00
|
|
|
|
roleMinSortList, err := isql.User.GetUserMinRoleSortsByIds(r.UserIds)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil || len(roleMinSortList) == 0 {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("根据用户ID获取用户角色排序最小值失败"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前登陆用户角色排序最小值(最高等级角色)以及当前用户
|
|
|
|
|
minSort, ctxUser, err := isql.User.GetCurrentUserMinRoleSort(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("获取当前登陆用户角色排序最小值失败"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 不能删除自己
|
|
|
|
|
if funk.Contains(r.UserIds, ctxUser.ID) {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("用户不能删除自己"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 不能删除比自己(登陆用户)角色排序低(等级高)的用户
|
|
|
|
|
for _, sort := range roleMinSortList {
|
2022-07-20 09:43:00 +08:00
|
|
|
|
if int(minSort) > sort {
|
2022-05-18 17:57:03 +08:00
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("用户不能删除比自己角色等级高的用户"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
users, err := isql.User.GetUserByIds(r.UserIds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取用户信息失败: " + err.Error()))
|
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
// 先将用户从ldap中删除
|
2022-05-18 17:57:03 +08:00
|
|
|
|
for _, user := range users {
|
2022-06-14 11:17:38 +08:00
|
|
|
|
err := ildap.User.Delete(user.UserDN)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("在LDAP删除用户失败" + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
// 再将用户从MySQL中删除
|
2022-05-18 17:57:03 +08:00
|
|
|
|
err = isql.User.Delete(r.UserIds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("在MySQL删除用户失败: " + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ChangePwd 修改密码
|
|
|
|
|
func (l UserLogic) ChangePwd(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.UserChangePwdReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
// 前端传来的密码是rsa加密的,先解密
|
|
|
|
|
// 密码通过RSA解密
|
|
|
|
|
decodeOldPassword, err := tools.RSADecrypt([]byte(r.OldPassword), config.Conf.System.RSAPrivateBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("原密码解析失败"))
|
|
|
|
|
}
|
|
|
|
|
decodeNewPassword, err := tools.RSADecrypt([]byte(r.NewPassword), config.Conf.System.RSAPrivateBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("新密码解析失败"))
|
|
|
|
|
}
|
|
|
|
|
r.OldPassword = string(decodeOldPassword)
|
|
|
|
|
r.NewPassword = string(decodeNewPassword)
|
|
|
|
|
// 获取当前用户
|
|
|
|
|
user, err := isql.User.GetCurrentLoginUser(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取当前登陆用户失败"))
|
|
|
|
|
}
|
|
|
|
|
// 获取用户的真实正确密码
|
|
|
|
|
// correctPasswd := user.Password
|
|
|
|
|
// 判断前端请求的密码是否等于真实密码
|
|
|
|
|
// err = tools.ComparePasswd(correctPasswd, r.OldPassword)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return nil, tools.NewValidatorError(fmt.Errorf("原密码错误"))
|
|
|
|
|
// }
|
|
|
|
|
if tools.NewParPasswd(user.Password) != r.OldPassword {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("原密码错误"))
|
|
|
|
|
}
|
|
|
|
|
// ldap更新密码时可以直接指定用户DN和新密码即可更改成功
|
2022-06-14 11:17:38 +08:00
|
|
|
|
err = ildap.User.ChangePwd(user.UserDN, "", r.NewPassword)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("在LDAP更新密码失败" + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新密码
|
|
|
|
|
err = isql.User.ChangePwd(user.Username, tools.NewGenPasswd(r.NewPassword))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("在MySQL更新密码失败: " + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ChangeUserStatus 修改用户状态
|
|
|
|
|
func (l UserLogic) ChangeUserStatus(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.UserChangeUserStatusReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
// 校验工作
|
|
|
|
|
filter := tools.H{"id": r.ID}
|
|
|
|
|
if !isql.User.Exist(filter) {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("该用户不存在"))
|
|
|
|
|
}
|
|
|
|
|
user := new(model.User)
|
|
|
|
|
err := isql.User.Find(filter, user)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("在MySQL查询用户失败: " + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.Status == 1 && r.Status == user.Status {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("用户已经是在职状态"))
|
|
|
|
|
}
|
|
|
|
|
if r.Status == 2 && r.Status == user.Status {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("用户已经是离职状态"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前登录用户,只有管理员才能够将用户状态改变
|
|
|
|
|
// 获取当前登陆用户角色排序最小值(最高等级角色)以及当前用户
|
|
|
|
|
minSort, _, err := isql.User.GetCurrentUserMinRoleSort(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("获取当前登陆用户角色排序最小值失败"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if int(minSort) != 1 {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("只有管理员才能更改用户状态"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.Status == 2 {
|
2022-06-14 11:17:38 +08:00
|
|
|
|
err = ildap.User.Delete(user.UserDN)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("在LDAP删除用户失败" + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-05-28 22:22:36 +08:00
|
|
|
|
err = ildap.User.Add(user)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("在LDAP添加用户失败" + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = isql.User.ChangeStatus(int(r.ID), int(r.Status))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("在MySQL更新用户状态失败: " + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetUserInfo 获取用户信息
|
|
|
|
|
func (l UserLogic) GetUserInfo(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.UserGetUserInfoReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ = c
|
|
|
|
|
_ = r
|
|
|
|
|
|
|
|
|
|
user, err := isql.User.GetCurrentLoginUser(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取当前用户信息失败: " + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
return user, nil
|
|
|
|
|
}
|