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 d63efc7 commit 9dfae2d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/components/admin/tenants/new_component.html.erb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions app/controllers/admin/tenants_controller.rb
Original file line number Diff line number Diff line change
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 All @@ -32,4 +33,17 @@ def update
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.update!(tenant_administrator_credentials)
end
end
end
1 change: 1 addition & 0 deletions config/locales/en/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,7 @@ en:
index:
create: Create tenant
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
update:
notice: Tenant updated successfully
Expand Down
1 change: 1 addition & 0 deletions config/locales/es/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,7 @@ es:
index:
create: Crear entidad
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
update:
notice: Entidad actualizada correctamente
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions spec/system/admin/tenants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@
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, 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"

click_link "earth.lvh.me"

click_link "Sign in"
fill_in "Email or username", with: user.username
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 9dfae2d

Please sign in to comment.