-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: renterd bucket policy and read access
- Loading branch information
1 parent
0b3226a
commit 69a696c
Showing
7 changed files
with
216 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'renterd': minor | ||
--- | ||
|
||
The bucket context menu now allows you to edit the bucket policy and toggle the read access between public and private. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
apps/renterd/components/Files/FilesBucketPolicyDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { | ||
Paragraph, | ||
Dialog, | ||
triggerErrorToast, | ||
triggerSuccessToast, | ||
ConfigFields, | ||
useOnInvalid, | ||
FormSubmitButton, | ||
FieldSelect, | ||
} from '@siafoundation/design-system' | ||
import { useCallback, useEffect, useMemo } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
import { useDialog } from '../../contexts/dialog' | ||
import { useBucket, useBucketPolicyUpdate } from '@siafoundation/react-renterd' | ||
|
||
const defaultValues = { | ||
visibility: 'public', | ||
} | ||
|
||
function getFields(name: string): ConfigFields<typeof defaultValues, never> { | ||
return { | ||
visibility: { | ||
type: 'text', | ||
title: 'Read Access', | ||
placeholder: name, | ||
validation: { | ||
required: 'required', | ||
}, | ||
options: [ | ||
{ | ||
label: 'Public', | ||
value: 'public', | ||
}, | ||
{ | ||
label: 'Private', | ||
value: 'private', | ||
}, | ||
], | ||
}, | ||
} | ||
} | ||
|
||
type Props = { | ||
trigger?: React.ReactNode | ||
open: boolean | ||
onOpenChange: (val: boolean) => void | ||
} | ||
|
||
export function FilesBucketPolicyDialog({ | ||
trigger, | ||
open, | ||
onOpenChange, | ||
}: Props) { | ||
const { id: name, closeDialog } = useDialog() | ||
const bucket = useBucket({ | ||
disabled: !open, | ||
params: { | ||
name: name, | ||
}, | ||
config: { | ||
swr: { | ||
revalidateOnFocus: false, | ||
}, | ||
}, | ||
}) | ||
|
||
const policyUpdate = useBucketPolicyUpdate() | ||
const form = useForm({ | ||
mode: 'all', | ||
defaultValues, | ||
}) | ||
|
||
useEffect(() => { | ||
form.reset({ | ||
visibility: bucket.data?.policy?.publicReadAccess ? 'public' : 'private', | ||
}) | ||
}, [form, bucket.data]) | ||
|
||
const onSubmit = useCallback( | ||
async (values: typeof defaultValues) => { | ||
const response = await policyUpdate.put({ | ||
params: { | ||
name, | ||
}, | ||
payload: { | ||
policy: { | ||
publicReadAccess: values.visibility === 'public', | ||
}, | ||
}, | ||
}) | ||
if (response.error) { | ||
triggerErrorToast(response.error) | ||
} else { | ||
triggerSuccessToast('Bucket policy has been updated.') | ||
form.reset() | ||
closeDialog() | ||
} | ||
}, | ||
[form, name, policyUpdate, closeDialog] | ||
) | ||
|
||
const fields = useMemo(() => getFields(name), [name]) | ||
|
||
const onInvalid = useOnInvalid(fields) | ||
|
||
return ( | ||
<Dialog | ||
title={`Change Policy: ${name}`} | ||
trigger={trigger} | ||
open={open} | ||
onOpenChange={(val) => { | ||
if (!val) { | ||
form.reset(defaultValues) | ||
} | ||
onOpenChange(val) | ||
}} | ||
contentVariants={{ | ||
className: 'w-[400px]', | ||
}} | ||
onSubmit={form.handleSubmit(onSubmit, onInvalid)} | ||
> | ||
<div className="flex flex-col gap-4"> | ||
<Paragraph size="14"> | ||
{`Update the bucket's policy to set read access to either private or public. Files in public read access buckets can be accessed without authentication via the S3 API.`} | ||
</Paragraph> | ||
<FieldSelect name="visibility" form={form} fields={fields} /> | ||
<FormSubmitButton variant="accent" form={form}> | ||
Update policy | ||
</FormSubmitButton> | ||
</div> | ||
</Dialog> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters