Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fca160ae42 | |||
| 1653e1ae79 | |||
| 5c1295f0ff | |||
| cf7f9621e1 |
@@ -1,16 +1,14 @@
|
||||
class ApplicationController < ActionController::API
|
||||
before_action :reject_banned_ip_address!
|
||||
before_action :authenticate_user
|
||||
before_action :reject_banned_user!
|
||||
|
||||
def current_user = @current_user
|
||||
def current_user
|
||||
@current_user
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def authenticate_user
|
||||
code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_CODE']
|
||||
return if code.blank?
|
||||
|
||||
@current_user = User.find_by(inheritance_code: code)
|
||||
end
|
||||
|
||||
@@ -24,17 +22,4 @@ class ApplicationController < ActionController::API
|
||||
s.in?(['', '1', 'true', 'on', 'yes'])
|
||||
end
|
||||
end
|
||||
|
||||
def reject_banned_ip_address!
|
||||
ip_address = IpAddress.find_by(ip_address: IPAddr.new(request.remote_ip).hton)
|
||||
return unless ip_address&.banned?
|
||||
|
||||
head :forbidden
|
||||
end
|
||||
|
||||
def reject_banned_user!
|
||||
return unless current_user&.banned?
|
||||
|
||||
head :forbidden
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
class UsersController < ApplicationController
|
||||
def create
|
||||
return head :unprocessable_entity if request.remote_ip.blank?
|
||||
return head :forbidden if ip_address_banned?
|
||||
|
||||
user = nil
|
||||
|
||||
User.transaction do
|
||||
user = User.create!(inheritance_code: SecureRandom.uuid, role: :guest)
|
||||
attach_ip_address!(user)
|
||||
@@ -15,12 +15,10 @@ class UsersController < ApplicationController
|
||||
end
|
||||
|
||||
def verify
|
||||
return head :unprocessable_entity if request.remote_ip.blank?
|
||||
return head :forbidden if ip_address_banned?
|
||||
|
||||
user = User.find_by(inheritance_code: params[:code])
|
||||
return render json: { valid: false } unless user
|
||||
return head :forbidden if user.banned?
|
||||
|
||||
return head :unprocessable_entity if request.remote_ip.blank?
|
||||
|
||||
attach_ip_address!(user)
|
||||
|
||||
@@ -65,11 +63,4 @@ class UsersController < ApplicationController
|
||||
|
||||
UserIp.create_or_find_by!(user:, ip_address:)
|
||||
end
|
||||
|
||||
def ip_address_banned?
|
||||
ip_address = IpAddress.find_by(ip_address: IPAddr.new(request.remote_ip).hton)
|
||||
return false unless ip_address
|
||||
|
||||
ip_address.banned?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
class IpAddress < ApplicationRecord
|
||||
validates :ip_address, presence: true, length: { maximum: 16 }
|
||||
validates :banned, inclusion: { in: [true, false] }
|
||||
|
||||
has_many :user_ips, dependent: :destroy
|
||||
has_many :users, through: :user_ips
|
||||
|
||||
def banned? = banned_at.present?
|
||||
def ban! = banned? or update!(banned_at: Time.current)
|
||||
def unban! = update!(banned_at: nil)
|
||||
end
|
||||
|
||||
@@ -4,6 +4,7 @@ class User < ApplicationRecord
|
||||
validates :name, length: { maximum: 255 }
|
||||
validates :inheritance_code, presence: true, length: { maximum: 64 }
|
||||
validates :role, presence: true, inclusion: { in: roles.keys }
|
||||
validates :banned, inclusion: { in: [true, false] }
|
||||
|
||||
has_many :created_posts,
|
||||
class_name: 'Post', foreign_key: :uploaded_user_id, dependent: :nullify
|
||||
@@ -18,10 +19,5 @@ class User < ApplicationRecord
|
||||
class_name: 'WikiPage', foreign_key: :updated_user_id, dependent: :nullify
|
||||
|
||||
def viewed?(post) = user_post_views.exists?(post_id: post.id)
|
||||
|
||||
def gte_member? = member? || admin?
|
||||
|
||||
def banned? = banned_at.present?
|
||||
def ban! = banned? or update!(banned_at: Time.current)
|
||||
def unban! = update!(banned_at: nil)
|
||||
end
|
||||
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
class RenameBannedToBannedAtInUsersAndIpAddresses < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
[:users, :ip_addresses].each do
|
||||
add_column _1, :banned_at, :datetime, after: :banned
|
||||
add_index _1, :banned_at
|
||||
remove_column _1, :banned
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
[:ip_addresses, :users].each do
|
||||
add_column _1, :banned, :boolean, null: false, default: false, after: :banned_at
|
||||
remove_column _1, :banned_at
|
||||
end
|
||||
end
|
||||
end
|
||||
Generated
+3
-5
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_05_01_153900) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_04_27_214800) do
|
||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@@ -50,10 +50,9 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_01_153900) do
|
||||
|
||||
create_table "ip_addresses", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.binary "ip_address", limit: 16, null: false
|
||||
t.datetime "banned_at"
|
||||
t.boolean "banned", default: false, null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["banned_at"], name: "index_ip_addresses_on_banned_at"
|
||||
t.index ["ip_address"], name: "index_ip_addresses_on_ip_address", unique: true
|
||||
end
|
||||
|
||||
@@ -333,10 +332,9 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_01_153900) do
|
||||
t.string "name"
|
||||
t.string "inheritance_code", limit: 64, null: false
|
||||
t.string "role", null: false
|
||||
t.datetime "banned_at"
|
||||
t.boolean "banned", default: false, null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["banned_at"], name: "index_users_on_banned_at"
|
||||
end
|
||||
|
||||
create_table "wiki_assets", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
|
||||
Reference in New Issue
Block a user