authentication-zero 2.16.29 → 2.16.31

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1ca404e21064ef6548cb7fd3dd0d62259e55cc1712ccbd37266de5a5009cae4
4
- data.tar.gz: 5470a05b5863a993997d9cc17d7c104d53ed5f8277182f029f9831e8c51be6fa
3
+ metadata.gz: 8bff3de0a9b6c8fb09557b580d1f7bc3b9be99f71aadeb4e84d080be2ae022da
4
+ data.tar.gz: f014555758b4bc5d8c5c4f3b7a7eeb3aef9790ef5d0726b56d20ed59e7c2a32c
5
5
  SHA512:
6
- metadata.gz: 044747617b27c4a38aae36572364ce3e64483fe15a7ca44518209e1db30a2207269b92edb28ce690449249c728073d5d661eb4df15a19afa8cceeaa20eb763fd
7
- data.tar.gz: 47ed5cb9bcef11dfc71a4ed3c761b5a30d346c0b1923e52f1e71f24d5c1703b23b7e52552cd12f7ca9ad6e9031d3cc8a6a5e4a124e0d3a8f6753fdbea202cd13
6
+ metadata.gz: d48574ced8a36ac0e2ddb342176558b084f5bf892cf2cce8d8d96ee7183d59850bd170154c76cd6b1092a52bf6184b85353b592c9c395e5139a3536ac5e38468
7
+ data.tar.gz: 83079c90bdee50d97ffb2f9c89dad298491363568784792ca1012683ccca5f4f3203e0eb424a92f61c8c18ffc7783bc252f5c93338fa931fbefc2015682bc323
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## Authentication Zero 2.16.30 ##
2
+
3
+ * Add multi-tenant artifacts that you can use. (--tenantable)
4
+
1
5
  ## Authentication Zero 2.16.29 ##
2
6
 
3
7
  * Replaced session with session_record, it has a conflict on rails 7.1 (bug-fix)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- authentication-zero (2.16.29)
4
+ authentication-zero (2.16.31)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -45,8 +45,8 @@ Since Authentication Zero generates this code into your application instead of b
45
45
  - Social login with omni auth (--omniauthable)
46
46
  - Passwordless authentication (--passwordless)
47
47
  - Send invitations (--invitable)
48
- - "Sign-in as" button functionallity (--masqueradable)
49
-
48
+ - "Sign-in as" button (--masqueradable)
49
+ - Multi-tentant application (--tenantable)
50
50
 
51
51
  ## Generated code
52
52
 
