2022-05-03 21:10:19 +00:00
|
|
|
package codersdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2024-05-16 18:47:47 +00:00
|
|
|
// SlimRole omits permission information from a role.
|
|
|
|
// At present, this is because our apis do not return permission information,
|
|
|
|
// and it would require extra db calls to fetch this information. The UI does
|
|
|
|
// not need it, so most api calls will use this structure that omits information.
|
|
|
|
type SlimRole struct {
|
2022-05-06 19:18:00 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
}
|
|
|
|
|
2022-08-16 15:39:42 +00:00
|
|
|
type AssignableRoles struct {
|
2024-05-21 18:14:00 +00:00
|
|
|
Role `table:"r,recursive_inline"`
|
|
|
|
Assignable bool `json:"assignable" table:"assignable"`
|
|
|
|
// BuiltIn roles are immutable
|
|
|
|
BuiltIn bool `json:"built_in" table:"built_in"`
|
2022-08-16 15:39:42 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 18:47:47 +00:00
|
|
|
// Permission is the format passed into the rego.
|
|
|
|
type Permission struct {
|
|
|
|
// Negate makes this a negative permission
|
|
|
|
Negate bool `json:"negate"`
|
|
|
|
ResourceType RBACResource `json:"resource_type"`
|
|
|
|
Action RBACAction `json:"action"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Role is a longer form of SlimRole used to edit custom roles.
|
|
|
|
type Role struct {
|
2024-05-23 17:54:59 +00:00
|
|
|
Name string `json:"name" table:"name,default_sort" validate:"username"`
|
|
|
|
OrganizationID string `json:"organization_id" table:"organization_id" format:"uuid"`
|
2024-05-21 18:14:00 +00:00
|
|
|
DisplayName string `json:"display_name" table:"display_name"`
|
|
|
|
SitePermissions []Permission `json:"site_permissions" table:"site_permissions"`
|
2024-05-16 18:47:47 +00:00
|
|
|
// map[<org_id>] -> Permissions
|
2024-05-21 18:14:00 +00:00
|
|
|
OrganizationPermissions map[string][]Permission `json:"organization_permissions" table:"org_permissions"`
|
|
|
|
UserPermissions []Permission `json:"user_permissions" table:"user_permissions"`
|
2024-05-16 18:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PatchRole will upsert a custom site wide role
|
|
|
|
func (c *Client) PatchRole(ctx context.Context, req Role) (Role, error) {
|
|
|
|
res, err := c.Request(ctx, http.MethodPatch, "/api/v2/users/roles", req)
|
|
|
|
if err != nil {
|
|
|
|
return Role{}, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return Role{}, ReadBodyAsError(res)
|
|
|
|
}
|
|
|
|
var role Role
|
|
|
|
return role, json.NewDecoder(res.Body).Decode(&role)
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:27:48 +00:00
|
|
|
// ListSiteRoles lists all assignable site wide roles.
|
2022-08-16 15:39:42 +00:00
|
|
|
func (c *Client) ListSiteRoles(ctx context.Context) ([]AssignableRoles, error) {
|
2022-05-17 18:43:19 +00:00
|
|
|
res, err := c.Request(ctx, http.MethodGet, "/api/v2/users/roles", nil)
|
2022-05-03 21:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
2023-01-29 21:47:24 +00:00
|
|
|
return nil, ReadBodyAsError(res)
|
2022-05-03 21:10:19 +00:00
|
|
|
}
|
2022-08-16 15:39:42 +00:00
|
|
|
var roles []AssignableRoles
|
2022-05-03 21:10:19 +00:00
|
|
|
return roles, json.NewDecoder(res.Body).Decode(&roles)
|
|
|
|
}
|
|
|
|
|
2022-08-12 22:27:48 +00:00
|
|
|
// ListOrganizationRoles lists all assignable roles for a given organization.
|
2022-08-16 15:39:42 +00:00
|
|
|
func (c *Client) ListOrganizationRoles(ctx context.Context, org uuid.UUID) ([]AssignableRoles, error) {
|
2022-05-17 18:43:19 +00:00
|
|
|
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/organizations/%s/members/roles", org.String()), nil)
|
2022-05-03 21:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
2023-01-29 21:47:24 +00:00
|
|
|
return nil, ReadBodyAsError(res)
|
2022-05-03 21:10:19 +00:00
|
|
|
}
|
2022-08-16 15:39:42 +00:00
|
|
|
var roles []AssignableRoles
|
2022-05-03 21:10:19 +00:00
|
|
|
return roles, json.NewDecoder(res.Body).Decode(&roles)
|
|
|
|
}
|
2024-05-16 18:47:47 +00:00
|
|
|
|
|
|
|
// CreatePermissions is a helper function to quickly build permissions.
|
|
|
|
func CreatePermissions(mapping map[RBACResource][]RBACAction) []Permission {
|
|
|
|
perms := make([]Permission, 0)
|
|
|
|
for t, actions := range mapping {
|
|
|
|
for _, action := range actions {
|
|
|
|
perms = append(perms, Permission{
|
|
|
|
ResourceType: t,
|
|
|
|
Action: action,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return perms
|
|
|
|
}
|