Copy the main tenant administrator login credentials into the new tenant

Co-Authored-By: Senén Rodero <senenrodero@gmail.com>
This commit is contained in:
taitus
2022-12-15 14:35:07 +01:00
committed by Senén Rodero Rodríguez
parent 171cf4e634
commit 951eec7d08
7 changed files with 51 additions and 1 deletions

View File

@@ -1,3 +1,7 @@
<%= back_link_to admin_tenants_path %> <%= back_link_to admin_tenants_path %>
<%= header %> <%= header %>
<div class="callout primary">
<%= sanitize(t("admin.tenants.new.admin_information", username: current_user.username,
email: current_user.email)) %>
</div>
<%= render Admin::Tenants::FormComponent.new(tenant) %> <%= render Admin::Tenants::FormComponent.new(tenant) %>

View File

@@ -1,6 +1,7 @@
class Admin::Tenants::NewComponent < ApplicationComponent class Admin::Tenants::NewComponent < ApplicationComponent
include Header include Header
attr_reader :tenant attr_reader :tenant
delegate :current_user, to: :helpers
def initialize(tenant) def initialize(tenant)
@tenant = tenant @tenant = tenant

View File

@@ -13,6 +13,7 @@ class Admin::TenantsController < Admin::BaseController
def create def create
if @tenant.save if @tenant.save
update_default_tenant_administrator
redirect_to admin_tenants_path, notice: t("admin.tenants.create.notice") redirect_to admin_tenants_path, notice: t("admin.tenants.create.notice")
else else
render :new render :new
@@ -50,4 +51,19 @@ class Admin::TenantsController < Admin::BaseController
def tenant_params def tenant_params
params.require(:tenant).permit(:name, :schema, :schema_type) params.require(:tenant).permit(:name, :schema, :schema_type)
end end
def update_default_tenant_administrator
tenant_administrator_credentials = {
email: current_user.email,
username: current_user.username,
encrypted_password: current_user.encrypted_password
}
Tenant.switch(@tenant.schema) do
default_admin_account = User.administrators.first
default_admin_account.skip_confirmation_notification!
default_admin_account.update!(tenant_administrator_credentials)
default_admin_account.confirm
end
end
end end

View File

@@ -1642,6 +1642,7 @@ en:
enable: "Enable tenant %{tenant}" enable: "Enable tenant %{tenant}"
enabled: Enabled enabled: Enabled
new: new:
admin_information: "When you create a tenant, your current user <strong>\"%{username}\"</strong> with the email <strong>\"%{email}\"</strong> and your password will be copied into the new tenant's database and will be automatically granted administration permissions. Note that these user accounts are stored in completely separate databases, so if you change the password of your user in one tenant, your accounts in other tenants will remain intact."
title: New tenant title: New tenant
restore: restore:
notice: Tenant enabled successfully notice: Tenant enabled successfully

View File

@@ -1641,6 +1641,7 @@ es:
enable: "Habilitar entidad %{tenant}" enable: "Habilitar entidad %{tenant}"
enabled: Habilitada enabled: Habilitada
new: new:
admin_information: 'Al crear una entidad se copiará tu usuario actual <strong>"%{username}"</strong> con el email <strong>"%{email}"</strong> y tu actual contraseña en la base de datos de la nueva entidad y se te otorgarán permisos de administración en la nueva entidad automáticamente. Nótese que serán cuentas de usuarios que están en bases de datos completamente independientes por lo que si cambias la contraseña de tu usuario en una entidad, las cuentas que tengas en otras entidades permanecerán intactas.'
title: Nueva entidad title: Nueva entidad
restore: restore:
notice: Entidad habilitada correctamente notice: Entidad habilitada correctamente

View File

@@ -1,5 +1,5 @@
# Default admin user (change password after first deploy to a server!) # Default admin user (change password after first deploy to a server!)
if Administrator.count == 0 && !Rails.env.test? if Administrator.count == 0 && (!Rails.env.test? || !Tenant.default?)
admin = User.create!(username: "admin", email: "admin@consul.dev", password: "12345678", admin = User.create!(username: "admin", email: "admin@consul.dev", password: "12345678",
password_confirmation: "12345678", confirmed_at: Time.current, password_confirmation: "12345678", confirmed_at: Time.current,
terms_of_service: "1") terms_of_service: "1")

View File

@@ -40,6 +40,33 @@ describe "Tenants", :admin, :seed_tenants do
expect(page).to have_current_path root_path expect(page).to have_current_path root_path
expect(page).to have_link "Sign in" expect(page).to have_link "Sign in"
end end
scenario "Updates new tenant default user with the main tenant administrator login credentials" do
user = create(:user, email: "super@consul.dev", password: "secret_password")
create(:administrator, user: user)
login_as(user)
visit new_admin_tenant_path
expect(page).to have_content "When you create a tenant, your current user"
fill_in "Name", with: "Earthlings"
fill_in "Subdomain", with: "earth"
click_button "Create tenant"
expect(page).to have_content "Tenant created successfully"
expect(ActionMailer::Base.deliveries.count).to eq(0)
click_link "earth.lvh.me"
click_link "Sign in"
fill_in "Email or username", with: "super@consul.dev"
fill_in "Password", with: "secret_password"
click_button "Enter"
expect(page).to have_content "You have been signed in successfully."
expect(current_host).to eq "http://earth.lvh.me"
end
end end
scenario "Update" do scenario "Update" do