TT-005: unify platform frontend and permissions
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import type { ReactNode } from 'react'
|
||||
import { Navigate } from 'react-router-dom'
|
||||
import { useAuth } from '../contexts/AuthContext'
|
||||
|
||||
interface PermissionGuardProps {
|
||||
children: ReactNode
|
||||
permission?: string
|
||||
permissions?: string[]
|
||||
requireAll?: boolean
|
||||
fallback?: ReactNode
|
||||
}
|
||||
|
||||
/**
|
||||
* Guard component that checks user permissions before rendering children.
|
||||
*
|
||||
* Usage:
|
||||
* <PermissionGuard permission="platform.users.read">
|
||||
* <UsersPage />
|
||||
* </PermissionGuard>
|
||||
*
|
||||
* <PermissionGuard permissions={["platform.users.read", "platform.users.write"]} requireAll>
|
||||
* <UsersEditPage />
|
||||
* </PermissionGuard>
|
||||
*/
|
||||
export default function PermissionGuard({
|
||||
children,
|
||||
permission,
|
||||
permissions,
|
||||
requireAll = false,
|
||||
fallback,
|
||||
}: PermissionGuardProps) {
|
||||
const { user, loading, hasPermission, hasAnyPermission } = useAuth()
|
||||
|
||||
if (loading) {
|
||||
return <div className="flex items-center justify-center h-screen">Загрузка...</div>
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return <Navigate to="/login" />
|
||||
}
|
||||
|
||||
// Check single permission
|
||||
if (permission && !hasPermission(permission)) {
|
||||
if (fallback) return <>{fallback}</>
|
||||
return (
|
||||
<div className="flex items-center justify-center h-full">
|
||||
<div className="text-center">
|
||||
<h2 className="text-2xl font-bold text-gray-900 mb-2">403</h2>
|
||||
<p className="text-gray-500">Нет прав доступа</p>
|
||||
<p className="text-sm text-gray-400 mt-1">Требуется: {permission}</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// Check multiple permissions
|
||||
if (permissions && permissions.length > 0) {
|
||||
const hasAccess = requireAll
|
||||
? permissions.every(p => hasPermission(p))
|
||||
: hasAnyPermission(permissions)
|
||||
|
||||
if (!hasAccess) {
|
||||
if (fallback) return <>{fallback}</>
|
||||
return (
|
||||
<div className="flex items-center justify-center h-full">
|
||||
<div className="text-center">
|
||||
<h2 className="text-2xl font-bold text-gray-900 mb-2">403</h2>
|
||||
<p className="text-gray-500">Нет прав доступа</p>
|
||||
<p className="text-sm text-gray-400 mt-1">
|
||||
Требуется: {permissions.join(requireAll ? ' + ' : ' или ')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return <>{children}</>
|
||||
}
|
||||
Reference in New Issue
Block a user