github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/cmd/hkserver/commands/group.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 "fmt" 8 9 "github.com/pkg/errors" 10 "github.com/spf13/cobra" 11 12 "github.com/masterhung0112/hk_server/v5/audit" 13 "github.com/masterhung0112/hk_server/v5/model" 14 ) 15 16 var GroupCmd = &cobra.Command{ 17 Use: "group", 18 Short: "Management of groups", 19 } 20 21 var ChannelGroupCmd = &cobra.Command{ 22 Use: "channel", 23 Short: "Management of channel groups", 24 } 25 26 var ChannelGroupEnableCmd = &cobra.Command{ 27 Use: "enable [team]:[channel]", 28 Short: "Enables group constraint on the specified channel", 29 Example: " group channel enable myteam:mychannel", 30 Args: cobra.ExactArgs(1), 31 RunE: channelGroupEnableCmdF, 32 } 33 34 var ChannelGroupDisableCmd = &cobra.Command{ 35 Use: "disable [team]:[channel]", 36 Short: "Disables group constraint on the specified channel", 37 Example: " group channel disable myteam:mychannel", 38 Args: cobra.ExactArgs(1), 39 RunE: channelGroupDisableCmdF, 40 } 41 42 var ChannelGroupStatusCmd = &cobra.Command{ 43 Use: "status [team]:[channel]", 44 Short: "Shows the group constraint status of the specified channel", 45 Example: " group channel status myteam:mychannel", 46 Args: cobra.ExactArgs(1), 47 RunE: channelGroupStatusCmdF, 48 } 49 50 var ChannelGroupListCmd = &cobra.Command{ 51 Use: "list [team]:[channel]", 52 Short: "List channel groups", 53 Long: "Lists the groups associated with a channel", 54 Example: " group channel list myteam:mychannel", 55 Args: cobra.ExactArgs(1), 56 RunE: channelGroupListCmdF, 57 } 58 59 var TeamGroupCmd = &cobra.Command{ 60 Use: "team", 61 Short: "Management of team groups", 62 } 63 64 var TeamGroupEnableCmd = &cobra.Command{ 65 Use: "enable [team]", 66 Short: "Enables group constraint on the specified team", 67 Example: " group team enable myteam", 68 Args: cobra.ExactArgs(1), 69 RunE: teamGroupEnableCmdF, 70 } 71 72 var TeamGroupDisableCmd = &cobra.Command{ 73 Use: "disable [team]", 74 Short: "Disables group constraint on the specified team", 75 Example: " group team disable myteam", 76 Args: cobra.ExactArgs(1), 77 RunE: teamGroupDisableCmdF, 78 } 79 80 var TeamGroupStatusCmd = &cobra.Command{ 81 Use: "status [team]", 82 Short: "Shows the group constraint status of the specified team", 83 Example: " group team status myteam", 84 Args: cobra.ExactArgs(1), 85 RunE: teamGroupStatusCmdF, 86 } 87 88 var TeamGroupListCmd = &cobra.Command{ 89 Use: "list [team]", 90 Short: "List team groups", 91 Long: "Lists the groups associated with a team", 92 Example: " group team list myteam", 93 Args: cobra.ExactArgs(1), 94 RunE: teamGroupListCmdF, 95 } 96 97 func init() { 98 ChannelGroupCmd.AddCommand( 99 ChannelGroupEnableCmd, 100 ChannelGroupDisableCmd, 101 ChannelGroupStatusCmd, 102 ChannelGroupListCmd, 103 ) 104 105 TeamGroupCmd.AddCommand( 106 TeamGroupEnableCmd, 107 TeamGroupDisableCmd, 108 TeamGroupStatusCmd, 109 TeamGroupListCmd, 110 ) 111 112 GroupCmd.AddCommand( 113 ChannelGroupCmd, 114 TeamGroupCmd, 115 ) 116 117 RootCmd.AddCommand(GroupCmd) 118 } 119 120 func channelGroupEnableCmdF(command *cobra.Command, args []string) error { 121 a, err := InitDBCommandContextCobra(command) 122 if err != nil { 123 return err 124 } 125 defer a.Srv().Shutdown() 126 127 channel := getChannelFromChannelArg(a, args[0]) 128 if channel == nil { 129 return errors.New("Unable to find channel '" + args[0] + "'") 130 } 131 132 if channel.Type != model.CHANNEL_PRIVATE { 133 return errors.New("Channel '" + args[0] + "' is not private. It cannot be group-constrained") 134 } 135 136 groups, _, appErr := a.GetGroupsByChannel(channel.Id, model.GroupSearchOpts{}) 137 if appErr != nil { 138 return appErr 139 } 140 141 if len(groups) == 0 { 142 return errors.New("Channel '" + args[0] + "' has no groups associated. It cannot be group-constrained") 143 } 144 145 channel.GroupConstrained = model.NewBool(true) 146 if _, appErr = a.UpdateChannel(channel); appErr != nil { 147 return appErr 148 } 149 150 auditRec := a.MakeAuditRecord("channelGroupEnable", audit.Success) 151 auditRec.AddMeta("channel", channel) 152 a.LogAuditRec(auditRec, nil) 153 154 return nil 155 } 156 157 func channelGroupDisableCmdF(command *cobra.Command, args []string) error { 158 a, err := InitDBCommandContextCobra(command) 159 if err != nil { 160 return err 161 } 162 defer a.Srv().Shutdown() 163 164 channel := getChannelFromChannelArg(a, args[0]) 165 if channel == nil { 166 return errors.New("Unable to find channel '" + args[0] + "'") 167 } 168 169 channel.GroupConstrained = model.NewBool(false) 170 if _, appErr := a.UpdateChannel(channel); appErr != nil { 171 return appErr 172 } 173 174 auditRec := a.MakeAuditRecord("channelGroupDisable", audit.Success) 175 auditRec.AddMeta("channel", channel) 176 a.LogAuditRec(auditRec, nil) 177 178 return nil 179 } 180 181 func channelGroupStatusCmdF(command *cobra.Command, args []string) error { 182 a, err := InitDBCommandContextCobra(command) 183 if err != nil { 184 return err 185 } 186 defer a.Srv().Shutdown() 187 188 channel := getChannelFromChannelArg(a, args[0]) 189 if channel == nil { 190 return errors.New("Unable to find channel '" + args[0] + "'") 191 } 192 193 if channel.IsGroupConstrained() { 194 fmt.Println("Enabled") 195 } else { 196 fmt.Println("Disabled") 197 } 198 199 return nil 200 } 201 202 func channelGroupListCmdF(command *cobra.Command, args []string) error { 203 a, err := InitDBCommandContextCobra(command) 204 if err != nil { 205 return err 206 } 207 defer a.Srv().Shutdown() 208 209 channel := getChannelFromChannelArg(a, args[0]) 210 if channel == nil { 211 return errors.New("Unable to find channel '" + args[0] + "'") 212 } 213 214 groups, _, appErr := a.GetGroupsByChannel(channel.Id, model.GroupSearchOpts{}) 215 if appErr != nil { 216 return appErr 217 } 218 219 for _, group := range groups { 220 fmt.Println(group.DisplayName) 221 } 222 223 return nil 224 } 225 226 func teamGroupEnableCmdF(command *cobra.Command, args []string) error { 227 a, err := InitDBCommandContextCobra(command) 228 if err != nil { 229 return err 230 } 231 defer a.Srv().Shutdown() 232 233 team := getTeamFromTeamArg(a, args[0]) 234 if team == nil { 235 return errors.New("Unable to find team '" + args[0] + "'") 236 } 237 238 groups, _, appErr := a.GetGroupsByTeam(team.Id, model.GroupSearchOpts{}) 239 if appErr != nil { 240 return appErr 241 } 242 243 if len(groups) == 0 { 244 return errors.New("Team '" + args[0] + "' has no groups associated. It cannot be group-constrained") 245 } 246 247 team.GroupConstrained = model.NewBool(true) 248 if _, appErr = a.UpdateTeam(team); appErr != nil { 249 return appErr 250 } 251 252 auditRec := a.MakeAuditRecord("teamGroupEnable", audit.Success) 253 auditRec.AddMeta("team", team) 254 a.LogAuditRec(auditRec, nil) 255 256 return nil 257 } 258 259 func teamGroupDisableCmdF(command *cobra.Command, args []string) error { 260 a, err := InitDBCommandContextCobra(command) 261 if err != nil { 262 return err 263 } 264 defer a.Srv().Shutdown() 265 266 team := getTeamFromTeamArg(a, args[0]) 267 if team == nil { 268 return errors.New("Unable to find team '" + args[0] + "'") 269 } 270 271 team.GroupConstrained = model.NewBool(false) 272 if _, appErr := a.UpdateTeam(team); appErr != nil { 273 return appErr 274 } 275 276 auditRec := a.MakeAuditRecord("teamGroupDisable", audit.Success) 277 auditRec.AddMeta("team", team) 278 a.LogAuditRec(auditRec, nil) 279 280 return nil 281 } 282 283 func teamGroupStatusCmdF(command *cobra.Command, args []string) error { 284 a, err := InitDBCommandContextCobra(command) 285 if err != nil { 286 return err 287 } 288 defer a.Srv().Shutdown() 289 290 team := getTeamFromTeamArg(a, args[0]) 291 if team == nil { 292 return errors.New("Unable to find team '" + args[0] + "'") 293 } 294 295 if team.IsGroupConstrained() { 296 fmt.Println("Enabled") 297 } else { 298 fmt.Println("Disabled") 299 } 300 301 return nil 302 } 303 304 func teamGroupListCmdF(command *cobra.Command, args []string) error { 305 a, err := InitDBCommandContextCobra(command) 306 if err != nil { 307 return err 308 } 309 defer a.Srv().Shutdown() 310 311 team := getTeamFromTeamArg(a, args[0]) 312 if team == nil { 313 return errors.New("Unable to find team '" + args[0] + "'") 314 } 315 316 groups, _, appErr := a.GetGroupsByTeam(team.Id, model.GroupSearchOpts{}) 317 if appErr != nil { 318 return appErr 319 } 320 321 for _, group := range groups { 322 fmt.Println(group.DisplayName) 323 } 324 325 return nil 326 }