Skip to content

Commit bd9b7fd

Browse files
better use of user object, increased polling delays
1 parent f8338c1 commit bd9b7fd

File tree

5 files changed

+61
-31
lines changed

5 files changed

+61
-31
lines changed

cx1client.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -254,22 +254,22 @@ func (c *Cx1Client) InitializeClient() {
254254
}
255255

256256
c.consts.MigrationPollingMaxSeconds = 300 // 5 min
257-
c.consts.MigrationPollingDelaySeconds = 15
257+
c.consts.MigrationPollingDelaySeconds = 30
258258

259259
c.consts.AuditEnginePollingMaxSeconds = 300 // 5 min
260-
c.consts.AuditEnginePollingDelaySeconds = 15
260+
c.consts.AuditEnginePollingDelaySeconds = 30
261261

262262
c.consts.AuditScanPollingMaxSeconds = 600 // 10 min
263-
c.consts.AuditScanPollingDelaySeconds = 15
263+
c.consts.AuditScanPollingDelaySeconds = 30
264264

265265
c.consts.AuditLanguagePollingMaxSeconds = 300 // 5 min
266-
c.consts.AuditLanguagePollingDelaySeconds = 15
266+
c.consts.AuditLanguagePollingDelaySeconds = 30
267267

268268
c.consts.AuditCompilePollingMaxSeconds = 600 // 10 min
269-
c.consts.AuditCompilePollingDelaySeconds = 15
269+
c.consts.AuditCompilePollingDelaySeconds = 30
270270

271271
c.consts.ScanPollingMaxSeconds = 0
272-
c.consts.ScanPollingDelaySeconds = 15
272+
c.consts.ScanPollingDelaySeconds = 30
273273

274274
c.consts.ProjectApplicationLinkPollingDelaySeconds = 5
275275
c.consts.ProjectApplicationLinkPollingMaxSeconds = 300 // 5 min

scans.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (c Cx1Client) GetScanSummariesByID(scanIDs []string) ([]ScanSummary, error)
200200

201201
if len(ScansSummaries.ScanSum) == 0 {
202202
c.logger.Tracef("Failed to parse data, 0-len ScanSum.\n%v", string(data))
203-
return []ScanSummary{}, fmt.Errorf("Failed to parse data")
203+
return []ScanSummary{}, fmt.Errorf("failed to parse data")
204204
}
205205

206206
return ScansSummaries.ScanSum, nil

types.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,16 @@ type Status struct {
537537
}
538538

539539
type User struct {
540-
Enabled bool `json:"enabled"`
541-
UserID string `json:"id,omitempty"`
542-
FirstName string `json:"firstName"`
543-
LastName string `json:"lastName"`
544-
UserName string `json:"username"`
545-
Email string `json:"email"`
546-
Groups []Group `json:"-"` // only returned from regular /auth/realms/../user endpoint, as string IDs
547-
Roles []Role `json:"-"` // only returned from regular /auth/realms/../user endpoint, as string IDs
540+
Enabled bool `json:"enabled"`
541+
UserID string `json:"id,omitempty"`
542+
FirstName string `json:"firstName"`
543+
LastName string `json:"lastName"`
544+
UserName string `json:"username"`
545+
Email string `json:"email"`
546+
Groups []Group `json:"-"` // only returned from /users/{id}/groups. Use GetUserGroups to fill.
547+
FilledGroups bool `json:"-"` // indicates if the user object has had the Groups array filled.
548+
Roles []Role `json:"-"` // only returned from /users/{id}/role-mappings. Use GetUserRoles to fill.
549+
FilledRoles bool `json:"-"` // indicates if the user object has had the Roles array filled.
548550
}
549551

