mirror of
https://github.com/beilunyang/moemail.git
synced 2026-05-11 01:50:34 +08:00
feat: Implement role-based access control and enhance permissions system
This commit is contained in:
25
app/hooks/use-role-permission.ts
Normal file
25
app/hooks/use-role-permission.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
"use client"
|
||||
|
||||
import { useSession } from "next-auth/react"
|
||||
import { Permission, Role, hasPermission } from "@/lib/permissions"
|
||||
|
||||
export function useRolePermission() {
|
||||
const { data: session } = useSession()
|
||||
const roles = session?.user?.roles
|
||||
|
||||
const checkPermission = (permission: Permission) => {
|
||||
if (!roles) return false
|
||||
return hasPermission(roles.map(r => r.name) as Role[], permission)
|
||||
}
|
||||
|
||||
const hasRole = (role: Role) => {
|
||||
if (!roles) return false
|
||||
return roles.some(r => r.name === role)
|
||||
}
|
||||
|
||||
return {
|
||||
checkPermission,
|
||||
hasRole,
|
||||
roles,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user