This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CASL to frontend, conditional insufficient permissions pages
- Loading branch information
Showing
8 changed files
with
187 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Ability } from "@casl/ability"; | ||
import type { AbilityClass } from "@casl/ability"; | ||
import { ABILITY_TOKEN, useAbility } from "@casl/vue"; | ||
import type { InjectionKey, Ref } from "vue"; | ||
import { computed } from "vue"; | ||
|
||
export type AbilityActions = "read" | "create" | "update" | "delete" | "manage"; | ||
export type AbilitySubjects = | ||
| "Production" | ||
| "User" | ||
| "Role" | ||
| "ContactSubmission"; | ||
export type GlimpseAbility = Ability<[AbilityActions, AbilitySubjects]>; | ||
|
||
export const GlimpseAbility = Ability as AbilityClass<GlimpseAbility>; | ||
export const TOKEN = ABILITY_TOKEN as InjectionKey<GlimpseAbility>; | ||
export const ability = new GlimpseAbility(); | ||
|
||
export function useGlimpseAbility() { | ||
return useAbility<GlimpseAbility>(); | ||
} | ||
|
||
export function requirePermission( | ||
action: AbilityActions, | ||
subject: AbilitySubjects, | ||
field?: string | ||
): () => Ref<boolean> { | ||
return () => { | ||
return computed(() => { | ||
return ability.can(action, subject, field); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<template> | ||
<div> | ||
<StaticBackground /> | ||
<n-result | ||
status="403" | ||
title="Insufficient Permissions" | ||
description="You do not have permission to access this page." | ||
size="huge" | ||
> | ||
<template #icon> | ||
<FontAwesomeIcon icon="fa-duotone fa-do-not-enter" class="icon" /> | ||
</template> | ||
<template #footer> | ||
<div class="buttons-row"> | ||
<a @click="$router.back()"> | ||
<n-button>Go Back</n-button> | ||
</a> | ||
<RouterLink to="/"> | ||
<n-button>Go Home</n-button> | ||
</RouterLink> | ||
<LoginLogoutPopupButton v-if="!authStore.isLoggedIn"> | ||
<n-button>Login</n-button> | ||
</LoginLogoutPopupButton> | ||
</div> | ||
</template> | ||
</n-result> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import StaticBackground from "@/components/StaticBackground.vue"; | ||
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome"; | ||
import {NResult, NButton} from "naive-ui"; | ||
import {useAuthStore} from "@/stores/auth"; | ||
import LoginLogoutPopupButton from "@/components/LoginLogoutPopupButton.vue"; | ||
const authStore = useAuthStore(); | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
.icon { | ||
--fa-primary-color: #ff6363; | ||
--fa-secondary-color: #f4fbff; | ||
--fa-secondary-opacity: 1.0; | ||
font-size: 10em; | ||
} | ||
div { | ||
padding-top: 5vw; | ||
} | ||
a { | ||
text-decoration: none; | ||
} | ||
.buttons-row { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
* { | ||
margin-right: 5px; | ||
} | ||
padding-bottom: 15px; | ||
} | ||
</style> |