feat: Unify user and super admin credentials (#3830)
Fixes: #3061, #3489
This commit is contained in:
42
db/migrate/20220121055444_add_type_to_users.rb
Normal file
42
db/migrate/20220121055444_add_type_to_users.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
class AddTypeToUsers < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
add_column :users, :type, :string
|
||||
migrate_existing_super_admins
|
||||
|
||||
drop_table :super_admins do |t|
|
||||
t.string :email, null: false, default: ''
|
||||
t.string :encrypted_password, null: false, default: ''
|
||||
t.datetime :remember_created_at
|
||||
t.integer :sign_in_count, default: 0, null: false
|
||||
t.datetime :current_sign_in_at
|
||||
t.datetime :last_sign_in_at
|
||||
t.inet :current_sign_in_ip
|
||||
t.inet :last_sign_in_ip
|
||||
t.timestamps null: false
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def old_super_admins
|
||||
ActiveRecord::Base.connection.execute('SELECT * from super_admins').to_a
|
||||
end
|
||||
|
||||
def create_user_account_for_super_admin(super_admin)
|
||||
u = User.new(email: super_admin['email'], name: "SuperUser #{super_admin['id']}", encrypted_password: super_admin['encrypted_password'],
|
||||
confirmed_at: DateTime.now, type: 'SuperAdmin')
|
||||
u.save(validate: false)
|
||||
end
|
||||
|
||||
def migrate_existing_super_admins
|
||||
old_super_admins.each do |super_admin|
|
||||
user = User.find_by(email: super_admin['email'])
|
||||
if user.present?
|
||||
user.update(type: 'SuperAdmin')
|
||||
else
|
||||
Rails.logger.debug { "User with email #{super_admin['email']} not found" }
|
||||
create_user_account_for_super_admin(super_admin)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2022_01_11_223630) do
|
||||
ActiveRecord::Schema.define(version: 2022_01_21_055444) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pg_stat_statements"
|
||||
@@ -745,6 +745,7 @@ ActiveRecord::Schema.define(version: 2022_01_11_223630) do
|
||||
t.integer "availability", default: 0
|
||||
t.jsonb "ui_settings", default: {}
|
||||
t.jsonb "custom_attributes", default: {}
|
||||
t.string "type"
|
||||
t.index ["email"], name: "index_users_on_email"
|
||||
t.index ["pubsub_token"], name: "index_users_on_pubsub_token", unique: true
|
||||
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
||||
|
||||
@@ -10,7 +10,6 @@ end
|
||||
|
||||
## Seeds for Local Development
|
||||
unless Rails.env.production?
|
||||
SuperAdmin.create!(email: 'john@acme.inc', password: 'Password1!')
|
||||
|
||||
account = Account.create!(
|
||||
name: 'Acme Inc'
|
||||
@@ -20,7 +19,7 @@ unless Rails.env.production?
|
||||
name: 'Acme Org'
|
||||
)
|
||||
|
||||
user = User.new(name: 'John', email: 'john@acme.inc', password: 'Password1!')
|
||||
user = User.new(name: 'John', email: 'john@acme.inc', password: 'Password1!', type: 'SuperAdmin')
|
||||
user.skip_confirmation!
|
||||
user.save!
|
||||
|
||||
|
||||
Reference in New Issue
Block a user