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