Skip to content

fix: only show valid organizations in CreateTemplateForm #14174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: only show valid organizations in CreateTemplateForm
hide any organizations that the user might have view permissions for, but not permission to create a template
  • Loading branch information
aslilac committed Aug 5, 2024
commit 44869aba098b3a28776412502651626ee4ac184b
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
useState,
} from "react";
import { useQuery } from "react-query";
import { checkAuthorization } from "api/queries/authCheck";
import { organizations } from "api/queries/organizations";
import type { Organization } from "api/typesGenerated";
import type { AuthorizationCheck, Organization } from "api/typesGenerated";
import { Avatar } from "components/Avatar/Avatar";
import { AvatarData } from "components/AvatarData/AvatarData";
import { useDebouncedFunction } from "hooks/debounce";
Expand All @@ -22,6 +23,7 @@ export type OrganizationAutocompleteProps = {
className?: string;
size?: ComponentProps<typeof TextField>["size"];
required?: boolean;
check?: AuthorizationCheck;
};

export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
Expand All @@ -31,6 +33,7 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
className,
size = "small",
required,
check,
}) => {
const [autoComplete, setAutoComplete] = useState<{
value: string;
Expand All @@ -41,6 +44,22 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
});
const organizationsQuery = useQuery(organizations());

const permissionsQuery = useQuery(
check && organizationsQuery.data
? checkAuthorization({
checks: Object.fromEntries(
organizationsQuery.data.map((org) => [
org.id,
{
...check,
object: { ...check.object, organization_id: org.id },
},
]),
),
})
: { enabled: false },
);

const { debounced: debouncedInputOnChange } = useDebouncedFunction(
(event: ChangeEvent<HTMLInputElement>) => {
setAutoComplete((state) => ({
Expand All @@ -51,11 +70,18 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
750,
);

// If an authorization check was provided, filter the organizations based on
// the results of that check.
let options = organizationsQuery.data ?? [];
if (check && permissionsQuery.data) {
options = options.filter((org) => permissionsQuery.data[org.id]);
}

return (
<Autocomplete
noOptionsText="No organizations found"
className={className}
options={organizationsQuery.data ?? []}
options={options}
loading={organizationsQuery.isLoading}
value={value}
data-testid="organization-autocomplete"
Expand Down
1 change: 1 addition & 0 deletions site/src/contexts/auth/permissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const permissionsToCheck = {
[checks.createTemplates]: {
object: {
resource_type: "template",
any_org: true,
},
action: "update",
},
Expand Down
4 changes: 4 additions & 0 deletions site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = (props) => {
void form.setFieldValue("organization", newValue?.name || "");
}}
size="medium"
check={{
object: { resource_type: "template" },
action: "create",
}}
/>
</>
)}
Expand Down