github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/cmd/mattermost/commands/ldap.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"github.com/mattermost/mattermost-server/model"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var LdapCmd = &cobra.Command{
    12  	Use:   "ldap",
    13  	Short: "LDAP related utilities",
    14  }
    15  
    16  var LdapSyncCmd = &cobra.Command{
    17  	Use:     "sync",
    18  	Short:   "Synchronize now",
    19  	Long:    "Synchronize all LDAP users now.",
    20  	Example: "  ldap sync",
    21  	RunE:    ldapSyncCmdF,
    22  }
    23  
    24  var LdapIdMigrate = &cobra.Command{
    25  	Use:     "idmigrate",
    26  	Short:   "Migrate LDAP IdAttribute to new value",
    27  	Long:    "Migrate LDAP IdAttribute to new value. Run this utility then change the IdAttribute to the new value.",
    28  	Example: " ldap idmigrate objectGUID",
    29  	Args:    cobra.ExactArgs(1),
    30  	RunE:    ldapIdMigrateCmdF,
    31  }
    32  
    33  func init() {
    34  	LdapCmd.AddCommand(
    35  		LdapSyncCmd,
    36  		LdapIdMigrate,
    37  	)
    38  	RootCmd.AddCommand(LdapCmd)
    39  }
    40  
    41  func ldapSyncCmdF(command *cobra.Command, args []string) error {
    42  	a, err := InitDBCommandContextCobra(command)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	defer a.Shutdown()
    47  
    48  	if ldapI := a.Ldap; ldapI != nil {
    49  		job, err := ldapI.StartSynchronizeJob(true)
    50  		if err != nil || job.Status == model.JOB_STATUS_ERROR || job.Status == model.JOB_STATUS_CANCELED {
    51  			CommandPrintErrorln("ERROR: AD/LDAP Synchronization please check the server logs")
    52  		} else {
    53  			CommandPrettyPrintln("SUCCESS: AD/LDAP Synchronization Complete")
    54  		}
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func ldapIdMigrateCmdF(command *cobra.Command, args []string) error {
    61  	a, err := InitDBCommandContextCobra(command)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	defer a.Shutdown()
    66  
    67  	toAttribute := args[0]
    68  	if ldapI := a.Ldap; ldapI != nil {
    69  		if err := ldapI.MigrateIDAttribute(toAttribute); err != nil {
    70  			CommandPrintErrorln("ERROR: AD/LDAP IdAttribute migration failed! Error: " + err.Error())
    71  		} else {
    72  			CommandPrettyPrintln("SUCCESS: AD/LDAP IdAttribute migration complete. You can now change your IdAttribute to: " + toAttribute)
    73  		}
    74  	}
    75  
    76  	return nil
    77  }