This commit is contained in:
laodaming 2023-11-15 14:28:14 +08:00
parent 6822c3d94c
commit 1041caad23
2 changed files with 56 additions and 37 deletions

View File

@ -1,37 +0,0 @@
package ldap_lib
import (
"github.com/go-ldap/ldap/v3"
)
type LdapGroup struct {
conn *ldap.Conn
}
func NewLdapGroup(conn *ldap.Conn) *LdapGroup {
return &LdapGroup{conn}
}
// 更新分组信息
func (l *LdapGroup) UpdateGroup(groupDN string, attr map[string][]string) error {
modify := ldap.NewModifyRequest(groupDN, nil)
for key, v := range attr {
modify.Replace(key, v)
}
return l.conn.Modify(modify)
}
// 删除分组
func (l *LdapGroup) DeleteGroup(groupDN string) error {
del := ldap.NewDelRequest(groupDN, nil)
return l.conn.Del(del)
}
// 创建分组
func (l *LdapGroup) CreateGroup(groupDN string, attr map[string][]string) error {
add := ldap.NewAddRequest(groupDN, nil)
for key, v := range attr {
add.Attribute(key, v)
}
return l.conn.Add(add)
}

View File

@ -0,0 +1,56 @@
package ldap_lib
import (
"errors"
"github.com/go-ldap/ldap/v3"
)
type Ldap struct {
conn *ldap.Conn
}
func NewLdap(conn *ldap.Conn) *Ldap {
return &Ldap{conn}
}
// 更新资源(分组/用户)
func (l *Ldap) Update(DN string, attr map[string][]string) error {
modify := ldap.NewModifyRequest(DN, nil)
for key, v := range attr {
modify.Replace(key, v)
}
return l.conn.Modify(modify)
}
// 创建资源(分组/用户)
func (l *Ldap) Create(DN string, attr map[string][]string) error {
add := ldap.NewAddRequest(DN, nil)
for key, v := range attr {
add.Attribute(key, v)
}
return l.conn.Add(add)
}
// 删除资源(分组/用户)
func (l *Ldap) Delete(DN string) error {
del := ldap.NewDelRequest(DN, nil)
return l.conn.Del(del)
}
// AddUserToGroup 添加用户到分组
func (l *Ldap) AddUserToGroup(groupDN, userDN string) error {
//判断dn是否以ou开头
if groupDN[:3] == "ou=" {
return errors.New("不能添加用户到OU组织单元")
}
modify := ldap.NewModifyRequest(groupDN, nil)
modify.Add("uniqueMember", []string{userDN})
return l.conn.Modify(modify)
}
// DelUserFromGroup 将用户从分组删除
func (l *Ldap) RemoveUserFromGroup(groupDN, userDN string) error {
modify := ldap.NewModifyRequest(groupDN, nil)
modify.Delete("uniqueMember", []string{userDN})
return l.conn.Modify(modify)
}