-
Notifications
You must be signed in to change notification settings - Fork 1
/
tx.go
903 lines (738 loc) · 25.6 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
package cli
import (
"fmt"
"strings"
"time"
"github.com/cosmos/cosmos-sdk/x/authz"
subspacesauthz "github.com/gridiron-zone/huddle/x/subspaces/authz"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
authzcli "github.com/cosmos/cosmos-sdk/x/authz/client/cli"
"github.com/spf13/cobra"
"github.com/gridiron-zone/huddle/x/subspaces/types"
)
// DONTCOVER
const (
FlagName = "name"
FlagDescription = "description"
FlagParent = "parent"
FlagSection = "section"
FlagTreasury = "treasury"
FlagOwner = "owner"
FlagPermissions = "permissions"
FlagInitialMembers = "initial-members"
)
// NewTxCmd returns a new command to perform subspaces transactions
func NewTxCmd() *cobra.Command {
subspacesTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Subspaces transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
subspacesTxCmd.AddCommand(
GetCmdCreateSubspace(),
GetCmdEditSubspace(),
GetCmdDeleteSubspace(),
NewSectionsTxCmd(),
NewGroupsTxCmd(),
GetCmdSetUserPermissions(),
GetCmdGrantAuthorization(),
)
return subspacesTxCmd
}
// GetCmdCreateSubspace returns the command used to create a subspace
func GetCmdCreateSubspace() *cobra.Command {
cmd := &cobra.Command{
Use: "create [name]",
Args: cobra.ExactArgs(1),
Short: "Create a new subspace",
Long: `Create a new subspace.
The name must be a human readable name.`,
Example: fmt.Sprintf(`
%s tx subspaces create "Huddle" \
--description "The official subspace of Huddle" \
--treasury huddle1jqk5p244yl4ktukq5xhavvlfzl8z4we4qfmuyh \
--owner huddle1p8r4guvdze03md4g9zclhh6mr8ljvtd80pehr3 \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
name := args[0]
description, err := cmd.Flags().GetString(FlagDescription)
if err != nil {
return err
}
treasury, err := cmd.Flags().GetString(FlagTreasury)
if err != nil {
return err
}
owner, err := cmd.Flags().GetString(FlagOwner)
if err != nil {
return err
}
msg := types.NewMsgCreateSubspace(name, description, treasury, owner, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagDescription, "", "Description of the subspace")
cmd.Flags().String(FlagTreasury, "", "Treasury of the subspace")
cmd.Flags().String(FlagOwner, "", "Owner of the subspace")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdEditSubspace returns the command to edit a subspace
func GetCmdEditSubspace() *cobra.Command {
cmd := &cobra.Command{
Use: "edit [subspace-id]",
Args: cobra.ExactArgs(1),
Short: "Edit the subspace with the given id",
Example: fmt.Sprintf(`
%s tx subspaces edit 1 \
--name "Huddle - Democratizing social networks"
--description "The official subspace of Huddle" \
--treasury huddle1jqk5p244yl4ktukq5xhavvlfzl8z4we4qfmuyh \
--owner huddle1p8r4guvdze03md4g9zclhh6mr8ljvtd80pehr3 \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
name, err := cmd.Flags().GetString(FlagName)
if err != nil {
return err
}
description, err := cmd.Flags().GetString(FlagDescription)
if err != nil {
return err
}
treasury, err := cmd.Flags().GetString(FlagTreasury)
if err != nil {
return err
}
owner, err := cmd.Flags().GetString(FlagOwner)
if err != nil {
return err
}
msg := types.NewMsgEditSubspace(subspaceID, name, description, treasury, owner, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagName, types.DoNotModify, "New human readable name of the subspace")
cmd.Flags().String(FlagDescription, types.DoNotModify, "Description of the subspace")
cmd.Flags().String(FlagTreasury, types.DoNotModify, "Treasury of the subspace")
cmd.Flags().String(FlagOwner, types.DoNotModify, "Owner of the subspace")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdDeleteSubspace returns the command to delete a subspace
func GetCmdDeleteSubspace() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [subspace-id]",
Args: cobra.ExactArgs(1),
Short: "Deletes the subspace with the given id",
Example: fmt.Sprintf(`%s tx subspaces delete 1 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
msg := types.NewMsgDeleteSubspace(subspaceID, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// -------------------------------------------------------------------------------------------------------------------
// NewSectionsTxCmd returns a new command to perform subspaces sections transactions
func NewSectionsTxCmd() *cobra.Command {
groupsTxCmd := &cobra.Command{
Use: "sections",
Short: "Subspace sections transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
groupsTxCmd.AddCommand(
GetCmdCreateSection(),
GetCmdEditSection(),
GetCmdMoveSection(),
GetCmdDeleteSection(),
)
return groupsTxCmd
}
// GetCmdCreateSection returns the command used to create a subspace section
func GetCmdCreateSection() *cobra.Command {
cmd := &cobra.Command{
Use: "create [subspace-id] [name]",
Args: cobra.ExactArgs(2),
Short: "Create a new section within a subspace",
Long: `Create a new section within the subspace with the provided id.
The name must be a human readable name.`,
Example: fmt.Sprintf(`
%s tx subspaces sections create 1 "Custom section" \
--description "This is my custom section" \
--parent 1 \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
name := args[1]
description, err := cmd.Flags().GetString(FlagDescription)
if err != nil {
return err
}
parentID, err := cmd.Flags().GetUint32(FlagParent)
if err != nil {
return err
}
msg := types.NewMsgCreateSection(subspaceID, name, description, parentID, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagDescription, "", "Description of the section")
cmd.Flags().Uint32(FlagParent, 0, "Id of the parent section")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdEditSection returns the command to edit a subspace section
func GetCmdEditSection() *cobra.Command {
cmd := &cobra.Command{
Use: "edit [subspace-id] [section-id]",
Args: cobra.ExactArgs(2),
Short: "Edit the subspace section with the given id",
Example: fmt.Sprintf(`
%s tx subspaces edit 1 1 \
--name "Huddle - Democratizing social networks"
--description "The official subspace of Huddle" \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
sectionID, err := types.ParseSectionID(args[1])
if err != nil {
return err
}
name, err := cmd.Flags().GetString(FlagName)
if err != nil {
return err
}
description, err := cmd.Flags().GetString(FlagDescription)
if err != nil {
return err
}
msg := types.NewMsgEditSection(subspaceID, sectionID, name, description, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagName, types.DoNotModify, "New human readable name of the section")
cmd.Flags().String(FlagDescription, types.DoNotModify, "New description of the section")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdMoveSection returns the command to delete a subspace section
func GetCmdMoveSection() *cobra.Command {
cmd := &cobra.Command{
Use: "move [subspace-id] [section-id] [new-parent-id]",
Args: cobra.ExactArgs(3),
Short: "Move the subspace section with the given id to the new parent",
Example: fmt.Sprintf(`%s tx subspaces delete 1 1 2 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
sectionID, err := types.ParseSectionID(args[1])
if err != nil {
return err
}
newParentID, err := types.ParseSectionID(args[2])
if err != nil {
return err
}
msg := types.NewMsgMoveSection(subspaceID, sectionID, newParentID, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdDeleteSection returns the command to delete a subspace section
func GetCmdDeleteSection() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [subspace-id] [section-id]",
Args: cobra.ExactArgs(2),
Short: "Deletes the subspace section with the given id",
Example: fmt.Sprintf(`%s tx subspaces delete 1 1 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
sectionID, err := types.ParseSectionID(args[1])
if err != nil {
return err
}
msg := types.NewMsgDeleteSection(subspaceID, sectionID, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// -------------------------------------------------------------------------------------------------------------------
// NewGroupsTxCmd returns a new command to perform subspaces groups transactions
func NewGroupsTxCmd() *cobra.Command {
groupsTxCmd := &cobra.Command{
Use: "groups",
Short: "Subspace groups transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
groupsTxCmd.AddCommand(
GetCmdCreateUserGroup(),
GetCmdEditUserGroup(),
GetCmdMoveUserGroup(),
GetCmdSetUserGroupPermissions(),
GetCmdDeleteUserGroup(),
GetCmdAddUserToUserGroup(),
GetCmdRemoveUserFromUserGroup(),
)
return groupsTxCmd
}
// GetCmdCreateUserGroup returns the command to create a user group
func GetCmdCreateUserGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "create [subspace-id] [group-name]",
Args: cobra.ExactArgs(2),
Short: "Create a new user group within a subspace",
Long: fmt.Sprintf(`Create a new user group within the subspace having the provided id.
An optional description of this group can be provided with the %[1]s flag.
The permissions of this group can be set using the %[2]s flag.
If no permissions are set, the default PermissionNothing will be used instead.
Multiple permissions must be specified separating them with a comma (,).`, FlagDescription, FlagPermissions),
Example: fmt.Sprintf(`
%s tx subspaces groups create 1 "Admins" \
--description "Group of the subspace admins" \
--permissions "WRITE,MODERATE_CONTENT,SET_USER_PERMISSIONS" \
--initial-members "cosmos1x5pjlvufs4znnhhkwe8v4tw3kz30f3lxgwza53,cosmos1g4yzh3q3grf804t4y4fuynrvrxtshgxy7j783f" \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
name := args[1]
description, err := cmd.Flags().GetString(FlagDescription)
if err != nil {
return err
}
sectionID, err := cmd.Flags().GetUint32(FlagSection)
if err != nil {
return err
}
flagPermissions, err := cmd.Flags().GetStringSlice(FlagPermissions)
if err != nil {
return err
}
var permissions types.Permissions
for _, arg := range flagPermissions {
permissions = types.CombinePermissions(append(permissions, arg)...)
}
initialMembers, err := cmd.Flags().GetStringSlice(FlagInitialMembers)
if err != nil {
return err
}
msg := types.NewMsgCreateUserGroup(subspaceID, sectionID, name, description, permissions, initialMembers, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().Uint32(FlagSection, 0, "Id of the section inside which to create the group")
cmd.Flags().String(FlagDescription, "", "Description of the group")
cmd.Flags().StringSlice(FlagPermissions, nil, "Permissions of the group")
cmd.Flags().StringSlice(FlagInitialMembers, nil, "Initial members of the group")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdEditUserGroup returns the command to edit a user group
func GetCmdEditUserGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "edit [subspace-id] [group-id]",
Args: cobra.ExactArgs(2),
Short: "Edit the group with the given id",
Example: fmt.Sprintf(`
%s tx subspaces groups edit 1 1 \
--name "Super admins"
--description "This is the group of super users" \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
groupID, err := types.ParseGroupID(args[1])
if err != nil {
return err
}
name, err := cmd.Flags().GetString(FlagName)
if err != nil {
return err
}
description, err := cmd.Flags().GetString(FlagDescription)
if err != nil {
return err
}
msg := types.NewMsgEditUserGroup(subspaceID, groupID, name, description, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(FlagName, types.DoNotModify, "New human readable name of the group")
cmd.Flags().String(FlagDescription, types.DoNotModify, "Description of the group")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdMoveUserGroup returns the command to move a user group to another section
func GetCmdMoveUserGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "move [subspace-id] [group-id] [new-section-id]",
Args: cobra.ExactArgs(3),
Short: "Move a user group to a new section",
Example: fmt.Sprintf(`%s tx subspaces groups move 1 1 2 --from alice`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
groupID, err := types.ParseGroupID(args[1])
if err != nil {
return err
}
newSectionID, err := types.ParseSectionID(args[2])
if err != nil {
return err
}
msg := types.NewMsgMoveUserGroup(subspaceID, groupID, newSectionID, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdSetUserGroupPermissions returns the command to set the permissions for a user group
func GetCmdSetUserGroupPermissions() *cobra.Command {
cmd := &cobra.Command{
Use: "set-permissions [subspace-id] [group-id] [permissions]",
Args: cobra.ExactArgs(3),
Short: "Set the permissions for a specific group",
Long: `Set the permissions for a specific user group a given subspace.
It is mandatory to specify at least one permission to be set.
When specifying multiple permissions, they must be separated by a comma (,).`,
Example: fmt.Sprintf(`
%s tx subspaces groups set-permissions 1 1 "WRITE_CONTENT,INTERACT_WITH_CONTENT,EDIT_OWN_CONTENT,MODERATE_CONTENT,
EDIT_SUBSPACE,DELETE_SUBSPACE,MANAGE_SECTIONS,MANAGE_GROUPS,SET_PERMISSIONS,EVERYTHING" \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
groupID, err := types.ParseGroupID(args[1])
if err != nil {
return err
}
var permissions types.Permissions
for _, arg := range strings.Split(args[2], ",") {
permissions = types.CombinePermissions(append(permissions, arg)...)
}
msg := types.NewMsgSetUserGroupPermissions(subspaceID, groupID, permissions, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdDeleteUserGroup returns the command to delete a user group
func GetCmdDeleteUserGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [subspace-id] [group-id]",
Args: cobra.ExactArgs(2),
Short: "Delete a user group from a subspace",
Example: fmt.Sprintf(`
%s tx subspaces groups delete 1 1 --from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
groupID, err := types.ParseGroupID(args[1])
if err != nil {
return err
}
msg := types.NewMsgDeleteUserGroup(subspaceID, groupID, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdAddUserToUserGroup returns the command to add a user to a user group
func GetCmdAddUserToUserGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "add-user [subspace-id] [group-id] [user]",
Args: cobra.ExactArgs(3),
Short: "Add a user to a user group",
Example: fmt.Sprintf(`
%s tx subspaces groups add-user 1 1 huddle1p8r4guvdze03md4g9zclhh6mr8ljvtd80pehr3 \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
groupID, err := types.ParseGroupID(args[1])
if err != nil {
return err
}
user := args[2]
msg := types.NewMsgAddUserToUserGroup(subspaceID, groupID, user, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdRemoveUserFromUserGroup returns the command to remove a user from a user group
func GetCmdRemoveUserFromUserGroup() *cobra.Command {
cmd := &cobra.Command{
Use: "remove-user [subspace-id] [group-id] [user]",
Args: cobra.ExactArgs(3),
Short: "Remove a user from a user group",
Example: fmt.Sprintf(`
%s tx subspaces groups remove-user 1 1 huddle1p8r4guvdze03md4g9zclhh6mr8ljvtd80pehr3 \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
groupID, err := types.ParseGroupID(args[1])
if err != nil {
return err
}
user := args[2]
msg := types.NewMsgRemoveUserFromUserGroup(subspaceID, groupID, user, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// --------------------------------------------------------------------------------------------------------------------
// GetCmdSetUserPermissions returns the command to set the permissions for a user
func GetCmdSetUserPermissions() *cobra.Command {
cmd := &cobra.Command{
Use: "set-user-permissions [subspace-id] [user] [permissions]",
Args: cobra.ExactArgs(3),
Short: "Set the permissions for a specific user",
Long: `Set the permissions for a specific user inside a given subspace.
It is mandatory to specify at least one permission to be set.
When specifying multiple permissions, they must be separated by a comma (,).`,
Example: fmt.Sprintf(`
%s tx subspaces set-user-permissions 1 \
huddle1463vltcqk6ql6zpk0g6s595jjcrzk4804hyqw7 \
"WRITE_CONTENT,INTERACT_WITH_CONTENT,EDIT_OWN_CONTENT,MODERATE_CONTENT,
EDIT_SUBSPACE,DELETE_SUBSPACE,MANAGE_SECTIONS,MANAGE_GROUPS,SET_PERMISSIONS,EVERYTHING" \
--from alice
`, version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspaceID, err := types.ParseSubspaceID(args[0])
if err != nil {
return err
}
sectionID, err := cmd.Flags().GetUint32(FlagSection)
if err != nil {
return err
}
user := args[1]
var permissions types.Permissions
for _, arg := range strings.Split(args[2], ",") {
permissions = types.CombinePermissions(append(permissions, arg)...)
}
msg := types.NewMsgSetUserPermissions(subspaceID, sectionID, user, permissions, clientCtx.FromAddress.String())
if err = msg.ValidateBasic(); err != nil {
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().Uint32(FlagSection, 0, "Id of the section inside which to set the permissions")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// --------------------------------------------------------------------------------------------------------------------
// GetCmdGrantAuthorization returns the command to grant a subspace authorization
func GetCmdGrantAuthorization() *cobra.Command {
cmd := &cobra.Command{
Use: "grant [subspaces-ids] [grantee]",
Short: "Grant an authorization to an address inside one or more subspaces",
Long: `Grant an authorization to an address inside one or more subspaces.
If you want to grant the same authorization inside multiple subspaces, simply specify the subspaces ids separating them with a comma (,).`,
Example: fmt.Sprintf(`
%s tx subspaces grant 1,2,3 huddle1463vltcqk6ql6zpk0g6s595jjcrzk4804hyqw7 --msg-type=%s --from alice
`, version.AppName, sdk.MsgTypeURL(&types.MsgSetUserPermissions{})),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
subspacesIDs, err := types.ParseSubspacesIDs(args[0])
if err != nil {
return err
}
grantee, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}
msgType, err := cmd.Flags().GetString(authzcli.FlagMsgType)
if err != nil {
return err
}
exp, err := cmd.Flags().GetInt64(authzcli.FlagExpiration)
if err != nil {
return err
}
authorization := subspacesauthz.NewGenericSubspaceAuthorization(subspacesIDs, msgType)
msg, err := authz.NewMsgGrant(clientCtx.GetFromAddress(), grantee, authorization, time.Unix(exp, 0))
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
cmd.Flags().String(authzcli.FlagMsgType, "", "The msg method name for which we are creating the authorization")
cmd.Flags().Int64(authzcli.FlagExpiration, time.Now().AddDate(1, 0, 0).Unix(), "The Unix timestamp. Default is one year.")
flags.AddTxFlagsToCmd(cmd)
return cmd
}