Skip to content

Commit

Permalink
Copy the main tenant administrator login credentials into the new tenant
Browse files Browse the repository at this point in the history
Co-Authored-By: Senén Rodero <senenrodero@gmail.com>
  • Loading branch information
taitus and Senen committed Dec 29, 2022
1 parent 171cf4e commit 951eec7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/components/admin/tenants/new_component.html.erb
@@ -1,3 +1,7 @@
<%= back_link_to admin_tenants_path %>
<%= 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) %>
1 change: 1 addition & 0 deletions 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
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/admin/tenants_controller.rb
Expand Up @@ -13,6 +13,7 @@ def edit

def create
if @tenant.save
update_default_tenant_administrator
redirect_to admin_tenants_path, notice: t("admin.tenants.create.notice")
else
render :new
Expand Down Expand Up @@ -50,4 +51,19 @@ def restore
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
1 change: 1 addition & 0 deletions config/locales/en/admin.yml
Expand Up @@ -1642,6 +1642,7 @@ en:
enable: "Enable tenant %{tenant}"
enabled: Enabled
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
restore:
notice: Tenant enabled successfully
Expand Down
1 change: 1 addition & 0 deletions config/locales/es/admin.yml
Expand Up @@ -1641,6 +1641,7 @@ es:
enable: "Habilitar entidad %{tenant}"
enabled: Habilitada
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
restore:
notice: Entidad habilitada correctamente
Expand Down
2 changes: 1 addition & 1 deletion 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")
Expand Down
27 changes: 27 additions & 0 deletions spec/system/admin/tenants_spec.rb
Expand Up @@ -40,6 +40,33 @@
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
Expand Down

0 comments on commit 951eec7

Please sign in to comment.