-- =====================================================================
--  ALTALYS PROTECTION — Securite des connexions
--  Blocage apres plusieurs echecs, codes de depannage, liste des comptes.
--  Import via phpMyAdmin > moso8599_altalys > Importer
-- =====================================================================
SET NAMES utf8mb4;

-- ---------------------------------------------------------------------
-- Journal des tentatives de connexion
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `connexions` (
  `id`      BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `login`   VARCHAR(80) NULL,
  `ip`      VARCHAR(45) NULL,
  `reussie` TINYINT(1) NOT NULL DEFAULT 0,
  `quand`   DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_login` (`login`,`quand`),
  KEY `idx_ip` (`ip`,`quand`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
-- Codes de depannage a usage unique
--   Le mot de passe habituel reste valable : le code ne le remplace pas.
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `codes_depannage` (
  `id`             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `utilisateur_id` BIGINT UNSIGNED NOT NULL,
  `code_hash`      VARCHAR(255) NOT NULL,
  `cree_par`       VARCHAR(120) NULL,
  `cree_le`        DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `expire_le`      DATETIME NOT NULL,
  `utilise_le`     DATETIME NULL,
  PRIMARY KEY (`id`),
  KEY `idx_user` (`utilisateur_id`,`utilise_le`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
-- Demandes d'aide a la connexion (code oublie)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `demandes_acces` (
  `id`        BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `login`     VARCHAR(80) NOT NULL,
  `message`   VARCHAR(255) NULL,
  `ip`        VARCHAR(45) NULL,
  `statut`    ENUM('nouvelle','traitee') NOT NULL DEFAULT 'nouvelle',
  `cree_le`   DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_statut` (`statut`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
-- Ordre d'affichage dans la liste de connexion
-- ---------------------------------------------------------------------
SET @sql := (SELECT IF(COUNT(*) = 0,
  'ALTER TABLE `utilisateurs` ADD COLUMN `ordre_affichage` INT NOT NULL DEFAULT 0',
  'DO 0') FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME='utilisateurs' AND COLUMN_NAME='ordre_affichage');
PREPARE r FROM @sql; EXECUTE r; DEALLOCATE PREPARE r;

-- ---------------------------------------------------------------------
-- Reglages
-- ---------------------------------------------------------------------
INSERT INTO `parametres` (`cle`,`valeur`) VALUES
 ('connexion_liste','1'),
 ('connexion_essais_max','3'),
 ('connexion_blocage_min','15'),
 ('mdp_longueur_min','12'),
 ('depannage_duree_h','2')
ON DUPLICATE KEY UPDATE `cle` = `cle`;
