github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/cmd/mattermost/commands/permissions.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/mattermost/mattermost-server/utils"
    14  )
    15  
    16  var PermissionsCmd = &cobra.Command{
    17  	Use:   "permissions",
    18  	Short: "Management of the Permissions system",
    19  }
    20  
    21  var ResetPermissionsCmd = &cobra.Command{
    22  	Use:     "reset",
    23  	Short:   "Reset the permissions system to its default state",
    24  	Long:    "Reset the permissions system to its default state",
    25  	Example: "  permissions reset",
    26  	RunE:    resetPermissionsCmdF,
    27  }
    28  
    29  var ExportPermissionsCmd = &cobra.Command{
    30  	Use:     "export",
    31  	Short:   "Export permissions data",
    32  	Long:    "Export Roles and Schemes to JSONL for use by Mattermost permissions import.",
    33  	Example: " permissions export > export.jsonl",
    34  	RunE:    exportPermissionsCmdF,
    35  	PreRun: func(cmd *cobra.Command, args []string) {
    36  		os.Setenv("MM_LOGSETTINGS_CONSOLELEVEL", "error")
    37  	},
    38  }
    39  
    40  var ImportPermissionsCmd = &cobra.Command{
    41  	Use:     "import [file]",
    42  	Short:   "Import permissions data",
    43  	Long:    "Import Roles and Schemes JSONL data as created by the Mattermost permissions export.",
    44  	Example: " permissions import export.jsonl",
    45  	RunE:    importPermissionsCmdF,
    46  }
    47  
    48  func init() {
    49  	ResetPermissionsCmd.Flags().Bool("confirm", false, "Confirm you really want to reset the permissions system and a database backup has been performed.")
    50  
    51  	PermissionsCmd.AddCommand(
    52  		ResetPermissionsCmd,
    53  		ExportPermissionsCmd,
    54  		ImportPermissionsCmd,
    55  	)
    56  	RootCmd.AddCommand(PermissionsCmd)
    57  }
    58  
    59  func resetPermissionsCmdF(command *cobra.Command, args []string) error {
    60  	a, err := InitDBCommandContextCobra(command)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	confirmFlag, _ := command.Flags().GetBool("confirm")
    66  	if !confirmFlag {
    67  		var confirm string
    68  		CommandPrettyPrintln("Have you performed a database backup? (YES/NO): ")
    69  		fmt.Scanln(&confirm)
    70  
    71  		if confirm != "YES" {
    72  			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
    73  		}
    74  		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): ")
    75  		fmt.Scanln(&confirm)
    76  		if confirm != "YES" {
    77  			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
    78  		}
    79  	}
    80  
    81  	if err := a.ResetPermissionsSystem(); err != nil {
    82  		return errors.New(err.Error())
    83  	}
    84  
    85  	CommandPrettyPrintln("Permissions system successfully reset.")
    86  	CommandPrettyPrintln("Changes will take effect gradually as the server caches expire.")
    87  	CommandPrettyPrintln("For the changes to take effect immediately, go to the Mattermost System Console > General > Configuration and click \"Purge All Caches\".")
    88  
    89  	return nil
    90  }
    91  
    92  func exportPermissionsCmdF(command *cobra.Command, args []string) error {
    93  	a, err := InitDBCommandContextCobra(command)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	defer a.Shutdown()
    98  
    99  	if license := a.License(); license == nil {
   100  		return errors.New(utils.T("cli.license.critical"))
   101  	}
   102  
   103  	if err = a.ExportPermissions(os.Stdout); err != nil {
   104  		return errors.New(err.Error())
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  func importPermissionsCmdF(command *cobra.Command, args []string) error {
   111  	a, err := InitDBCommandContextCobra(command)
   112  	if err != nil {
   113  		return err
   114  	}
   115  	defer a.Shutdown()
   116  
   117  	if license := a.License(); license == nil {
   118  		return errors.New(utils.T("cli.license.critical"))
   119  	}
   120  
   121  	file, err := os.Open(args[0])
   122  	if err != nil {
   123  		return err
   124  	}
   125  	defer file.Close()
   126  
   127  	if err := a.ImportPermissions(file); err != nil {
   128  		return err
   129  	}
   130  
   131  	return nil
   132  }