# 🛠️ Accès au Panel Admin - Web Sentinel

## 📍 Comment accéder au panel admin

### En local (développement)

1. **Démarrer le backend admin:**
   ```bash
   cd admin/backend
   uvicorn app.main:app --reload --port 8000
   ```

2. **Démarrer le frontend admin:**
   ```bash
   cd admin/frontend
   npm install  # Première fois seulement
   npm run dev
   ```

3. **Accéder au panel:**
   - URL: **http://localhost:5173**
   - Le frontend se connecte automatiquement au backend (port 8000)

### Via le site (comptes SYSOP uniquement)

Si vous avez un compte SYSOP :

1. **Se connecter sur:** https://web-sentinel.taaazzz-prog.fr/account.html
2. **Un bouton "🛠️ Panel Admin"** apparaît automatiquement à côté de "Se déconnecter"
3. **Cliquer dessus** pour ouvrir le panel dans un nouvel onglet

⚠️ **Note:** Le bouton pointe vers `http://localhost:5173`, donc vous devez avoir le frontend qui tourne en local.

---

## 🔧 Technologies du Panel Admin

- **Backend:** FastAPI (Python) sur port 8000
- **Frontend:** React + TypeScript + Vite sur port 5173
- **Base de données:** PostgreSQL (via `WEB_SENTINEL_POSTGRES_URL`)
- **API:** http://localhost:8000/api/

---

## 📊 Fonctionnalités disponibles

### Gestion des licences
- ✅ Créer/Éditer/Supprimer des licences
- ✅ Voir toutes les API keys
- ✅ Gérer les tiers (FREE, PRO, ENTERPRISE, SYSOP)
- ✅ Configurer les quotas SAST par licence
- ✅ Voir les statistiques d'utilisation

### Gestion des clients
- ✅ Créer des comptes clients
- ✅ Gérer les abonnements Stripe
- ✅ Voir l'historique de paiement
- ✅ Activer/Désactiver des comptes

### Quotas SAST (nouveau)
- ✅ `allow_source_scan`: Autorisation scan de code
- ✅ `max_source_files`: Nombre max de fichiers par scan
- ✅ `max_source_size_mb`: Taille max totale en MB
- ✅ `advanced_rules`: Règles avancées activées

---

## 🚀 Configuration requise

### Variables d'environnement (.env.local)

```bash
# PostgreSQL local
WEB_SENTINEL_POSTGRES_URL=postgresql://websentinel_user:WebSentinelDB2025!@localhost:5433/websentinel_prod
DATABASE_URL=postgresql://websentinel_user:WebSentinelDB2025!@localhost:5433/websentinel_prod

# Backend admin
WEB_SENTINEL_LICENSE_BACKEND=postgresql
WEB_SENTINEL_ADMIN_JWT_SECRET=your-secret-key

# Stripe (TEST pour dev local)
STRIPE_SECRET_KEY=sk_test_...
STRIPE_PUBLISHABLE_KEY=pk_test_...
```

### Démarrage PostgreSQL local

```bash
# Lancer PostgreSQL + PgAdmin
docker-compose -f docker-compose.local.yml up -d

# Vérifier que la base est prête
docker ps | grep web-sentinel-db-local
```

---

## 🐛 Troubleshooting

### Le bouton "Panel Admin" n'apparaît pas

**Cause:** Votre compte n'est pas SYSOP

**Solution:**
```sql
-- Via PgAdmin (http://localhost:5050) ou psql
UPDATE users SET tier = 'sysop' WHERE email = 'votre@email.com';
-- OU dans api_key si vous utilisez cette table
UPDATE api_key SET tier = 'SYSOP' WHERE email = 'votre@email.com';
```

### Le panel admin ne charge pas (localhost:5173)

**Cause:** Le frontend n'est pas démarré

**Solution:**
```bash
cd admin/frontend
npm run dev
```

### Erreur "Cannot connect to database"

**Cause:** PostgreSQL local pas démarré ou mauvaise URL

**Solutions:**
```bash
# 1. Démarrer PostgreSQL
docker-compose -f docker-compose.local.yml up -d

# 2. Vérifier l'URL dans .env.local
echo $WEB_SENTINEL_POSTGRES_URL

# 3. Tester la connexion
docker exec web-sentinel-db-local psql -U websentinel_user -d websentinel_prod -c "\dt"
```

### Le backend retourne 401 Unauthorized

**Cause:** JWT secret manquant ou invalide

**Solution:**
```bash
# Dans .env.local
export WEB_SENTINEL_ADMIN_JWT_SECRET="mon-secret-ultra-securise-2025"

# Redémarrer le backend
cd admin/backend
uvicorn app.main:app --reload
```

---

## 📱 Captures d'écran

### Bouton sur account.html (SYSOP)
```
┌─────────────────────────────────────────┐
│ 👤 Bienvenue, Admin                     │
│                                         │
│ Email: admin@example.com                │
│ Type de compte: 🔧 SYSOP (Admin)       │
│                                         │
│ [Se déconnecter] [🛠️ Panel Admin]     │
└─────────────────────────────────────────┘
```

### Panel Admin (localhost:5173)
```
┌─────────────────────────────────────────┐
│ Web Sentinel Admin                      │
├─────────────────────────────────────────┤
│ 📊 Licenses (27)                        │
│ 👥 Clients (11)                         │
│ 💳 Paiements                            │
│ 📈 Statistiques                         │
└─────────────────────────────────────────┘
```

---

## 🔐 Sécurité

⚠️ **IMPORTANT:**
- Le panel admin est **uniquement accessible en local** (localhost:5173)
- **Jamais exposé publiquement** sur internet
- **Réservé aux comptes SYSOP** uniquement
- Utilise JWT pour l'authentification backend

Pour la production, il faudrait:
1. Déployer le frontend admin sur un sous-domaine privé (ex: admin.web-sentinel.internal)
2. Mettre en place un VPN ou whitelist IP
3. Activer l'authentification OAuth2/OpenID Connect
4. Configurer HTTPS avec certificat

---

**Auteur:** Équipe Web Sentinel  
**Date:** 7 novembre 2025  
**Version:** 1.0
