54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package group
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
ldap "github.com/go-ldap/ldap/v3"
|
|
)
|
|
|
|
func init() {
|
|
// define group delete subcommand flags
|
|
groupDeleteCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your group entries are stored")
|
|
groupDeleteCmd.Flags().StringP("id_attribute", "a", "cn", "LDAP DN attribute for groups")
|
|
groupDeleteCmd.Flags().StringVarP(&flagGroupName, "groupname", "n", "", "Username of group to delete")
|
|
// bind config file values to group delete flags
|
|
viper.BindPFlag("group.base_ou", groupDeleteCmd.Flags().Lookup("base_ou"))
|
|
viper.BindPFlag("group.id_attr", groupDeleteCmd.Flags().Lookup("id_attribute"))
|
|
}
|
|
|
|
// define group delete subcommand
|
|
var groupDeleteCmd = &cobra.Command{
|
|
Use: "delete",
|
|
Short: "Delete an LDAP group from the directory",
|
|
Long: `Delete an LDAP group resource from the directory.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
// create new LDAP connection
|
|
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer l.Close()
|
|
|
|
// bind as the admin group
|
|
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// set up group DN
|
|
groupDn := fmt.Sprintf("%s=%s,%s", viper.GetString("group.id_attr"), flagGroupName, viper.GetString("group.base_ou"))
|
|
|
|
// create a new delete request object
|
|
deleteRequest := ldap.NewDelRequest(groupDn, []ldap.Control{})
|
|
|
|
// perform the delete operation
|
|
err = l.Del(deleteRequest)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
},
|
|
}
|