-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathmanageuser.go
197 lines (175 loc) · 5.41 KB
/
manageuser.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
// Copyright (c) 2017-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
pd "github.com/decred/politeia/politeiad/api/v1"
cms "github.com/decred/politeia/politeiawww/api/cms/v1"
"github.com/decred/politeia/politeiawww/cmd/shared"
"github.com/google/uuid"
)
// CMSManageUserCmd allows an administrator to update Domain, ContractorType
// and SupervisorID of a given user.
type CMSManageUserCmd struct {
Args struct {
UserID string `positional-arg-name:"userid" required:"true"`
} `positional-args:"true" optional:"true"`
Domain string `long:"domain" optional:"true"`
ContractorType string `long:"contractortype" optional:"true"`
SupervisorUserIDs string `long:"supervisoruserids" optional:"true"`
ProposalsOwned string `long:"proposalsowned" optional:"true"`
}
// Execute executes the cms manage user command.
func (cmd *CMSManageUserCmd) Execute(args []string) error {
domains := map[string]cms.DomainTypeT{
"developer": cms.DomainTypeDeveloper,
"marketing": cms.DomainTypeMarketing,
"research": cms.DomainTypeResearch,
"design": cms.DomainTypeDesign,
"documentation": cms.DomainTypeDocumentation,
}
contractorTypes := map[string]cms.ContractorTypeT{
"direct": cms.ContractorTypeDirect,
"supervisor": cms.ContractorTypeSupervisor,
"contractor": cms.ContractorTypeSubContractor,
"nominee": cms.ContractorTypeNominee,
"revoked": cms.ContractorTypeRevoked,
}
userID := cmd.Args.UserID
// Validate user ID
_, err := uuid.Parse(userID)
if err != nil {
return fmt.Errorf("invalid user ID: %v", err)
}
// Validate domain. The domain can be either the numeric code
// or the human readable equivalent.
var domain cms.DomainTypeT
if cmd.Domain != "" {
d, err := strconv.ParseUint(cmd.Domain, 10, 32)
if err == nil {
// Numeric code found
domain = cms.DomainTypeT(d)
} else if d, ok := domains[cmd.Domain]; ok {
// Human readable domain found
domain = d
} else {
// Invalid domain
return fmt.Errorf("invalid domain; use the command " +
"'cmswww help manageuser' for list of valid domains")
}
}
// Validate contractor type. The contractor type can be either
// a numeric code or the human readable equivalent.
var contractorType cms.ContractorTypeT
if cmd.ContractorType != "" {
ct, err := strconv.ParseUint(cmd.ContractorType, 10, 32)
if err == nil {
// Numeric code found
contractorType = cms.ContractorTypeT(ct)
} else if ct, ok := contractorTypes[cmd.ContractorType]; ok {
// Human readable contractor type found
contractorType = ct
} else {
// Invalid contrator type
return fmt.Errorf("invalid contractor type; use the command " +
"'cmswww help manageuser' for list of valid contractor types")
}
}
// Validate supervisor user IDs
var manageSupervisors cms.CMSManageUserAction
switch cmd.SupervisorUserIDs {
case "":
manageSupervisors.Action = cmd.SupervisorUserIDs
case "reset":
manageSupervisors.Action = cmd.SupervisorUserIDs
default:
ids := strings.Split(cmd.SupervisorUserIDs, ",")
for _, id := range ids {
_, err := uuid.Parse(id)
if err != nil {
return fmt.Errorf("invalid supervisor ID '%v': %v", id, err)
}
}
manageSupervisors.Action = "set"
manageSupervisors.Payload = ids
}
// Validate proposals owned
var manageProposalsOwned cms.CMSManageUserAction
switch cmd.ProposalsOwned {
case "":
manageProposalsOwned.Action = cmd.ProposalsOwned
case "reset":
manageProposalsOwned.Action = cmd.ProposalsOwned
default:
tokens := strings.Split(cmd.ProposalsOwned, ",")
for _, token := range tokens {
b, err := hex.DecodeString(token)
if err != nil || len(b) != pd.TokenSize {
return fmt.Errorf("invalid proposal token '%v': %v", token, err)
}
}
manageProposalsOwned.Action = "set"
manageProposalsOwned.Payload = tokens
}
// Send request
mu := cms.CMSManageUser{
UserID: cmd.Args.UserID,
Domain: domain,
ContractorType: contractorType,
SupervisorUserIDs: manageSupervisors,
ProposalsOwned: manageProposalsOwned,
}
err = shared.PrintJSON(mu)
if err != nil {
return err
}
mur, err := client.CMSManageUser(mu)
if err != nil {
return err
}
err = shared.PrintJSON(mur)
if err != nil {
return err
}
return nil
}
const cmsManageUserHelpMsg = `cmsmanageuser [flags] "userid"
Update the Domain, ContractorType and SupervisorID of the specified user. This
command requires admin privileges.
Numeric codes or their human readable equivalents can be used to specify the
domain and contractory type. See below for the available options.
Arguments:
1. userid (string, required) ID of the user to manage
Flags:
--domain (string, optional) Domain of the contractor
--contractortype (string, optional) Contractor Type
--supervisoruserids (string, optional) Supervisor user IDs (comma separated)
--proposalsowned (string, optional) Proposals owned (comma separated)
Domain types:
1. developer
2. marketing
4. research
5. design
6. documentation
Contractor types:
1. direct
2. supervisor
3. subcontractor
4. nominee
5. revoked
Request:
{
"userid": "c1261442-dc61-4861-9d6c-f104fd0b076b",
"domain": 1,
"contractortype": 1,
"supervisoruserids": [
"f43a040c-585c-4431-a9dd-6dbdde885bb5",
"914de514-f861-41e3-8fcb-e3ebbb26a333"
]
}
Response:
{}`