-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi tenant Examples #260
Closed
+380
−267
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3df7f0c
Add native route helpers
janko 61a8ef4
Drop support for Ruby 2.3 and Ruby 2.4
janko c6c7f05
Remove format from Rodauth routes
janko 51a408c
Support older Rails versions
janko 08f6397
Enable per-action controller callbacks
janko 797075f
✅ Examples for multi-tenant support
pboling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ruby 2.7.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,20 @@ class EmailTest < IntegrationTest | |
assert_includes email.body.to_s, "Someone has created an account with this email address" | ||
end | ||
|
||
test "mailer delivery - multi-tenant" do | ||
register(login: "[email protected]", prefix: "/multi/tenant/animal-farm") | ||
|
||
assert_equal 1, ActionMailer::Base.deliveries.count | ||
|
||
email = ActionMailer::Base.deliveries[0] | ||
|
||
assert_equal "[email protected]", email[:to].to_s | ||
assert_equal "[email protected]", email[:from].to_s | ||
assert_equal "[RodauthTest] Verify Account", email[:subject].to_s | ||
|
||
assert_includes email.body.to_s, "Someone has created an account with this email address" | ||
end | ||
|
||
test "verify login change email" do | ||
register(login: "[email protected]", password: "secret", verify: true) | ||
|
||
|
@@ -26,4 +40,16 @@ class EmailTest < IntegrationTest | |
|
||
assert_equal 2, ActionMailer::Base.deliveries.count | ||
end | ||
|
||
test "verify login change email - multi-tenant" do | ||
register(login: "[email protected]", password: "secret", prefix: "/multi/tenant/kiwi", verify: true) | ||
|
||
visit "/change-email" | ||
|
||
fill_in "Login", with: "[email protected]" | ||
fill_in "Password", with: "secret" | ||
click_on "Change Login" | ||
|
||
assert_equal 2, ActionMailer::Base.deliveries.count | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class RodauthMultiTenantMailer < ActionMailer::Base | ||
default to: -> { @rodauth.email_to }, from: -> { @rodauth.email_from } | ||
|
||
def verify_account(name, account_id, path_key, key) | ||
@rodauth = rodauth(name, account_id, path_key) { @verify_account_key_value = key } | ||
@account = @rodauth.rails_account | ||
|
||
mail(subject: @rodauth.email_subject_prefix + @rodauth.verify_account_email_subject) | ||
end | ||
|
||
def reset_password(name, account_id, path_key, key) | ||
@rodauth = rodauth(name, account_id, path_key) { @reset_password_key_value = key } | ||
@account = @rodauth.rails_account | ||
|
||
mail(subject: @rodauth.email_subject_prefix + @rodauth.reset_password_email_subject) | ||
end | ||
|
||
def verify_login_change(name, account_id, path_key, key) | ||
@rodauth = rodauth(name, account_id, path_key) { @verify_login_change_key_value = key } | ||
@account = @rodauth.rails_account | ||
@new_email = @account.login_change_key.login | ||
|
||
mail(to: @new_email, subject: @rodauth.email_subject_prefix + @rodauth.verify_login_change_email_subject) | ||
end | ||
|
||
def password_changed(name, account_id, path_key) | ||
@rodauth = rodauth(name, account_id, path_key) | ||
@account = @rodauth.rails_account | ||
|
||
mail(subject: @rodauth.email_subject_prefix + @rodauth.password_changed_email_subject) | ||
end | ||
|
||
# ... | ||
private | ||
def rodauth(name, account_id, path_key, &block) | ||
instance = RodauthApp.new({ path_key: path_key }).rodauth(name) | ||
instance.path_key = path_key | ||
instance.instance_eval { @account = account_ds(account_id).first! } | ||
instance.instance_eval(&block) if block | ||
instance | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class RodauthMultiTenant < Rodauth::Rails::Auth | ||
configure do | ||
enable :create_account, :verify_account, :verify_account_grace_period, | ||
:login, :remember, :logout, :active_sessions, | ||
:reset_password, :change_password, :change_password_notify, | ||
:change_login, :verify_login_change, | ||
:close_account, :lockout, :recovery_codes, :internal_request, | ||
:path_class_methods, :jwt | ||
|
||
prefix { "/multi/tenant/#{path_key}" } | ||
|
||
rails_controller { RodauthController } | ||
|
||
before_rodauth do | ||
if param_or_nil("raise") | ||
raise NotImplementedError | ||
elsif param_or_nil("fail") | ||
fail "failed" | ||
end | ||
end | ||
|
||
account_status_column :status | ||
|
||
email_subject_prefix "[RodauthTest] " | ||
email_from "[email protected]" | ||
create_reset_password_email do | ||
RodauthMultiTenantMailer.reset_password(:multi_tenant, account_id, request.env[:path_key], reset_password_key_value) | ||
end | ||
create_verify_account_email { RodauthMultiTenantMailer.verify_account(:multi_tenant, account_id, request.env[:path_key], verify_account_key_value) } | ||
create_verify_login_change_email { |_login| RodauthMultiTenantMailer.verify_login_change(:multi_tenant, account_id, request.env[:path_key], verify_login_change_key_value) } | ||
create_password_changed_email { RodauthMultiTenantMailer.password_changed(:multi_tenant, account_id, request.env[:path_key]) } | ||
|
||
require_login_confirmation? false | ||
verify_account_set_password? false | ||
extend_remember_deadline? true | ||
max_invalid_logins 3 | ||
|
||
if defined?(::Turbo) | ||
after_login_failure do | ||
if rails_request.format.turbo_stream? | ||
return_response rails_render(turbo_stream: [turbo_stream.append("login-form", %(<div id="turbo-stream">login failed</div>))]) | ||
end | ||
end | ||
check_csrf? { rails_request.format.turbo_stream? ? false : super() } | ||
end | ||
|
||
after_login { remember_login } | ||
|
||
logout_redirect { rails_routes.root_path } | ||
login_redirect do | ||
segs = login_path.split('/') | ||
segs.insert(-2, request.env[:path_key]) | ||
segs.join('/') | ||
end | ||
verify_account_redirect { login_redirect } | ||
reset_password_redirect do | ||
segs = login_path.split('/') | ||
segs.insert(-2, request.env[:path_key]) | ||
segs.join('/') | ||
end | ||
title_instance_variable :@page_title | ||
|
||
verify_login_change_route nil | ||
change_login_route "change-email" | ||
end | ||
|
||
attr_accessor :path_key | ||
end |
2 changes: 2 additions & 0 deletions
2
test/rails_app/app/views/rodauth_multi_tenant_mailer/password_changed.text.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Someone (hopefully you) has changed the password for the account | ||
associated to this email address. |
5 changes: 5 additions & 0 deletions
5
test/rails_app/app/views/rodauth_multi_tenant_mailer/reset_password.text.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Someone has requested a password reset for the account with this email | ||
address. If you did not request a password reset, please ignore this | ||
message. If you requested a password reset, please go to | ||
<%= @rodauth.reset_password_email_link %> | ||
to reset the password for the account. |
4 changes: 4 additions & 0 deletions
4
test/rails_app/app/views/rodauth_multi_tenant_mailer/verify_account.text.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Someone has created an account with this email address. If you did not create | ||
this account, please ignore this message. If you created this account, please go to | ||
<%= @rodauth.verify_account_email_link %> | ||
to verify the account. |
10 changes: 10 additions & 0 deletions
10
test/rails_app/app/views/rodauth_multi_tenant_mailer/verify_login_change.text.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Someone with an account has requested their login be changed to this email address: | ||
|
||
Old email: <%= @account.email %> | ||
|
||
New email: <%= @new_email %> | ||
|
||
If you did not request this login change, please ignore this message. If you | ||
requested this login change, please go to | ||
<%= @rodauth.verify_login_change_email_link %> | ||
to verify the login change. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,31 +49,31 @@ class IntegrationTest < ActionDispatch::IntegrationTest | |
include Capybara::DSL | ||
include TestSetupTeardown | ||
|
||
def register(login: "[email protected]", password: "secret", verify: false) | ||
visit "/create-account" | ||
def register(login: "[email protected]", password: "secret", verify: false, prefix: "") | ||
visit "#{prefix}/create-account" | ||
fill_in "Login", with: login | ||
fill_in "Password", with: password | ||
fill_in "Confirm Password", with: password | ||
click_on "Create Account" | ||
|
||
if verify | ||
email = ActionMailer::Base.deliveries.last | ||
verify_account_link = email.body.to_s[%r{/verify-account\S+}] | ||
verify_account_link = email.body.to_s[%r{#{prefix}/verify-account\S+}] | ||
|
||
visit verify_account_link | ||
click_on "Verify Account" | ||
end | ||
end | ||
|
||
def login(login: "[email protected]", password: "secret") | ||
visit "/login" | ||
def login(login: "[email protected]", password: "secret", prefix: "") | ||
visit "#{prefix}/login" | ||
fill_in "Login", with: login | ||
fill_in "Password", with: password | ||
click_on "Login" | ||
end | ||
|
||
def logout | ||
visit "/logout" | ||
def logout(prefix: "") | ||
visit "#{prefix}/logout" | ||
click_on "Logout" | ||
end | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed, BTW? Won't the overridden
#prefix
method automatically add thepath_key
?It seems you have two sources of
path_key
– the attribute accessor andrequest.env
. Maybe that's why it isn't working?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That shouldn't be there... It was one of my transitional hacks as I figured it out... Not sure why it is still there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a wiki page once I have it all setup and working!