-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupAddGateKeeper.go
76 lines (60 loc) · 2.14 KB
/
groupAddGateKeeper.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
package cmd
import (
"fmt"
"github.com/inpher/sb/internal/commands"
"github.com/inpher/sb/internal/helpers"
"github.com/inpher/sb/internal/models"
)
// GroupAddGateKeeper describes the command
type GroupAddGateKeeper struct{}
func init() {
commands.RegisterCommand("group gate-keeper add", func() (c commands.Command, r models.Right, helper helpers.Helper, args map[string]commands.Argument) {
return new(GroupAddGateKeeper), models.GroupOwner, helpers.Helper{
Header: "add an account as a group gate keeper",
Usage: "group gate-keeper add --account USERNAME --group GROUP",
Description: "add an account as a group gate keeper",
Aliases: []string{"groupAddGateKeeper"},
}, map[string]commands.Argument{
"account": {
Required: true,
Description: "The username of the account",
},
"group": {
Required: true,
Description: "The group to which attach of the account",
},
}
})
}
// Checks checks whether or not the user can execute this method
func (c *GroupAddGateKeeper) Checks(ct *commands.Context) error {
// Check if the user exists
user, err := models.LoadUser(ct.FormattedArguments["account"])
if err != nil {
return fmt.Errorf("account %s doesn't exist", ct.FormattedArguments["account"])
}
if user.IsGateKeeperOfGroup(ct.FormattedArguments["group"]) {
return fmt.Errorf("account %s is already a group gate keeper", ct.FormattedArguments["account"])
}
return nil
}
// Execute executes the command
func (c *GroupAddGateKeeper) Execute(ct *commands.Context) (repl models.ReplicationData, cmdError error, err error) {
repl = models.ReplicationData{
"group": ct.FormattedArguments["group"],
"account": ct.FormattedArguments["account"],
}
err = c.Replicate(repl)
return
}
func (c *GroupAddGateKeeper) PostExecute(repl models.ReplicationData) (err error) {
return
}
func (c *GroupAddGateKeeper) Replicate(repl models.ReplicationData) (err error) {
err = helpers.AddAccountInGroup(repl["group"], repl["account"], "gk")
if err != nil {
return
}
fmt.Printf("Account %s was successfully added as a gate keeper of group %s\n", repl["account"], repl["group"])
return
}