2023-11-27 14:10:09 +00:00
|
|
|
import { rbacAnnotations } from "@/constants/annotations";
|
2022-11-23 08:39:29 +00:00
|
|
|
import { useUserStore } from "@/stores/user";
|
2022-10-11 09:00:14 +00:00
|
|
|
import type { Router } from "vue-router";
|
|
|
|
|
2024-03-27 13:14:06 +00:00
|
|
|
const whiteList = ["Setup", "Login", "Binding", "ResetPassword", "Redirect"];
|
2023-03-30 09:44:15 +00:00
|
|
|
|
2022-10-11 09:00:14 +00:00
|
|
|
export function setupAuthCheckGuard(router: Router) {
|
|
|
|
router.beforeEach((to, from, next) => {
|
2022-11-23 08:39:29 +00:00
|
|
|
const userStore = useUserStore();
|
|
|
|
|
2023-03-23 14:52:34 +00:00
|
|
|
if (userStore.isAnonymous) {
|
2023-05-31 06:55:01 +00:00
|
|
|
if (whiteList.includes(to.name as string)) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-25 14:30:18 +00:00
|
|
|
next({
|
|
|
|
name: "Login",
|
|
|
|
query: {
|
2023-08-15 09:00:13 +00:00
|
|
|
redirect_uri: encodeURIComponent(window.location.href),
|
2023-05-25 14:30:18 +00:00
|
|
|
},
|
|
|
|
});
|
2022-10-11 09:00:14 +00:00
|
|
|
return;
|
2023-05-31 06:55:01 +00:00
|
|
|
} else {
|
|
|
|
if (to.name === "Login") {
|
|
|
|
if (to.query.redirect_uri) {
|
|
|
|
next({
|
|
|
|
name: "Redirect",
|
|
|
|
query: {
|
|
|
|
redirect_uri: to.query.redirect_uri,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2023-11-27 14:10:09 +00:00
|
|
|
|
|
|
|
const roleHasRedirectOnLogin = userStore.currentRoles?.find(
|
|
|
|
(role) =>
|
|
|
|
role.metadata.annotations?.[rbacAnnotations.REDIRECT_ON_LOGIN]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (roleHasRedirectOnLogin) {
|
|
|
|
window.location.href =
|
|
|
|
roleHasRedirectOnLogin.metadata.annotations?.[
|
|
|
|
rbacAnnotations.REDIRECT_ON_LOGIN
|
2023-12-01 02:38:09 +00:00
|
|
|
] || "/uc";
|
2023-11-27 14:10:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next({
|
|
|
|
name: "Dashboard",
|
|
|
|
});
|
|
|
|
return;
|
2023-05-31 06:55:01 +00:00
|
|
|
}
|
2023-12-01 02:38:09 +00:00
|
|
|
|
2024-03-27 13:14:06 +00:00
|
|
|
if (to.name && whiteList.includes(to.name as string)) {
|
2023-12-01 02:38:09 +00:00
|
|
|
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;
|
2022-10-11 09:00:14 +00:00
|
|
|
}
|
2023-05-31 06:55:01 +00:00
|
|
|
|
2022-10-11 09:00:14 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|