From 6239080096e2952b47afd657ca7daa1d96955dc2 Mon Sep 17 00:00:00 2001 From: Jacob See <5027680+jacobsee@users.noreply.github.com> Date: Wed, 2 Aug 2023 15:00:19 -0700 Subject: [PATCH 1/3] user and group endpoints return info about direct membership, not just all enumerated membership --- pkg/api/v1alpha1/authenticated_user.go | 20 +++++++++++++++++--- pkg/api/v1alpha1/groups.go | 10 +++++++++- pkg/api/v1alpha1/users.go | 10 +++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/pkg/api/v1alpha1/authenticated_user.go b/pkg/api/v1alpha1/authenticated_user.go index 58736ec..65a02ee 100644 --- a/pkg/api/v1alpha1/authenticated_user.go +++ b/pkg/api/v1alpha1/authenticated_user.go @@ -35,6 +35,7 @@ type AuthenticatedUserGroup struct { Organizations models.OrganizationSlice `json:"organizations"` Applications models.ApplicationSlice `json:"applications"` Admin bool `json:"admin"` + Direct bool `json:"direct"` } // AuthenticatedUserRequests is a list of application and member requests for the authenticated user @@ -73,7 +74,7 @@ func (r *Router) getAuthenticatedUser(c *gin.Context) { if ctxUser.R == nil { c.JSON(http.StatusOK, AuthenticatedUser{ - User: &User{ctxUser, []string{}, []string{}}, + User: &User{ctxUser, []string{}, []string{}, []string{}}, Admin: *ctxAdmin, }) @@ -87,8 +88,15 @@ func (r *Router) getAuthenticatedUser(c *gin.Context) { } memberships := make([]string, len(enumeratedMemberships)) + + membershipsDirect := make([]string, 0) + for i, m := range enumeratedMemberships { memberships[i] = m.GroupID + + if m.Direct { + membershipsDirect = append(membershipsDirect, m.GroupID) + } } requests := make([]string, len(ctxUser.R.GroupMembershipRequests)) @@ -97,7 +105,7 @@ func (r *Router) getAuthenticatedUser(c *gin.Context) { } c.JSON(http.StatusOK, AuthenticatedUser{ - User: &User{ctxUser, memberships, requests}, + User: &User{ctxUser, memberships, membershipsDirect, requests}, Admin: *ctxAdmin, }) } @@ -112,6 +120,8 @@ func (r *Router) getAuthenticatedUserGroups(c *gin.Context) { var userAdminGroups []string + var userDirectGroups []string + enumeratedMemberships, err := dbtools.GetMembershipsForUser(c, r.DB.DB, ctxUser.ID, false) if err != nil { sendError(c, http.StatusInternalServerError, "error enumerating group membership: "+err.Error()) @@ -125,6 +135,10 @@ func (r *Router) getAuthenticatedUserGroups(c *gin.Context) { if g.IsAdmin { userAdminGroups = append(userAdminGroups, g.GroupID) } + + if g.Direct { + userDirectGroups = append(userDirectGroups, g.GroupID) + } } groups, err := models.Groups( @@ -152,7 +166,7 @@ func (r *Router) getAuthenticatedUserGroups(c *gin.Context) { apps = append(apps, a.R.Application) } - userGroups = append(userGroups, AuthenticatedUserGroup{g, orgs, apps, contains(userAdminGroups, g.ID)}) + userGroups = append(userGroups, AuthenticatedUserGroup{g, orgs, apps, contains(userAdminGroups, g.ID), contains(userDirectGroups, g.ID)}) } c.JSON(http.StatusOK, userGroups) diff --git a/pkg/api/v1alpha1/groups.go b/pkg/api/v1alpha1/groups.go index daa82cc..3ac0c2f 100644 --- a/pkg/api/v1alpha1/groups.go +++ b/pkg/api/v1alpha1/groups.go @@ -22,6 +22,7 @@ import ( type Group struct { *models.Group Members []string `json:"members,omitempty"` + MembersDirect []string `json:"members_direct,omitempty"` MembershipRequests []string `json:"membership_requests,omitempty"` Organizations []string `json:"organizations"` Applications []string `json:"applications"` @@ -109,8 +110,15 @@ func (r *Router) getGroup(c *gin.Context) { } members := make([]string, len(enumeratedMembers)) + + membersDirect := make([]string, 0) + for i, m := range enumeratedMembers { members[i] = m.UserID + + if m.Direct { + membersDirect = append(membersDirect, m.UserID) + } } requests := make([]string, len(group.R.GroupMembershipRequests)) @@ -128,7 +136,7 @@ func (r *Router) getGroup(c *gin.Context) { applications[i] = o.R.Application.ID } - c.JSON(http.StatusOK, Group{group, members, requests, organizations, applications}) + c.JSON(http.StatusOK, Group{group, members, membersDirect, requests, organizations, applications}) } func createGroupRequestValidator(group *models.Group) (string, error) { diff --git a/pkg/api/v1alpha1/users.go b/pkg/api/v1alpha1/users.go index 1ed41f2..79ed5d1 100644 --- a/pkg/api/v1alpha1/users.go +++ b/pkg/api/v1alpha1/users.go @@ -35,6 +35,7 @@ var permittedListUsersParams = []string{"external_id", "email"} type User struct { *models.User Memberships []string `json:"memberships,omitempty"` + MembershipsDirect []string `json:"memberships_direct,omitempty"` MembershipRequests []string `json:"membership_requests,omitempty"` } @@ -135,8 +136,15 @@ func (r *Router) getUser(c *gin.Context) { } memberships := make([]string, len(enumeratedMemberships)) + + membershipsDirect := make([]string, 0) + for i, m := range enumeratedMemberships { memberships[i] = m.GroupID + + if m.Direct { + membershipsDirect = append(membershipsDirect, m.GroupID) + } } requests := make([]string, len(user.R.GroupMembershipRequests)) @@ -144,7 +152,7 @@ func (r *Router) getUser(c *gin.Context) { requests[i] = r.GroupID } - c.JSON(http.StatusOK, User{user, memberships, requests}) + c.JSON(http.StatusOK, User{user, memberships, membershipsDirect, requests}) } // createUser creates a user in the database From 10417e4005380f8b880779b4d0e80b6ba2a814bd Mon Sep 17 00:00:00 2001 From: Jacob See <5027680+jacobsee@users.noreply.github.com> Date: Thu, 3 Aug 2023 09:16:20 -0700 Subject: [PATCH 2/3] move authenticated user to named fields --- pkg/api/v1alpha1/authenticated_user.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/api/v1alpha1/authenticated_user.go b/pkg/api/v1alpha1/authenticated_user.go index 65a02ee..c57f638 100644 --- a/pkg/api/v1alpha1/authenticated_user.go +++ b/pkg/api/v1alpha1/authenticated_user.go @@ -74,7 +74,12 @@ func (r *Router) getAuthenticatedUser(c *gin.Context) { if ctxUser.R == nil { c.JSON(http.StatusOK, AuthenticatedUser{ - User: &User{ctxUser, []string{}, []string{}, []string{}}, + User: &User{ + User: ctxUser, + Memberships: []string{}, + MembershipsDirect: []string{}, + MembershipRequests: []string{}, + }, Admin: *ctxAdmin, }) From 7d9001d2e4c1639075a36c263aaa24a04c5c1557 Mon Sep 17 00:00:00 2001 From: Jacob See <5027680+jacobsee@users.noreply.github.com> Date: Thu, 3 Aug 2023 13:59:03 -0700 Subject: [PATCH 3/3] remove more anonymous struct fields --- pkg/api/v1alpha1/application_types.go | 4 +++- pkg/api/v1alpha1/applications.go | 5 ++++- pkg/api/v1alpha1/authenticated_user.go | 24 ++++++++++++++++++++---- pkg/api/v1alpha1/groups.go | 9 ++++++++- pkg/api/v1alpha1/organizations.go | 4 +++- pkg/api/v1alpha1/users.go | 7 ++++++- 6 files changed, 44 insertions(+), 9 deletions(-) diff --git a/pkg/api/v1alpha1/application_types.go b/pkg/api/v1alpha1/application_types.go index 11e3315..609ce74 100644 --- a/pkg/api/v1alpha1/application_types.go +++ b/pkg/api/v1alpha1/application_types.go @@ -147,7 +147,9 @@ func (r *Router) getApplicationType(c *gin.Context) { return } - c.JSON(http.StatusOK, ApplicationType{app}) + c.JSON(http.StatusOK, ApplicationType{ + ApplicationType: app, + }) } // createApplicationType creates an application type in the database diff --git a/pkg/api/v1alpha1/applications.go b/pkg/api/v1alpha1/applications.go index d761514..de8bdbe 100644 --- a/pkg/api/v1alpha1/applications.go +++ b/pkg/api/v1alpha1/applications.go @@ -151,7 +151,10 @@ func (r *Router) getApplication(c *gin.Context) { return } - c.JSON(http.StatusOK, Application{app, app.R.Type}) + c.JSON(http.StatusOK, Application{ + Application: app, + Type: app.R.Type, + }) } // createApplication creates an application in the database diff --git a/pkg/api/v1alpha1/authenticated_user.go b/pkg/api/v1alpha1/authenticated_user.go index c57f638..a91c19c 100644 --- a/pkg/api/v1alpha1/authenticated_user.go +++ b/pkg/api/v1alpha1/authenticated_user.go @@ -110,7 +110,12 @@ func (r *Router) getAuthenticatedUser(c *gin.Context) { } c.JSON(http.StatusOK, AuthenticatedUser{ - User: &User{ctxUser, memberships, membershipsDirect, requests}, + User: &User{ + User: ctxUser, + Memberships: memberships, + MembershipsDirect: membershipsDirect, + MembershipRequests: requests, + }, Admin: *ctxAdmin, }) } @@ -171,7 +176,13 @@ func (r *Router) getAuthenticatedUserGroups(c *gin.Context) { apps = append(apps, a.R.Application) } - userGroups = append(userGroups, AuthenticatedUserGroup{g, orgs, apps, contains(userAdminGroups, g.ID), contains(userDirectGroups, g.ID)}) + userGroups = append(userGroups, AuthenticatedUserGroup{ + Group: g, + Organizations: orgs, + Applications: apps, + Admin: contains(userAdminGroups, g.ID), + Direct: contains(userDirectGroups, g.ID), + }) } c.JSON(http.StatusOK, userGroups) @@ -319,7 +330,10 @@ func (r *Router) getAuthenticatedUserGroupRequests(c *gin.Context) { Note: m.Note, } - memberRequests[i] = AuthenticatedUserGroupMemberRequest{&gmr, false} + memberRequests[i] = AuthenticatedUserGroupMemberRequest{ + GroupMemberRequest: &gmr, + Admin: false, + } } applicationRequests := make([]AuthenticatedUserGroupApplicationRequest, len(ctxUser.R.RequesterUserGroupApplicationRequests)) @@ -345,7 +359,9 @@ func (r *Router) getAuthenticatedUserGroupRequests(c *gin.Context) { UpdatedAt: a.UpdatedAt, } - applicationRequests[i] = AuthenticatedUserGroupApplicationRequest{&gar} + applicationRequests[i] = AuthenticatedUserGroupApplicationRequest{ + GroupApplicationRequest: &gar, + } } c.JSON(http.StatusOK, AuthenticatedUserRequests{ diff --git a/pkg/api/v1alpha1/groups.go b/pkg/api/v1alpha1/groups.go index 3ac0c2f..469efe1 100644 --- a/pkg/api/v1alpha1/groups.go +++ b/pkg/api/v1alpha1/groups.go @@ -136,7 +136,14 @@ func (r *Router) getGroup(c *gin.Context) { applications[i] = o.R.Application.ID } - c.JSON(http.StatusOK, Group{group, members, membersDirect, requests, organizations, applications}) + c.JSON(http.StatusOK, Group{ + Group: group, + Members: members, + MembersDirect: membersDirect, + MembershipRequests: requests, + Organizations: organizations, + Applications: applications, + }) } func createGroupRequestValidator(group *models.Group) (string, error) { diff --git a/pkg/api/v1alpha1/organizations.go b/pkg/api/v1alpha1/organizations.go index 25f4e5d..91f6a39 100644 --- a/pkg/api/v1alpha1/organizations.go +++ b/pkg/api/v1alpha1/organizations.go @@ -142,7 +142,9 @@ func (r *Router) getOrganization(c *gin.Context) { return } - c.JSON(http.StatusOK, Organization{org}) + c.JSON(http.StatusOK, Organization{ + Organization: org, + }) } // createOrganization creates an org in the database diff --git a/pkg/api/v1alpha1/users.go b/pkg/api/v1alpha1/users.go index 79ed5d1..82f57e7 100644 --- a/pkg/api/v1alpha1/users.go +++ b/pkg/api/v1alpha1/users.go @@ -152,7 +152,12 @@ func (r *Router) getUser(c *gin.Context) { requests[i] = r.GroupID } - c.JSON(http.StatusOK, User{user, memberships, membershipsDirect, requests}) + c.JSON(http.StatusOK, User{ + User: user, + Memberships: memberships, + MembershipsDirect: membershipsDirect, + MembershipRequests: requests, + }) } // createUser creates a user in the database