Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add disallow access console option for custom role #4958

Merged
merged 7 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ metadata:
annotations:
rbac.authorization.halo.run/module: "Posts Management"
rbac.authorization.halo.run/display-name: "Post Author"
rbac.authorization.halo.run/disallow-access-console: "true"
rbac.authorization.halo.run/redirect-on-login: "/uc"
rbac.authorization.halo.run/dependencies: |
[ "post-contributor", "post-publisher" ]
rules: [ ]
Expand All @@ -29,6 +31,8 @@ metadata:
annotations:
rbac.authorization.halo.run/module: "Posts Management"
rbac.authorization.halo.run/display-name: "Post Contributor"
rbac.authorization.halo.run/disallow-access-console: "true"
rbac.authorization.halo.run/redirect-on-login: "/uc"
rbac.authorization.halo.run/dependencies: |
[ "role-template-view-categories", "role-template-view-tags" ]
rbac.authorization.halo.run/ui-permissions: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ metadata:
rbac.authorization.halo.run/system-reserved: "true"
annotations:
rbac.authorization.halo.run/display-name: "访客"
rules: [ ]
rbac.authorization.halo.run/disallow-access-console: "true"
rbac.authorization.halo.run/redirect-on-login: "/uc"
rules: []

---
apiVersion: v1alpha1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ const handleResetForm = () => {
type="text"
:label="$t('core.role.editing_modal.fields.redirect_on_login')"
></FormKit>
<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>
</FormKit>
</div>
</div>
Expand Down
26 changes: 25 additions & 1 deletion console/console-src/router/guards/auth-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function setupAuthCheckGuard(router: Router) {
window.location.href =
roleHasRedirectOnLogin.metadata.annotations?.[
rbacAnnotations.REDIRECT_ON_LOGIN
] || "/";
] || "/uc";
return;
}

Expand All @@ -51,6 +51,30 @@ export function setupAuthCheckGuard(router: Router) {
});
return;
}

if (to.name === "whiteList") {
next();
return;
}

// Check allow access console
const { currentRoles } = userStore;

const hasDisallowAccessConsoleRole = currentRoles?.some((role) => {
return (
role.metadata.annotations?.[
rbacAnnotations.DISALLOW_ACCESS_CONSOLE
] === "true"
);
});

if (hasDisallowAccessConsoleRole) {
window.location.href = "/uc";
return;
}

next();
return;
}

next();
Expand Down
1 change: 1 addition & 0 deletions console/src/constants/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum rbacAnnotations {
AVATAR_ATTACHMENT_NAME = "halo.run/avatar-attachment-name",
LAST_AVATAR_ATTACHMENT_NAME = "halo.run/last-avatar-attachment-name",
REDIRECT_ON_LOGIN = "rbac.authorization.halo.run/redirect-on-login",
DISALLOW_ACCESS_CONSOLE = "rbac.authorization.halo.run/disallow-access-console",
}

// content
Expand Down
3 changes: 3 additions & 0 deletions console/src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,9 @@ core:
fields:
display_name: Display name
redirect_on_login: Default redirect location after logging in
disallow_access_console:
label: Disable access to Console
help: Once checked, this role will not be able to access the Console
identity_authentication:
title: Identity Authentication
tabs:
Expand Down
3 changes: 3 additions & 0 deletions console/src/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,9 @@ core:
fields:
display_name: 名称
redirect_on_login: 登录之后默认跳转位置
disallow_access_console:
label: 禁止访问 Console
help: 勾选之后,该角色将无法访问 Console
identity_authentication:
title: 身份认证
tabs:
Expand Down
3 changes: 3 additions & 0 deletions console/src/locales/zh-TW.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,9 @@ core:
fields:
display_name: 名稱
redirect_on_login: 登入之後預設跳轉位置
disallow_access_console:
label: 禁止訪問 Console
help: 勾選之後,該角色將無法存取 Console
identity_authentication:
title: 身份認證
tabs:
Expand Down
13 changes: 12 additions & 1 deletion console/uc-src/layouts/BasicLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { RoutesMenu } from "@/components/menu/RoutesMenu";
import IconLogo from "~icons/core/logo?width=5rem&height=2rem";
import { RouterView, useRoute, useRouter } from "vue-router";
import { onMounted, reactive, ref } from "vue";
import { computed, onMounted, reactive, ref } from "vue";
import axios from "axios";
import LoginModal from "@/components/login/LoginModal.vue";
import { coreMenuGroups } from "@console/router/constant";
Expand Down Expand Up @@ -95,6 +95,16 @@ onMounted(() => {
initialize({ target: navbarScroller.value });
}
});

const disallowAccessConsole = computed(() => {
const hasDisallowAccessConsoleRole = currentRoles?.value?.some((role) => {
return (
role.metadata.annotations?.[rbacAnnotations.DISALLOW_ACCESS_CONSOLE] ===
"true"
);
});
return !!hasDisallowAccessConsoleRole;
});
</script>

<template>
Expand Down Expand Up @@ -145,6 +155,7 @@ onMounted(() => {
</div>
<div class="flex items-center gap-1">
<a
v-if="!disallowAccessConsole"
v-tooltip="$t('core.uc_sidebar.operations.console.tooltip')"
class="group inline-block cursor-pointer rounded-full p-1.5 transition-all hover:bg-gray-100"
href="/console"
Expand Down
Loading