-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathupdate_group.go
72 lines (65 loc) · 1.58 KB
/
update_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package pkg
import (
"fmt"
"strconv"
"github.com/go-ldap/ldap/v3"
ldaperror "github.com/romnn/ldap-manager/pkg/err"
pb "github.com/romnn/ldap-manager/pkg/grpc/gen"
log "github.com/sirupsen/logrus"
)
// UpdateGroup updates a group
func (m *LDAPManager) UpdateGroup(req *pb.UpdateGroupRequest) error {
groupName := req.GetName()
if groupName == "" {
return &ldaperror.ValidationError{
Message: "group name must not be empty",
}
}
conn, err := m.Pool.Get()
if err != nil {
return err
}
defer conn.Close()
newGroupName := req.GetNewName()
if newGroupName != "" && newGroupName != groupName {
modifyRequest := &ldap.ModifyDNRequest{
DN: m.GroupDN(groupName),
NewRDN: fmt.Sprintf("cn=%s", newGroupName),
DeleteOldRDN: true,
NewSuperior: "",
}
log.Debug(PrettyPrint(modifyRequest))
if err := conn.ModifyDN(modifyRequest); err != nil {
return fmt.Errorf(
"failed to rename group %q to %q",
groupName, newGroupName,
)
}
log.Infof(
"renamed group from %q to %q",
groupName, newGroupName,
)
groupName = newGroupName
}
modifyGroupRequest := ldap.NewModifyRequest(
m.GroupDN(groupName),
[]ldap.Control{},
)
// update GID
if req.GetGID() >= MinGID {
GID := strconv.Itoa(int(req.GetGID()))
modifyGroupRequest.Replace("gidNumber", []string{GID})
}
if err := conn.Modify(modifyGroupRequest); err != nil {
return fmt.Errorf(
"failed to modify group %q: %v",
groupName, err,
)
}
updated := len(modifyGroupRequest.Changes)
log.Infof(
"updated %d attributes of group %q",
updated, groupName,
)
return nil
}