From c49f755a1472d04e3b34d16218050812fe9ecf60 Mon Sep 17 00:00:00 2001 From: Joshua Bezaleel Abednego Date: Wed, 17 Jul 2019 16:49:12 +0700 Subject: [PATCH] Create CLI command "team modify" to modify team's privacy setting. (#10898) --- app/team.go | 18 +++++++++++ cmd/mattermost/commands/team.go | 47 ++++++++++++++++++++++++++++ cmd/mattermost/commands/team_test.go | 22 +++++++++++++ 3 files changed, 87 insertions(+) diff --git a/app/team.go b/app/team.go index fa96d5d814..0e2fbd107c 100644 --- a/app/team.go +++ b/app/team.go @@ -183,6 +183,24 @@ func (a *App) UpdateTeamScheme(team *model.Team) (*model.Team, *model.AppError) return oldTeam, nil } +func (a *App) UpdateTeamPrivacy(teamId string, teamType string, allowOpenInvite bool) *model.AppError { + oldTeam, err := a.GetTeam(teamId) + if err != nil { + return err + } + + oldTeam.Type = teamType + oldTeam.AllowOpenInvite = allowOpenInvite + + if oldTeam, err = a.Srv.Store.Team().Update(oldTeam); err != nil { + return err + } + + a.sendTeamEvent(oldTeam, model.WEBSOCKET_EVENT_UPDATE_TEAM) + + return nil +} + func (a *App) PatchTeam(teamId string, patch *model.TeamPatch) (*model.Team, *model.AppError) { team, err := a.GetTeam(teamId) if err != nil { diff --git a/cmd/mattermost/commands/team.go b/cmd/mattermost/commands/team.go index 0fbeaada6a..e5a8f72e7c 100644 --- a/cmd/mattermost/commands/team.go +++ b/cmd/mattermost/commands/team.go @@ -100,6 +100,15 @@ var TeamRenameCmd = &cobra.Command{ RunE: renameTeamCmdF, } +var ModifyTeamCmd = &cobra.Command{ + Use: "modify [team] [flag]", + Short: "Modify a team's privacy setting to public or private", + Long: `Modify a team's privacy setting to public or private.`, + Example: " team modify myteam --private", + Args: cobra.ExactArgs(1), + RunE: modifyTeamCmdF, +} + func init() { TeamCreateCmd.Flags().String("name", "", "Team Name") TeamCreateCmd.Flags().String("display_name", "", "Team Display Name") @@ -110,6 +119,9 @@ func init() { TeamRenameCmd.Flags().String("display_name", "", "Team Display Name") + ModifyTeamCmd.Flags().Bool("private", false, "Convert the team to a private team") + ModifyTeamCmd.Flags().Bool("public", false, "Convert the team to a public team") + TeamCmd.AddCommand( TeamCreateCmd, RemoveUsersCmd, @@ -120,6 +132,7 @@ func init() { ArchiveTeamCmd, RestoreTeamsCmd, TeamRenameCmd, + ModifyTeamCmd, ) RootCmd.AddCommand(TeamCmd) } @@ -410,3 +423,37 @@ func renameTeamCmdF(command *cobra.Command, args []string) error { return nil } + +func modifyTeamCmdF(command *cobra.Command, args []string) error { + a, err := InitDBCommandContextCobra(command) + if err != nil { + return err + } + defer a.Shutdown() + + team := getTeamFromTeamArg(a, args[0]) + if team == nil { + return errors.New("Unable to find team '" + args[0] + "'") + } + + public, _ := command.Flags().GetBool("public") + private, _ := command.Flags().GetBool("private") + + if public == private { + return errors.New("You must specify only one of --public or --private") + } + + if public { + team.Type = model.TEAM_OPEN + team.AllowOpenInvite = true + } else if private { + team.Type = model.TEAM_INVITE + team.AllowOpenInvite = false + } + + if err := a.UpdateTeamPrivacy(team.Id, team.Type, team.AllowOpenInvite); err != nil { + return errors.New("Failed to update privacy for team" + args[0]) + } + + return nil +} diff --git a/cmd/mattermost/commands/team_test.go b/cmd/mattermost/commands/team_test.go index 1afe8da244..3aae12ee44 100644 --- a/cmd/mattermost/commands/team_test.go +++ b/cmd/mattermost/commands/team_test.go @@ -271,3 +271,25 @@ func TestRenameTeam(t *testing.T) { } } + +func TestModifyTeam(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + team := th.CreateTeam() + + th.CheckCommand(t, "team", "modify", team.Name, "--private") + + updatedTeam, _ := th.App.GetTeam(team.Id) + + if !updatedTeam.AllowOpenInvite && team.Type == model.TEAM_INVITE { + t.Fatal("Failed modifying team's privacy to private") + } + + th.CheckCommand(t, "team", "modify", team.Name, "--public") + + if updatedTeam.AllowOpenInvite && team.Type == model.TEAM_OPEN { + t.Fatal("Failed modifying team's privacy to private") + } + +}