lesli_shield 1.1.6 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +3 -0
  3. data/app/assets/stylesheets/lesli_shield/application.css +1222 -1
  4. data/app/assets/stylesheets/lesli_shield/confirmations.css +917 -18
  5. data/app/assets/stylesheets/lesli_shield/confirmations.tailwind.css +918 -0
  6. data/app/assets/stylesheets/lesli_shield/invitations.css +1008 -0
  7. data/app/assets/stylesheets/lesli_shield/passwords.css +872 -18
  8. data/app/assets/stylesheets/lesli_shield/passwords.tailwind.css +873 -0
  9. data/app/assets/stylesheets/lesli_shield/registrations.css +904 -832
  10. data/app/assets/stylesheets/lesli_shield/registrations.tailwind.css +908 -0
  11. data/app/assets/stylesheets/lesli_shield/sessions.css +984 -32
  12. data/app/assets/stylesheets/lesli_shield/sessions.tailwind.css +983 -1
  13. data/app/controllers/lesli_shield/invites_controller.rb +100 -49
  14. data/app/controllers/lesli_shield/profile/passwords_controller.rb +119 -0
  15. data/app/controllers/lesli_shield/profiles_controller.rb +69 -0
  16. data/app/controllers/lesli_shield/role/actions_controller.rb +134 -38
  17. data/app/controllers/lesli_shield/roles_controller.rb +114 -141
  18. data/app/controllers/lesli_shield/sessions_controller.rb +1 -2
  19. data/app/controllers/lesli_shield/users_controller.rb +11 -4
  20. data/app/controllers/users/confirmations_controller.rb +162 -49
  21. data/app/controllers/users/invitations_controller.rb +101 -0
  22. data/app/controllers/users/passwords_controller.rb +211 -28
  23. data/app/controllers/users/registrations_controller.rb +108 -83
  24. data/app/controllers/users/sessions_controller.rb +124 -84
  25. data/app/interfaces/lesli_shield/authorization_interface.rb +2 -2
  26. data/app/jobs/lesli_shield/invitation_delivery_job.rb +123 -0
  27. data/app/mailers/lesli_shield/devise_mailer.rb +73 -17
  28. data/app/mailers/lesli_shield/invitation_mailer.rb +58 -0
  29. data/app/models/concerns/lesli_shield/role_management.rb +70 -0
  30. data/app/models/concerns/lesli_shield/user_authentication.rb +54 -0
  31. data/app/models/concerns/lesli_shield/user_security.rb +22 -19
  32. data/app/models/lesli_shield/account.rb +4 -1
  33. data/app/models/lesli_shield/invite.rb +183 -12
  34. data/app/models/lesli_shield/user/role.rb +1 -1
  35. data/app/models/lesli_shield/user.rb +5 -2
  36. data/app/services/lesli_shield/invitation_audit.rb +34 -0
  37. data/app/services/lesli_shield/invitation_email_policy.rb +64 -0
  38. data/app/services/lesli_shield/invitation_service.rb +405 -0
  39. data/app/services/lesli_shield/invite_service.rb +34 -5
  40. data/app/services/lesli_shield/profile/password_service.rb +47 -0
  41. data/app/services/lesli_shield/profile_service.rb +25 -0
  42. data/app/services/lesli_shield/role_action_service.rb +37 -92
  43. data/app/services/lesli_shield/role_audit.rb +42 -0
  44. data/app/services/lesli_shield/role_permission_service.rb +347 -0
  45. data/app/services/lesli_shield/role_privilege_deployment_service.rb +79 -0
  46. data/app/services/lesli_shield/role_privilege_service.rb +69 -105
  47. data/app/services/lesli_shield/role_service.rb +431 -0
  48. data/app/services/lesli_shield/user_registration_service.rb +41 -74
  49. data/app/services/lesli_shield/user_session_service.rb +1 -1
  50. data/app/services/lesli_shield/user_validator_service.rb +37 -39
  51. data/app/views/devise/confirmations/new.html.erb +58 -12
  52. data/app/views/devise/confirmations/show.html.erb +27 -60
  53. data/app/views/devise/passwords/edit.html.erb +59 -25
  54. data/app/views/devise/passwords/new.html.erb +61 -26
  55. data/app/views/devise/registrations/new.html.erb +83 -56
  56. data/app/views/devise/sessions/new.html.erb +58 -41
  57. data/app/views/devise/shared/_application-devise-simple.erb +18 -19
  58. data/app/views/devise/shared/_demo.html.erb +8 -6
  59. data/app/views/devise/shared/_error_messages.html.erb +45 -10
  60. data/app/views/devise/shared/_links.html.erb +10 -20
  61. data/app/views/lesli_shield/invites/_form.html.erb +64 -7
  62. data/app/views/lesli_shield/invites/_invite.html.erb +55 -2
  63. data/app/views/lesli_shield/invites/_invite_actions.html.erb +34 -0
  64. data/app/views/lesli_shield/invites/_status_badge.html.erb +13 -0
  65. data/app/views/lesli_shield/invites/index.html.erb +119 -62
  66. data/app/views/lesli_shield/invites/new.html.erb +11 -36
  67. data/app/views/lesli_shield/invites/show.html.erb +13 -52
  68. data/app/views/lesli_shield/profiles/_information-form.html.erb +11 -0
  69. data/app/views/lesli_shield/profiles/_password-form.html.erb +68 -0
  70. data/app/views/lesli_shield/profiles/show.html.erb +26 -0
  71. data/app/views/lesli_shield/role/actions/_custom_actions.html.erb +24 -0
  72. data/app/views/lesli_shield/role/actions/_deployment_status.html.erb +14 -0
  73. data/app/views/lesli_shield/role/actions/_matrix.html.erb +69 -0
  74. data/app/views/lesli_shield/role/actions/_permissions.html.erb +95 -0
  75. data/app/views/lesli_shield/role/actions/_toggle.html.erb +34 -0
  76. data/app/views/lesli_shield/role/actions/index.html.erb +14 -10
  77. data/app/views/lesli_shield/roles/_deployment_badge.html.erb +15 -0
  78. data/app/views/lesli_shield/roles/_empty_state.html.erb +20 -0
  79. data/app/views/lesli_shield/roles/_field_errors.html.erb +5 -0
  80. data/app/views/lesli_shield/roles/_filters.html.erb +66 -0
  81. data/app/views/lesli_shield/roles/_form.html.erb +149 -18
  82. data/app/views/lesli_shield/roles/_management_menu.html.erb +37 -0
  83. data/app/views/lesli_shield/roles/_pagination.html.erb +43 -0
  84. data/app/views/lesli_shield/roles/_role_list.html.erb +116 -0
  85. data/app/views/lesli_shield/roles/_status_badge.html.erb +11 -0
  86. data/app/views/lesli_shield/roles/_type_badge.html.erb +9 -0
  87. data/app/views/lesli_shield/roles/index.html.erb +15 -38
  88. data/app/views/lesli_shield/roles/new.html.erb +11 -0
  89. data/app/views/lesli_shield/roles/show.html.erb +79 -7
  90. data/app/views/lesli_shield/users/_information-form.html.erb +15 -1
  91. data/app/views/users/invitations/show.html.erb +142 -0
  92. data/app/views/users/invitations/unavailable.html.erb +19 -0
  93. data/config/initializers/devise.rb +52 -328
  94. data/config/locales/devise.en.yml +8 -0
  95. data/config/locales/devise.es.yml +8 -0
  96. data/config/locales/translations.en.yml +314 -16
  97. data/config/locales/translations.es.yml +314 -16
  98. data/config/routes.rb +14 -3
  99. data/db/migrate/v1/0801120110_create_lesli_shield_invites.rb +40 -6
  100. data/lib/lesli_shield/engine.rb +42 -4
  101. data/lib/lesli_shield/router.rb +62 -18
  102. data/lib/lesli_shield/version.rb +2 -2
  103. data/lib/tasks/lesli_shield_tasks.rake +1 -1
  104. data/readme.md +17 -3
  105. metadata +63 -8
  106. data/app/assets/config/lesli_shield_manifest.js +0 -48
  107. data/app/assets/stylesheets/lesli_shield/application.tailwind.css +0 -2
  108. data/app/mailers/lesli_shield/invitation.html.erb +0 -23
  109. data/app/views/lesli_shield/invites/edit.html.erb +0 -12
  110. data/app/views/lesli_shield/role/actions/_form.html.erb +0 -75
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ # Installs Shield's Devise-based authentication implementation on the core
5
+ # user model. Lesli itself only consumes current_user and remains free of a
6
+ # direct dependency on this engine or on Devise.
7
+ module UserAuthentication
8
+ extend ActiveSupport::Concern
9
+
10
+ # This module is prepended after Devise installs its model modules so
11
+ # LesliShield can extend, rather than replace, Devise's eligibility
12
+ # checks. Calling super preserves confirmable, lockable, and any other
13
+ # authentication provider rules.
14
+ module LoginEligibility
15
+ def active_for_authentication?
16
+ super && lesli_shield_login_validation.valid?
17
+ end
18
+
19
+ def inactive_message
20
+ return super if lesli_shield_login_validation.valid?
21
+
22
+ :invalid
23
+ end
24
+
25
+ private
26
+
27
+ def lesli_shield_login_validation
28
+ return @lesli_shield_login_validation if defined?(
29
+ @lesli_shield_login_validation
30
+ )
31
+
32
+ @lesli_shield_login_validation = LesliShield::UserValidatorService
33
+ .new(self)
34
+ .validate
35
+ end
36
+ end
37
+
38
+ DEVISE_MODULES = %i[
39
+ database_authenticatable
40
+ registerable
41
+ rememberable
42
+ recoverable
43
+ validatable
44
+ confirmable
45
+ trackable
46
+ lockable
47
+ ].freeze
48
+
49
+ included do
50
+ devise(*LesliShield::UserAuthentication::DEVISE_MODULES)
51
+ prepend LesliShield::UserAuthentication::LoginEligibility
52
+ end
53
+ end
54
+ end
@@ -39,7 +39,7 @@ module LesliShield
39
39
 
