This commit is contained in:
laodaming 2023-11-21 15:07:44 +08:00
parent 787f33f555
commit be076197d2
5 changed files with 75 additions and 35 deletions

View File

@ -2,11 +2,15 @@ package gmodel
import (
"gorm.io/gorm"
"time"
)
// ldap_user ldap_用户id递增表
type LdapUser struct {
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` //
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` //
UserDn *string `gorm:"index;default:'';" json:"user_dn"` //
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` //
Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` //
}
type LdapUserModel struct {
db *gorm.DB

View File

@ -6,3 +6,18 @@ import "context"
func (u *LdapUserModel) Create(ctx context.Context, data *LdapUser) error {
return u.db.WithContext(ctx).Model(&LdapUser{}).Create(&data).Error
}
func (u *LdapUserModel) Update(ctx context.Context, userDN string, data *LdapUser) error {
return u.db.WithContext(ctx).Model(&LdapUser{}).Where("user_dn = ?", userDN).Updates(&data).Error
}
func (u *LdapUserModel) Delete(ctx context.Context, id int64) error {
return u.db.WithContext(ctx).Model(&LdapUser{}).Where("id = ?", id).Delete(&LdapUser{}).Error
}
func (u *LdapUserModel) GetAllByIds(ctx context.Context, ids []int64) (resp []LdapUser, err error) {
if len(ids) == 0 {
return
}
err = u.db.WithContext(ctx).Model(&LdapUser{}).Where("id in (?)", ids).Find(&resp).Error
return resp, err
}

View File

@ -9,7 +9,9 @@ import (
"fusenapi/utils/email"
"fusenapi/utils/encryption_decryption"
"fusenapi/utils/ldap_lib"
"gorm.io/gorm"
"strings"
"time"
"context"
@ -57,40 +59,45 @@ func (l *CreateLdapUserLogic) CreateLdapUser(req *types.CreateLdapUserReq, useri
ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN, l.svcCtx.Config.Ldap.PeopleGroupDN)
//把用户名转pinyin
userNamePinyin := chinese_to_pinyin.ChineseToPinyin(req.UserName)
//新增一条记录获取递增用户id
userData := &gmodel.LdapUser{}
if err := l.svcCtx.AllModels.LdapUser.Create(l.ctx, userData); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "获取自增用户id失败")
}
userDN := fmt.Sprintf("cn=%s,%s", req.Email, l.svcCtx.Config.Ldap.PeopleGroupDN)
pwd, err := encryption_decryption.CBCEncrypt(req.Password)
//新增一条记录获取递增用户id
now := time.Now().UTC()
err := l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error {
userData := &gmodel.LdapUser{
UserDn: &userDN,
Ctime: &now,
Utime: &now,
}
if err := tx.WithContext(l.ctx).Model(&gmodel.LdapUser{}).Create(userData).Error; err != nil {
return err
}
pwd, err := encryption_decryption.CBCEncrypt(req.Password)
if err != nil {
return err
}
return ldapServer.Create(userDN, map[string][]string{
"objectClass": {"person", "organizationalPerson", "inetOrgPerson", "posixAccount", "top", "shadowAccount"}, //固有属性
"shadowLastChange": {"19676"}, //固有属性
"shadowMin": {"0"}, //固有属性
"shadowMax": {"99999"}, //固有属性
"shadowWarning": {"7"}, //固有属性
"loginShell": {"/usr/sbin/nologin"}, //固有属性
"homeDirectory": {"/home/users/" + userNamePinyin},
"employeeType": {fmt.Sprintf("%d", req.EmployeeType)}, //员工类型1正式 2实习 3外包
"uidNumber": {fmt.Sprintf("%d", userData.Id)}, //用户id
"gidNumber": {fmt.Sprintf("%d", userData.Id)}, //用户id
"uid": {userNamePinyin}, //用户名(拼音)
"cn": {req.Email}, //邮箱
"sn": {req.UserName}, //用户名
"mail": {req.Email}, //邮箱
"postalCode": {fmt.Sprintf("%d", req.Status)}, //状态
"departmentNumber": {fmt.Sprintf("%d", req.GroupId)}, //权限分组id
"postalAddress": {req.Avatar}, //头像
"mobile": {req.Mobile}, //手机号
"userPassword": {"{crypt}" + pwd}, //密码
})
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "加密密码失败")
}
if err := ldapServer.Create(userDN, map[string][]string{
"objectClass": {"person", "organizationalPerson", "inetOrgPerson", "posixAccount", "top", "shadowAccount"}, //固有属性
"shadowLastChange": {"19676"}, //固有属性
"shadowMin": {"0"}, //固有属性
"shadowMax": {"99999"}, //固有属性
"shadowWarning": {"7"}, //固有属性
"loginShell": {"/usr/sbin/nologin"}, //固有属性
"homeDirectory": {"/home/users/" + userNamePinyin},
"employeeType": {fmt.Sprintf("%d", req.EmployeeType)}, //员工类型1正式 2实习 3外包
"uidNumber": {fmt.Sprintf("%d", userData.Id)}, //用户id
"gidNumber": {fmt.Sprintf("%d", userData.Id)}, //用户id
"uid": {userNamePinyin}, //用户名(拼音)
"cn": {req.Email}, //邮箱
"sn": {req.UserName}, //用户名
"mail": {req.Email}, //邮箱
"postalCode": {fmt.Sprintf("%d", req.Status)}, //状态
"departmentNumber": {fmt.Sprintf("%d", req.GroupId)}, //权限分组id
"postalAddress": {req.Avatar}, //头像
"mobile": {req.Mobile}, //手机号
"userPassword": {"{crypt}" + pwd}, //密码
}); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "添加用户失败,"+err.Error())
}
return resp.SetStatusWithMessage(basic.CodeOK, "添加用户成功")

