2022-07-04 11:28:53 +00:00
|
|
|
|
<script lang="ts" setup>
|
2022-09-26 07:48:55 +00:00
|
|
|
|
import SubmitButton from "@/components/button/SubmitButton.vue";
|
2023-11-13 08:56:08 +00:00
|
|
|
|
import { useRoleForm, useRoleTemplateSelection } from "@/composables/use-role";
|
2024-06-26 10:42:50 +00:00
|
|
|
|
import { rbacAnnotations } from "@/constants/annotations";
|
2023-09-25 03:30:14 +00:00
|
|
|
|
import { pluginLabels, roleLabels } from "@/constants/labels";
|
2024-06-26 10:42:50 +00:00
|
|
|
|
import { setFocus } from "@/formkit/utils/focus";
|
|
|
|
|
import type { Role } from "@halo-dev/api-client";
|
2024-06-25 04:31:44 +00:00
|
|
|
|
import { coreApiClient } from "@halo-dev/api-client";
|
2024-06-26 10:42:50 +00:00
|
|
|
|
import { VButton, VModal, VSpace } from "@halo-dev/components";
|
2023-09-25 03:30:14 +00:00
|
|
|
|
import { useQuery } from "@tanstack/vue-query";
|
2024-06-26 10:42:50 +00:00
|
|
|
|
import { cloneDeep } from "lodash-es";
|
|
|
|
|
import { onMounted, ref, watch } from "vue";
|
|
|
|
|
import { useI18n } from "vue-i18n";
|
2023-03-23 08:54:33 +00:00
|
|
|
|
|
|
|
|
|
const { t } = useI18n();
|
2022-07-04 11:28:53 +00:00
|
|
|
|
|
2022-08-15 09:56:13 +00:00
|
|
|
|
const props = withDefaults(
|
|
|
|
|
defineProps<{
|
2022-09-28 10:37:01 +00:00
|
|
|
|
role?: Role;
|
2022-08-15 09:56:13 +00:00
|
|
|
|
}>(),
|
|
|
|
|
{
|
2022-09-28 10:37:01 +00:00
|
|
|
|
role: undefined,
|
2022-08-15 09:56:13 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
2022-07-04 11:28:53 +00:00
|
|
|
|
|
2022-08-15 12:31:51 +00:00
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
(event: "close"): void;
|
|
|
|
|
}>();
|
2022-07-04 11:28:53 +00:00
|
|
|
|
|
2024-05-27 08:56:57 +00:00
|
|
|
|
const modal = ref<InstanceType<typeof VModal> | null>(null);
|
2024-05-24 07:18:51 +00:00
|
|
|
|
|
2023-09-25 03:30:14 +00:00
|
|
|
|
const { data: roleTemplates } = useQuery({
|
|
|
|
|
queryKey: ["role-templates"],
|
|
|
|
|
queryFn: async () => {
|
2024-06-25 04:31:44 +00:00
|
|
|
|
const { data } = await coreApiClient.role.listRole({
|
2023-09-25 03:30:14 +00:00
|
|
|
|
page: 0,
|
|
|
|
|
size: 0,
|
|
|
|
|
labelSelector: [`${roleLabels.TEMPLATE}=true`, "!halo.run/hidden"],
|
|
|
|
|
});
|
|
|
|
|
return data.items;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-26 10:04:51 +00:00
|
|
|
|
const { roleTemplateGroups, handleRoleTemplateSelect, selectedRoleTemplates } =
|
2023-09-25 03:30:14 +00:00
|
|
|
|
useRoleTemplateSelection(roleTemplates);
|
2022-07-27 07:05:54 +00:00
|
|
|
|
|
2024-05-24 07:18:51 +00:00
|
|
|
|
const { formState, isSubmitting, handleCreateOrUpdate } = useRoleForm();
|
2022-07-26 10:04:51 +00:00
|
|
|
|
|
2022-07-26 12:21:11 +00:00
|
|
|
|
watch(
|
|
|
|
|
() => selectedRoleTemplates.value,
|
|
|
|
|
(newValue) => {
|
|
|
|
|
if (formState.value.metadata.annotations) {
|
|
|
|
|
formState.value.metadata.annotations[rbacAnnotations.DEPENDENCIES] =
|
|
|
|
|
JSON.stringify(Array.from(newValue));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-24 07:18:51 +00:00
|
|
|
|
onMounted(() => {
|
|
|
|
|
setFocus("displayNameInput");
|
2022-08-23 07:27:42 +00:00
|
|
|
|
|
2024-05-24 07:18:51 +00:00
|
|
|
|
if (props.role) {
|
|
|
|
|
formState.value = cloneDeep(props.role);
|
|
|
|
|
const dependencies =
|
|
|
|
|
props.role.metadata.annotations?.[rbacAnnotations.DEPENDENCIES];
|
|
|
|
|
if (dependencies) {
|
|
|
|
|
selectedRoleTemplates.value = new Set(JSON.parse(dependencies));
|
2022-08-23 07:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-27 07:05:54 +00:00
|
|
|
|
});
|
|
|
|
|
|
2024-05-24 07:18:51 +00:00
|
|
|
|
const editingModalTitle = props.role
|
|
|
|
|
? t("core.role.editing_modal.titles.update")
|
|
|
|
|
: t("core.role.editing_modal.titles.create");
|
|
|
|
|
|
2022-07-26 12:21:11 +00:00
|
|
|
|
const handleCreateOrUpdateRole = async () => {
|
2022-07-04 11:28:53 +00:00
|
|
|
|
try {
|
2022-07-26 12:21:11 +00:00
|
|
|
|
await handleCreateOrUpdate();
|
2024-05-24 07:18:51 +00:00
|
|
|
|
|
|
|
|
|
modal.value?.close();
|
2022-07-04 11:28:53 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
<template>
|
|
|
|
|
<VModal
|
2024-05-24 07:18:51 +00:00
|
|
|
|
ref="modal"
|
2022-08-15 06:43:06 +00:00
|
|
|
|
:title="editingModalTitle"
|
2022-07-22 04:49:06 +00:00
|
|
|
|
:width="700"
|
2024-05-24 07:18:51 +00:00
|
|
|
|
@close="emit('close')"
|
2022-07-04 11:28:53 +00:00
|
|
|
|
>
|
2022-12-26 06:10:31 +00:00
|
|
|
|
<div>
|
|
|
|
|
<div class="md:grid md:grid-cols-4 md:gap-6">
|
|
|
|
|
<div class="md:col-span-1">
|
|
|
|
|
<div class="sticky top-0">
|
2023-03-23 08:54:33 +00:00
|
|
|
|
<span class="text-base font-medium text-gray-900">
|
|
|
|
|
{{ $t("core.role.editing_modal.groups.general") }}
|
|
|
|
|
</span>
|
2022-12-26 06:10:31 +00:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="mt-5 divide-y divide-gray-100 md:col-span-3 md:mt-0">
|
2022-07-07 08:50:50 +00:00
|
|
|
|
<FormKit
|
2022-12-26 06:10:31 +00:00
|
|
|
|
v-if="formState.metadata.annotations"
|
|
|
|
|
id="role-form"
|
|
|
|
|
name="role-form"
|
|
|
|
|
:actions="false"
|
|
|
|
|
type="form"
|
|
|
|
|
:config="{ validationVisibility: 'submit' }"
|
|
|
|
|
@submit="handleCreateOrUpdateRole"
|
2022-11-28 14:20:18 +00:00
|
|
|
|
>
|
2022-12-26 06:10:31 +00:00
|
|
|
|
<FormKit
|
|
|
|
|
id="displayNameInput"
|
|
|
|
|
v-model="
|
|
|
|
|
formState.metadata.annotations[rbacAnnotations.DISPLAY_NAME]
|
|
|
|
|
"
|
2023-03-23 08:54:33 +00:00
|
|
|
|
:label="$t('core.role.editing_modal.fields.display_name')"
|
2022-12-26 06:10:31 +00:00
|
|
|
|
type="text"
|
|
|
|
|
validation="required|length:0,50"
|
|
|
|
|
></FormKit>
|
2023-12-01 02:38:09 +00:00
|
|
|
|
<FormKit
|
|
|
|
|
v-model="
|
|
|
|
|
formState.metadata.annotations[
|
|
|
|
|
rbacAnnotations.DISALLOW_ACCESS_CONSOLE
|
|
|
|
|
]
|
|
|
|
|
"
|
|
|
|
|
on-value="true"
|
|
|
|
|
off-value="false"
|
|
|
|
|
type="checkbox"
|
|
|
|
|
:label="
|
|
|
|
|
$t(
|
|
|
|
|
'core.role.editing_modal.fields.disallow_access_console.label'
|
|
|
|
|
)
|
|
|
|
|
"
|
|
|
|
|
:help="
|
|
|
|
|
$t(
|
|
|
|
|
'core.role.editing_modal.fields.disallow_access_console.help'
|
|
|
|
|
)
|
|
|
|
|
"
|
|
|
|
|
></FormKit>
|
2022-12-26 06:10:31 +00:00
|
|
|
|
</FormKit>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="py-5">
|
|
|
|
|
<div class="border-t border-gray-200"></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="md:grid md:grid-cols-4 md:gap-6">
|
|
|
|
|
<div class="md:col-span-1">
|
|
|
|
|
<div class="sticky top-0">
|
2023-03-23 08:54:33 +00:00
|
|
|
|
<span class="text-base font-medium text-gray-900">
|
|
|
|
|
{{ $t("core.role.editing_modal.groups.permissions") }}
|
|
|
|
|
</span>
|
2022-12-26 06:10:31 +00:00
|
|
|
|
</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">
|
2023-02-21 05:16:10 +00:00
|
|
|
|
<div>
|
2023-03-23 08:54:33 +00:00
|
|
|
|
{{ $t(`core.rbac.${group.module}`, group.module as string) }}
|
2023-02-21 05:16:10 +00:00
|
|
|
|
</div>
|
2022-12-26 06:10:31 +00:00
|
|
|
|
<div
|
|
|
|
|
v-if="
|
|
|
|
|
group.roles.length &&
|
|
|
|
|
group.roles[0].metadata.labels?.[pluginLabels.NAME]
|
|
|
|
|
"
|
|
|
|
|
class="mt-3 text-xs text-gray-500"
|
|
|
|
|
>
|
2023-03-23 08:54:33 +00:00
|
|
|
|
<i18n-t
|
|
|
|
|
keypath="core.role.common.text.provided_by_plugin"
|
|
|
|
|
tag="div"
|
2022-11-28 14:20:18 +00:00
|
|
|
|
>
|
2023-03-23 08:54:33 +00:00
|
|
|
|
<template #plugin>
|
|
|
|
|
<RouterLink
|
|
|
|
|
:to="{
|
|
|
|
|
name: 'PluginDetail',
|
|
|
|
|
params: {
|
|
|
|
|
name: group.roles[0].metadata.labels?.[
|
|
|
|
|
pluginLabels.NAME
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
}"
|
|
|
|
|
class="hover:text-blue-600"
|
|
|
|
|
>
|
|
|
|
|
{{
|
|
|
|
|
group.roles[0].metadata.labels?.[pluginLabels.NAME]
|
|
|
|
|
}}
|
|
|
|
|
</RouterLink>
|
|
|
|
|
</template>
|
|
|
|
|
</i18n-t>
|
2022-12-26 06:10:31 +00:00
|
|
|
|
</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">
|
|
|
|
|
{{
|
2023-02-21 05:16:10 +00:00
|
|
|
|
$t(
|
2023-03-23 08:54:33 +00:00
|
|
|
|
`core.rbac.${
|
2023-02-21 05:16:10 +00:00
|
|
|
|
roleTemplate.metadata.annotations?.[
|
|
|
|
|
rbacAnnotations.DISPLAY_NAME
|
|
|
|
|
]
|
|
|
|
|
}`,
|
|
|
|
|
roleTemplate.metadata.annotations?.[
|
|
|
|
|
rbacAnnotations.DISPLAY_NAME
|
|
|
|
|
] as string
|
|
|
|
|
)
|
2022-12-26 06:10:31 +00:00
|
|
|
|
}}
|
|
|
|
|
</span>
|
|
|
|
|
<span
|
|
|
|
|
v-if="
|
2022-08-30 09:30:43 +00:00
|
|
|
|
roleTemplate.metadata.annotations?.[
|
2022-07-20 14:47:10 +00:00
|
|
|
|
rbacAnnotations.DEPENDENCIES
|
2022-07-04 11:28:53 +00:00
|
|
|
|
]
|
2022-12-26 06:10:31 +00:00
|
|
|
|
"
|
|
|
|
|
class="text-xs text-gray-400"
|
|
|
|
|
>
|
|
|
|
|
{{
|
2023-03-23 08:54:33 +00:00
|
|
|
|
$t("core.role.common.text.dependent_on", {
|
|
|
|
|
roles: JSON.parse(
|
|
|
|
|
roleTemplate.metadata.annotations?.[
|
|
|
|
|
rbacAnnotations.DEPENDENCIES
|
|
|
|
|
]
|
2023-02-21 05:16:10 +00:00
|
|
|
|
)
|
2023-03-23 08:54:33 +00:00
|
|
|
|
.map((item: string) =>
|
|
|
|
|
$t(`core.rbac.${item}`, item as string)
|
|
|
|
|
)
|
|
|
|
|
.join(","),
|
|
|
|
|
})
|
2022-12-26 06:10:31 +00:00
|
|
|
|
}}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</label>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</dd>
|
|
|
|
|
</div>
|
|
|
|
|
</dl>
|
|
|
|
|
</div>
|
2022-11-28 14:20:18 +00:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2022-07-04 11:28:53 +00:00
|
|
|
|
<template #footer>
|
2022-07-27 07:05:54 +00:00
|
|
|
|
<VSpace>
|
2022-09-26 07:48:55 +00:00
|
|
|
|
<SubmitButton
|
2024-05-24 07:18:51 +00:00
|
|
|
|
:loading="isSubmitting"
|
2022-07-27 07:05:54 +00:00
|
|
|
|
type="secondary"
|
2023-03-23 08:54:33 +00:00
|
|
|
|
:text="$t('core.common.buttons.submit')"
|
2022-09-26 07:48:55 +00:00
|
|
|
|
@submit="$formkit.submit('role-form')"
|
2022-07-27 07:05:54 +00:00
|
|
|
|
>
|
2022-09-26 07:48:55 +00:00
|
|
|
|
</SubmitButton>
|
2024-05-24 07:18:51 +00:00
|
|
|
|
<VButton @click="modal?.close()">
|
2023-03-23 08:54:33 +00:00
|
|
|
|
{{ $t("core.common.buttons.cancel_and_shortcut") }}
|
|
|
|
|
</VButton>
|
2022-07-27 07:05:54 +00:00
|
|
|
|
</VSpace>
|
2022-07-04 11:28:53 +00:00
|
|
|
|
</template>
|
|
|
|
|
</VModal>
|
|
|
|
|
</template>
|