Skip to content

Commit

Permalink
Add account to user by default when tenantable
Browse files Browse the repository at this point in the history
  • Loading branch information
lazaronixon committed Jun 2, 2023
1 parent 64f3883 commit 3e710ae
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,11 @@ Some artifacts are generated in the application, which makes it possible to impl

- Add `account_id` to each scoped table using `rails g migration add_account_to_projects account:references`.
- Add `include AccountScoped` to scoped models. It set up the account relationship and default scope using the current account.
- The `Current.account` is set according to the url. `http://mywebsite.com/1234/projects`.
- You should customize the authentication flow yourself, it means:
- Add `account_id` to your users table.
- Add `include AccountScoped` to your user model.
- Use `joins(:user).find_by_id...` in the `authenticate` method.
- Use `redirect_to "/#{user.account_id}"` after sign-in.
- Override `Current#user=` to also set the account, `super; self.account = user.account`.
- etc...

#### Set Current.account through the URL. `http://myapp.com/:account_id`

- Add `require_relative "../lib/account_middleware"` to `config/application.rb`.
- Add `config.middleware.use AccountMiddleware` to your application class.

## Development

Expand Down
5 changes: 0 additions & 5 deletions lib/generators/authentication/authentication_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ def add_environment_configurations
application "config.action_mailer.default_url_options = { host: \"localhost\", port: 3000 }", env: "development"
application "config.action_mailer.default_url_options = { host: \"localhost\", port: 3000 }", env: "test"
environment ratelimit_block, env: "production" if options.ratelimit?

if options.tenantable?
prepend_to_file "config/application.rb", "require_relative \"../lib/account_middleware\"\n"
application "config.middleware.use AccountMiddleware"
end
end

def create_configuration_files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Mi
t.string :provider
t.string :uid
<%- end -%>
<%- if options.tenantable? %>
t.references :account, null: false, foreign_key: true
<%- end -%>

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Account < ApplicationRecord
has_one :account, dependent: :destroy
end
5 changes: 5 additions & 0 deletions lib/generators/authentication/templates/models/current.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ class Current < ActiveSupport::CurrentAttributes
def session=(session)
super; self.user = session.user
end
<%- if options.tenantable? %>
def user=(user)
super; self.account = user.account
end
<%- end -%>
end
12 changes: 10 additions & 2 deletions lib/generators/authentication/templates/models/user.rb.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class User < ApplicationRecord
has_secure_password
<%- if options.tenantable? %>
belongs_to :account
<%- end -%>

has_many :email_verification_tokens, dependent: :destroy
has_many :password_reset_tokens, dependent: :destroy
Expand Down Expand Up @@ -31,15 +34,20 @@ class User < ApplicationRecord
self.verified = false
end
<%- if two_factor? %>
before_create do
before_validation on: :create do
self.otp_secret = ROTP::Base32.random
end
<%- end -%>
<%- if webauthn? %>
before_create do
before_validation on: :create do
self.webauthn_id = WebAuthn.generate_user_id
end
<%- end -%>
<%- if options.tenantable? %>
before_validation on: :create do
self.account = Account.new
end
<%- end -%>

after_update if: :password_digest_previously_changed? do
sessions.where.not(id: Current.session).delete_all
Expand Down

0 comments on commit 3e710ae

Please sign in to comment.