View File

@ -90,6 +90,13 @@ func (l *GetLdapOrganizationMembersLogic) GetLdapOrganizationMembers(req *types.
}
list := make([]types.GetLdapOrganizationMembersItem, 0, memberCount)
for _, user := range userList {
if user.Status != 1 {
//从部门member中移出
if err = ldapServer.RemoveUserFromOrganization(req.OrganizationDN, user.UserDN); err != nil {
logx.Error("移除用户成员失败:", err)
}
continue
}
list = append(list, types.GetLdapOrganizationMembersItem{
UserId: user.UserId,
UserDN: user.UserDN,
@ -99,8 +106,6 @@ func (l *GetLdapOrganizationMembersLogic) GetLdapOrganizationMembers(req *types.
Avatar: user.Avatar,
EmployeeType: user.EmployeeType,
Status: user.Status,
//CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
//UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"),
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetLdapOrganizationMembersRsp{

View File

@ -2,12 +2,14 @@ package logic
import (
"fmt"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/chinese_to_pinyin"
"fusenapi/utils/email"
"fusenapi/utils/ldap_lib"
"strings"
"time"
"context"
@ -53,6 +55,7 @@ func (l *UpdateLdapUserLogic) UpdateLdapUser(req *types.UpdateLdapUserReq, useri
//把用户名转pinyin
userNamePinyin := chinese_to_pinyin.ChineseToPinyin(req.UserName)
ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN, l.svcCtx.Config.Ldap.PeopleGroupDN)
now := time.Now()
//更新的属性
attr := map[string][]string{
"homeDirectory": {"/home/users/" + userNamePinyin},
@ -69,6 +72,12 @@ func (l *UpdateLdapUserLogic) UpdateLdapUser(req *types.UpdateLdapUserReq, useri
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "更新用户失败,"+err.Error())
}
err = l.svcCtx.AllModels.LdapUser.Update(l.ctx, req.UserDN, &gmodel.LdapUser{
Utime: &now,
})
if err != nil {
logx.Error(err)
}
return resp.SetStatusWithMessage(basic.CodeOK, "更新用户成功")
}