github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/cmd/hkserver/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/masterhung0112/hk_server/v5/audit"
    10  	"github.com/masterhung0112/hk_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  	LdapSyncCmd.Flags().Bool("include-removed-members", false, "Include members who left or were removed from a group-synced team/channel")
    37  	LdapCmd.AddCommand(
    38  		LdapSyncCmd,
    39  		LdapIdMigrate,
    40  	)
    41  	RootCmd.AddCommand(LdapCmd)
    42  }
    43  
    44  func ldapSyncCmdF(command *cobra.Command, args []string) error {
    45  	a, err := InitDBCommandContextCobra(command)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	defer a.Srv().Shutdown()
    50  
    51  	includeRemovedMembers, _ := command.Flags().GetBool("include-removed-members")
    52  	if ldapI := a.Ldap(); ldapI != nil {
    53  		job, err := ldapI.StartSynchronizeJob(true, includeRemovedMembers)
    54  		if err != nil || job.Status == model.JOB_STATUS_ERROR || job.Status == model.JOB_STATUS_CANCELED {
    55  			CommandPrintErrorln("ERROR: AD/LDAP Synchronization please check the server logs")
    56  		} else {
    57  			CommandPrettyPrintln("SUCCESS: AD/LDAP Synchronization Complete")
    58  			auditRec := a.MakeAuditRecord("ldapSync", audit.Success)
    59  			a.LogAuditRec(auditRec, nil)
    60  		}
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func ldapIdMigrateCmdF(command *cobra.Command, args []string) error {
    67  	a, err := InitDBCommandContextCobra(command)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	defer a.Srv().Shutdown()
    72  
    73  	toAttribute := args[0]
    74  	if ldapI := a.Ldap(); ldapI != nil {
    75  		if err := ldapI.MigrateIDAttribute(toAttribute); err != nil {
    76  			CommandPrintErrorln("ERROR: AD/LDAP IdAttribute migration failed! Error: " + err.Error())
    77  		} else {
    78  			CommandPrettyPrintln("SUCCESS: AD/LDAP IdAttribute migration complete. You can now change your IdAttribute to: " + toAttribute)
    79  			auditRec := a.MakeAuditRecord("ldapMigrate", audit.Success)
    80  			a.LogAuditRec(auditRec, nil)
    81  		}
    82  	}
    83  
    84  	return nil
    85  }