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