Added delete command and delete user subcommand
This commit is contained in:
parent
80b6182900
commit
4e8d5bb4e1
72
cmd/delete.go
Normal file
72
cmd/delete.go
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
ldap "gopkg.in/ldap.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// user subcommand arguments
|
||||||
|
flagUsername string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// define delete user subcommand flags
|
||||||
|
deleteUserCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your user entries are stored")
|
||||||
|
deleteUserCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
|
||||||
|
deleteUserCmd.Flags().StringVarP(&flagUsername, "username", "u", "", "Username of user to delete")
|
||||||
|
// bind config file values to user flags
|
||||||
|
viper.BindPFlag("user.base_ou", deleteUserCmd.Flags().Lookup("base_ou"))
|
||||||
|
viper.BindPFlag("user.uid_attr", deleteUserCmd.Flags().Lookup("uid_attribute"))
|
||||||
|
|
||||||
|
// register delete command and subcommands
|
||||||
|
deleteCmd.AddCommand(deleteUserCmd)
|
||||||
|
rootCmd.AddCommand(deleteCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// define delete command
|
||||||
|
var deleteCmd = &cobra.Command{
|
||||||
|
Use: "delete",
|
||||||
|
Short: "delete an LDAP resource from the directory",
|
||||||
|
Long: `Delete an LDAP resource from the directory.`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("Metaunix.net LDAP tool, delete command. Available subcommands are: user")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// define delete user subcommand
|
||||||
|
var deleteUserCmd = &cobra.Command{
|
||||||
|
Use: "user",
|
||||||
|
Short: "delete an LDAP user resource from the directory",
|
||||||
|
Long: `Delete an LDAP user 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 user
|
||||||
|
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// set up user DN
|
||||||
|
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUsername, viper.GetString("user.base_ou"))
|
||||||
|
|
||||||
|
// create a new delete request object
|
||||||
|
deleteRequest := ldap.NewDelRequest(userDn, []ldap.Control{})
|
||||||
|
|
||||||
|
// perform the delete operation
|
||||||
|
err = l.Del(deleteRequest)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user