Skip to content

Commit

Permalink
User and group endpoints should return info about direct membership (#25
Browse files Browse the repository at this point in the history
)

* user and group endpoints return info about direct membership, not just all enumerated membership

* move authenticated user to named fields

* remove more anonymous struct fields
  • Loading branch information
jacobsee authored Aug 3, 2023
1 parent 38c4eab commit b35d2fe
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 10 deletions.
4 changes: 3 additions & 1 deletion pkg/api/v1alpha1/application_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pkg/api/v1alpha1/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 40 additions & 5 deletions pkg/api/v1alpha1/authenticated_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,7 +74,12 @@ func (r *Router) getAuthenticatedUser(c *gin.Context) {

if ctxUser.R == nil {
c.JSON(http.StatusOK, AuthenticatedUser{
User: &User{ctxUser, []string{}, []string{}},
User: &User{
User: ctxUser,
Memberships: []string{},
MembershipsDirect: []string{},
MembershipRequests: []string{},
},
Admin: *ctxAdmin,
})

Expand All @@ -87,8 +93,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))
Expand All @@ -97,7 +110,12 @@ func (r *Router) getAuthenticatedUser(c *gin.Context) {
}

c.JSON(http.StatusOK, AuthenticatedUser{
User: &User{ctxUser, memberships, requests},
User: &User{
User: ctxUser,
Memberships: memberships,
MembershipsDirect: membershipsDirect,
MembershipRequests: requests,
},
Admin: *ctxAdmin,
})
}
Expand All @@ -112,6 +130,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())
Expand All @@ -125,6 +145,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(
Expand Down Expand Up @@ -152,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)})
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)
Expand Down Expand Up @@ -300,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))
Expand All @@ -326,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{
Expand Down
17 changes: 16 additions & 1 deletion pkg/api/v1alpha1/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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))
Expand All @@ -128,7 +136,14 @@ 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: group,
Members: members,
MembersDirect: membersDirect,
MembershipRequests: requests,
Organizations: organizations,
Applications: applications,
})
}

func createGroupRequestValidator(group *models.Group) (string, error) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/v1alpha1/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion pkg/api/v1alpha1/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -135,16 +136,28 @@ 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))
for i, r := range user.R.GroupMembershipRequests {
requests[i] = r.GroupID
}

c.JSON(http.StatusOK, User{user, memberships, requests})
c.JSON(http.StatusOK, User{
User: user,
Memberships: memberships,
MembershipsDirect: membershipsDirect,
MembershipRequests: requests,
})
}

// createUser creates a user in the database
Expand Down

0 comments on commit b35d2fe

Please sign in to comment.