Skip to content

Commit

Permalink
Merge pull request #397 from Dray56/role_create_username
Browse files Browse the repository at this point in the history
feat: Add role via username and origin
  • Loading branch information
sneal authored Apr 9, 2024
2 parents d4d9ebf + 3fd26f1 commit b34fc9a
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 6 deletions.
34 changes: 34 additions & 0 deletions client/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ func (c *RoleClient) CreateSpaceRole(ctx context.Context, spaceGUID, userGUID st
return &r, nil
}

// CreateSpaceRoleWithUsername creates a new role for a user in the space via username and origin.
// If origin need not be passed, it must be "".
//
// To create a space role you must be an admin, an organization manager
// in the parent organization of the space associated with the role,
// or a space manager in the space associated with the role.
//
// For a user to be assigned a space role, the user must already
// have an organization role in the parent organization.
func (c *RoleClient) CreateSpaceRoleWithUsername(ctx context.Context, spaceGUID string, userName string, roleType resource.SpaceRoleType, origin string) (*resource.Role, error) {
req := resource.NewRoleSpaceCreateWithUserName(spaceGUID, userName, roleType, origin)
var r resource.Role
_, err := c.client.post(ctx, "/v3/roles", req, &r)
if err != nil {
return nil, err
}
return &r, nil
}

// CreateOrganizationRole creates a new role for a user in the organization
//
// To create an organization role you must be an admin or organization
Expand All @@ -81,6 +100,21 @@ func (c *RoleClient) CreateOrganizationRole(ctx context.Context, organizationGUI
return &r, nil
}

// CreateOrganizationRoleWithUsername creates a new role for a user in the organization via username and origin.
// If origin need not be passed, it must be "".
//
// To create an organization role you must be an admin or organization
// manager in the organization associated with the role.
func (c *RoleClient) CreateOrganizationRoleWithUsername(ctx context.Context, organizationGUID string, userName string, roleType resource.OrganizationRoleType, origin string) (*resource.Role, error) {
req := resource.NewRoleOrganizationCreateWithUserName(organizationGUID, userName, roleType, origin)
var r resource.Role
_, err := c.client.post(ctx, "/v3/roles", req, &r)
if err != nil {
return nil, err
}
return &r, nil
}

// Delete the specified role asynchronously and return a jobGUID
func (c *RoleClient) Delete(ctx context.Context, guid string) (string, error) {
return c.client.delete(ctx, path.Format("/v3/roles/%s", guid))
Expand Down
59 changes: 59 additions & 0 deletions client/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,65 @@ func TestRoles(t *testing.T) {
"0c03442d-c5ae-4661-a929-68f0eeb9ed9a", resource.SpaceRoleDeveloper)
},
},
{
Description: "Create organization role with username",
Route: testutil.MockRoute{
Method: "POST",
Endpoint: "/v3/roles",
Output: g.Single(role),
Status: http.StatusCreated,
PostForm: `{
"type": "organization_user",
"relationships": {
"user": {
"data": {
"username": "test@gmail.com",
"origin" : "id.dbs"
}
},
"organization": {
"data": {
"guid": "ea77cd9e-a072-41e8-9d0b-b2e9180c50bf"
}
}
}
}`,
},
Expected: role,
Action: func(c *Client, t *testing.T) (any, error) {
return c.Roles.CreateOrganizationRoleWithUsername(context.Background(), "ea77cd9e-a072-41e8-9d0b-b2e9180c50bf",
"test@gmail.com", resource.OrganizationRoleUser, "id.dbs")
},
},
{
Description: "Create space role with username",
Route: testutil.MockRoute{
Method: "POST",
Endpoint: "/v3/roles",
Output: g.Single(role),
Status: http.StatusCreated,
PostForm: `{
"type": "space_manager",
"relationships": {
"user": {
"data": {
"username": "test@gmail.com"
}
},
"space": {
"data": {
"guid": "c0c8988d-2f97-4768-832a-677557f18174"
}
}
}
}`,
},
Expected: role,
Action: func(c *Client, t *testing.T) (any, error) {
return c.Roles.CreateSpaceRoleWithUsername(context.Background(), "c0c8988d-2f97-4768-832a-677557f18174",
"test@gmail.com", resource.SpaceRoleManager, "")
},
},
{
Description: "Delete role",
Route: testutil.MockRoute{
Expand Down
66 changes: 60 additions & 6 deletions resource/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type RoleOrganizationCreate struct {

type RoleSpaceUserRelationships struct {
Space ToOneRelationship `json:"space"`
User ToOneRelationship `json:"user"`
User RoleUserData `json:"user"`
}

type RoleOrganizationUserRelationships struct {
Org ToOneRelationship `json:"organization"`
User ToOneRelationship `json:"user"`
User RoleUserData `json:"user"`
}

type RoleSpaceUserOrganizationRelationships struct {
Expand All @@ -39,6 +39,16 @@ type RoleSpaceUserOrganizationRelationships struct {
Org ToOneRelationship `json:"organization"`
}

type RoleUserData struct {
Data UserData `json:"data"`
}

type UserData struct {
UserName string `json:"username,omitempty"`
Origin string `json:"origin,omitempty"`
GUID string `json:"guid,omitempty"`
}

type RoleWithIncluded struct {
Role
Included *RoleIncluded `json:"included"`
Expand Down Expand Up @@ -134,15 +144,37 @@ func NewRoleSpaceCreate(spaceGUID, userGUID string, roleType SpaceRoleType) *Rol
GUID: spaceGUID,
},
},
User: ToOneRelationship{
Data: &Relationship{
User: RoleUserData{
Data: UserData{
GUID: userGUID,
},
},
},
}
}

func NewRoleSpaceCreateWithUserName(spaceGUID, userName string, roleType SpaceRoleType, origin string) *RoleSpaceCreate {
role := &RoleSpaceCreate{
RoleType: roleType.String(),
Relationships: RoleSpaceUserRelationships{
Space: ToOneRelationship{
Data: &Relationship{
GUID: spaceGUID,
},
},
User: RoleUserData{
Data: UserData{
UserName: userName,
},
},
},
}
if origin != "" {
role.Relationships.User.Data.Origin = origin
}
return role
}

func NewRoleOrganizationCreate(orgGUID, userGUID string, roleType OrganizationRoleType) *RoleOrganizationCreate {
return &RoleOrganizationCreate{
RoleType: roleType.String(),
Expand All @@ -152,11 +184,33 @@ func NewRoleOrganizationCreate(orgGUID, userGUID string, roleType OrganizationRo
GUID: orgGUID,
},
},
User: ToOneRelationship{
Data: &Relationship{
User: RoleUserData{
Data: UserData{
GUID: userGUID,
},
},
},
}
}

func NewRoleOrganizationCreateWithUserName(orgGUID, userName string, roleType OrganizationRoleType, origin string) *RoleOrganizationCreate {
role := &RoleOrganizationCreate{
RoleType: roleType.String(),
Relationships: RoleOrganizationUserRelationships{
Org: ToOneRelationship{
Data: &Relationship{
GUID: orgGUID,
},
},
User: RoleUserData{
Data: UserData{
UserName: userName,
},
},
},
}
if origin != "" {
role.Relationships.User.Data.Origin = origin
}
return role
}

0 comments on commit b34fc9a

Please sign in to comment.