From 18aff0bf3d7a80bc3f80bf3fe12f1a088f9315b2 Mon Sep 17 00:00:00 2001 From: ylambda Date: Thu, 13 Jun 2024 08:19:15 -0400 Subject: [PATCH] fix(docker): REON account creation script and minor documentation --- Dockerfile | 7 ++++-- Makefile | 10 ++++----- README.md | 16 ++++++++++++++ config.example.json | 6 +++--- docker-compose.yml | 7 +++--- web/classes/UserUtil.php | 22 +++++++++++++------ web/scripts/add_user.php | 46 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 web/scripts/add_user.php diff --git a/Dockerfile b/Dockerfile index 067fffb..428b0f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ +# Web Service FROM php:8.3.2-fpm as web - RUN apt-get update \ && apt-get install -y libzip-dev sendmail \ && docker-php-ext-install zip \ @@ -15,8 +15,11 @@ RUN sed -i '/#!\/bin\/sh/aecho "$(hostname -i)\t$(hostname) $(hostname).localhos COPY --from=composer/composer:latest-bin /composer /usr/local/bin/composer COPY web /var/www/reon/web -RUN cd /var/www/reon/web && composer install && chown -R www-data:www-data /var/www/reon; +WORKDIR /var/www/reon/web +RUN composer install && chown -R www-data:www-data /var/www/reon; + +# Mail Service FROM node:22.2.0 as mail COPY mail /home/node/app RUN cd /home/node/app && npm install diff --git a/Makefile b/Makefile index 684ab16..a4bc71d 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,8 @@ - -run: - @docker compose up --detach --build - @echo "Web server available at http://localhost:7400" +all: + @docker compose up --detach clean: @docker compose down -account: run - @docker compose exec web php < \ No newline at end of file +account: all + @docker compose exec -w /var/www/reon/web/scripts web php add_user.php \ No newline at end of file diff --git a/README.md b/README.md index ff1c23e..796b187 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,22 @@ This repository holds various folders for the service, and each has its own READ 3. Run tables.sql in MySQL 4. Continue these steps later once the production server is setup. +# Running Locally + +A `docker-compose.yml` is provided which will run all the required services. To get started, be sure to have docker available on your system. + +1. Copy `config.example.json` to `config.json` +2. Run `make` to start the services +3. Run `make account` to create an account without having to configure email delivery + +### `make all` - Run all the services +Checkout http://localhost/ to see if it worked. + +### `make clean` - Tear down all services + +### `make account` - Create a REON user acccount +Avoids having to setup email properly + # License This code is licenced under MIT. diff --git a/config.example.json b/config.example.json index 895e46c..e2ee0e3 100644 --- a/config.example.json +++ b/config.example.json @@ -2,9 +2,9 @@ "hostname": "example.net", "email_domain": "example.net", "email_domain_dion": "xxxx.dion.ne.jp", - "mysql_host": "localhost", - "mysql_user": "USER", - "mysql_password": "PASS", + "mysql_host": "db", + "mysql_user": "reon", + "mysql_password": "password", "mysql_database": "db", "amoj_regist": "h200" } \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 014f939..1cd5d0e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,10 +15,10 @@ services: - ./examples/nginx/default.conf.template:/etc/nginx/templates/default.conf.template - ./web/:/var/www/reon/web ports: - - 7400:80 + - 80:80 environment: - NGINX_PORT=80 - - NGINX_HOST="localhost gameboy.datacenter.ne.jp mgb.dion.ne.jp" + - NGINX_HOST="localhost gameboy.datacenter.ne.jp" # Hostname and port for fastcgi_pass directive - WEB_URL=web:9000 @@ -37,10 +37,11 @@ services: ports: - 3306:3306 volumes: + # Run tables.sql on startup - ./tables.sql:/docker-entrypoint-initdb.d/tables.sql:ro - type: volume source: reon_db - target: /var/lib/postgresql/data + target: /var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=password - MYSQL_USER=reon diff --git a/web/classes/UserUtil.php b/web/classes/UserUtil.php index fa85d5c..57d3c57 100644 --- a/web/classes/UserUtil.php +++ b/web/classes/UserUtil.php @@ -264,6 +264,20 @@ public function verifySignupRequest($id, $key) { public function completeSignupAction($id, $key, $reonEmail, $password, $passwordConfirm) { $email = self::$instance->verifySignupRequest($id, $key); + + $result = self::$instance->createUser($email, $reonEmail, $password, $passwodConfirm); + if ($result > 0) { + return $result; + } + + $stmt = $db->prepare("delete from sys_signup where email = ?"); + $stmt->bind_param("s", $email); + $stmt->execute(); + + return 0; + } + + public function createUser($email, $reonEmail, $password, $passwordConfirm) { if (!isset($email)) return 1; if (!self::$instance->isDionEmailValidAndFree($reonEmail)) return 2; if ($password != $passwordConfirm) return 3; @@ -273,14 +287,10 @@ public function completeSignupAction($id, $key, $reonEmail, $password, $password $dion_ppp_id = self::$instance->generatePPPId(); $log_in_password = self::$instance->generateLogInPassword(); $db = DBUtil::getInstance()->getDB(); - $stmt = $db->prepare("insert into sys_users (email, password, dion_ppp_id, dion_email_local, log_in_password) values (?)"); + $stmt = $db->prepare("insert into sys_users (email, password, dion_ppp_id, dion_email_local, log_in_password, money_spent) values (?, ?, ?, ?, ?, 0)"); $stmt->bind_param("sssss", $email, $password_hash, $dion_ppp_id, $reonEmail, $log_in_password); $stmt->execute(); - - $stmt = $db->prepare("delete from sys_signup where email = ?"); - $stmt->bind_param("s", $email); - $stmt->execute(); - + return 0; } diff --git a/web/scripts/add_user.php b/web/scripts/add_user.php new file mode 100644 index 0000000..85b7613 --- /dev/null +++ b/web/scripts/add_user.php @@ -0,0 +1,46 @@ +getDB(); + $user = UserUtil::getInstance(); + + $email = prompt("Email: "); + $password = prompt("Password: "); + $passwordConfirm = prompt("Confirm Password: "); + $reonEmail = prompt("DION Account (8 chars): "); + + if (!isEmailAvailable($db, $email)) { + exit("Email is unavailable"); + } + + $result = $user->createUser($email, $reonEmail, $password, $passwordConfirm); + + $detail = match ($result) { + 0 => "Account created!", + 1 => "Invalid email", + 2 => "DION Email is invalid or unavailable", + 3 => "Passwords do not match", + 4 => "Password does not minimum requirements", + }; + + echo $detail."\n"; + exit($result); + } + + function prompt($s) { + echo $s; + return rtrim(fgets(STDIN)); + } + + function isEmailAvailable($db, $email) { + $stmt = $db->prepare("select id from sys_users where email = ?"); + $stmt->bind_param("s", $email); + $stmt->execute(); + $row = $stmt->get_result()->fetch_assoc(); + if (isset($row)) return false; + return true; + } + + main(); \ No newline at end of file