github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/cmd/mattermost/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/mattermost/mattermost-server/v5/audit" 14 "github.com/mattermost/mattermost-server/v5/utils" 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 66 confirmFlag, _ := command.Flags().GetBool("confirm") 67 if !confirmFlag { 68 var confirm string 69 CommandPrettyPrintln("Have you performed a database backup? (YES/NO): ") 70 fmt.Scanln(&confirm) 71 72 if confirm != "YES" { 73 return errors.New("ABORTED: You did not answer YES exactly, in all capitals.") 74 } 75 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): ") 76 fmt.Scanln(&confirm) 77 if confirm != "YES" { 78 return errors.New("ABORTED: You did not answer YES exactly, in all capitals.") 79 } 80 } 81 82 if err := a.ResetPermissionsSystem(); err != nil { 83 return errors.New(err.Error()) 84 } 85 86 CommandPrettyPrintln("Permissions system successfully reset.") 87 CommandPrettyPrintln("Changes will take effect gradually as the server caches expire.") 88 CommandPrettyPrintln("For the changes to take effect immediately, go to the Mattermost System Console > General > Configuration and click \"Purge All Caches\".") 89 90 auditRec := a.MakeAuditRecord("resetPermissions", audit.Success) 91 a.LogAuditRec(auditRec, nil) 92 93 return nil 94 } 95 96 func exportPermissionsCmdF(command *cobra.Command, args []string) error { 97 a, err := InitDBCommandContextCobra(command) 98 if err != nil { 99 return err 100 } 101 defer a.Srv().Shutdown() 102 103 if license := a.Srv().License(); license == nil { 104 return errors.New(utils.T("cli.license.critical")) 105 } 106 107 if err = a.ExportPermissions(os.Stdout); err != nil { 108 return errors.New(err.Error()) 109 } 110 111 auditRec := a.MakeAuditRecord("exportPermissions", audit.Success) 112 a.LogAuditRec(auditRec, nil) 113 114 return nil 115 } 116 117 func importPermissionsCmdF(command *cobra.Command, args []string) error { 118 a, err := InitDBCommandContextCobra(command) 119 if err != nil { 120 return err 121 } 122 defer a.Srv().Shutdown() 123 124 if license := a.Srv().License(); license == nil { 125 return errors.New(utils.T("cli.license.critical")) 126 } 127 128 file, err := os.Open(args[0]) 129 if err != nil { 130 return err 131 } 132 defer file.Close() 133 134 auditRec := a.MakeAuditRecord("importPermissions", audit.Success) 135 auditRec.AddMeta("file", args[0]) 136 a.LogAuditRec(auditRec, nil) 137 138 return a.ImportPermissions(file) 139 }