40
40
  # get the max level permission from roles assigned to the user
41
41
  def max_level_permission
42
- self.roles.maximum(:permission_level) || 0
42
+ roles.where(active: true).maximum(:permission_level) || 0
43
43
  end
44
44
 
45
45
  # check if user has roles with specific names
@@ -51,7 +51,10 @@ module LesliShield
51
51
  # to perform a specific action in a specific controller
52
52
  def has_privileges_for?(controller, action)
53
53
  begin
54
- return self.privileges.where(
54
+ active_role_ids = roles.where(active: true).select(:id)
55
+
56
+ return Role::Privilege.where(
57
+ role_id: active_role_ids,
55
58
  controller: controller,
56
59
  action: action,
57
60
  active: true
@@ -62,26 +65,26 @@ module LesliShield
62
65
  end
63
66
  end
64
67
 
65
- # Check if user has enough privilege to work with the given role
66
- def can_work_with_role?(role_id)
67
-
68
- # get the role if only id is given
69
- role = self.account.roles.find_by(:id => role_id)
70
-
71
- # false if role not found
72
- return false if role.blank?
68
+ # A user can assign only active roles from their own account and never
69
+ # roles above the highest active permission level they already hold.
70
+ def can_assign_role?(role)
71
+ return false if role.blank? || !role.active?
72
+ return false unless role.account_id == account_id
73
+ return false if role.permission_level.blank?
73
74
 
74
- # not valid role without object levelpermission defined
75
- return false if role.level_permission.blank?
75
+ role.permission_level <= max_level_permission
76
+ end
76
77
 
77
- # get the max level permission from the roles the user has assigned
78
- user_role_max_level_permission = self.roles.map(&:level_permission).max()
78
+ # Preserve the existing public API while using the current
79
+ # permission_level column and the stricter assignment rules.
80
+ def can_work_with_role?(role_or_id)
81
+ role = if role_or_id.is_a?(Lesli::Role)
82
+ role_or_id
83
+ else
84
+ account.roles.find_by(id: role_or_id)
85
+ end
79
86
 
80
- # check if user can work with the level permission of the role is trying to modify
81
- # Note: user only can assigned an level permission below the max of his own roles
82
- # Current user cannot assign role if role to assign is the same of the greater role
83
- # assigned to the current user
84
- user_role_max_level_permission >= role.level_permission
87
+ can_assign_role?(role)
85
88
  end
86
89
 
87
90
  # Checks configuration of all the roles assigned to the user
@@ -32,7 +32,10 @@ Building a better future, one line of code at a time.
32
32
 
33
33
  module LesliShield
34
34
  class Account < ApplicationRecord
35
- belongs_to :account, class_name: "Lesli::Account"
35
+ belongs_to :account,
36
+ class_name: "Lesli::Account",
37
+ inverse_of: :shield
38
+ validates :account_id, uniqueness: true
36
39
  has_many :dashboards
37
40
  has_many :invites
38
41
 
@@ -1,24 +1,195 @@
1
1
  module LesliShield
2
2
  class Invite < ApplicationRecord
3
- belongs_to :account
4
- belongs_to :user, class_name: "Lesli::User"
3
+ class InvalidStatusTransition < StandardError; end
5
4
 
6
- validates :email, presence: true, on: :create
5
+ DEFAULT_EXPIRATION = 7.days
6
+ STATUS_TRANSITIONS = {
7
+ "pending" => %w[accepted cancelled expired],
8
+ "expired" => %w[pending],
9
+ "accepted" => [],
10
+ "cancelled" => []
11
+ }.freeze
7
12
 
8
- before_create :before_create_invite
13
+ belongs_to :account, class_name: "LesliShield::Account"
14
+ belongs_to :invited_by,
15
+ class_name: "Lesli::User",
16
+ foreign_key: :invited_by_id
17
+ belongs_to :role,
18
+ class_name: "Lesli::Role",
19
+ foreign_key: :role_id,
20
+ optional: true
21
+ belongs_to :accepted_user,
22
+ class_name: "Lesli::User",
23
+ foreign_key: :accepted_user_id,
24
+ optional: true
9
25
 
10
- enum :status, {
11
- created: 0,
12
- accepted: 1,
26
+ enum :status, {
27
+ pending: 0,
28
+ accepted: 1,
13
29
  cancelled: 2,
14
- sent: 5,
15
- requested: 6
30
+ expired: 3
31
+ }, default: :pending, validate: true, scopes: false
32
+
33
+ scope :pending, -> {
34
+ where(status: statuses.fetch("pending"))
35
+ }
36
+ scope :active, -> {
37
+ pending.where(expires_at: Time.current..)
38
+ }
39
+ scope :expired, -> {
40
+ expired_by_status = where(status: statuses.fetch("expired"))
41
+ expired_by_time = pending.where(expires_at: (..Time.current))
42
+
43
+ expired_by_status.or(expired_by_time)
44
+ }
45
+ scope :for_account, ->(account) {
46
+ where(account: account)
16
47
  }
17
48
 
18
- private
49
+ before_validation :normalize_email
50
+ before_validation :set_default_expiration, on: :create
51
+
52
+ validates :email,
53
+ presence: true,
54
+ format: { with: URI::MailTo::EMAIL_REGEXP }
55
+ validate :email_can_be_invited, on: :create
56
+ validate :role_belongs_to_inviting_account
57
+ validate :status_transition_is_allowed, on: :update
58
+
59
+ class << self
60
+ def expiration_period
61
+ configured_expiration = Lesli.config.shield&.dig(
62
+ :invitation_expiration
63
+ )
64
+
65
+ configured_expiration.presence || DEFAULT_EXPIRATION
66
+ end
67
+
68
+ def allowed_status_transition?(from:, to:)
69
+ STATUS_TRANSITIONS.fetch(from.to_s, []).include?(to.to_s)
70
+ end
71
+ end
72
+
73
+ # Accepting an invitation is intentionally a terminal operation. The
74
+ # acceptance service will call this only after creating the user,
75
+ # assigning its account and role, and confirming its email atomically.
76
+ def accept!(accepted_user)
77
+ transition_to!(
78
+ :accepted,
79
+ accepted_user: accepted_user,
80
+ accepted_at: Time.current
81
+ )
82
+ end
83
+
84
+ def cancel!
85
+ transition_to!(
86
+ :cancelled,
87
+ cancelled_at: Time.current
88
+ )
89
+ end
90
+
91
+ def expire!
92
+ transition_to!(:expired)
93
+ end
94
+
95
+ # Resending is the only operation that can reactivate an expired
96
+ # invitation. No user record is created or modified here.
97
+ def resend!
98
+ unless resendable?
99
+ raise InvalidStatusTransition,
100
+ "cannot resend an invitation with status #{status}"
101
+ end
102
+
103
+ transition_to!(
104
+ :pending,
105
+ expires_at: Time.current + self.class.expiration_period
106
+ )
107
+ end
108
+
109
+ def resendable?
110
+ expired?
111
+ end
112
+
113
+ def resend_available?
114
+ resendable? || (pending? && expires_at.present? && expires_at.past?)
115
+ end
116
+
117
+ def cancellable?
118
+ pending?
119
+ end
120
+
121
+ def lifecycle_status
122
+ return "expired" if pending? && expires_at.present? && expires_at.past?
123
+
124
+ status
125
+ end
126
+
127
+ def acceptance_available?
128
+ pending? && expires_at.present? && expires_at.future?
129
+ end
130
+
131
+ private
132
+
133
+ def normalize_email
134
+ self.email = email.to_s.strip.downcase.presence
135
+ end
136
+
137
+ def set_default_expiration
138
+ self.expires_at ||= Time.current + self.class.expiration_period
139
+ end
140
+
141
+ def email_can_be_invited
142
+ destination_account = account&.account
143
+ return if destination_account.blank? || email.blank?
144
+
145
+ decision = InvitationEmailPolicy.new(
146
+ account: destination_account,
147
+ email: email
148
+ ).evaluate
149
+ return if decision.allowed?
150
+
151
+ errors.add(
152
+ :email,
153
+ I18n.t("lesli_shield.invites.email_#{decision.status}")
154
+ )
155
+ end
156
+
157
+ def role_belongs_to_inviting_account
158
+ return if role.blank? || account.blank?
159
+ return if role.account == account.account
160
+
161
+ errors.add(
162
+ :role,
163
+ I18n.t("lesli_shield.invites.role_account_mismatch")
164
+ )
165
+ end
166
+
167
+ def transition_to!(target_status, attributes = {})
168
+ unless self.class.allowed_status_transition?(
169
+ from: status,
170
+ to: target_status
171
+ )
172
+ raise InvalidStatusTransition,
173
+ "cannot transition invitation from #{status} to #{target_status}"
174
+ end
175
+
176
+ update!(attributes.merge(status: target_status))
177
+ end
178
+
179
+ def status_transition_is_allowed
180
+ previous_status, next_status = status_change_to_be_saved
181
+ return if previous_status.nil?
182
+ return if self.class.allowed_status_transition?(
183
+ from: previous_status,
184
+ to: next_status
185
+ )
19
186
 
20
- def before_create_invite
21
- self.status = :created
187
+ errors.add(
188
+ :status,
189
+ I18n.t("lesli_shield.invites.invalid_status_transition"),
190
+ from: previous_status,
191
+ to: next_status
192
+ )
22
193
  end
23
194
  end
24
195
  end
@@ -1,7 +1,7 @@
1
1
  module LesliShield
2
2
  class User::Role < ApplicationRecord
3
3
  self.table_name = 'lesli_shield_user_roles'
4
- belongs_to :user
4
+ belongs_to :user, class_name: "Lesli::User"
5
5
  belongs_to :role, class_name: "Lesli::Role"
6
6
  has_many :roles
7
7
  end
@@ -1,5 +1,8 @@
1
1
  module LesliShield
2
- class User < ::Lesli::User
3
- #has_many :activities, class_name: "Lesli::Item::Activity"
2
+ # Namespace for Shield-owned records associated with Lesli::User, such as
3
+ # User::Role and User::Session. The authenticatable model remains
4
+ # Lesli::User; defining a second ActiveRecord subclass for the same table
5
+ # causes association type mismatches and can confuse Devise mappings.
6
+ module User
4
7
  end
5
8
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ # Provides one explicit audit vocabulary for the invitation lifecycle.
5
+ # This component does not hide persistence errors; each caller decides
6
+ # whether the event belongs to a database transaction or an async delivery.
7
+ class InvitationAudit
8
+ EVENT_DESCRIPTIONS = {
9
+ "invitation_sent" => "Invitation delivered",
10
+ "invitation_resent" => "Invitation delivered again",
11
+ "invitation_cancelled" => "Invitation cancelled",
12
+ "invitation_accepted" => "Invitation accepted",
13
+ "invitation_delivery_failed" => "Invitation delivery failed"
14
+ }.freeze
15
+
16
+ class << self
17
+ def record!(operation, invitation:, actor:, source:)
18
+ operation = operation.to_s
19
+ description = EVENT_DESCRIPTIONS.fetch(operation)
20
+
21
+ raise ArgumentError, "audit actor is required" unless actor
22
+
23
+ actor.log(
24
+ engine: LesliShield,
25
+ source: source,
26
+ action: operation,
27
+ operation: operation,
28
+ description: description,
29
+ subject: invitation
30
+ )
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LesliShield
4
+ # Classifies an invitation recipient without creating or modifying a user.
5
+ # The result is evaluated again while accepting the invitation to protect
6
+ # against a user being created between invitation delivery and acceptance.
7
+ class InvitationEmailPolicy
8
+ AVAILABLE = :available
9
+ MEMBER = :member
10
+ OTHER_ACCOUNT = :other_account
11
+ RECOVERABLE = :recoverable
12
+ DELETED = :deleted
13
+ EXISTING_WITHOUT_ACCOUNT = :existing_without_account
14
+
15
+ ALLOWED_STATUSES = [AVAILABLE, RECOVERABLE].freeze
16
+
17
+ Decision = Struct.new(:status, :user, keyword_init: true) do
18
+ def allowed?
19
+ ALLOWED_STATUSES.include?(status)
20
+ end
21
+
22
+ def rejected?
23
+ !allowed?
24
+ end
25
+
26
+ def recoverable?
27
+ status == RECOVERABLE
28
+ end
29
+ end
30
+
31
+ attr_reader :account, :email
32
+
33
+ def initialize(account:, email:)
34
+ raise ArgumentError, "account is required" unless account
35
+
36
+ @account = account
37
+ @email = email.to_s.strip.downcase
38
+ end
39
+
40
+ def evaluate
41
+ user = find_user
42
+
43
+ return decision(AVAILABLE) unless user
44
+ return decision(DELETED, user) if user.deleted_at.present?
45
+ return decision(MEMBER, user) if user.account_id == account.id
46
+ return decision(OTHER_ACCOUNT, user) if user.account_id.present?
47
+ return decision(RECOVERABLE, user) unless user.confirmed?
48
+
49
+ decision(EXISTING_WITHOUT_ACCOUNT, user)
50
+ end
51
+
52
+ private
53
+
54
+ def find_user
55
+ Lesli::User
56
+ .with_deleted
57
+ .find_by("LOWER(email) = ?", email)
58
+ end
59
+
60
+ def decision(status, user = nil)
61
+ Decision.new(status: status, user: user)
62
+ end
63
+ end
64
+ end