550552
type WhoAmI struct {

user.go

+30-14
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,60 @@ func (u WhoAmI) String() string {
1212
return fmt.Sprintf("[%v] %v", ShortenGUID(u.UserID), u.Name)
1313
}
1414

15-
func (u User) HasRole(role *Role) bool {
15+
func (u User) HasRole(role *Role) (bool, error) {
1616
return u.HasRoleByID(role.RoleID)
1717
}
18-
func (u User) HasRoleByID(roleID string) bool {
18+
func (u User) HasRoleByID(roleID string) (bool, error) {
19+
if !u.FilledRoles {
20+
return false, fmt.Errorf("user roles have not been retrieved, use GetUserRoles(user)")
21+
}
22+
1923
for _, r := range u.Roles {
2024
if r.RoleID == roleID {
21-
return true
25+
return true, nil
2226
}
2327
}
24-
return false
28+
return false, nil
2529
}
26-
func (u User) HasRoleByName(role string) bool {
30+
func (u User) HasRoleByName(role string) (bool, error) {
31+
if !u.FilledRoles {
32+
return false, fmt.Errorf("user roles have not been retrieved, use GetUserRoles(user)")
33+
}
34+
2735
for _, r := range u.Roles {
2836
if r.Name == role {
29-
return true
37+
return true, nil
3038
}
3139
}
32-
return false
40+
return false, nil
3341
}
3442

35-
func (u User) IsInGroup(group *Group) bool {
43+
func (u User) IsInGroup(group *Group) (bool, error) {
3644
return u.IsInGroupByID(group.GroupID)
3745
}
38-
func (u User) IsInGroupByID(groupId string) bool {
46+
func (u User) IsInGroupByID(groupId string) (bool, error) {
47+
if !u.FilledGroups {
48+
return false, fmt.Errorf("user groups have not been retrieved, use GetUserGroups(user)")
49+
}
50+
3951
for _, g := range u.Groups {
4052
if g.GroupID == groupId {
41-
return true
53+
return true, nil
4254
}
4355
}
44-
return false
56+
return false, nil
4557
}
46-
func (u User) IsInGroupByName(groupName string) bool {
58+
func (u User) IsInGroupByName(groupName string) (bool, error) {
59+
if !u.FilledGroups {
60+
return false, fmt.Errorf("user groups have not been retrieved, use GetUserGroups(user)")
61+
}
62+
4763
for _, g := range u.Groups {
4864
if g.Name == groupName {
49-
return true
65+
return true, nil
5066
}
5167
}
52-
return false
68+
return false, nil
5369
}
5470

5571
func (u *User) AddGroup(client *Cx1Client, group *Group) error {

users.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func (c Cx1Client) GetUserGroups(user *User) ([]Group, error) {
189189
}
190190

191191
user.Groups = usergroups
192+
user.FilledGroups = true
192193

193194
return user.Groups, nil
194195
}
@@ -199,7 +200,12 @@ func (c Cx1Client) AssignUserToGroup(user *User, groupId string) error {
199200
}
200201

201202
func (c Cx1Client) AssignUserToGroupByID(user *User, groupId string) error {
202-
if !user.IsInGroupByID(groupId) {
203+
inGroup, err := user.IsInGroupByID(groupId)
204+
if err != nil {
205+
return err
206+
}
207+
208+
if !inGroup {
203209
params := map[string]string{
204210
"realm": c.tenant,
205211
"userId": user.UserID,
@@ -235,7 +241,12 @@ func (c Cx1Client) RemoveUserFromGroup(user *User, groupId string) error {
235241
}
236242

237243
func (c Cx1Client) RemoveUserFromGroupByID(user *User, groupId string) error {
238-
if user.IsInGroupByID(groupId) {
244+
inGroup, err := user.IsInGroupByID(groupId)
245+
if err != nil {
246+
return err
247+
}
248+
249+
if !inGroup {
239250
params := map[string]string{
240251
"realm": c.tenant,
241252
"userId": user.UserID,
@@ -282,6 +293,7 @@ func (c Cx1Client) GetUserRoles(user *User) ([]Role, error) {
282293
}
283294

284295
user.Roles = append(appRoles, iamRoles...)
296+
user.FilledRoles = true
285297

286298
return user.Roles, nil
287299
}

0 commit comments

Comments
 (0)