diff --git a/app/components/admin/tenants/new_component.html.erb b/app/components/admin/tenants/new_component.html.erb index f83758392..2b0b4a3af 100644 --- a/app/components/admin/tenants/new_component.html.erb +++ b/app/components/admin/tenants/new_component.html.erb @@ -1,3 +1,7 @@ <%= back_link_to admin_tenants_path %> <%= header %> +
+ <%= sanitize(t("admin.tenants.new.admin_information", username: current_user.username, + email: current_user.email)) %> +
<%= render Admin::Tenants::FormComponent.new(tenant) %> diff --git a/app/components/admin/tenants/new_component.rb b/app/components/admin/tenants/new_component.rb index d0ebacf4d..769ce9507 100644 --- a/app/components/admin/tenants/new_component.rb +++ b/app/components/admin/tenants/new_component.rb @@ -1,6 +1,7 @@ class Admin::Tenants::NewComponent < ApplicationComponent include Header attr_reader :tenant + delegate :current_user, to: :helpers def initialize(tenant) @tenant = tenant diff --git a/app/controllers/admin/tenants_controller.rb b/app/controllers/admin/tenants_controller.rb index e7d6e949a..055e464d6 100644 --- a/app/controllers/admin/tenants_controller.rb +++ b/app/controllers/admin/tenants_controller.rb @@ -13,6 +13,7 @@ class Admin::TenantsController < Admin::BaseController def create if @tenant.save + update_default_tenant_administrator redirect_to admin_tenants_path, notice: t("admin.tenants.create.notice") else render :new @@ -50,4 +51,19 @@ class Admin::TenantsController < Admin::BaseController def tenant_params params.require(:tenant).permit(:name, :schema, :schema_type) 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 diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index b128ec8cf..4ed87723a 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -1642,6 +1642,7 @@ en: enable: "Enable tenant %{tenant}" enabled: Enabled new: + admin_information: "When you create a tenant, your current user \"%{username}\" with the email \"%{email}\" 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 restore: notice: Tenant enabled successfully diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 509748e57..00fb0fc3e 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -1641,6 +1641,7 @@ es: enable: "Habilitar entidad %{tenant}" enabled: Habilitada new: + admin_information: 'Al crear una entidad se copiará tu usuario actual "%{username}" con el email "%{email}" 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 restore: notice: Entidad habilitada correctamente diff --git a/db/seeds.rb b/db/seeds.rb index 28704c29d..a10330b7c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,5 +1,5 @@ # 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", password_confirmation: "12345678", confirmed_at: Time.current, terms_of_service: "1") diff --git a/spec/system/admin/tenants_spec.rb b/spec/system/admin/tenants_spec.rb index 38389dbe8..acabe6464 100644 --- a/spec/system/admin/tenants_spec.rb +++ b/spec/system/admin/tenants_spec.rb @@ -40,6 +40,33 @@ describe "Tenants", :admin, :seed_tenants do expect(page).to have_current_path root_path expect(page).to have_link "Sign in" 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 scenario "Update" do