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