Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de9c2e9

Browse files
committed
fix: don't require organization_id in body when updating a custom role
1 parent a27ac30 commit de9c2e9

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

cli/organizationroles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (r *RootCmd) editOrganizationRole(orgContext *OrganizationContext) *serpent
203203
// Do not actually post
204204
updated = customRole
205205
} else {
206-
updated, err = client.PatchOrganizationRole(ctx, org.ID, customRole)
206+
updated, err = client.PatchOrganizationRole(ctx, customRole)
207207
if err != nil {
208208
return xerrors.Errorf("patch role: %w", err)
209209
}

coderd/roles.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
// roles. Ideally only included in the enterprise package, but the routes are
2121
// intermixed with AGPL endpoints.
2222
type CustomRoleHandler interface {
23-
PatchOrganizationRole(ctx context.Context, rw http.ResponseWriter, r *http.Request, orgID uuid.UUID, role codersdk.Role) (codersdk.Role, bool)
23+
PatchOrganizationRole(ctx context.Context, rw http.ResponseWriter, r *http.Request, orgID uuid.UUID, role codersdk.PatchRoleRequest) (codersdk.Role, bool)
2424
}
2525

2626
type agplCustomRoleHandler struct{}
2727

28-
func (agplCustomRoleHandler) PatchOrganizationRole(ctx context.Context, rw http.ResponseWriter, _ *http.Request, _ uuid.UUID, _ codersdk.Role) (codersdk.Role, bool) {
28+
func (agplCustomRoleHandler) PatchOrganizationRole(ctx context.Context, rw http.ResponseWriter, _ *http.Request, _ uuid.UUID, _ codersdk.PatchRoleRequest) (codersdk.Role, bool) {
2929
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
3030
Message: "Creating and updating custom roles is an Enterprise feature. Contact sales!",
3131
})
@@ -49,7 +49,7 @@ func (api *API) patchOrgRoles(rw http.ResponseWriter, r *http.Request) {
4949
organization = httpmw.OrganizationParam(r)
5050
)
5151

52-
var req codersdk.Role
52+
var req codersdk.PatchRoleRequest
5353
if !httpapi.Read(ctx, rw, r, &req) {
5454
return
5555
}

codersdk/roles.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ type Role struct {
6161
UserPermissions []Permission `json:"user_permissions" table:"user_permissions"`
6262
}
6363

64+
// Role is a longer form of SlimRole used to edit custom roles.
65+
type PatchRoleRequest struct {
66+
Name string `json:"name" table:"name,default_sort" validate:"username"`
67+
DisplayName string `json:"display_name" table:"display_name"`
68+
SitePermissions []Permission `json:"site_permissions" table:"site_permissions"`
69+
// OrganizationPermissions are specific to the organization the role belongs to.
70+
OrganizationPermissions []Permission `json:"organization_permissions" table:"organization_permissions"`
71+
UserPermissions []Permission `json:"user_permissions" table:"user_permissions"`
72+
}
73+
6474
// FullName returns the role name scoped to the organization ID. This is useful if
6575
// printing a set of roles from different scopes, as duplicated names across multiple
6676
// scopes will become unique.
@@ -73,18 +83,26 @@ func (r Role) FullName() string {
7383
}
7484

7585
// PatchOrganizationRole will upsert a custom organization role
76-
func (c *Client) PatchOrganizationRole(ctx context.Context, organizationID uuid.UUID, req Role) (Role, error) {
86+
func (c *Client) PatchOrganizationRole(ctx context.Context, role Role) (Role, error) {
87+
req := PatchRoleRequest{
88+
Name: role.Name,
89+
DisplayName: role.DisplayName,
90+
SitePermissions: role.SitePermissions,
91+
OrganizationPermissions: role.OrganizationPermissions,
92+
UserPermissions: role.UserPermissions,
93+
}
94+
7795
res, err := c.Request(ctx, http.MethodPatch,
78-
fmt.Sprintf("/api/v2/organizations/%s/members/roles", organizationID.String()), req)
96+
fmt.Sprintf("/api/v2/organizations/%s/members/roles", role.OrganizationID), req)
7997
if err != nil {
8098
return Role{}, err
8199
}
82100
defer res.Body.Close()
83101
if res.StatusCode != http.StatusOK {
84102
return Role{}, ReadBodyAsError(res)
85103
}
86-
var role Role
87-
return role, json.NewDecoder(res.Body).Decode(&role)
104+
var r Role
105+
return r, json.NewDecoder(res.Body).Decode(&r)
88106
}
89107

90108
// ListSiteRoles lists all assignable site wide roles.

enterprise/coderd/roles.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type enterpriseCustomRoleHandler struct {
2121
Enabled bool
2222
}
2323

24-
func (h enterpriseCustomRoleHandler) PatchOrganizationRole(ctx context.Context, rw http.ResponseWriter, r *http.Request, orgID uuid.UUID, role codersdk.Role) (codersdk.Role, bool) {
24+
func (h enterpriseCustomRoleHandler) PatchOrganizationRole(ctx context.Context, rw http.ResponseWriter, r *http.Request, orgID uuid.UUID, role codersdk.PatchRoleRequest) (codersdk.Role, bool) {
2525
if !h.Enabled {
2626
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
2727
Message: "Custom roles are not enabled",
@@ -77,14 +77,6 @@ func (h enterpriseCustomRoleHandler) PatchOrganizationRole(ctx context.Context,
7777
return codersdk.Role{}, false
7878
}
7979

80-
if role.OrganizationID != orgID.String() {
81-
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
82-
Message: "Invalid request, organization in role and url must match",
83-
Detail: fmt.Sprintf("role organization=%q does not match URL=%q", role.OrganizationID, orgID.String()),
84-
})
85-
return codersdk.Role{}, false
86-
}
87-
8880
originalRoles, err := db.CustomRoles(ctx, database.CustomRolesParams{
8981
LookupRoles: []database.NameOrganizationPair{
9082
{

0 commit comments

Comments
 (0)