Create user ban model

Co-authored-by: Georg Gadinger <nilsding@nilsding.org>
This commit is contained in:
Karina Kwiatek 2021-08-14 16:07:12 +02:00
parent fab007b2a2
commit e4241d2001
5 changed files with 51 additions and 0 deletions

View file

@ -49,6 +49,11 @@ class User < ApplicationRecord
has_one :profile, dependent: :destroy
has_one :theme, dependent: :destroy
has_many :user_bans, dependent: :destroy
has_many :banned_users, class_name: 'UserBan',
foreign_key: 'banned_by_id',
dependent: :nullify
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i

4
app/models/user_ban.rb Normal file
View file

@ -0,0 +1,4 @@
class UserBan < ApplicationRecord
belongs_to :user
belongs_to :banned_by, class_name: 'User'
end

View file

@ -0,0 +1,28 @@
class CreateUserBans < ActiveRecord::Migration[5.2]
def up
create_table :user_bans do |t|
t.bigint :user_id
t.string :reason
t.datetime :expires_at
t.bigint :banned_by_id, nullable: true
t.timestamps
end
# foxy's functional fqueries
execute "INSERT INTO user_bans
(user_id, reason, expires_at, created_at, updated_at)
SELECT users.id, users.ban_reason, users.banned_until, users.updated_at, NOW() FROM users
WHERE banned_until IS NOT NULL AND NOT permanently_banned;"
execute "INSERT INTO user_bans
(user_id, reason, expires_at, created_at, updated_at)
SELECT users.id, users.ban_reason, NULL, users.updated_at, NOW() FROM users
WHERE permanently_banned;"
end
def down
drop_table :user_bans
end
end

View file

@ -248,6 +248,15 @@ ActiveRecord::Schema.define(version: 2021_12_28_135426) do
t.index ["user_id", "code"], name: "index_totp_recovery_codes_on_user_id_and_code"
end
create_table "user_bans", force: :cascade do |t|
t.bigint "user_id"
t.string "reason"
t.datetime "expires_at"
t.bigint "banned_by_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", id: :bigint, default: -> { "gen_timestamp_id('users'::text)" }, force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe UserBan, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end