diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a516dac --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM postgres:latest + +# Installer curl +RUN apt-get update && apt-get install -y curl ruby ruby-dev build-essential +RUN gem install bcrypt + +# Copier le script d'initialisation +COPY initDocusealUsers.sh /docker-entrypoint-initdb.d/initDocusealUsers.sh + +# Donner les permissions d'exécution au script +RUN chmod +x /docker-entrypoint-initdb.d/initDocusealUsers.sh + +# Commande par défaut pour démarrer le conteneur +ENTRYPOINT ["/bin/bash", "/docker-entrypoint-initdb.d/initDocusealUsers.sh"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 770841e..276fde1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,7 +22,7 @@ services: docuseal: image: docuseal/docuseal:latest depends_on: - - database + - database ports: - 3001:3000 environment: @@ -49,17 +49,34 @@ services: - docuseal command: python start.py - # frontend: - # build: - # context: ./Front-End - # args: - # - BUILD_MODE=development - # ports: - # - 3000:3000 - # volumes: - # - ./Front-End:/app - # environment: - # - TZ=Europe/Paris - # depends_on: - # - backend + init_docuseal_users: + build: + context: . + dockerfile: Dockerfile + depends_on: + - docuseal + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + USER_FIRST_NAME: n3wt + USER_LAST_NAME: school + USER_COMPANY: n3wt.innov + USER_EMAIL: n3wt.school@gmail.com + USER_PASSWORD: n3wt1234 + volumes: + - ./initDocusealUsers.sh:/docker-entrypoint-initdb.d/initDocusealUsers.sh + + frontend: + build: + context: ./Front-End + args: + - BUILD_MODE=development + ports: + - 3000:3000 + volumes: + - ./Front-End:/app + environment: + - TZ=Europe/Paris + depends_on: + - backend diff --git a/initDocusealUsers.sh b/initDocusealUsers.sh new file mode 100644 index 0000000..dc82f3a --- /dev/null +++ b/initDocusealUsers.sh @@ -0,0 +1,41 @@ +#!/bin/bash +set -e + +# Attendre que le service docuseal soit prêt +until curl -s http://docuseal:3000 > /dev/null; do + echo "Waiting for docuseal to be ready..." + sleep 5 +done + +encrypt_password() { + local password=$1 + ruby -e "require 'bcrypt'; puts BCrypt::Password.create('$password')" +} + +# Mot de passe encrypté +ENCRYPTED_PASSWORD=$(encrypt_password "$USER_PASSWORD") + +# Insérer les données dans les tables accounts et users si elles n'existent pas déjà +PGPASSWORD=$POSTGRES_PASSWORD psql -v ON_ERROR_STOP=1 --host=database --port=5432 --username="$POSTGRES_USER" --dbname=docuseal <<-EOSQL + -- Vérifier l'existence du compte + DO \$\$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM accounts WHERE name = '$USER_COMPANY') THEN + -- Insérer dans la table accounts + INSERT INTO accounts (name, timezone, locale, created_at, updated_at, uuid) VALUES + ('$USER_COMPANY', 'UTC', 'en', NOW(), NOW(), gen_random_uuid()); + END IF; + END + \$\$; + + -- Vérifier l'existence de l'utilisateur + DO \$\$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM users WHERE email = '$USER_EMAIL') THEN + -- Insérer dans la table users + INSERT INTO users (first_name, last_name, email, encrypted_password, account_id, role, sign_in_count, failed_attempts, created_at, updated_at, uuid, otp_required_for_login) VALUES + ('$USER_FIRST_NAME', '$USER_LAST_NAME', '$USER_EMAIL', '$ENCRYPTED_PASSWORD', (SELECT id FROM accounts WHERE name = '$USER_COMPANY'), 'admin', 1, 0, NOW(), NOW(), gen_random_uuid(), false); + END IF; + END + \$\$; +EOSQL \ No newline at end of file