From a2d0edaaa6ccefb954e09736bb2fd6234d621f17 Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Wed, 24 May 2023 23:09:43 +0900 Subject: [PATCH 1/3] Add: Add: sonolus_handle field --- ...30524140147_add_sonolus_handle_to_users.rb | 17 +++++++++++++ backend/db/schema.rb | 25 +++++++++---------- 2 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb diff --git a/backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb b/backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb new file mode 100644 index 0000000..62477e4 --- /dev/null +++ b/backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb @@ -0,0 +1,17 @@ +class AddSonolusHandleToUsers < ActiveRecord::Migration[7.0] + def up + add_column :users, :sonolus_handle, :string, unique: true, index: true + User.find_each do |user| + if user.owner_id.present? + user.update(sonolus_handle: "x" + user.handle) + else + user.update(sonolus_handle: user.handle) + end + end + change_column :users, :sonolus_handle, :string, null: false + end + + def down + remove_column :users, :sonolus_handle + end +end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 53d4da2..6150e04 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_04_06_030051) do +ActiveRecord::Schema[7.0].define(version: 2023_05_24_140147) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -21,9 +21,7 @@ t.bigint "blob_id", null: false t.datetime "created_at", null: false t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" - t.index %w[record_type record_id name blob_id], - name: "index_active_storage_attachments_uniqueness", - unique: true + t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true end create_table "active_storage_blobs", force: :cascade do |t| @@ -41,9 +39,7 @@ create_table "active_storage_variant_records", force: :cascade do |t| t.bigint "blob_id", null: false t.string "variation_digest", null: false - t.index %w[blob_id variation_digest], - name: "index_active_storage_variant_records_uniqueness", - unique: true + t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end create_table "charts", force: :cascade do |t| @@ -118,17 +114,20 @@ t.string "bg_color" t.bigint "owner_id" t.integer "charts_count", default: 0, null: false + t.string "discord_id" + t.string "discord_name" + t.string "discord_discriminator" + t.string "discord_token" + t.string "discord_refresh_token" + t.datetime "discord_expires_at" + t.string "sonolus_handle", null: false t.index ["handle"], name: "index_users_on_handle" t.index ["name"], name: "index_users_on_name" t.index ["owner_id"], name: "index_users_on_owner_id" end - add_foreign_key "active_storage_attachments", - "active_storage_blobs", - column: "blob_id" - add_foreign_key "active_storage_variant_records", - "active_storage_blobs", - column: "blob_id" + add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" + add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "charts", "charts", column: "variant_id" add_foreign_key "charts", "users", column: "author_id" add_foreign_key "co_authors", "users" From 81ff6c820679df657c7f365107d53ec08f29f81e Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Wed, 31 May 2023 20:39:18 +0900 Subject: [PATCH 2/3] Add: Add sonolus_handle field --- README.md | 9 ++++++--- backend/app/controllers/api/admin_controller.rb | 13 ++----------- backend/app/controllers/api/charts_controller.rb | 12 ++++-------- backend/app/controllers/api/users_controller.rb | 11 ++--------- .../app/controllers/sonolus/levels_controller.rb | 13 ++++++------- backend/app/controllers/sonolus_controller.rb | 4 ++-- backend/app/models/user.rb | 10 +++++----- backend/config/environments/production.rb | 4 ++++ .../20230524140147_add_sonolus_handle_to_users.rb | 2 +- 9 files changed, 32 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 2fe7d7d..7fd5641 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ Chart Cyanvas is a sekai custom charts platform. > **Warning** > This project is still in development! +## Running + +1. Install [Docker](https://www.docker.com/) +2. Copy `.env.prod.example` to `.env.prod` and fill the variables +3. Run `docker compose -fdocker-compose.prod.yml --profile prod --env-file .env.prod up -d` + ## Architecture ![Architecture](./architecture.svg) @@ -49,9 +55,6 @@ docker compose --profile dev up -d goreman start ``` - - - ## License This project is licensed under the GPLv3 License. diff --git a/backend/app/controllers/api/admin_controller.rb b/backend/app/controllers/api/admin_controller.rb index c370d08..392cdab 100644 --- a/backend/app/controllers/api/admin_controller.rb +++ b/backend/app/controllers/api/admin_controller.rb @@ -29,16 +29,7 @@ def reconvert_sus def show_user params.require(:handle) - @user = - if params[:handle].start_with?("x") - User - .where(handle: params[:handle].delete_prefix("x")) - .where.not(owner_id: nil) - .first - .owner - else - User.find_by(handle: params[:handle]) - end + @user = User.find_by(handle: params[:handle]) if @user user_data = @user.to_frontend user_data[:altUsers] = @user.alt_users.map(&:to_frontend) @@ -50,7 +41,7 @@ def show_user end around_action do |controller, action| - if !ENV["ADMIN_HANDLE"] || current_user&.handle != ENV["ADMIN_HANDLE"] + unless current_user&.admin? logger.warn "Unauthorized admin access attempt by #{current_user&.handle} (Admin handle: #{ENV["ADMIN_HANDLE"]})" render json: { code: "forbidden" }, status: :forbidden next diff --git a/backend/app/controllers/api/charts_controller.rb b/backend/app/controllers/api/charts_controller.rb index 22ef20d..617a489 100644 --- a/backend/app/controllers/api/charts_controller.rb +++ b/backend/app/controllers/api/charts_controller.rb @@ -118,7 +118,7 @@ def all end if params[:author] - user = User.find_by(handle: params[:author].delete_prefix("x")) + user = User.find_by(handle: params[:author]) unless user render json: { code: "not_found", @@ -202,8 +202,7 @@ def process_chart_request return end - author = - User.find_by(handle: data_parsed[:authorHandle].delete_prefix("x")) + author = User.find_by(handle: data_parsed[:authorHandle]) unless author render json: { code: "not_found", @@ -214,9 +213,7 @@ def process_chart_request end session_user = User.find_by(id: session[:user_id]) - unless ( - ENV["ADMIN_HANDLE"] && session_user.handle == ENV["ADMIN_HANDLE"] - ) || author.id == session[:user_id] || + unless (session_user.admin?) || author.id == session[:user_id] || author.owner_id == session[:user_id] render json: { code: "forbidden", @@ -410,8 +407,7 @@ def delete end user = User.find_by(id: session[:user_id]) - unless (ENV["ADMIN_HANDLE"] && user&.handle == ENV["ADMIN_HANDLE"]) || - chart.author_id == session[:user_id] || + unless user&.admin? || chart.author_id == session[:user_id] || chart.author.owner_id == session[:user_id] render json: { code: "forbidden", diff --git a/backend/app/controllers/api/users_controller.rb b/backend/app/controllers/api/users_controller.rb index 3725e59..9dca0c4 100644 --- a/backend/app/controllers/api/users_controller.rb +++ b/backend/app/controllers/api/users_controller.rb @@ -3,15 +3,8 @@ module Api class UsersController < FrontendController def show params.require(:handle) - @user = - if params[:handle].start_with?("x") - User - .where(handle: params[:handle].delete_prefix("x")) - .where.not(owner_id: nil) - .first - else - User.find_by(handle: params[:handle]) - end + @user = User.find_by(sonolus_handle: params[:handle]) + @user = User.find_by(handle: params[:handle]) unless @user if @user render json: { code: "ok", user: @user.to_frontend } else diff --git a/backend/app/controllers/sonolus/levels_controller.rb b/backend/app/controllers/sonolus/levels_controller.rb index 7a8621a..28d4223 100644 --- a/backend/app/controllers/sonolus/levels_controller.rb +++ b/backend/app/controllers/sonolus/levels_controller.rb @@ -120,12 +120,8 @@ def list if params[:q_author].present? authors = params[:q_author].split.map do |author| - user = - if author.start_with?("x") - User.find_by(handle: author[1..]) - else - User.find_by(handle: author) - end + user = User.find_by(sonolus_handle: author) + user = User.find_by(handle: author) if user.nil? user&.id end if authors.any?(nil) @@ -182,7 +178,10 @@ def list def test_list require_login! - params.permit(:page, *(self.class.test_search_options.map { |o| o[:query] })) + params.permit( + :page, + *(self.class.test_search_options.map { |o| o[:query] }) + ) charts = Chart.where(author_id: current_user.id) diff --git a/backend/app/controllers/sonolus_controller.rb b/backend/app/controllers/sonolus_controller.rb index 40b69f7..030acc5 100644 --- a/backend/app/controllers/sonolus_controller.rb +++ b/backend/app/controllers/sonolus_controller.rb @@ -53,7 +53,7 @@ class SonolusController < ApplicationController self.session_data = { user: user_data[:userProfile] } user_profile = user_data[:userProfile] table_contents = { - handle: user_profile[:handle], + sonolus_handle: user_profile[:handle], name: user_profile[:name], about_me: user_profile[:aboutMe], fg_color: user_profile[:avatarForegroundColor], @@ -61,7 +61,7 @@ class SonolusController < ApplicationController } user = - if (u = User.find_by(handle: user_profile[:handle])) + if (u = User.find_by(sonolus_handle: user_profile[:handle])) if table_contents.each_pair.any? { |k, v| u[k] != v } logger.info "User #{u.handle} updated, updating table" u.update!(table_contents) diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index d226103..b8a5059 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -14,11 +14,7 @@ class User < ApplicationRecord class_name: "User", inverse_of: :user - def display_handle - owner_id ? "x#{handle}" : handle - end - - def to_frontend() + def to_frontend { handle: owner_id ? "x#{handle}" : handle, name:, @@ -28,4 +24,8 @@ def to_frontend() chartCount: charts_count } end + + def admin? + ENV["ADMIN_HANDLE"] == sonolus_handle + end end diff --git a/backend/config/environments/production.rb b/backend/config/environments/production.rb index 909e1e6..4050156 100644 --- a/backend/config/environments/production.rb +++ b/backend/config/environments/production.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true require "active_support/core_ext/integer/time" +unless ENV["ADMIN_HANDLE"] + raise "ADMIN_HANDLE is not set! Please set it in .env.prod" +end + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. diff --git a/backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb b/backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb index 62477e4..97f0c0e 100644 --- a/backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb +++ b/backend/db/migrate/20230524140147_add_sonolus_handle_to_users.rb @@ -3,7 +3,7 @@ def up add_column :users, :sonolus_handle, :string, unique: true, index: true User.find_each do |user| if user.owner_id.present? - user.update(sonolus_handle: "x" + user.handle) + user.update(handle: "x" + user.handle, sonolus_handle: "x" + user.handle) else user.update(sonolus_handle: user.handle) end From 7b1090e002eab841621d780a044f2571b2b0b86e Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Wed, 31 May 2023 21:02:46 +0900 Subject: [PATCH 3/3] Fix: Fix seed --- backend/db/seeds.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/db/seeds.rb b/backend/db/seeds.rb index 44a9f31..426ccce 100644 --- a/backend/db/seeds.rb +++ b/backend/db/seeds.rb @@ -8,14 +8,16 @@ # Character.create(name: "Luke", movie: movies.first) user = User.create!( - handle: 1073, + handle: "1073", + sonolus_handle: "1073", name: "Nanashi.", about_me: "I'm a admin.", fg_color: "#fff", bg_color: "#48b0d5" ) user.create_user!( - handle: 740, + handle: "x740", + sonolus_handle: "x740", name: "Nanatsuki Kuten", about_me: "Alt account of Nanashi.", fg_color: "#48b0d5",