diff --git a/cmd/mattermost/commands/command.go b/cmd/mattermost/commands/command.go index bd0bed1fd0..02bdd0c826 100644 --- a/cmd/mattermost/commands/command.go +++ b/cmd/mattermost/commands/command.go @@ -90,6 +90,18 @@ func createCommandCmdF(command *cobra.Command, args []string) error { return errors.New("unable to find team '" + args[0] + "'") } + // get the creator + creator, _ := command.Flags().GetString("creator") + user := getUserFromUserArg(a, creator) + if user == nil { + return errors.New("unable to find user '" + creator + "'") + } + + // check if creator has permission to create slash commands + if !a.HasPermissionToTeam(user.Id, team.Id, model.PERMISSION_MANAGE_SLASH_COMMANDS) { + return errors.New("the creator must be a user who has permissions to manage slash commands") + } + title, _ := command.Flags().GetString("title") description, _ := command.Flags().GetString("description") trigger, _ := command.Flags().GetString("trigger-word") @@ -102,11 +114,6 @@ func createCommandCmdF(command *cobra.Command, args []string) error { } url, _ := command.Flags().GetString("url") - creator, _ := command.Flags().GetString("creator") - user := getUserFromUserArg(a, creator) - if user == nil { - return errors.New("unable to find user '" + creator + "'") - } responseUsername, _ := command.Flags().GetString("response-username") icon, _ := command.Flags().GetString("icon") autocomplete, _ := command.Flags().GetBool("autocomplete") diff --git a/cmd/mattermost/commands/command_test.go b/cmd/mattermost/commands/command_test.go index 73ebacd083..bc6c7304ec 100644 --- a/cmd/mattermost/commands/command_test.go +++ b/cmd/mattermost/commands/command_test.go @@ -19,6 +19,7 @@ func TestCreateCommand(t *testing.T) { th.InitSystemAdmin() defer th.TearDown() team := th.BasicTeam + adminUser := th.TeamAdminUser user := th.BasicUser testCases := []struct { @@ -28,17 +29,17 @@ func TestCreateCommand(t *testing.T) { }{ { "nil error", - []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "", }, { "Team not specified", - []string{"command", "create", "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "Error: requires at least 1 arg(s), only received 0", }, { "Team not found", - []string{"command", "create", "fakeTeam", "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", "fakeTeam", "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "Error: unable to find team", }, { @@ -51,54 +52,59 @@ func TestCreateCommand(t *testing.T) { []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", "fakeuser"}, "unable to find user", }, + { + "Creator not team admin", + []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + "the creator must be a user who has permissions to manage slash commands", + }, { "Command not specified", - []string{"command", "", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "Error: unknown flag: --trigger-word", }, { "Trigger not specified", - []string{"command", "create", team.Name, "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", team.Name, "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, `Error: required flag(s) "trigger-word" not set`, }, { "Blank trigger", - []string{"command", "create", team.Name, "--trigger-word", "", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-word", "", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "Invalid trigger", }, { "Trigger with space", - []string{"command", "create", team.Name, "--trigger-word", "test cmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-word", "test cmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "Error: a trigger word must not contain spaces", }, { "Trigger starting with /", - []string{"command", "create", team.Name, "--trigger-word", "/testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-word", "/testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "Error: a trigger word cannot begin with a /", }, { "URL not specified", - []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--creator", adminUser.Username}, `Error: required flag(s) "url" not set`, }, { "Blank URL", - []string{"command", "create", team.Name, "--trigger-word", "testcmd2", "--url", "", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-word", "testcmd2", "--url", "", "--creator", adminUser.Username}, "Invalid URL", }, { "Invalid URL", - []string{"command", "create", team.Name, "--trigger-word", "testcmd2", "--url", "localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-word", "testcmd2", "--url", "localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "Invalid URL", }, { "Duplicate Command", - []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-word", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "This trigger word is already in use", }, { "Misspelled flag", - []string{"command", "create", team.Name, "--trigger-wor", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", user.Username}, + []string{"command", "create", team.Name, "--trigger-wor", "testcmd", "--url", "http://localhost:8000/my-slash-handler", "--creator", adminUser.Username}, "Error: unknown flag:", }, }