Create CLI command "team modify" to modify team's privacy setting. (#10898)

This commit is contained in:
Joshua Bezaleel Abednego
2019-07-17 16:49:12 +07:00
committed by Hanzei
parent dd30488a09
commit c49f755a14
3 changed files with 87 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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")
}
}