Skip to content

Commit

Permalink
feat: display overriden permissions warning
Browse files Browse the repository at this point in the history
  • Loading branch information
fgmadeira committed Feb 25, 2024
1 parent 5790397 commit 0cb79a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectVa
import { ApiKey, ApiKeyPermissions, apiKeyPermissionsSchema, NewApiKey, newApiKeySchema } from '@/data/users/dto';
import { useSession } from 'next-auth/react';
import { notFound } from 'next/navigation';
import { ShieldBan } from 'lucide-react';

type ProjectLabels = {
id: string,
Expand Down Expand Up @@ -129,6 +130,7 @@ export function ApiKeyForm({ apyKey, availableProjects, onComplete }: ApiKeyDial
control={form.control}
name={'permissions'}
render={({ field }) => {
const restricted = session?.permissions[item.id] !== field.value?.[item.id]
return (
<FormItem
key={item.id}
Expand All @@ -137,7 +139,9 @@ export function ApiKeyForm({ apyKey, availableProjects, onComplete }: ApiKeyDial
<FormLabel className="font-normal">
{item.name}
</FormLabel>
<FormControl>
<div className="flex gap-2">
{restricted ? <span className="flex items-center gap-1 text-muted-foreground text-sm" ><ShieldBan className="h-4 w-4" color="red" />Restricted</span> : null}
<FormControl>
<Select
value={field.value?.[item.id]}
defaultValue={session?.permissions[item.id] ?? 'read'}
Expand All @@ -154,12 +158,15 @@ export function ApiKeyForm({ apyKey, availableProjects, onComplete }: ApiKeyDial
<SelectContent>
<SelectGroup>
<SelectItem value="none">No access</SelectItem>
<SelectItem value="read">Can read</SelectItem>
<SelectItem value="write">Can edit</SelectItem>
<SelectItem value="read" disabled={session?.permissions[item.id] === 'none'}>Can read</SelectItem>
<SelectItem value="write" disabled={session?.permissions[item.id] !== 'write'}>Can edit</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</FormControl>

</div>
<FormMessage />
</FormItem>
)
}}
Expand Down
2 changes: 1 addition & 1 deletion ethereal-nexus-dashboard/src/data/users/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export async function getApiKeyById(apiKey: string): ActionResponse<ApiKey> {
where: eq(apiKeys.id, id),
});

const safe = apiKeySchema.safeParse(result);
const safe = apiKeySchema.omit({member_permissions: true}).safeParse(result);
if (!safe.success) {
return actionZodError(
'There\'s an issue with the api key record.',
Expand Down
1 change: 1 addition & 0 deletions ethereal-nexus-dashboard/src/data/users/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const transformId = val => ({
key: '************' + val.key.substr(val.id.length - 13)
});
export const apiKeyPublicSchema = apiKeySchema
.omit({member_permissions: true})
.transform(transformId);
export type PublicApiKey = z.infer<typeof apiKeyPublicSchema>

Expand Down

0 comments on commit 0cb79a4

Please sign in to comment.