github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/cmd/mattermost/commands/ldap.go (about)

     1  // Copyright (c) 2015-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/v5/audit"
     8  	"github.com/mattermost/mattermost-server/v5/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  	RootCmd.AddCommand(LdapCmd)
    40  }
    41  
    42  func ldapSyncCmdF(command *cobra.Command, args []string) error {
    43  	a, err := InitDBCommandContextCobra(command)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	defer a.Srv().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  			CommandPrintErrorln("ERROR: AD/LDAP Synchronization please check the server logs")
    53  		} else {
    54  			CommandPrettyPrintln("SUCCESS: AD/LDAP Synchronization Complete")
    55  			auditRec := a.MakeAuditRecord("ldapSync", audit.Success)
    56  			a.LogAuditRec(auditRec, nil)
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func ldapIdMigrateCmdF(command *cobra.Command, args []string) error {
    64  	a, err := InitDBCommandContextCobra(command)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	defer a.Srv().Shutdown()
    69  
    70  	toAttribute := args[0]
    71  	if ldapI := a.Ldap(); ldapI != nil {
    72  		if err := ldapI.MigrateIDAttribute(toAttribute); err != nil {
    73  			CommandPrintErrorln("ERROR: AD/LDAP IdAttribute migration failed! Error: " + err.Error())
    74  		} else {
    75  			CommandPrettyPrintln("SUCCESS: AD/LDAP IdAttribute migration complete. You can now change your IdAttribute to: " + toAttribute)
    76  			auditRec := a.MakeAuditRecord("ldapMigrate", audit.Success)
    77  			a.LogAuditRec(auditRec, nil)
    78  		}
    79  	}
    80  
    81  	return nil
    82  }