2023-09-25 03:30:14 +00:00
|
|
|
|
<script lang="ts" setup>
|
|
|
|
|
import SubmitButton from "@/components/button/SubmitButton.vue";
|
|
|
|
|
import { patAnnotations, rbacAnnotations } from "@/constants/annotations";
|
|
|
|
|
import { apiClient } from "@/utils/api-client";
|
|
|
|
|
import { toISOString } from "@/utils/date";
|
|
|
|
|
import { Dialog, Toast, VButton, VModal, VSpace } from "@halo-dev/components";
|
|
|
|
|
import { useMutation, useQueryClient } from "@tanstack/vue-query";
|
|
|
|
|
import { useClipboard } from "@vueuse/core";
|
|
|
|
|
import type { PatSpec, PersonalAccessToken } from "@halo-dev/api-client";
|
2024-01-31 07:42:57 +00:00
|
|
|
|
import { computed, ref } from "vue";
|
2023-11-13 08:56:08 +00:00
|
|
|
|
import { useRoleTemplateSelection } from "@/composables/use-role";
|
2023-09-25 03:30:14 +00:00
|
|
|
|
import { useRoleStore } from "@/stores/role";
|
|
|
|
|
import { useI18n } from "vue-i18n";
|
2024-01-31 07:42:57 +00:00
|
|
|
|
import { roleLabels } from "@/constants/labels";
|
2023-09-25 03:30:14 +00:00
|
|
|
|
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
const { t } = useI18n();
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
(event: "close"): void;
|
|
|
|
|
}>();
|
|
|
|
|
|
2024-05-27 08:56:57 +00:00
|
|
|
|
const modal = ref<InstanceType<typeof VModal> | null>(null);
|
2024-01-10 04:50:26 +00:00
|
|
|
|
|
2023-09-25 03:30:14 +00:00
|
|
|
|
const formState = ref<
|
|
|
|
|
Omit<PersonalAccessToken, "spec"> & {
|
|
|
|
|
spec: PatSpec;
|
|
|
|
|
}
|
|
|
|
|
>({
|
|
|
|
|
kind: "PersonalAccessToken",
|
|
|
|
|
apiVersion: "security.halo.run/v1alpha1",
|
|
|
|
|
metadata: {
|
|
|
|
|
generateName: "pat-",
|
|
|
|
|
name: "",
|
|
|
|
|
},
|
|
|
|
|
spec: {
|
|
|
|
|
description: "",
|
|
|
|
|
expiresAt: "",
|
|
|
|
|
name: "",
|
|
|
|
|
roles: [],
|
|
|
|
|
tokenId: "",
|
|
|
|
|
username: "",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { permissions } = useRoleStore();
|
|
|
|
|
|
2024-01-31 07:42:57 +00:00
|
|
|
|
const availableRoleTemplates = computed(() => {
|
|
|
|
|
return permissions.permissions.filter((role) => {
|
|
|
|
|
return (
|
|
|
|
|
role.metadata.labels?.[roleLabels.TEMPLATE] === "true" &&
|
|
|
|
|
role.metadata.labels?.[roleLabels.HIDDEN] !== "true"
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-25 03:30:14 +00:00
|
|
|
|
const { roleTemplateGroups, handleRoleTemplateSelect, selectedRoleTemplates } =
|
2024-01-31 07:42:57 +00:00
|
|
|
|
useRoleTemplateSelection(availableRoleTemplates);
|
2023-09-25 03:30:14 +00:00
|
|
|
|
|
2024-01-29 09:25:12 +00:00
|
|
|
|
const { copy } = useClipboard({
|
|
|
|
|
legacy: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { mutate, isLoading } = useMutation({
|
2023-09-25 03:30:14 +00:00
|
|
|
|
mutationKey: ["pat-creation"],
|
|
|
|
|
mutationFn: async () => {
|
|
|
|
|
if (formState.value.spec?.expiresAt) {
|
|
|
|
|
formState.value.spec.expiresAt = toISOString(
|
|
|
|
|
formState.value.spec.expiresAt
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
formState.value.spec = {
|
|
|
|
|
...formState.value.spec,
|
|
|
|
|
roles: Array.from(selectedRoleTemplates.value),
|
|
|
|
|
};
|
|
|
|
|
const { data } = await apiClient.pat.generatePat({
|
|
|
|
|
personalAccessToken: formState.value,
|
|
|
|
|
});
|
|
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
onSuccess(data) {
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: ["personal-access-tokens"] });
|
|
|
|
|
emit("close");
|
|
|
|
|
|
2024-01-29 09:25:12 +00:00
|
|
|
|
const token = data.metadata.annotations?.[patAnnotations.ACCESS_TOKEN];
|
|
|
|
|
|
2023-09-25 03:30:14 +00:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
Dialog.info({
|
2023-11-30 10:56:10 +00:00
|
|
|
|
title: t("core.uc_profile.pat.operations.copy.title"),
|
2024-01-29 09:25:12 +00:00
|
|
|
|
description: token,
|
2023-09-25 03:30:14 +00:00
|
|
|
|
confirmType: "secondary",
|
|
|
|
|
confirmText: t("core.common.buttons.copy"),
|
|
|
|
|
showCancel: false,
|
|
|
|
|
onConfirm: () => {
|
2024-01-29 09:25:12 +00:00
|
|
|
|
copy(token || "");
|
2023-09-25 03:30:14 +00:00
|
|
|
|
Toast.success(t("core.common.toast.copy_success"));
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<VModal
|
2024-01-10 04:50:26 +00:00
|
|
|
|
ref="modal"
|
2023-09-25 03:30:14 +00:00
|
|
|
|
:width="700"
|
2023-11-30 10:56:10 +00:00
|
|
|
|
:title="$t('core.uc_profile.pat.creation_modal.title')"
|
2023-09-25 03:30:14 +00:00
|
|
|
|
@close="emit('close')"
|
|
|
|
|
>
|
|
|
|
|
<div>
|
|
|
|
|
<div class="md:grid md:grid-cols-4 md:gap-6">
|
|
|
|
|
<div class="md:col-span-1">
|
|
|
|
|
<div class="sticky top-0">
|
|
|
|
|
<span class="text-base font-medium text-gray-900">
|
2023-11-30 10:56:10 +00:00
|
|
|
|
{{ $t("core.uc_profile.pat.creation_modal.groups.general") }}
|
2023-09-25 03:30:14 +00:00
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="mt-5 divide-y divide-gray-100 md:col-span-3 md:mt-0">
|
|
|
|
|
<FormKit
|
|
|
|
|
id="pat-creation-form"
|
|
|
|
|
v-model="formState.spec"
|
|
|
|
|
type="form"
|
|
|
|
|
name="pat-creation-form"
|
|
|
|
|
@submit="mutate()"
|
|
|
|
|
>
|
|
|
|
|
<FormKit
|
|
|
|
|
validation="required"
|
|
|
|
|
type="text"
|
|
|
|
|
name="name"
|
2023-11-30 10:56:10 +00:00
|
|
|
|
:label="
|
|
|
|
|
$t('core.uc_profile.pat.creation_modal.fields.name.label')
|
|
|
|
|
"
|
2023-09-25 03:30:14 +00:00
|
|
|
|
></FormKit>
|
|
|
|
|
<FormKit
|
|
|
|
|
type="datetime-local"
|
|
|
|
|
name="expiresAt"
|
2023-11-30 10:56:10 +00:00
|
|
|
|
:label="
|
|
|
|
|
$t('core.uc_profile.pat.creation_modal.fields.expiresAt.label')
|
|
|
|
|
"
|
|
|
|
|
:help="
|
|
|
|
|
$t('core.uc_profile.pat.creation_modal.fields.expiresAt.help')
|
|
|
|
|
"
|
2023-09-25 03:30:14 +00:00
|
|
|
|
></FormKit>
|
|
|
|
|
<FormKit
|
|
|
|
|
type="textarea"
|
|
|
|
|
name="description"
|
|
|
|
|
:label="
|
2023-11-30 10:56:10 +00:00
|
|
|
|
$t(
|
|
|
|
|
'core.uc_profile.pat.creation_modal.fields.description.label'
|
|
|
|
|
)
|
2023-09-25 03:30:14 +00:00
|
|
|
|
"
|
|
|
|
|
></FormKit>
|
|
|
|
|
</FormKit>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div v-if="roleTemplateGroups.length" class="py-5">
|
|
|
|
|
<div class="border-t border-gray-200"></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
v-if="roleTemplateGroups.length"
|
|
|
|
|
class="md:grid md:grid-cols-4 md:gap-6"
|
|
|
|
|
>
|
|
|
|
|
<div class="md:col-span-1">
|
|
|
|
|
<div class="sticky top-0">
|
|
|
|
|
<span class="text-base font-medium text-gray-900">
|
2023-11-30 10:56:10 +00:00
|
|
|
|
{{ $t("core.uc_profile.pat.creation_modal.groups.permissions") }}
|
2023-09-25 03:30:14 +00:00
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="mt-5 divide-y divide-gray-100 md:col-span-3 md:mt-0">
|
|
|
|
|
<dl class="divide-y divide-gray-100">
|
|
|
|
|
<div
|
|
|
|
|
v-for="(group, groupIndex) in roleTemplateGroups"
|
|
|
|
|
:key="groupIndex"
|
|
|
|
|
class="flex flex-col gap-3 bg-white py-5 first:pt-0"
|
|
|
|
|
>
|
|
|
|
|
<dt class="text-sm font-medium text-gray-900">
|
|
|
|
|
<div>
|
|
|
|
|
{{ $t(`core.rbac.${group.module}`, group.module as string) }}
|
|
|
|
|
</div>
|
|
|
|
|
</dt>
|
|
|
|
|
<dd class="text-sm text-gray-900">
|
|
|
|
|
<ul class="space-y-2">
|
|
|
|
|
<li v-for="(roleTemplate, index) in group.roles" :key="index">
|
|
|
|
|
<label
|
|
|
|
|
class="inline-flex w-full cursor-pointer flex-row items-center gap-4 rounded-base border p-5 hover:border-primary"
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
v-model="selectedRoleTemplates"
|
|
|
|
|
:value="roleTemplate.metadata.name"
|
|
|
|
|
type="checkbox"
|
|
|
|
|
@change="handleRoleTemplateSelect"
|
|
|
|
|
/>
|
|
|
|
|
<div class="flex flex-1 flex-col gap-y-3">
|
|
|
|
|
<span class="font-medium text-gray-900">
|
|
|
|
|
{{
|
|
|
|
|
$t(
|
|
|
|
|
`core.rbac.${
|
|
|
|
|
roleTemplate.metadata.annotations?.[
|
|
|
|
|
rbacAnnotations.DISPLAY_NAME
|
|
|
|
|
]
|
|
|
|
|
}`,
|
|
|
|
|
roleTemplate.metadata.annotations?.[
|
|
|
|
|
rbacAnnotations.DISPLAY_NAME
|
|
|
|
|
] as string
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
</span>
|
|
|
|
|
<span
|
|
|
|
|
v-if="
|
|
|
|
|
roleTemplate.metadata.annotations?.[
|
|
|
|
|
rbacAnnotations.DEPENDENCIES
|
|
|
|
|
]
|
|
|
|
|
"
|
|
|
|
|
class="text-xs text-gray-400"
|
|
|
|
|
>
|
|
|
|
|
{{
|
|
|
|
|
$t("core.role.common.text.dependent_on", {
|
|
|
|
|
roles: JSON.parse(
|
|
|
|
|
roleTemplate.metadata.annotations?.[
|
|
|
|
|
rbacAnnotations.DEPENDENCIES
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
.map((item: string) =>
|
|
|
|
|
$t(`core.rbac.${item}`, item as string)
|
|
|
|
|
)
|
|
|
|
|
.join(","),
|
|
|
|
|
})
|
|
|
|
|
}}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</label>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</dd>
|
|
|
|
|
</div>
|
|
|
|
|
</dl>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<template #footer>
|
|
|
|
|
<VSpace>
|
|
|
|
|
<SubmitButton
|
|
|
|
|
:loading="isLoading"
|
|
|
|
|
type="secondary"
|
|
|
|
|
:text="$t('core.common.buttons.submit')"
|
|
|
|
|
@submit="$formkit.submit('pat-creation-form')"
|
|
|
|
|
/>
|
2024-05-27 08:56:57 +00:00
|
|
|
|
<VButton @click="modal?.close()">
|
2023-09-25 03:30:14 +00:00
|
|
|
|
{{ $t("core.common.buttons.cancel_and_shortcut") }}
|
|
|
|
|
</VButton>
|
|
|
|
|
</VSpace>
|
|
|
|
|
</template>
|
|
|
|
|
</VModal>
|
|
|
|
|
</template>
|