Skip to content

Commit

Permalink
update auth
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Sep 29, 2023
1 parent a059c36 commit 391e750
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
"build": "run-p type-check build-only build-style && run-p build-minify",
"build-minify": "uglifyjs dist/servicestack-vue.mjs --compress --mangle -o dist/servicestack-vue.min.mjs",
"build-style": "postcss src/tailwind.css -o dist/styles.css",
"build-copy": "npm run build && npm run copy-src && npm run copy-coffeeshop",
"build-copy": "npm run build && npm run copy-src",
"copy-src": "shx cp dist/servicestack-vue.min.mjs ../ServiceStack/ServiceStack/src/ServiceStack/js/servicestack-vue.mjs && shx cp dist/servicestack-vue.mjs ../ServiceStack/ServiceStack/tests/NorthwindAuto/wwwroot/lib/mjs/",
"copy-diffusion": "shx cp dist/servicestack-vue.m* ../../netcore/VueDiffusion/MyApp/wwwroot/lib/mjs/ && shx cp dist/index.d.ts ../../netcore/VueDiffusion/MyApp/wwwroot/lib/typings/@servicestack/vue/",
"copy-creatorkit": "shx cp dist/servicestack-vue.m* ../../netcore/CreatorKit/CreatorKit/wwwroot/lib/mjs/ && shx cp dist/index.d.ts ../../netcore/CreatorKit/CreatorKit/wwwroot/lib/typings/@servicestack/vue/",
"copy-coffeeshop": "shx cp dist/servicestack-vue.m* ../../netcore/CoffeeShop/CoffeeShop/wwwroot/lib/mjs/ && shx cp dist/index.d.ts ../../netcore/CoffeeShop/CoffeeShop/wwwroot/lib/typings/@servicestack/vue/",
"preview": "vite preview",
"build-only": "vite build -l error",
"type-check": "vue-tsc --noEmit",
Expand Down
26 changes: 19 additions & 7 deletions src/use/auth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { AuthenticateResponse, MetadataOperationType } from "@/types"
import { computed } from "vue"
import { Sole } from "./config"
import { sanitize } from "@servicestack/client"

function toAuth(auth?:AuthenticateResponse) {
return auth && (auth as any).SessionId
? sanitize(auth)
: auth
}

/** Sign In the currently Authenticated User */
function signIn(user:AuthenticateResponse) {
Sole.user.value = user
Sole.user.value = toAuth(user)!
Sole.events.publish('signIn', user)
}

Expand All @@ -14,14 +21,19 @@ function signOut() {
Sole.events.publish('signOut', null)
}

/** @returns {string[]} */
const getRoles = (user:any) => user.roles || []
/** @returns {string[]} */
const getPermissions = (user:any) => user.permissions || []

/** Check if the Authenticated User has a specific role */
function hasRole(role:string) {
return (Sole.user.value?.roles || []).indexOf(role) >= 0
return getRoles(Sole.user.value).indexOf(role) >= 0
}

/** Check if the Authenticated User has a specific permission */
function hasPermission(permission:string) {
return (Sole.user.value?.permissions || []).indexOf(permission) >= 0
return getPermissions(Sole.user.value).indexOf(permission) >= 0
}

/** Check if the Authenticated User has the Admin role */
Expand All @@ -39,7 +51,7 @@ export function canAccess(op?:MetadataOperationType|null) {
return false
if (isAdmin())
return true
let [roles, permissions] = [auth.roles || [], auth.permissions || []]
let [roles, permissions] = [getRoles(auth), getPermissions(auth)]
let [requiredRoles, requiredPermissions, requiresAnyRole, requiresAnyPermission] = [
op.requiredRoles || [], op.requiredPermissions || [], op.requiresAnyRole || [], op.requiresAnyPermission || []]
if (!requiredRoles.every(role => roles.indexOf(role) >= 0))
Expand All @@ -62,8 +74,8 @@ export function invalidAccessMessage(op:MetadataOperationType) {
}
if (isAdmin())
return null;
let [roles, permissions] = [auth.roles || [], auth.permissions || []]
let [requiredRoles, requiredPermissions, requiresAnyRole, requiresAnyPermission] = [
let [roles, permissions] = [getRoles(auth), getPermissions(auth)]
let [requiredRoles, requiredPermissions, requiresAnyRole, requiresAnyPermission] = [
op.requiredRoles || [], op.requiredPermissions || [], op.requiresAnyRole || [], op.requiresAnyPermission || []]
let missingRoles = requiredRoles.filter(x => roles.indexOf(x) < 0)
if (missingRoles.length > 0)
Expand All @@ -87,5 +99,5 @@ export function useAuth() {
/** Check if the current user is Authenticated in a reactive Ref<boolean> */
const isAuthenticated = computed(() => Sole.user.value != null)

return { signIn, signOut, user, isAuthenticated, hasRole, hasPermission, isAdmin, canAccess, invalidAccessMessage }
return { signIn, signOut, user, toAuth, isAuthenticated, hasRole, hasPermission, isAdmin, canAccess, invalidAccessMessage }
}
5 changes: 3 additions & 2 deletions src/use/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,10 @@ export function isValid(metadata:AppMetadata|null|undefined) {
}

/** Get get AppMetadata instance */
export function getMetadata(opt?:{assert?:boolean}):any { // use 'any' to avoid type explosion
if (opt?.assert && !Sole.metadata.value)
export function getMetadata(opt?:{assert?:boolean}):any { // use 'any' to avoid type explosion
if (!tryLoad() && opt?.assert && !Sole.metadata.value)
throw new Error('useMetadata() not configured, see: https://docs.servicestack.net/vue/use-metadata')

return Sole.metadata.value
}

Expand Down

0 comments on commit 391e750

Please sign in to comment.