From b13a228b0451098ea32933a36fe64566e366583d Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Wed, 18 Apr 2018 10:18:07 +0100 Subject: MM-10121: CLI command to reset permissions system to default state. (#8637) * MM-10121: CLI command to reset permissions system to default state. * Review comment. --- cmd/commands/permissions.go | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 cmd/commands/permissions.go (limited to 'cmd/commands') diff --git a/cmd/commands/permissions.go b/cmd/commands/permissions.go new file mode 100644 index 000000000..33c255a31 --- /dev/null +++ b/cmd/commands/permissions.go @@ -0,0 +1,66 @@ +// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package commands + +import ( + "errors" + "fmt" + + "github.com/spf13/cobra" + + "github.com/mattermost/mattermost-server/cmd" +) + +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, +} + +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, + ) + cmd.RootCmd.AddCommand(PermissionsCmd) +} + +func resetPermissionsCmdF(command *cobra.Command, args []string) error { + a, err := cmd.InitDBCommandContextCobra(command) + if err != nil { + return err + } + + confirmFlag, _ := command.Flags().GetBool("confirm") + if !confirmFlag { + var confirm string + cmd.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.") + } + cmd.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()) + } + + cmd.CommandPrettyPrintln("Permissions system successfully reset") + + return nil +} -- cgit v1.2.3-1-g7c22