summaryrefslogtreecommitdiffstats
path: root/cmd/mattermost/commands/permissions.go
blob: 9d9962ce516abd56b0e5b47925d4cda310b9df95 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package commands

import (
	"errors"
	"fmt"
	"os"

	"github.com/spf13/cobra"

	"github.com/mattermost/mattermost-server/utils"
)

var PermissionsCmd = &cobra.Command{
	Use:   "permissions",
	Short: "Management of the Permissions system",
}

var ResetPermissionsCmd = &cobra.Command{
	Use:     "reset",
	Short:   "Reset the permissions system to its default state",
	Long:    "Reset the permissions system to its default state",
	Example: "  permissions reset",
	RunE:    resetPermissionsCmdF,
}

var ExportPermissionsCmd = &cobra.Command{
	Use:     "export",
	Short:   "Export permissions data",
	Long:    "Export Roles and Schemes to JSONL for use by Mattermost permissions import.",
	Example: " permissions export > export.jsonl",
	RunE:    exportPermissionsCmdF,
	PreRun: func(cmd *cobra.Command, args []string) {
		os.Setenv("MM_LOGSETTINGS_CONSOLELEVEL", "error")
	},
}

var ImportPermissionsCmd = &cobra.Command{
	Use:     "import [file]",
	Short:   "Import permissions data",
	Long:    "Import Roles and Schemes JSONL data as created by the Mattermost permissions export.",
	Example: " permissions import export.jsonl",
	RunE:    importPermissionsCmdF,
}

func init() {
	ResetPermissionsCmd.Flags().Bool("confirm", false, "Confirm you really want to reset the permissions system and a database backup has been performed.")

	PermissionsCmd.AddCommand(
		ResetPermissionsCmd,
		ExportPermissionsCmd,
		ImportPermissionsCmd,
	)
	RootCmd.AddCommand(PermissionsCmd)
}

func resetPermissionsCmdF(command *cobra.Command, args []string) error {
	a, err := InitDBCommandContextCobra(command)
	if err != nil {
		return err
	}

	confirmFlag, _ := command.Flags().GetBool("confirm")
	if !confirmFlag {
		var confirm string
		CommandPrettyPrintln("Have you performed a database backup? (YES/NO): ")
		fmt.Scanln(&confirm)

		if confirm != "YES" {
			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
		}
		CommandPrettyPrintln("Are you sure you want to reset the permissions system? All data related to the permissions system will be permanently deleted and all users will revert to having the default permissions. (YES/NO): ")
		fmt.Scanln(&confirm)
		if confirm != "YES" {
			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
		}
	}

	if err := a.ResetPermissionsSystem(); err != nil {
		return errors.New(err.Error())
	}

	CommandPrettyPrintln("Permissions system successfully reset.")
	CommandPrettyPrintln("Changes will take effect gradually as the server caches expire.")
	CommandPrettyPrintln("For the changes to take effect immediately, go to the Mattermost System Console > General > Configuration and click \"Purge All Caches\".")

	return nil
}

func exportPermissionsCmdF(command *cobra.Command, args []string) error {
	a, err := InitDBCommandContextCobra(command)
	if err != nil {
		return err
	}
	defer a.Shutdown()

	if license := a.License(); license == nil {
		return errors.New(utils.T("cli.license.critical"))
	}

	if err = a.ExportPermissions(os.Stdout); err != nil {
		return errors.New(err.Error())
	}

	return nil
}

func importPermissionsCmdF(command *cobra.Command, args []string) error {
	a, err := InitDBCommandContextCobra(command)
	if err != nil {
		return err
	}
	defer a.Shutdown()

	if license := a.License(); license == nil {
		return errors.New(utils.T("cli.license.critical"))
	}

	file, err := os.Open(args[0])
	if err != nil {
		return err
	}
	defer file.Close()

	if err := a.ImportPermissions(file); err != nil {
		return err
	}

	return nil
}