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