|
12 | 12 | import Plus from '$lib/components/icons/Plus.svelte'; |
13 | 13 | import AddAccessModal from './AddAccessModal.svelte'; |
14 | 14 | import Tooltip from '$lib/components/common/Tooltip.svelte'; |
| 15 | + import Switch from '$lib/components/common/Switch.svelte'; |
15 | 16 |
|
16 | 17 | type AccessGrant = { |
17 | 18 | id?: string; |
|
159 | 160 | grant.principal_type === 'user' && grant.principal_id === '*' && grant.permission === 'read' |
160 | 161 | ); |
161 | 162 |
|
| 163 | + const hasPublicWriteGrant = (grants: AccessGrant[]): boolean => |
| 164 | + grants.some( |
| 165 | + (grant) => |
| 166 | + grant.principal_type === 'user' && grant.principal_id === '*' && grant.permission === 'write' |
| 167 | + ); |
| 168 | +
|
162 | 169 | const currentGrants = (): AccessGrant[] => |
163 | 170 | Array.isArray(accessGrants) ? (accessGrants as AccessGrant[]) : []; |
164 | 171 |
|
|
194 | 201 | }; |
195 | 202 |
|
196 | 203 | const setPublic = (isPublic: boolean) => { |
| 204 | + // Remove all user:* grants |
197 | 205 | const filtered = currentGrants().filter( |
198 | 206 | (grant) => |
199 | 207 | !( |
200 | 208 | grant.principal_type === 'user' && |
201 | | - grant.principal_id === '*' && |
202 | | - grant.permission === 'read' |
| 209 | + grant.principal_id === '*' |
203 | 210 | ) |
204 | 211 | ); |
205 | 212 | if (isPublic) { |
|
212 | 219 | commitAccessGrants(filtered); |
213 | 220 | }; |
214 | 221 |
|
| 222 | + const togglePublicWrite = () => { |
| 223 | + let next = [...currentGrants()]; |
| 224 | + if (hasPublicWriteGrant(next)) { |
| 225 | + next = next.filter( |
| 226 | + (grant) => |
| 227 | + !( |
| 228 | + grant.principal_type === 'user' && |
| 229 | + grant.principal_id === '*' && |
| 230 | + grant.permission === 'write' |
| 231 | + ) |
| 232 | + ); |
| 233 | + } else { |
| 234 | + next = upsertPrincipalGrant('user', '*', 'write', next); |
| 235 | + } |
| 236 | + commitAccessGrants(next); |
| 237 | + }; |
| 238 | +
|
215 | 239 | const upsertPrincipalGrant = ( |
216 | 240 | principalType: 'user' | 'group', |
217 | 241 | principalId: string, |
|
491 | 515 | </div> |
492 | 516 | </div> |
493 | 517 | </div> |
| 518 | + |
| 519 | + {#if hasPublicReadGrant(accessGrants ?? []) && accessRoles.includes('write')} |
| 520 | + <div class="flex w-full justify-between mt-2 ml-0.5"> |
| 521 | + <div class="self-center text-xs"> |
| 522 | + {$i18n.t('Allow everyone to edit')} |
| 523 | + </div> |
| 524 | + <Switch |
| 525 | + state={hasPublicWriteGrant(accessGrants ?? [])} |
| 526 | + on:change={() => { |
| 527 | + togglePublicWrite(); |
| 528 | + }} |
| 529 | + /> |
| 530 | + </div> |
| 531 | + {/if} |
494 | 532 | </div> |
495 | 533 |
|
496 | 534 | {#if share} |
|
0 commit comments