Change default role_id to -99

This commit is contained in:
Claire 2024-08-01 12:10:31 +02:00
parent a50c8e951f
commit 010c33db43
4 changed files with 36 additions and 3 deletions

View File

@ -36,7 +36,7 @@
# sign_in_token_sent_at :datetime
# webauthn_id :string
# sign_up_ip :inet
# role_id :bigint(8)
# role_id :bigint(8) default(-99)
# settings :text
# time_zone :string
# otp_secret :string
@ -129,6 +129,7 @@ class User < ApplicationRecord
before_validation :sanitize_role
before_create :set_approved
after_commit :send_pending_devise_notifications
after_find :set_role_id
after_create_commit :trigger_webhooks
normalizes :locale, with: ->(locale) { I18n.available_locales.exclude?(locale.to_sym) ? nil : locale }
@ -156,6 +157,10 @@ class User < ApplicationRecord
Rails.env.local?
end
def set_role_id
self.role_id ||= -99
end
def role
if role_id.nil?
UserRole.everyone

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
class PopulateEveryoneRole < ActiveRecord::Migration[7.1]
disable_ddl_transaction!
class UserRole < ApplicationRecord
EVERYONE_ROLE_ID = -99
FLAGS = {
invite_users: (1 << 16),
}.freeze
end
def up
UserRole.create!(id: UserRole::EVERYONE_ROLE_ID, permissions: UserRole::FLAGS[:invite_users])
rescue ActiveRecord::RecordNotUnique
nil
end
def down; end
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class ChangeUsersDefaultRoleId < ActiveRecord::Migration[7.1]
def change
change_column_default :users, :role_id, from: nil, to: -99
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_07_24_181224) do
ActiveRecord::Schema[7.1].define(version: 2024_08_01_100823) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -1201,7 +1201,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_24_181224) do
t.string "webauthn_id"
t.inet "sign_up_ip"
t.boolean "skip_sign_in_token"
t.bigint "role_id"
t.bigint "role_id", default: -99
t.text "settings"
t.string "time_zone"
t.string "otp_secret"