feat: implement v9c feature enforcement system
Backend: - Add feature access checks to insights, export, import endpoints - Enforce ai_calls, ai_pipeline, data_export, csv_import limits - Return HTTP 403 (disabled) or 429 (limit exceeded) Frontend: - Create useFeatureAccess hook for feature checking - Create FeatureGate/FeatureBadge components - Gate KI-Analysen in Analysis page - Gate Export/Import in Settings page - Show usage counters (e.g. "3/10") Docs: - Update CLAUDE.md with implementation status Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
0210844522
commit
3745ebd6cd
12 files changed
+7176
-158
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import { useFeatureAccess } from '../hooks/useFeatureAccess'
|
||||
|
||||
/**
|
||||
* Feature Gate Component
|
||||
*
|
||||
* Hides children if feature is not available.
|
||||
* Optionally shows upgrade prompt or message.
|
||||
*
|
||||
* @param {string} feature - Feature slug (e.g. 'ai_calls', 'data_export')
|
||||
* @param {ReactNode} children - Content to gate
|
||||
* @param {ReactNode} fallback - Optional fallback when not allowed
|
||||
* @param {boolean} showUpgradePrompt - Show upgrade message instead of hiding
|
||||
*/
|
||||
export function FeatureGate({ feature, children, fallback = null, showUpgradePrompt = false }) {
|
||||
const { canUse, reason, loading, limit, remaining } = useFeatureAccess(feature)
|
||||
|
||||
// While loading, show children optimistically (better UX)
|
||||
if (loading) return <>{children}</>
|
||||
|
||||
// If allowed, render children
|
||||
if (canUse) return <>{children}</>
|
||||
|
||||
// Not allowed
|
||||
if (showUpgradePrompt) {
|
||||
return (
|
||||
<div style={{
|
||||
padding: 16,
|
||||
background: 'var(--accent-light)',
|
||||
borderRadius: 8,
|
||||
textAlign: 'center',
|
||||
color: 'var(--accent-dark)'
|
||||
}}>
|
||||
<div style={{ fontSize: 14, fontWeight: 600, marginBottom: 4 }}>
|
||||
Feature nicht verfügbar
|
||||
</div>
|
||||
<div style={{ fontSize: 12 }}>
|
||||
{reason === 'feature_disabled' && 'Dieses Feature ist in deinem Tier nicht enthalten.'}
|
||||
{reason === 'limit_exceeded' && `Limit erreicht (${limit}). Upgrade für mehr Zugriff.`}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// Hide completely
|
||||
return fallback
|
||||
}
|
||||
|
||||
/**
|
||||
* Feature Badge - Shows usage counter
|
||||
*
|
||||
* @param {string} feature - Feature slug
|
||||
* @param {string} label - Label text (optional)
|
||||
*/
|
||||
export function FeatureBadge({ feature, label }) {
|
||||
const { limit, used, remaining, loading } = useFeatureAccess(feature)
|
||||
|
||||
if (loading) return null
|
||||
if (limit === null) return null // Unlimited
|
||||
|
||||
const percentage = limit > 0 ? (used / limit) * 100 : 0
|
||||
const color = percentage > 90 ? 'var(--danger)' : percentage > 70 ? '#FFA726' : 'var(--accent)'
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
display: 'inline-flex',
|
||||
alignItems: 'center',
|
||||
gap: 6,
|
||||
padding: '4px 10px',
|
||||
background: 'var(--surface2)',
|
||||
borderRadius: 12,
|
||||
fontSize: 11,
|
||||
fontWeight: 600,
|
||||
color: 'var(--text3)'
|
||||
}}>
|
||||
{label && <span>{label}:</span>}
|
||||
<span style={{ color }}>{used}/{limit}</span>
|
||||
{remaining !== null && remaining === 0 && <span style={{ color: 'var(--danger)' }}>⚠️</span>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { api } from '../utils/api'
|
||||
|
||||
/**
|
||||
* Hook to check feature access and usage limits
|
||||
*
|
||||
* @param {string} featureSlug - Feature ID (e.g. 'ai_calls', 'data_export')
|
||||
* @returns {{
|
||||
* canUse: boolean,
|
||||
* limit: number|null,
|
||||
* used: number,
|
||||
* remaining: number|null,
|
||||
* reason: string,
|
||||
* loading: boolean,
|
||||
* error: string,
|
||||
* refresh: function
|
||||
* }}
|
||||
*/
|
||||
export function useFeatureAccess(featureSlug) {
|
||||
const [state, setState] = useState({
|
||||
canUse: true, // Optimistic default
|
||||
limit: null,
|
||||
used: 0,
|
||||
remaining: null,
|
||||
reason: 'unknown',
|
||||
loading: true,
|
||||
error: ''
|
||||
})
|
||||
|
||||
const refresh = async () => {
|
||||
if (!featureSlug) {
|
||||
setState(prev => ({ ...prev, loading: false }))
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
setState(prev => ({ ...prev, loading: true, error: '' }))
|
||||
const access = await api.checkFeatureAccess(featureSlug)
|
||||
setState({
|
||||
canUse: access.allowed,
|
||||
limit: access.limit,
|
||||
used: access.used,
|
||||
remaining: access.remaining,
|
||||
reason: access.reason,
|
||||
loading: false,
|
||||
error: ''
|
||||
})
|
||||
} catch (e) {
|
||||
console.error(`Feature access check failed for ${featureSlug}:`, e)
|
||||
setState({
|
||||
canUse: false,
|
||||
limit: null,
|
||||
used: 0,
|
||||
remaining: null,
|
||||
reason: 'error',
|
||||
loading: false,
|
||||
error: e.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
refresh()
|
||||
}, [featureSlug])
|
||||
|
||||
return { ...state, refresh }
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import { useState, useEffect } from 'react'
|
||||
import { Brain, Pencil, Trash2, ChevronDown, ChevronUp, Check, X } from 'lucide-react'
|
||||
import { api } from '../utils/api'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import { FeatureGate, FeatureBadge } from '../components/FeatureGate'
|
||||
import { useFeatureAccess } from '../hooks/useFeatureAccess'
|
||||
import Markdown from '../utils/Markdown'
|
||||
import dayjs from 'dayjs'
|
||||
import 'dayjs/locale/de'
|
||||
@@ -227,35 +229,39 @@ export default function Analysis() {
|
||||
|
||||
{/* Pipeline button - only if all sub-prompts are active */}
|
||||
{pipelineAvailable && (
|
||||
<div className="card" style={{marginBottom:16,borderColor:'var(--accent)',borderWidth:2}}>
|
||||
<div style={{display:'flex',alignItems:'flex-start',gap:12}}>
|
||||
<div style={{flex:1}}>
|
||||
<div style={{fontWeight:700,fontSize:15,color:'var(--accent)'}}>🔬 Mehrstufige Gesamtanalyse</div>
|
||||
<div style={{fontSize:12,color:'var(--text2)',marginTop:3,lineHeight:1.5}}>
|
||||
3 spezialisierte KI-Calls parallel (Körper + Ernährung + Aktivität),
|
||||
dann Synthese + Zielabgleich. Detaillierteste Auswertung.
|
||||
</div>
|
||||
{allInsights.find(i=>i.scope==='pipeline') && (
|
||||
<div style={{fontSize:11,color:'var(--text3)',marginTop:3}}>
|
||||
Letzte Analyse: {dayjs(allInsights.find(i=>i.scope==='pipeline').created).format('DD.MM.YYYY, HH:mm')}
|
||||
<FeatureGate feature="ai_pipeline" showUpgradePrompt>
|
||||
<div className="card" style={{marginBottom:16,borderColor:'var(--accent)',borderWidth:2}}>
|
||||
<div style={{display:'flex',alignItems:'flex-start',gap:12}}>
|
||||
<div style={{flex:1}}>
|
||||
<div style={{display:'flex',alignItems:'center',gap:8}}>
|
||||
<div style={{fontWeight:700,fontSize:15,color:'var(--accent)'}}>🔬 Mehrstufige Gesamtanalyse</div>
|
||||
<FeatureBadge feature="ai_pipeline" />
|
||||
</div>
|
||||
)}
|
||||
<div style={{fontSize:12,color:'var(--text2)',marginTop:3,lineHeight:1.5}}>
|
||||
3 spezialisierte KI-Calls parallel (Körper + Ernährung + Aktivität),
|
||||
dann Synthese + Zielabgleich. Detaillierteste Auswertung.
|
||||
</div>
|
||||
{allInsights.find(i=>i.scope==='pipeline') && (
|
||||
<div style={{fontSize:11,color:'var(--text3)',marginTop:3}}>
|
||||
Letzte Analyse: {dayjs(allInsights.find(i=>i.scope==='pipeline').created).format('DD.MM.YYYY, HH:mm')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<button className="btn btn-primary" style={{flexShrink:0,minWidth:100}}
|
||||
onClick={runPipeline} disabled={!!loading||pipelineLoading}>
|
||||
{pipelineLoading
|
||||
? <><div className="spinner" style={{width:13,height:13}}/> Läuft…</>
|
||||
: <><Brain size={13}/> Starten</>}
|
||||
</button>
|
||||
</div>
|
||||
<button className="btn btn-primary" style={{flexShrink:0,minWidth:100}}
|
||||
onClick={runPipeline} disabled={!!loading||pipelineLoading}>
|
||||
{pipelineLoading
|
||||
? <><div className="spinner" style={{width:13,height:13}}/> Läuft…</>
|
||||
: <><Brain size={13}/> Starten</>}
|
||||
</button>
|
||||
{!canUseAI && <div style={{fontSize:11,color:'#D85A30',marginTop:4}}>🔒 KI nicht freigeschaltet</div>}
|
||||
{pipelineLoading && (
|
||||
<div style={{marginTop:10,padding:'8px 12px',background:'var(--accent-light)',
|
||||
borderRadius:8,fontSize:12,color:'var(--accent-dark)'}}>
|
||||
⚡ Stufe 1: 3 parallele Analyse-Calls… dann Synthese… dann Zielabgleich
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{pipelineLoading && (
|
||||
<div style={{marginTop:10,padding:'8px 12px',background:'var(--accent-light)',
|
||||
borderRadius:8,fontSize:12,color:'var(--accent-dark)'}}>
|
||||
⚡ Stufe 1: 3 parallele Analyse-Calls… dann Synthese… dann Zielabgleich
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</FeatureGate>
|
||||
)}
|
||||
|
||||
{!canUseAI && (
|
||||
@@ -271,11 +277,13 @@ export default function Analysis() {
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{canUseAI && <p style={{fontSize:13,color:'var(--text2)',marginBottom:14,lineHeight:1.6}}>
|
||||
Oder wähle eine Einzelanalyse:
|
||||
</p>}
|
||||
<FeatureGate feature="ai_calls">
|
||||
<p style={{fontSize:13,color:'var(--text2)',marginBottom:14,lineHeight:1.6,display:'flex',alignItems:'center',gap:8}}>
|
||||
Oder wähle eine Einzelanalyse:
|
||||
<FeatureBadge feature="ai_calls" />
|
||||
</p>
|
||||
|
||||
{activePrompts.map(p => {
|
||||
{activePrompts.map(p => {
|
||||
// Show latest existing insight for this prompt
|
||||
const existing = allInsights.find(i=>i.scope===p.slug)
|
||||
return (
|
||||
@@ -305,10 +313,11 @@ export default function Analysis() {
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{activePrompts.length===0 && (
|
||||
<div className="empty-state"><p>Keine aktiven Prompts. Aktiviere im Tab "Prompts".</p></div>
|
||||
)}
|
||||
})}
|
||||
{activePrompts.length===0 && (
|
||||
<div className="empty-state"><p>Keine aktiven Prompts. Aktiviere im Tab "Prompts".</p></div>
|
||||
)}
|
||||
</FeatureGate>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Link } from 'react-router-dom'
|
||||
import { Save, Download, Upload, Trash2, Plus, Check, Pencil, X, LogOut, Shield, Key } from 'lucide-react'
|
||||
import { useProfile } from '../context/ProfileContext'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
import { FeatureGate, FeatureBadge } from '../components/FeatureGate'
|
||||
import { Avatar } from './ProfileSelect'
|
||||
import { api } from '../utils/api'
|
||||
import AdminPanel from './AdminPanel'
|
||||
@@ -354,20 +355,17 @@ export default function SettingsPage() {
|
||||
)}
|
||||
|
||||
{/* Export */}
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Daten exportieren</div>
|
||||
<p style={{fontSize:13,color:'var(--text2)',marginBottom:12,lineHeight:1.6}}>
|
||||
Exportiert alle Daten von <strong>{activeProfile?.name}</strong>:
|
||||
Gewicht, Umfänge, Caliper, Ernährung, Aktivität und KI-Auswertungen.
|
||||
</p>
|
||||
<div style={{display:'flex',flexDirection:'column',gap:8}}>
|
||||
{!canExport && (
|
||||
<div style={{padding:'10px 12px',background:'#FCEBEB',borderRadius:8,
|
||||
fontSize:13,color:'#D85A30',marginBottom:8}}>
|
||||
🔒 Export ist für dein Profil nicht freigeschaltet. Bitte den Admin kontaktieren.
|
||||
</div>
|
||||
)}
|
||||
{canExport && <>
|
||||
<FeatureGate feature="data_export" showUpgradePrompt>
|
||||
<div className="card section-gap">
|
||||
<div className="card-title" style={{display:'flex',alignItems:'center',gap:8}}>
|
||||
Daten exportieren
|
||||
<FeatureBadge feature="data_export" label="Exporte" />
|
||||
</div>
|
||||
<p style={{fontSize:13,color:'var(--text2)',marginBottom:12,lineHeight:1.6}}>
|
||||
Exportiert alle Daten von <strong>{activeProfile?.name}</strong>:
|
||||
Gewicht, Umfänge, Caliper, Ernährung, Aktivität und KI-Auswertungen.
|
||||
</p>
|
||||
<div style={{display:'flex',flexDirection:'column',gap:8}}>
|
||||
<button className="btn btn-primary btn-full"
|
||||
onClick={()=>api.exportZip()}>
|
||||
<Download size={14}/> ZIP exportieren
|
||||
@@ -378,61 +376,56 @@ export default function SettingsPage() {
|
||||
<Download size={14}/> JSON exportieren
|
||||
<span style={{fontSize:11,opacity:0.8,marginLeft:6}}>— maschinenlesbar, alles in einer Datei</span>
|
||||
</button>
|
||||
</>}
|
||||
</div>
|
||||
<p style={{fontSize:11,color:'var(--text3)',marginTop:8}}>
|
||||
Der ZIP-Export enthält separate Dateien für Excel und eine lesbare KI-Auswertungsdatei.
|
||||
</p>
|
||||
</div>
|
||||
<p style={{fontSize:11,color:'var(--text3)',marginTop:8}}>
|
||||
Der ZIP-Export enthält separate Dateien für Excel und eine lesbare KI-Auswertungsdatei.
|
||||
</p>
|
||||
</div>
|
||||
</FeatureGate>
|
||||
|
||||
{/* Import */}
|
||||
<div className="card section-gap">
|
||||
<div className="card-title">Backup importieren</div>
|
||||
<p style={{fontSize:13,color:'var(--text2)',marginBottom:12,lineHeight:1.6}}>
|
||||
Importiere einen ZIP-Export zurück in <strong>{activeProfile?.name}</strong>.
|
||||
Vorhandene Einträge werden nicht überschrieben.
|
||||
</p>
|
||||
<div style={{display:'flex',flexDirection:'column',gap:8}}>
|
||||
{!canExport && (
|
||||
<div style={{padding:'10px 12px',background:'#FCEBEB',borderRadius:8,
|
||||
fontSize:13,color:'#D85A30',marginBottom:8}}>
|
||||
🔒 Import ist für dein Profil nicht freigeschaltet. Bitte den Admin kontaktieren.
|
||||
</div>
|
||||
)}
|
||||
{canExport && (
|
||||
<>
|
||||
<label className="btn btn-primary btn-full"
|
||||
style={{cursor:importing?'wait':'pointer',opacity:importing?0.6:1}}>
|
||||
<input type="file" accept=".zip" onChange={handleImport}
|
||||
disabled={importing}
|
||||
style={{display:'none'}}/>
|
||||
{importing ? (
|
||||
<>Importiere...</>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={14}/> ZIP-Backup importieren
|
||||
</>
|
||||
)}
|
||||
</label>
|
||||
{importMsg && (
|
||||
<div style={{
|
||||
padding:'10px 12px',
|
||||
background: importMsg.type === 'success' ? '#E1F5EE' : '#FCEBEB',
|
||||
borderRadius:8,
|
||||
fontSize:12,
|
||||
color: importMsg.type === 'success' ? 'var(--accent)' : '#D85A30',
|
||||
lineHeight:1.4
|
||||
}}>
|
||||
{importMsg.text}
|
||||
</div>
|
||||
<FeatureGate feature="csv_import" showUpgradePrompt>
|
||||
<div className="card section-gap">
|
||||
<div className="card-title" style={{display:'flex',alignItems:'center',gap:8}}>
|
||||
Backup importieren
|
||||
<FeatureBadge feature="csv_import" label="Imports" />
|
||||
</div>
|
||||
<p style={{fontSize:13,color:'var(--text2)',marginBottom:12,lineHeight:1.6}}>
|
||||
Importiere einen ZIP-Export zurück in <strong>{activeProfile?.name}</strong>.
|
||||
Vorhandene Einträge werden nicht überschrieben.
|
||||
</p>
|
||||
<div style={{display:'flex',flexDirection:'column',gap:8}}>
|
||||
<label className="btn btn-primary btn-full"
|
||||
style={{cursor:importing?'wait':'pointer',opacity:importing?0.6:1}}>
|
||||
<input type="file" accept=".zip" onChange={handleImport}
|
||||
disabled={importing}
|
||||
style={{display:'none'}}/>
|
||||
{importing ? (
|
||||
<>Importiere...</>
|
||||
) : (
|
||||
<>
|
||||
<Upload size={14}/> ZIP-Backup importieren
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</label>
|
||||
{importMsg && (
|
||||
<div style={{
|
||||
padding:'10px 12px',
|
||||
background: importMsg.type === 'success' ? '#E1F5EE' : '#FCEBEB',
|
||||
borderRadius:8,
|
||||
fontSize:12,
|
||||
color: importMsg.type === 'success' ? 'var(--accent)' : '#D85A30',
|
||||
lineHeight:1.4
|
||||
}}>
|
||||
{importMsg.text}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<p style={{fontSize:11,color:'var(--text3)',marginTop:8}}>
|
||||
Der Import erkennt automatisch das Format und importiert nur neue Einträge.
|
||||
</p>
|
||||
</div>
|
||||
<p style={{fontSize:11,color:'var(--text3)',marginTop:8}}>
|
||||
Der Import erkennt automatisch das Format und importiert nur neue Einträge.
|
||||
</p>
|
||||
</div>
|
||||
</FeatureGate>
|
||||
|
||||
{saved && (
|
||||
<div style={{position:'fixed',bottom:80,left:'50%',transform:'translateX(-50%)',
|
||||
|
||||
@@ -180,4 +180,7 @@ export const api = {
|
||||
createAccessGrant: (d) => req('/access-grants',json(d)),
|
||||
updateAccessGrant: (id,d) => req(`/access-grants/${id}`,jput(d)),
|
||||
revokeAccessGrant: (id) => req(`/access-grants/${id}`,{method:'DELETE'}),
|
||||
|
||||
// Feature Access (v9c)
|
||||
checkFeatureAccess: (featureSlug) => req(`/features/${featureSlug}/check-access`),
|
||||
}
|
||||
Reference in new issue
Block a user