Added add command w/ user subcommand

This commit is contained in:
Gregory Ballantine 2018-07-14 22:18:32 -04:00
parent 034d8b1525
commit 9e8bd1ae76

101
cmd/add.go Normal file
View File

@ -0,0 +1,101 @@
package cmd
import (
"fmt"
"log"
"strconv"
"github.com/spf13/cobra"
ldap "gopkg.in/ldap.v2"
)
var (
// add command global arguments
flagBindDn string
flagBindPw string
// user subcommand arguments
flagUserUsername string
flagUserEmail string
flagUserFirstName string
flagUserLastName string
flagUserIdNumber int
)
func init() {
// define add command flags
addCmd.PersistentFlags().StringVarP(&flagBindDn, "bind_dn", "D", "", "Privileged LDAP user to bind as")
addCmd.PersistentFlags().StringVarP(&flagBindPw, "bind_pw", "P", "", "Privileged LDAP user password")
// mark required flags
addCmd.MarkFlagRequired("bind_dn")
addCmd.MarkFlagRequired("bind_pw")
// define add user subcommand flags
addUserCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username for a new user")
addUserCmd.Flags().StringVarP(&flagUserEmail, "email", "e", "", "Email address for a new user")
addUserCmd.Flags().StringVarP(&flagUserFirstName, "first_name", "f", "", "First name of a new user")
addUserCmd.Flags().StringVarP(&flagUserLastName, "last_name", "l", "", "Last name of a new user")
addUserCmd.Flags().IntVarP(&flagUserIdNumber, "id_number", "i", -1, "ID Number for a new user")
// register add command and subcommands
addCmd.AddCommand(addUserCmd)
rootCmd.AddCommand(addCmd)
}
// define add command
var addCmd = &cobra.Command{
Use: "add",
Short: "Add an LDAP resource to the directory",
Long: `Create and add an LDAP resource to your directory.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("This is a test")
},
}
// define add user subcommand
var addUserCmd = &cobra.Command{
Use: "user",
Short: "Add an LDAP user resource to the directory",
Long: `Create and add an LDAP user resource to your directory.`,
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", flagHost, flagPort))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// bind as the admin user
err = l.Bind(flagBindDn, flagBindPw)
if err != nil {
log.Fatal(err)
}
// set up user attributes
userDn := fmt.Sprintf("uid=%s,ou=Accounts,dc=example,dc=com", flagUserUsername)
userDisplayName := fmt.Sprintf("%s %s", flagUserFirstName, flagUserLastName)
userHome := fmt.Sprintf("/home/%s", flagUserUsername)
// create a new add request object
addRequest := ldap.NewAddRequest(userDn)
// add user attributes to the request
addRequest.Attribute("uid", []string{flagUserUsername})
addRequest.Attribute("objectClass", []string{"top", "inetOrgPerson", "posixAccount", "shadowAccount"})
addRequest.Attribute("mail", []string{flagUserEmail})
addRequest.Attribute("givenName", []string{flagUserFirstName})
addRequest.Attribute("sn", []string{flagUserLastName})
addRequest.Attribute("cn", []string{flagUserFirstName})
addRequest.Attribute("displayName", []string{userDisplayName})
addRequest.Attribute("uidNumber", []string{strconv.Itoa(flagUserIdNumber)})
addRequest.Attribute("gidNumber", []string{strconv.Itoa(flagUserIdNumber)})
addRequest.Attribute("homeDirectory", []string{userHome})
addRequest.Attribute("loginShell", []string{"/bin/bash"})
addRequest.Attribute("shadowExpire", []string{"-1"})
// perform the add operation
err = l.Add(addRequest)
if err != nil {
log.Fatal(err)
}
},
}