Added a subcommand to add a user resource

This commit is contained in:
Gregory Ballantine 2018-07-15 00:33:14 -04:00
parent 6ddd46f2ae
commit 80b6182900

View File

@ -20,19 +20,19 @@ var (
) )
func init() { func init() {
// define add command flags
addCmd.PersistentFlags().StringP("bind_dn", "D", "", "Privileged LDAP user to bind as")
addCmd.PersistentFlags().StringP("bind_pw", "P", "", "Privileged LDAP user password")
// bind config file values to add flags
viper.BindPFlag("bind_dn", searchCmd.Flags().Lookup("bind_dn"))
viper.BindPFlag("bind_pw", searchCmd.Flags().Lookup("bind_pw"))
// define add user subcommand flags // define add user subcommand flags
addUserCmd.Flags().StringP("base_ou", "o", "", "LDAP OU to create the new user entry under")
addUserCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
addUserCmd.Flags().StringP("home_directory", "d", "/home/%s", "User's home directory")
addUserCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username for a new user") 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(&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(&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().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") addUserCmd.Flags().IntVarP(&flagUserIdNumber, "id_number", "i", -1, "ID Number for a new user")
// bind config file values to user flags
viper.BindPFlag("user.base_ou", addUserCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("user.uid_attr", addUserCmd.Flags().Lookup("uid_attribute"))
viper.BindPFlag("user.home_directory", addUserCmd.Flags().Lookup("home_directory"))
// register add command and subcommands // register add command and subcommands
addCmd.AddCommand(addUserCmd) addCmd.AddCommand(addUserCmd)
@ -45,7 +45,7 @@ var addCmd = &cobra.Command{
Short: "Add an LDAP resource to the directory", Short: "Add an LDAP resource to the directory",
Long: `Create and add an LDAP resource to your directory.`, Long: `Create and add an LDAP resource to your directory.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("This is a test") fmt.Println("Metaunix.net LDAP tool, add command. Available subcommands are: user")
}, },
} }
@ -69,15 +69,15 @@ var addUserCmd = &cobra.Command{
} }
// set up user attributes // set up user attributes
userDn := fmt.Sprintf("uid=%s,ou=Accounts,dc=example,dc=com", flagUserUsername) userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
userDisplayName := fmt.Sprintf("%s %s", flagUserFirstName, flagUserLastName) userDisplayName := fmt.Sprintf("%s %s", flagUserFirstName, flagUserLastName)
userHome := fmt.Sprintf("/home/%s", flagUserUsername) userHome := fmt.Sprintf(viper.GetString("user.home_directory"), flagUserUsername)
// create a new add request object // create a new add request object
addRequest := ldap.NewAddRequest(userDn) addRequest := ldap.NewAddRequest(userDn)
// add user attributes to the request // add user attributes to the request
addRequest.Attribute("uid", []string{flagUserUsername}) addRequest.Attribute(viper.GetString("user.uid_attr"), []string{flagUserUsername})
addRequest.Attribute("objectClass", []string{"top", "inetOrgPerson", "posixAccount", "shadowAccount"}) addRequest.Attribute("objectClass", viper.GetStringSlice("user.object_class"))
addRequest.Attribute("mail", []string{flagUserEmail}) addRequest.Attribute("mail", []string{flagUserEmail})
addRequest.Attribute("givenName", []string{flagUserFirstName}) addRequest.Attribute("givenName", []string{flagUserFirstName})
addRequest.Attribute("sn", []string{flagUserLastName}) addRequest.Attribute("sn", []string{flagUserLastName})
@ -86,8 +86,10 @@ var addUserCmd = &cobra.Command{
addRequest.Attribute("uidNumber", []string{strconv.Itoa(flagUserIdNumber)}) addRequest.Attribute("uidNumber", []string{strconv.Itoa(flagUserIdNumber)})
addRequest.Attribute("gidNumber", []string{strconv.Itoa(flagUserIdNumber)}) addRequest.Attribute("gidNumber", []string{strconv.Itoa(flagUserIdNumber)})
addRequest.Attribute("homeDirectory", []string{userHome}) addRequest.Attribute("homeDirectory", []string{userHome})
addRequest.Attribute("loginShell", []string{"/bin/bash"}) // loop through extra attributes
addRequest.Attribute("shadowExpire", []string{"-1"}) for key, value := range viper.GetStringMapString("user.extra_attributes") {
addRequest.Attribute(key, []string{value})
}
// perform the add operation // perform the add operation
err = l.Add(addRequest) err = l.Add(addRequest)