MM-10264: Adds CLI command to import and export permissions. (#8787)

* MM-10264: Adds CLI command to import and export permissions.

* MM-10264: Changes Scheme Name to DisplayName and adds Name slug field.

* MM-10264: Changes display name max size.

* MM-10264: Another merge fix.

* MM-10264: Changes for more Schemes methods checking for migration.

* MM-10264: More updates for Schemes migration checking.
Этот коммит содержится в:
Martin Kraft
2018-05-17 11:37:00 -04:00
коммит произвёл GitHub
родитель 463065c8ba
Коммит e0390632b3
14 изменённых файлов: 670 добавлений и 14 удалений

Просмотреть файл

@@ -6,10 +6,12 @@ package commands
import (
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/mattermost/mattermost-server/cmd"
"github.com/mattermost/mattermost-server/utils"
)
var PermissionsCmd = &cobra.Command{
@@ -25,11 +27,32 @@ var ResetPermissionsCmd = &cobra.Command{
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,
)
cmd.RootCmd.AddCommand(PermissionsCmd)
}
@@ -64,3 +87,45 @@ func resetPermissionsCmdF(command *cobra.Command, args []string) error {
return nil
}
func exportPermissionsCmdF(command *cobra.Command, args []string) error {
a, err := cmd.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 := cmd.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
}

40
cmd/commands/permissions_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,40 @@
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package commands
import (
"os"
"os/exec"
"strings"
"testing"
"github.com/mattermost/mattermost-server/api4"
"github.com/mattermost/mattermost-server/utils"
)
func TestPermissionsExport_rejectsUnlicensed(t *testing.T) {
permissionsLicenseRequiredTest(t, "export")
}
func TestPermissionsImport_rejectsUnlicensed(t *testing.T) {
permissionsLicenseRequiredTest(t, "import")
}
func permissionsLicenseRequiredTest(t *testing.T, subcommand string) {
th := api4.Setup().InitBasic()
defer th.TearDown()
path, err := os.Executable()
if err != nil {
t.Fail()
}
args := []string{"-test.run", "ExecCommand", "--", "--disableconfigwatch", "permissions", subcommand}
output, err := exec.Command(path, args...).CombinedOutput()
actual := string(output)
expected := utils.T("cli.license.critical")
if !strings.Contains(actual, expected) {
t.Errorf("Expected '%v' but got '%v'.", expected, actual)
}
}