@@ -59,7 +59,25 @@ Since Authentication Zero generates this code into your application instead of b
59
59
  - [log filtering](https://guides.rubyonrails.org/action_controller_overview.html#log-filtering): Parameters 'token' and 'password' are marked [FILTERED] in the log.
60
60
  - [functional tests](https://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers): In Rails, testing the various actions of a controller is a form of writing functional tests.
61
61
  - [system testing](https://guides.rubyonrails.org/testing.html#system-testing): System tests allow you to test user interactions with your application, running tests in either a real or a headless browser.
62
- - **sudoable**: Use `before_action :require_sudo` in controllers with sensitive information, it will ask for your password on the first access or after 30 minutes.
62
+
63
+ ### Sudoable
64
+
65
+ Use `before_action :require_sudo` in controllers with sensitive information, it will ask for your password on the first access or after 30 minutes.
66
+
67
+ ### Tenantable
68
+
69
+ Some artifacts are generated in the application, which makes it possible to implement row-level multitenancy applications. You should follow some steps to make it work.
70
+
71
+ - Add `account_id` to each scoped table using `rails g migration add_account_to_projects account:references`
72
+ - Add `include AccountScoped` to scoped models. It set up the relationship with the account and default scope using the current account
73
+ - The `Current.account` is set according to the url ex: `http://mywebsite.com/1234/projects`
74
+ - You should customize the authentication flow yourself, it means:
75
+ - Add `account_id` to your users table using `rails g migration add_account_to_users account:references`
76
+ - Add `include AccountScoped` to your user model
77
+ - Use `Session.joins(:user).find_by_id` on `ApplicationController#authenticate`
78
+ - Use `redirect_to "/#{user.account_id}"` after sign-in.
79
+ - Override `Current#user=` to also set the account using `super; self.account = user.account`
80
+ - etc...
63
81
 
64
82
  ## Development
65
83
 
@@ -1,3 +1,3 @@
1
1
  module AuthenticationZero
2
- VERSION = "2.16.29"
2
+ VERSION = "2.16.31"
3
3
  end
@@ -15,6 +15,7 @@ class AuthenticationGenerator < Rails::Generators::Base
15
15
  class_option :webauthn, type: :boolean, desc: "Add two factor authentication using a hardware security key"
16
16
  class_option :invitable, type: :boolean, desc: "Add sending invitations"
17
17
  class_option :masqueradable, type: :boolean, desc: "Add sign-in as button functionallity"
18
+ class_option :tenantable, type: :boolean, desc: "Add artifacts to implement a row-level tenant app"
18
19
 
19
20
  source_root File.expand_path("templates", __dir__)
20
21
 
@@ -53,15 +54,25 @@ class AuthenticationGenerator < Rails::Generators::Base
53
54
  application "config.action_mailer.default_url_options = { host: \"localhost\", port: 3000 }", env: "development"
54
55
  application "config.action_mailer.default_url_options = { host: \"localhost\", port: 3000 }", env: "test"
55
56
  environment ratelimit_block, env: "production" if options.ratelimit?
57
+
58
+ if options.tenantable?
59
+ prepend_to_file "config/application.rb", "require_relative \"../lib/account_middleware\"\n"
60
+ application "config.middleware.use AccountMiddleware"
61
+ end
56
62
  end
57
63
 
58
64
  def create_configuration_files
59
- copy_file "config/redis/shared.yml", "config/redis/shared.yml" if redis?
60
- copy_file "config/initializers/omniauth.rb", "config/initializers/omniauth.rb" if omniauthable?
61
- copy_file "config/initializers/webauthn.rb", "config/initializers/webauthn.rb" if webauthn?
65
+ copy_file "config/redis/shared.yml" if redis?
66
+ copy_file "config/initializers/omniauth.rb" if omniauthable?
67
+ copy_file "config/initializers/webauthn.rb" if webauthn?
68
+ end
69
+
70
+ def create_lib_files
71
+ copy_file "lib/account_middleware.rb" if options.tenantable?
62
72
  end
63
73
 
64
74
  def create_migrations
75
+ migration_template "migrations/create_accounts_migration.rb", "#{db_migrate_path}/create_accounts_migration.rb" if options.tenantable?
65
76
  migration_template "migrations/create_users_migration.rb", "#{db_migrate_path}/create_users.rb"
66
77
  migration_template "migrations/create_sessions_migration.rb", "#{db_migrate_path}/create_sessions.rb"
67
78
  migration_template "migrations/create_password_reset_tokens_migration.rb", "#{db_migrate_path}/create_password_reset_tokens.rb"
@@ -73,6 +84,9 @@ class AuthenticationGenerator < Rails::Generators::Base
73
84
  end
74
85
 
75
86
  def create_models
87
+ copy_file "models/concerns/account_scoped.rb", "app/models/concerns/account_scoped.rb" if options.tenantable?
88
+
89
+ template "models/account.rb", "app/models/account.rb" if options.tenantable?
76
90
  template "models/current.rb", "app/models/current.rb"
77
91
  template "models/email_verification_token.rb", "app/models/email_verification_token.rb"
78
92
  template "models/event.rb", "app/models/event.rb" if options.trackable?
@@ -218,8 +232,8 @@ class AuthenticationGenerator < Rails::Generators::Base
218
232
  directory "test_unit/controllers/#{format}", "test/controllers"
219
233
  directory "test_unit/mailers/", "test/mailers"
220
234
  directory "test_unit/system", "test/system" unless options.api?
221
- template "test_unit/test_helper.rb", "test/test_helper.rb", force: true
222
- template "test_unit/application_system_test_case.rb", "test/application_system_test_case.rb", force: true unless options.api?
235
+ template "test_unit/test_helper.rb", "test/test_helper.rb", force: true
236
+ template "test_unit/application_system_test_case.rb", "test/application_system_test_case.rb", force: true unless options.api?
223
237
  end
224
238
 
225
239
  private
@@ -8,7 +8,6 @@ class SessionsController < ApplicationController
8
8
  end
9
9
 
10
10
  def new
11
- @user = User.new
12
11
  end
13
12
 
14
13
  def create
@@ -0,0 +1,30 @@
1
+ class AccountMiddleware
2
+ def initialize(app)
3
+ @app = app
4
+ end
5
+
6
+ def call(env)
7
+ request = ActionDispatch::Request.new(env)
8
+
9
+ _, account_id, request_path = request.path.split("/", 3)
10
+
11
+ if is_number?(account_id)
12
+ set_current_account(account_id)
13
+
14
+ request.script_name = "/#{account_id}"
15
+ request.path_info = "/#{request_path}"
16
+ @app.call(request.env)
17
+ else
18
+ @app.call(request.env)
19
+ end
20
+ end
21
+
22
+ private
23
+ def is_number?(value)
24
+ Integer(value, exception: false)
25
+ end
26
+
27
+ def set_current_account(account_id)
28
+ Current.account = Account.find(account_id)
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
+ def change
3
+ create_table :accounts
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ class Account < ApplicationRecord
2
+ end
@@ -0,0 +1,8 @@
1
+ module AccountScoped
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ belongs_to :account
6
+ default_scope { where account: Current.account }
7
+ end
8
+ end
@@ -1,6 +1,9 @@
1
1
  class Current < ActiveSupport::CurrentAttributes
2
2
  attribute :session, :user
3
3
  attribute :user_agent, :ip_address
4
+ <%- if options.tenantable? %>
5
+ attribute :account
6
+ <%- end -%>
4
7
 
5
8
  def session=(session)
6
9
  super; self.user = session.user
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authentication-zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.16.29
4
+ version: 2.16.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nixon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-17 00:00:00.000000000 Z
11
+ date: 2023-06-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -95,7 +95,9 @@ files:
95
95
  - lib/generators/authentication/templates/erb/user_mailer/password_reset.html.erb.tt
96
96
  - lib/generators/authentication/templates/erb/user_mailer/passwordless.html.erb.tt
97
97
  - lib/generators/authentication/templates/javascript/controllers/application.js
98
+ - lib/generators/authentication/templates/lib/account_middleware.rb
98
99
  - lib/generators/authentication/templates/mailers/user_mailer.rb.tt
100
+ - lib/generators/authentication/templates/migrations/create_accounts_migration.rb.tt
99
101
  - lib/generators/authentication/templates/migrations/create_email_verification_tokens_migration.rb.tt
100
102
  - lib/generators/authentication/templates/migrations/create_events_migration.rb.tt
101
103
  - lib/generators/authentication/templates/migrations/create_password_reset_tokens_migration.rb.tt
@@ -104,6 +106,8 @@ files:
104
106
  - lib/generators/authentication/templates/migrations/create_sessions_migration.rb.tt
105
107
  - lib/generators/authentication/templates/migrations/create_sign_in_tokens_migration.rb.tt
106
108
  - lib/generators/authentication/templates/migrations/create_users_migration.rb.tt
109
+ - lib/generators/authentication/templates/models/account.rb.tt
110
+ - lib/generators/authentication/templates/models/concerns/account_scoped.rb
107
111
  - lib/generators/authentication/templates/models/current.rb.tt
108
112
  - lib/generators/authentication/templates/models/email_verification_token.rb.tt
109
113
  - lib/generators/authentication/templates/models/event.rb.tt